[antlr-interest] Apparently equivalent code refactoring doesn't work

Steve Bennett stevagewp at gmail.com
Tue Jan 29 21:02:22 PST 2008


Hi all,

These two code fragments should be effectively equivalent, I think:

1:

internal_link:
    link_start COLON? pagename (PIPE link_caption?)? link_end
((letters)=>link_trail)?
-> ^(INTERNAL_LINK ^(PAGENAME pagename) ^(TEXT link_caption? link_trail?));

link_caption
@init {this.caption_levels++;}:
        simple_text;
finally {this.caption_levels--; }

----

2:

internal_link:
    link_start COLON? pagename (PIPE link_caption)? link_end
((letters)=>link_trail)?
-> ^(INTERNAL_LINK ^(PAGENAME pagename) ^(TEXT link_caption? link_trail?));

link_caption
@init {this.caption_levels++; }:
        simple_text?;
finally {this.caption_levels--; }


(the difference being the question mark after 'link_caption' or after
'simple_text')

However, version 2 matches [[foo|]] (which is what I want), but
version 1 doesn't. In particular, in the debugger, the parse tree for
1 shows 'simple_text' in red, and 'simple_inline_elem' is brown - then
the parse for internal_link apparently fails and it goes on to try
other rules.

Just wondering if anyone could think of why this would be? I feel iffy
about having rules that can match blank, so I'd prefer version 1 if
possible. Maybe I've missed something really obvious.

Thanks in advance,
Steve
PS using the latest ANTLRworks (1.1.6)


More information about the antlr-interest mailing list