How to deal with the defined macros in parser?

SyntaxEditor for WPF Forum

Posted 7 years ago by Peter Luo - AIMNEXT
Version: 12.1.0561
Avatar

Hi,

Is there any way to deal with the pre-defined macros while parsing a document (like C language)?

#define AA "this is a string with semicolon";

char * str = AA

#define BB 10 < 20

if (BB)
{
    ...
}

 Like the "AA" and "BB" above.

If I use a preprocessor, how to replace the macros in the text before parsing and how to replace back after parsing?

Thanks a lot !

Comments (1)

Answer - Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Peter,

That can be rather difficult because by the nature of preprocessors, they require two passes.  You may need to make an IParser that first resolves all the #defines.  And as it loops through the document text, builds up another temporary ICodeDocument by replacing the defined items.  In your case this temporary document would end up being:

char * str = "this is a string with semicolon";
 
if (10 < 20)
{
    ...
}

Then manually run your normal language parser on that document instead. The tricky thing will be that if your parser gives you AST nodes or syntax errors, all of those offsets will be off since they'd be based on the temporary document.  So you would possibly need to do some tracking of what ranges were merged in and the offset delta to apply to the AST and syntax error results.

Then modify all those before returning the IParseData result back from the parsing operation.


Actipro Software Support

The latest build of this product (v2019.1 build 0683) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.