[antlr-interest] gUnit test of lexer with predicates

denstar valliantster at gmail.com
Fri Dec 24 07:35:26 PST 2010


Hi folks!

I'm trying to do a gunit test of the example XML lexer/parser from the wiki:
http://www.antlr.org/wiki/display/ANTLR3/1.+Lexer

@lexer::members {
    boolean tagMode = false;
}

TAG_START_OPEN : '<' { tagMode = true; } ;
TAG_END_OPEN : '</' { tagMode = true; } ;
TAG_CLOSE : { tagMode }?=> '>' { tagMode = false; } ;
TAG_EMPTY_CLOSE : { tagMode }?=> '/>' { tagMode = false; } ;

ATTR_EQ : { tagMode }?=> '=' ;

ATTR_VALUE : { tagMode }?=>
        ( '"' (~'"')* '"'
        | '\'' (~'\'')* '\''
        )
    ;

PCDATA : { !tagMode }?=> (~'<')+ ;

GENERIC_ID
    : { tagMode }?=>
      ( LETTER | '_' | ':') (NAMECHAR)*
    ;

fragment NAMECHAR
    : LETTER | DIGIT | '.' | '-' | '_' | ':'
    ;

fragment DIGIT
    :    '0'..'9'
    ;

fragment LETTER
    : 'a'..'z'
    | 'A'..'Z'
    ;

WS  :  { tagMode }?=>
       (' '|'\r'|'\t'|'\u000C'|'\n') {$channel=99;}
    ;

But when I run this test:

GENERIC_ID:
"some" OK

I get this output:

     [echo] expected: OK
     [echo] actual: FailedPredicateException(GENERIC_ID,{ tagMode }?)

Which sorta makes sense, but is there a way around it?

I also tried doing a simple test of the parser with this:

emptyElement:
"<empty/>" -> ""

startTag:
"<wee>" OK

Which produces this:

     [echo] test4 (emptyElement, line17) -
     [echo] expected:
     [echo] actual: Invalid input

     [echo] test5 (startTag, line20) -
     [echo] expected: OK
     [echo] actual: java.util.EmptyStackException

Which has me kinda stumped.

If I run a test on some input in ANTLRWorks, it runs fine, but if I
try to run the interpreter (pointless, as there's predicates, right?),
ANTLRWorks hangs.  Same goes for antlrv3ide in Eclipse (which is even
worse, because it tries to run the interpreter before it will actually
run the test).

I'm guessing this is all probably because of the predicate, but I
don't know for sure.  I was really hoping to be able to leverage
gUnit.

I'm hoping I'm doing something dumb somewhere (besides trying to parse
HTML-like input), as I'm relatively new to ANTLR.

:Den

-- 
A philosopher is, no doubt, entitled to examine even those
distinctions that are to be found in the structure of all languages...
in that case, such a distinction may be imputed to a vulgar error,
which ought to be corrected in philosophy.
Thomas Reid


More information about the antlr-interest mailing list