I’m trying to implement a tool for merging different versions of some source code. Given two versions of the same source code, the idea would be to parse them, generate the respective Abstract Source Trees (AST), and finally merge them into a single output source keeping grammatical consistency – the lexer and parser are those of question ANTLR: How to skip multiline comments.
I know there is class ParserRuleReturnScope that helps… but getStop() and getStart() always return null 🙁
Here is a snippet that illustrates how I modified my perser to get rules printed:
parser grammar CodeTableParser;
options {
tokenVocab = CodeTableLexer;
backtrack = true;
output = AST;
}
@header {
package ch.bsource.ice.parsers;
}
@members {
private void log(ParserRuleReturnScope rule) {
System.out.println("Rule: " + rule.getClass().getName());
System.out.println(" getStart(): " + rule.getStart());
System.out.println(" getStop(): " + rule.getStop());
System.out.println(" getTree(): " + rule.getTree());
}
}
parse
: codeTabHeader codeTable endCodeTable eof { log(retval); }
;
codeTabHeader
: comment CodeTabHeader^ { log(retval); }
;
...
The call to
log(retval)in your parser code looks like it’s going to happen at the end of the rule, but it’s not. You’ll want to move the call into an@afterblock.I changed
logto spit out a message as well as the scope information and added calls to it to my own grammar like so:Parsing test input produced the following output:
The call in the
afterblock has both thestopandtreefields populated.I can’t say whether this will help you with your merging tool, but I think this will at least get you past the problem with the half-populated scope object.