Reputation: 1023
I’m trying to implement a simple parsing over custom .c files with added syntax.
Ex: test.c .
// I don’t need this in output
int func1(int a, int b);
//I need this.
@parseme int func2(int a, int b);
//and this …
@parseme
void func3()
{
Int a;
//put here where ever
…
{
//inside block
}
return;
}
.
I want to use a fuzzy parsing approach on the lexer phase then, on the parser rules, rewrite token with TokenRewriteStream and templates.
Well it’s a lexer piece …
lexer grammar Lexi;
options {filter = true;}
// Pick everything between @parseme and ';' or '{ }'
METHOD
: HEADER .* (';' | BODY )
;
fragment
HEADER
: '@' ('parseme' | 'PARSEME') ;
fragment
BODY: '{' .* '}' ;
.
… The problem is simple for a expert look: 1- Lexer stop parse when found ‘;’ before to reach the last ‘}’ on “ @parseme void func3() …. “ 2- Lexer stop parse when found inside block right curly. 3- And surely more cases don’t tested yet.
The problem is really obvious. Is the solution too?? I hope soo !!
Thanks.
Upvotes: 0
Views: 186
Reputation: 1023
Answer my self.
lexer grammar Lexi;
options {filter = true;}
// Pick everything between @parseme and ';' or '{}'
METHOD
: METHOD_HEADER (~'{')* METHOD_END ;
fragment
METHOD_HEADER
: '@' ('parseme' | 'PARSEME') ;
fragment
METHOD_END
: (';' | BLOCK ) ;
fragment
BLOCK
: '{' ( ~('{' | '}') | BLOCK )* '}' ;
WS : (' '|'\r'|'\t'|'\n')+ ;
The solution was very simple.
Upvotes: 1