I'm using ANTLR4 to try to implement a language supporting include files, like PHP's include.
var a = 4 + 5; // line a
include "some.inc"; // include statement
var b = 9 * 9; // line b
Contents of some.inc:
a *= 2;
a +== 3; // Typo here (extranous equals sign)
I need to parse the tree such that the contents of some.inc are inserted into the location of the include statement.
How do I do that in ANTLR4?
I could, of course, build a new string an do some concatenation (e.g. lineA + getContentsOf("some.inc") + lineB) and then pass it to the lexer, but I'm afraid that line and column numbers get messed up, so I'd rather preserve the source path, line and column.
Edit: I want to warn the author of a piece of code in the target language if he made a mistake in his code. In the example above, the author made a typo. I want to warn the user that there is an error on line 2 of some.inc. If the includes are resolved (i.e. replaced) before the whole input was passed to the lexer, then the input stream would look like this:
var a = 4 + 5; // line a
a *= 2;
a +== 3; // Typo here (extranous equals sign)
var b = 9 * 9; // line b
The parser would not know that the malformed expression a +== 3 originally came from line 2 of some.inc, thus reporting the wrong position.
My current code looks like this:
CharStream cs = CharStreams.fromPath(mySourceCode);
MyLexer lexer = new MyLexer(cs);
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
MyParser parser = new MyParser(tokenStream);
System.out.println(parser.startRule());
includeto work like a preprocessor-based include (which I assume you don't since you've mentioned PHP's include, not C's#include), you definitely shouldn't do this at the source or token level. What are you doing after parsing the source? Generating byte code? Evaluating the AST directly in a visitor? Is there a reason why you can't resolve includes at that stage rather than during parsing? - sepp2k