IParserLogService log = new DefaultLogService();
String
codeString = "";
/*******
Take a c source file ********/
File file
= new
File("Y:\\IFX_Tools\\Plugin_Eclipse\\workspace\\ParserCDT\\src\\CFile.c");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
while (dis.available() != 0) {
codeString =
codeString.concat(dis.readLine());
//System.out.println(dis.readLine());
}
fis.close();
bis.close();
dis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
char[]
code = codeString.toCharArray();
CodeReader reader = new
CodeReader(code);
Map
definedSymbols = new HashMap();
String[]
includePaths = {"<stdio.h>"};
IScannerInfo info = new
ScannerInfo(definedSymbols, includePaths);
ICodeReaderFactory readerFactory =
FileCodeReaderFactory.getInstance();
IASTTranslationUnit translationUnit =
GCCLanguage.getDefault().getASTTranslationUnit(reader, info, readerFactory,
null, log);
//
Function name
for
(IASTDeclaration declaration : translationUnit.getDeclarations()){
if(
declaration instanceof IASTFunctionDefinition){
System.out.println(((IASTFunctionDefinition)
declaration).getDeclarator().getName());
}
}
//
comments
for
(IASTComment com :translationUnit.getComments()){
System.out.println(com.toString());
}
function1
function2
/*doc function1*/
/*doc function2*/
But when I add some inclusions in my c file:
#include <stdio.h>
#include <stdlib.h>
I get nothing in console.
I want to know, should I do something like:
String[] includePaths = {"<stdio.h>"};// I know it should not be
that
IScannerInfo info = new
ScannerInfo(definedSymbols, includePaths);
Isn't it ? If not, how should i do?
Thanks a lot
> From:
glathe.helko@xxxxxxxxxxxxxx>
Date: Wed, 18 Aug 2010 14:01:34 +0200
> Subject: Re: [cdt-dev]
org.eclipse.cdt.core.dom.parser.c
> To:
cdt-dev@xxxxxxxxxxx>
> I
think I didn't get your point,
>
> Did you mean that you want
resolve the include node?
>
> Have a look at the method
"getInclusions()" of IASTTranslationUnit.
>
>
Sincerely,
>
> Helko Glathe
>
>
>
>
2010/8/18 Flo Menier
<josieenfrance@xxxxxxxxxxx>:
>
> I found the problem. I deleted the first line, the include, I got 2
function
> > names and 2 comments. So, one more question, I to
resolve this one?
> > String[] includePaths =
{"<stdio.h>"};// I know it should not be that
> >
IScannerInfo info = new ScannerInfo(definedSymbols, includePaths);
>
> #include <stdio.h>
> > /*doc function1*/
> >
int function1(int a){
> > printf("This is function
1");
> > return 0;
> > }
> > /*doc
function2*/
> > int function2(char a){
> > printf("This is
function 2");
> > return 0;
> > }
> >> From:
glathe.helko@xxxxxxxxxxxxxx>
>> Date: Wed, 18 Aug 2010 13:35:13 +0200
> >> Subject: Re:
[cdt-dev] org.eclipse.cdt.core.dom.parser.c
> >> To:
cdt-dev@xxxxxxxxxxx>
>>
> >> And there are no warning, problem or error
messages from the parser?
> >>
> >> E.g. is
EXIT_SUCCESS a valid argument?
> >>
> >> Try the
following: delete the main method + duplicate the method
> >>
"function" and rename it to e.g. "function2", so that you have to
>
>> methods and see if you get two declarations. If that works fine,
your
> >> main method seems to be invalid.
>
>>
> >> Sincerely,
> >>
> >> Helko
Glathe
> >>
> >>
> >>
> >>
2010/8/18 Flo Menier
<josieenfrance@xxxxxxxxxxx>:
>
>> > when I tested the c code with only one function(int
function(int
> >> > toto){int
> >> >
a;/*doc0*/
> >> >> > char b;/*doc1*/ return toto+a;}),
I got doc0,doc1, with
> >> >> > getComments().
>
>> >> > with "for (IASTDeclaration declaration :
>
>> >> > translationUnit.getDeclarations()){
> >>
> if( declaration instanceof IASTFunctionDefinition){
> >>
> System.out.println(((IASTFunctionDefinition)
> >> >
declaration).getDeclarator().getName());
> >> > }" I got
"function", so the function name in console.
> >> > But when
I tried a c file where I defined 2 functions. I got no function
>
>> > name. I think there would be something wrong in the
code.
> >> > my CFile.c
> >> > /*Documentation
function1*/
> >> > int function1(char a){
> >>
> printf("This is function1");
> >> > return 0;
>
>> > }
> >> > /*Documentation function
main*/
> >> > int main(void) {
> >> >
puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
> >>
> function1('1');
> >> > return EXIT_SUCCESS;
>
>> > }
> >> >> From:
glathe.helko@xxxxxxxxxxxxxx>
>> >> Date: Wed, 18 Aug 2010 12:44:12 +0200
> >>
>> Subject: Re: [cdt-dev] org.eclipse.cdt.core.dom.parser.c
>
>> >> To:
cdt-dev@xxxxxxxxxxx> >>
>>
> >> >> Hi Andreas,
> >>
>>
> >> >> but the c source code is not valid. Copy
and paste it in the Eclipse
> >> >> CDT C Editor and you
will see the error message.
> >> >>
> >>
>> I think that the non deprecated code you are using is propably
more
> >> >> tollerant against syntax errors.
>
>> >>
> >> >> Kind regards from Berlin
,Helko
> >> >>
> >> >>
Sincerely,
> >> >>
> >> >> Helko
Glathe
> >> >>
> >> >>
> >>
>>
> >> >> 2010/8/18 Andreas Graf
<andreas.graf@xxxxxxxxx>:
>
>> >> > Hmm,
> >> >> >
> >>
>> > I tried the following code to avoid the deprecated interfaces
and it
> >> >> > seems
> >> >> >
to parse fine:
> >> >> >
> >> >> >
IParserLogService log = new DefaultLogService();
> >>
>> >
String codeString = "int function(int
toto){int
> >> >> > a;/*doc0*/
> >>
>> > char b;/*doc1*/ return toto+a; //*doc2*//}";
> >>
>> >
char[] code = codeString.toCharArray();
>
>> >> >
FileContent reader =
FileContent.create("",code);
> >> >> >
Map definedSymbols = new HashMap();
> >> >> >
String[] includePaths = new String[0];
> >> >> >
IScannerInfo info = new ScannerInfo(definedSymbols,
> >>
>> > includePaths);
> >> >> >
> >>
>> >
try {
> >> >> >
IASTTranslationUnit translationUnit
> >>
>> > =GCCLanguage.getDefault().getASTTranslationUnit(reader, info,
null,
> >> >> > null, 0,
> >> >> >
log);
> >> >> >
String x
="";
> >> >> >
x="X";
>
>> >> >
} catch (CoreException e) {
>
>> >> >
// TODO
Auto-generated catch block
> >> >> >
e.printStackTrace();
> >> >> >
}
> >>
>> >
> >> >> > Am 18.08.2010 11:42, schrieb
Helko Glathe:
> >> >> >
> >> >> >
Hello Flo,
> >> >> >
> >> >> > I
think that your C Source Code example is invalid.
> >> >>
> The part "//*doc2*//" is not one block comment, but instead it is
a
> >> >> > single line comment. The closing curly
bracket "}" of your C function
> >> >> > ist also
included in this single line comment, because your posted
> >>
>> > code does not include any linebreaks. Thus, replace your "//"
through
> >> >> > a single "/" to get a block comment
and try your code again.
> >> >> >
> >>
>> > Cheers, Helko
> >> >> >
> >>
>> > Sincerely,
> >> >> >
> >>
>> > Helko Glathe
> >> >> >
> >>
>> >
> >> >> >
> >> >> >
2010/8/18 Flo Menier
<josieenfrance@xxxxxxxxxxx>:
>
>> >> >
> >> >> >
> >>
>> > Thanks Helko. I tried to do what you said:
> >>
>> > public static void main(String[] args) throws Exception
{
> >> >> > IParserLogService log = new
DefaultLogService();
> >> >> > String codeString = "int
function(int toto){int a;/*doc0*/ char
> >> >> >
b;/*doc1*/
> >> >> > return toto+a;
//*doc2*//}";
> >> >> > char[] code =
codeString.toCharArray();
> >> >> > CodeReader reader =
new CodeReader(code);
> >> >> > Map definedSymbols =
new HashMap();
> >> >> > String[] includePaths = new
String[0];
> >> >> > IScannerInfo info = new
ScannerInfo(definedSymbols, includePaths);
> >> >> >
ICodeReaderFactory readerFactory =
> >> >> >
FileCodeReaderFactory.getInstance();
> >> >> >
IASTTranslationUnit translationUnit
> >> >> >
=GCCLanguage.getDefault().getASTTranslationUnit(reader, info,
>
>> >> > readerFactory,
> >> >> > null,
log);
> >> >> > for (IASTComment com
:translationUnit.getComments()){
> >> >> >
System.out.println(com.getComment().toString());
> >> >>
> }
> >> >> > I got nothing printed in console.
getComments() gave me a
> >> >> >
IASTComment[10],
> >> >> > value is null for all 10
IASComment?
> >> >> >
> >> >>
>
> >> >> > From:
glathe.helko@xxxxxxxxxxxxxx>
>> >> > Date: Wed, 18 Aug 2010 00:41:38 +0200
>
>> >> > Subject: Re: [cdt-dev]
org.eclipse.cdt.core.dom.parser.c
> >> >> > To:
cdt-dev@xxxxxxxxxxx> >>
>> >
> >> >> > Hello,
> >>
>> >
> >> >> > have a look at the CVS
repository:
> >> >> >
> >> >> > "
1. Connect to CVS repository:
> >> >> >
:pserver:anonymous@xxxxxxxxxxxxxxx:2401/cvsroot/tools (Or
> >>
>> > :pserver:anonymous@xxxxxxxxxxxxxxxxx:80/cvsroot/tools from
behind
> >> >> > corporate firewall)
> >>
>> > 2. Check out: org.eclipse.cdt/all "
> >> >>
>
> >> >> > Underneath of "all" you will find the
folder "org.eclipse.cdt.core".
> >> >> > Inside their
is a subfolder for the "parser" where you can find the
> >>
>> > source code.
> >> >> >
> >>
>> >
> >> >> > But I think you can access
Block Comments right know using the AST of
> >> >> >
the C Source Code. The IASTTranslationUnit provides a method for
>
>> >> > getting all comments (getComments()). This Method
returns an Array of
> >> >> > IASTComment's. And
IASTComment provides a to determine whether it is
> >> >>
> a
> >> >> > block comment or not (see
IASTComment.isBlockComment()).
> >> >> >
>
>> >> > Cheers, Helko
> >> >> >
>
>> >> >
> >> >> >
> >>
>> > Sincerely,
> >> >> >
> >>
>> > Helko Glathe
> >> >> >
> >>
>> >
> >> >> >
> >> >> >
2010/8/17 Flo Menier
<josieenfrance@xxxxxxxxxxx>:
>
>> >> >
> >> >> >
> >>
>> > Can someone tell me where I can download the code
source
> >> >> >
of org.eclipse.cdt.core.dom.parser.c,
> >> >> >
and org.eclipse.cdt.internal.core.parser.scanner? I want to
> >>
>> > understand
> >> >> > how
> >>
>> > lexer works in CDT? I want to get block comments in a C source
file
> >> >> > instead
> >> >> >
of ignorer them.
> >> >> > Thanks a lot
>
>> >> >
_______________________________________________
> >> >>
> cdt-dev mailing list
> >> >> >
cdt-dev@xxxxxxxxxxx> >>
>> >
https://dev.eclipse.org/mailman/listinfo/cdt-dev>
>> >> >
> >> >> >
> >>
>> >
> >> >> >
> >> >> >
_______________________________________________
> >> >>
> cdt-dev mailing list
> >> >> >
cdt-dev@xxxxxxxxxxx> >>
>> >
https://dev.eclipse.org/mailman/listinfo/cdt-dev>
>> >> >
> >> >> >
> >>
>> > _______________________________________________
>
>> >> > cdt-dev mailing list
> >> >> >
cdt-dev@xxxxxxxxxxx> >>
>> >
https://dev.eclipse.org/mailman/listinfo/cdt-dev>
>> >> >
> >> >> >
> >>
>> >
> >> >> >
> >> >> >
_______________________________________________
> >> >>
> cdt-dev mailing list
> >> >> >
cdt-dev@xxxxxxxxxxx> >>
>> >
https://dev.eclipse.org/mailman/listinfo/cdt-dev>
>> >> >
> >> >> >
> >>
>> > --
> >> >> > Andreas Graf
>
>> >> > BDM Automotive
> >> >> > Mobil:
+49 (0) 151-10860479 (preferred)
> >> >> > Tel.: +49
(0) 7231 / 1 54 71-0
> >> >> > Fax.: +49 (0) 7231 / 1
54 71-29
> >> >> >
> >> >>
>
> >> >> > Web:
http://www.itemis.de>
>> >> > Mail:
andreas.graf@xxxxxxxxx>
>> >> > Xing:
http://www.xing.com/profile/Andreas_Graf> >>
>> > Twitter:
http://www.twitter.com/grafandreas> >>
>> > Blog:
http://5ise.quanxinquanyi.de> >> >>
>
> >> >> > itemis GmbH
> >> >>
> Blücherstrasse 32
> >> >> > D-75177
Pforzheim
> >> >> > Rechtlicher Hinweis:
>
>> >> > Amtsgericht Mannheim, HRB 50700996
> >>
>> > Ust.Id.Nr.: DE250574762
> >> >> >
Geschäftsführer: Sebastian Neus
> >> >> >
>
>> >> >
_______________________________________________
> >> >>
> cdt-dev mailing list
> >> >> >
cdt-dev@xxxxxxxxxxx> >>
>> >
https://dev.eclipse.org/mailman/listinfo/cdt-dev>
>> >> >
> >> >> >
> >>
>> _______________________________________________
> >>
>> cdt-dev mailing list
> >> >>
cdt-dev@xxxxxxxxxxx> >>
>>
https://dev.eclipse.org/mailman/listinfo/cdt-dev>
>> >
> >> >
_______________________________________________
> >> >
cdt-dev mailing list
> >> >
cdt-dev@xxxxxxxxxxx> >>
>
https://dev.eclipse.org/mailman/listinfo/cdt-dev>
>> >
> >> >
> >>
_______________________________________________
> >> cdt-dev
mailing list
> >>
cdt-dev@xxxxxxxxxxx> >>
https://dev.eclipse.org/mailman/listinfo/cdt-dev>
>
> > _______________________________________________
>
> cdt-dev mailing list
> >
cdt-dev@xxxxxxxxxxx> >
https://dev.eclipse.org/mailman/listinfo/cdt-dev>
>
> >
>
_______________________________________________
> cdt-dev mailing
list
>
cdt-dev@xxxxxxxxxxx>
https://dev.eclipse.org/mailman/listinfo/cdt-dev