[antlr-interest] Using input.getSourceName() and INCLUDE processing

Pauba, Kevin L KLPauba at west.com
Wed May 26 07:39:07 PDT 2010


I have a JAVA language rewrite translator that adds some more modern control constructs to an existing DSL.  ANTLR has been invaluable.

I've provided "include" file support using the methods outlined in http://www.antlr.org/wiki/pages/viewpage.action?pageId=557057 but I would like to use the file name in the string template that is used to generate code:

        |       kw=KW_while {
                            whileLabelStack.push(new Label("while"));
                }
                l=string cond=string r=string
                (s+=statement)+
                KW_endwhile
                -> template(lhs={$l.text},
                            operator={$cond.text.replaceAll("<", "\\<")},
                            rhs={$r.text},
                            s={$s},
                            file={input.getSourceName()},
                            line={$kw.line},
                            label={whileLabelStack.pop().getLabel()})
<<
; while <lhs> <operator> <rhs>
let ${_source_line} = <file>,<line>
<label>:
        test <label>_end: <lhs> <operator> <rhs>
        <s>
        ; endwhile
        goto <label>:
<label>_end:
>>
        ;


The problem is the <file> attribute is always the name of the file that contains the include directive.  That is:

file1.cls:
--------------
#include file2.cls

while $01 < 1
   let $01 = 1
endwhile
--------------

file2.cls:
--------------
while $01 < 2
   let $01 = 2
endwhile
--------------

Translates to:

; while ${index} < 2
let ${_source_line} = file1.cls,1
L_0_while:
        test L_0_while_end: ${index} < 2

        let ${index} = 2

        ; endwhile
        goto L_0_while:
L_0_while_end:

; while ${index} < 1
let ${_source_line} = file1.cls,3
L_1_while:
        test L_1_while_end: ${index} < 1

        let ${index} = 1

        ; endwhile
        goto L_1_while:
L_1_while_end:

Notice that even though the line numbers are correct, the <file> attribute evaluates to file1.cls when processing file2.cls.

I've poured over the ANTLR Java runtime API documentation and it would appear that input.getSourceName() should change when processing the included file.

Does anyone have an idea on what I'm doing wrong?


Thanks


More information about the antlr-interest mailing list