diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/ParseTree.java b/runtime/Java/src/org/antlr/v4/runtime/tree/ParseTree.java index 5a74f9bdf..af47a2496 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/ParseTree.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/ParseTree.java @@ -94,7 +94,7 @@ public interface ParseTree extends SyntaxTree { super(token); } @Override - public String toString() { return ""; } + public String toString() { return ""; } } // the following methods narrow the return type; they are not additional methods diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/Trees.java b/runtime/Java/src/org/antlr/v4/runtime/tree/Trees.java index 8e1c7604b..771405f10 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/Trees.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/Trees.java @@ -64,6 +64,9 @@ public class Trees { String ruleName = recog.getRuleNames()[ruleIndex]; return ruleName; } + else if ( t instanceof ParseTree.ErrorNodeImpl ) { + return t.toString(); + } else if ( t instanceof ParseTree.TokenNode ) { Token tok = ((ParseTree.TokenNode) t).getToken(); return tok.getText(); diff --git a/tool/playground/TestW.java b/tool/playground/TestW.java index 1c93835f1..a2ad66075 100644 --- a/tool/playground/TestW.java +++ b/tool/playground/TestW.java @@ -1,16 +1,14 @@ -import org.antlr.v4.runtime.*; -import org.antlr.v4.runtime.tree.ParseTreeWalker; +import org.antlr.v4.runtime.ANTLRFileStream; +import org.antlr.v4.runtime.CommonTokenStream; +import org.antlr.v4.runtime.RuleContext; public class TestW { public static void main(String[] args) throws Exception { WLexer t = new WLexer(new ANTLRFileStream(args[0])); CommonTokenStream tokens = new CommonTokenStream(t); -// tokens.fill(); -// for (Object tok : tokens.getTokens()) { -// System.out.println(tok); -// } WParser p = new WParser(tokens); p.setBuildParseTrees(true); - p.s(); + RuleContext ctx = p.s(); + //System.out.println("ctx="+ctx.toStringTree(p)); } } diff --git a/tool/playground/W.g b/tool/playground/W.g index 48c17fd02..6bc000914 100644 --- a/tool/playground/W.g +++ b/tool/playground/W.g @@ -1,16 +1,14 @@ grammar W; -s : a ';' {System.out.println("done");} ; - -a : '[' b ']' - | '(' b ')' +s +@init {setBuildParseTrees(true);} +@after {System.out.println(_localctx.toStringTree(this));} + : a ; - -b : c '^' INT ; - -c : ID - | INT + +a : 'x' | 'y' ; +Z : 'z'; EQ : '=' ; INT : '0'..'9'+ ; diff --git a/tool/src/org/antlr/v4/codegen/ActionTranslator.java b/tool/src/org/antlr/v4/codegen/ActionTranslator.java index a2b485f6d..c9769cea7 100644 --- a/tool/src/org/antlr/v4/codegen/ActionTranslator.java +++ b/tool/src/org/antlr/v4/codegen/ActionTranslator.java @@ -29,14 +29,19 @@ package org.antlr.v4.codegen; -import org.antlr.runtime.*; +import org.antlr.runtime.ANTLRStringStream; +import org.antlr.runtime.Token; import org.antlr.v4.codegen.model.RuleFunction; import org.antlr.v4.codegen.model.actions.*; -import org.antlr.v4.parse.*; +import org.antlr.v4.parse.ActionSplitter; +import org.antlr.v4.parse.ActionSplitterListener; import org.antlr.v4.tool.*; import java.lang.reflect.Constructor; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; /** */ public class ActionTranslator implements ActionSplitterListener { @@ -152,6 +157,10 @@ public class ActionTranslator implements ActionSplitterListener { chunks.add(new ListLabelRef(x.getText())); // $ids for ids+=ID etc... return; } + Rule r = factory.getGrammar().getRule(x.getText()); + if ( r!=null ) { + chunks.add(new LabelRef(getRuleLabel(x.getText()))); // $r for r rule ref + } } /** $x.y = expr; */ diff --git a/tool/src/org/antlr/v4/semantics/AttributeChecks.java b/tool/src/org/antlr/v4/semantics/AttributeChecks.java index 97d21d309..21fa567ca 100644 --- a/tool/src/org/antlr/v4/semantics/AttributeChecks.java +++ b/tool/src/org/antlr/v4/semantics/AttributeChecks.java @@ -149,8 +149,8 @@ public class AttributeChecks implements ActionSplitterListener { return; // $ids for ids+=ID etc... } if ( isolatedRuleRef(x.getText())!=null ) { - errMgr.grammarError(ErrorType.ISOLATED_RULE_REF, - g.fileName, x, x.getText(), expr); +// errMgr.grammarError(ErrorType.ISOLATED_RULE_REF, +// g.fileName, x, x.getText(), expr); return; } errMgr.grammarError(ErrorType.UNKNOWN_SIMPLE_ATTRIBUTE, diff --git a/tool/src/org/antlr/v4/tool/Rule.java b/tool/src/org/antlr/v4/tool/Rule.java index a289d227e..7437ab6c2 100644 --- a/tool/src/org/antlr/v4/tool/Rule.java +++ b/tool/src/org/antlr/v4/tool/Rule.java @@ -231,7 +231,10 @@ public class Rule implements AttributeResolver { } public boolean resolvesToLabel(String x, ActionAST node) { - return false; + LabelElementPair anyLabelDef = getAnyLabelDef(x); + return anyLabelDef!=null && + (anyLabelDef.type==LabelType.RULE_LABEL || + anyLabelDef.type==LabelType.TOKEN_LABEL); } public boolean resolvesToListLabel(String x, ActionAST node) { diff --git a/tool/test/org/antlr/v4/test/TestParseTrees.java b/tool/test/org/antlr/v4/test/TestParseTrees.java index 6c0734725..0352d3b3c 100644 --- a/tool/test/org/antlr/v4/test/TestParseTrees.java +++ b/tool/test/org/antlr/v4/test/TestParseTrees.java @@ -6,12 +6,12 @@ public class TestParseTrees extends BaseTest { @Test public void testToken() throws Exception { String grammar = "grammar T;\n" + - "a\n" + + "s\n" + "@init {setBuildParseTrees(true);}\n" + - "@after {System.out.println(_localctx.toStringTree(this));}\n" + - " : 'x'\n" + - " ;\n"; - String result = execParser("T.g", grammar, "TParser", "TLexer", "a", "x", false); + "@after {System.out.println($r.toStringTree(this));}\n" + + " :r=a ;\n" + + "a : 'x' ;\n"; + String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "x", false); String expecting = "(a x)\n"; assertEquals(expecting, result); } @@ -19,13 +19,89 @@ public class TestParseTrees extends BaseTest { @Test public void testToken2() throws Exception { String grammar = "grammar T;\n" + - "a\n" + + "s\n" + "@init {setBuildParseTrees(true);}\n" + - "@after {System.out.println(_localctx.toStringTree(this));}\n" + - " : 'x' 'y'\n" + + "@after {System.out.println($r.toStringTree(this));}\n" + + " :r=a ;\n" + + "a : 'x' 'y'\n" + " ;\n"; - String result = execParser("T.g", grammar, "TParser", "TLexer", "a", "xy", false); + String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "xy", false); String expecting = "(a x y)\n"; assertEquals(expecting, result); } + + @Test public void test2Alts() throws Exception { + String grammar = + "grammar T;\n" + + "s\n" + + "@init {setBuildParseTrees(true);}\n" + + "@after {System.out.println($r.toStringTree(this));}\n" + + " :r=a ;\n" + + "a : 'x' | 'y'\n" + + " ;\n"; + String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "y", false); + String expecting = "(a y)\n"; + assertEquals(expecting, result); + } + + @Test public void test2AltLoop() throws Exception { + String grammar = + "grammar T;\n" + + "s\n" + + "@init {setBuildParseTrees(true);}\n" + + "@after {System.out.println($r.toStringTree(this));}\n" + + " :r=a ;\n" + + "a : ('x' | 'y')* 'z'\n" + + " ;\n"; + String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "xyyxyxz", false); + String expecting = "(a x y y x y x z)\n"; + assertEquals(expecting, result); + } + + @Test public void testRuleRef() throws Exception { + String grammar = + "grammar T;\n" + + "s\n" + + "@init {setBuildParseTrees(true);}\n" + + "@after {System.out.println($r.toStringTree(this));}\n" + + " : r=a ;\n" + + "a : b 'x'\n" + + " ;\n" + + "b : 'y' ;\n"; + String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "yx", false); + String expecting = "(a (b y) x)\n"; + assertEquals(expecting, result); + } + + // ERRORS + + @Test public void testExtraToken() throws Exception { + String grammar = + "grammar T;\n" + + "s\n" + + "@init {setBuildParseTrees(true);}\n" + + "@after {System.out.println($r.toStringTree(this));}\n" + + " : r=a ;\n" + + "a : 'x' 'y'\n" + + " ;\n" + + "Z : 'z'; \n"; + String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "xzy", false); + String expecting = "(a x y)\n"; + assertEquals(expecting, result); + } + + @Test public void testNoViableAlt() throws Exception { + String grammar = + "grammar T;\n" + + "s\n" + + "@init {setBuildParseTrees(true);}\n" + + "@after {System.out.println($r.toStringTree(this));}\n" + + " : r=a ;\n" + + "a : 'x' | 'y'\n" + + " ;\n" + + "Z : 'z'; \n"; + String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "z", false); + String expecting = "(a )\n"; + assertEquals(expecting, result); + } }