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

Pauba, Kevin L KLPauba at west.com
Thu May 27 06:48:12 PDT 2010


Although it's bad form to reply to my own messages, I found a solution to my problem.

Jim Idle's suggestion to search the antlr.markmail.org site helped (it, indeed, is wonderful tool).

I modified:

file={input.getSourceName()},

to:

file={$kw.getInputStream().getSourceName()},

and that solved my problem.


Thanks Jim for helping me help myself!

-----Original Message-----
From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Pauba, Kevin L
Sent: Wednesday, May 26, 2010 9:39 AM
To: antlr-interest at antlr.org
Subject: [antlr-interest] Using input.getSourceName() and INCLUDE processing

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

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address


More information about the antlr-interest mailing list