Alexander Kostikov
2012-11-29 17:38:36 UTC
Hi,
The grammar I'm implementing is pretty big and I'm covering it with
unit tests to pin-point the rules that are already implemented and
stable. The problem is - when I test not the top-most rule, but the
smaller onces (the ones I want to pin-point and mark as stable), I
encounter a problem. Here is a small repro that I came up with:
---------------
grammar Test;
config: (rule)* EOF;
rule: 'rule' ID NEWLINE | 'rule' NEWLINE;
ID: CHAR (CHAR | NUMBER)*;
NEWLINE: ('\r' | '\n')+;
WS: (' ' | '\t') { $channel=HIDDEN; };
fragment CHAR: 'a'..'z' | 'A'..'Z' | '_' | '-';
fragment NUMBER: '0'..'9';
---------------
In tests I want to test that 'rule' is producing the correct AST.
Something like TestRuleAst( "rule test", "(RULE test)" ) where the
first argument is input to be parsed by 'rule', and the second
argument is expected text representation for the AST. But for that
test to work I'd like to treat all EOF tokens as NEWLINEs. Otherwise I
get NoViableAltException and MissingTokenException depending on the
rule I'm testing. But changing NEWLINE to 'NEWLINE: ('\r' | '\n')+ |
EOF;' doesn't solve this problem.
The simplest way to fix the tests for me would be to add '\n' at the
end of the tested strings. But I would like to know if it is possible
to treat EOF as NEWLINE in the grammar itself.
-- Alexander
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
The grammar I'm implementing is pretty big and I'm covering it with
unit tests to pin-point the rules that are already implemented and
stable. The problem is - when I test not the top-most rule, but the
smaller onces (the ones I want to pin-point and mark as stable), I
encounter a problem. Here is a small repro that I came up with:
---------------
grammar Test;
config: (rule)* EOF;
rule: 'rule' ID NEWLINE | 'rule' NEWLINE;
ID: CHAR (CHAR | NUMBER)*;
NEWLINE: ('\r' | '\n')+;
WS: (' ' | '\t') { $channel=HIDDEN; };
fragment CHAR: 'a'..'z' | 'A'..'Z' | '_' | '-';
fragment NUMBER: '0'..'9';
---------------
In tests I want to test that 'rule' is producing the correct AST.
Something like TestRuleAst( "rule test", "(RULE test)" ) where the
first argument is input to be parsed by 'rule', and the second
argument is expected text representation for the AST. But for that
test to work I'd like to treat all EOF tokens as NEWLINEs. Otherwise I
get NoViableAltException and MissingTokenException depending on the
rule I'm testing. But changing NEWLINE to 'NEWLINE: ('\r' | '\n')+ |
EOF;' doesn't solve this problem.
The simplest way to fix the tests for me would be to add '\n' at the
end of the tested strings. But I would like to know if it is possible
to treat EOF as NEWLINE in the grammar itself.
-- Alexander
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address