From 203ad4682038cf5432392041f1720d3241e83740 Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Mon, 13 Feb 2012 17:47:26 -0800 Subject: [PATCH] fixed 8 unit tests; only 9 fail now. --- .../analysis/LeftRecursiveRuleTransformer.java | 8 +++++--- .../org/antlr/v4/codegen/ActionTranslator.java | 17 ++++++++++++----- .../antlr/v4/codegen/model/RuleFunction.java | 18 +++++++++++------- .../antlr/v4/test/TestATNSerialization.java | 10 +++++----- .../antlr/v4/test/TestActionTranslation.java | 13 +++++++------ .../antlr/v4/test/TestToolSyntaxErrors.java | 3 ++- 6 files changed, 42 insertions(+), 27 deletions(-) diff --git a/tool/src/org/antlr/v4/analysis/LeftRecursiveRuleTransformer.java b/tool/src/org/antlr/v4/analysis/LeftRecursiveRuleTransformer.java index c6a0d4199..a4ef6e687 100644 --- a/tool/src/org/antlr/v4/analysis/LeftRecursiveRuleTransformer.java +++ b/tool/src/org/antlr/v4/analysis/LeftRecursiveRuleTransformer.java @@ -31,12 +31,14 @@ package org.antlr.v4.analysis; import org.antlr.runtime.*; import org.antlr.v4.Tool; -import org.antlr.v4.misc.*; +import org.antlr.v4.misc.OrderedHashMap; +import org.antlr.v4.misc.Pair; import org.antlr.v4.parse.*; import org.antlr.v4.tool.*; import org.antlr.v4.tool.ast.*; -import java.util.*; +import java.util.ArrayList; +import java.util.List; /** Remove left-recursive rule refs, add precedence args to recursive rule refs. * Rewrite rule so we can create ATN. @@ -192,7 +194,7 @@ public class LeftRecursiveRuleTransformer { */ public void setAltASTPointers(LeftRecursiveRule r, RuleAST t) { - System.out.println("RULE: "+t.toStringTree()); +// System.out.println("RULE: "+t.toStringTree()); BlockAST ruleBlk = (BlockAST)t.getFirstChildWithType(ANTLRParser.BLOCK); AltAST mainAlt = (AltAST)ruleBlk.getChild(0); BlockAST primaryBlk = (BlockAST)mainAlt.getChild(0); diff --git a/tool/src/org/antlr/v4/codegen/ActionTranslator.java b/tool/src/org/antlr/v4/codegen/ActionTranslator.java index b9f2b0d3c..09152f438 100644 --- a/tool/src/org/antlr/v4/codegen/ActionTranslator.java +++ b/tool/src/org/antlr/v4/codegen/ActionTranslator.java @@ -29,16 +29,24 @@ 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.chunk.*; import org.antlr.v4.codegen.model.decl.StructDecl; -import org.antlr.v4.parse.*; -import org.antlr.v4.tool.*; +import org.antlr.v4.parse.ActionSplitter; +import org.antlr.v4.parse.ActionSplitterListener; +import org.antlr.v4.tool.Attribute; +import org.antlr.v4.tool.ErrorType; +import org.antlr.v4.tool.Grammar; +import org.antlr.v4.tool.Rule; import org.antlr.v4.tool.ast.ActionAST; 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 { @@ -111,7 +119,6 @@ public class ActionTranslator implements ActionSplitterListener { translator.rf = rf; factory.getGrammar().tool.log("action-translator", "translate " + action); String altLabel = node.getAltLabel(); - System.out.println("label="+altLabel); if ( rf!=null ) translator.nodeContext = rf.ruleCtx; if ( altLabel!=null ) translator.nodeContext = rf.altLabelCtxs.get(altLabel); ANTLRStringStream in = new ANTLRStringStream(action); diff --git a/tool/src/org/antlr/v4/codegen/model/RuleFunction.java b/tool/src/org/antlr/v4/codegen/model/RuleFunction.java index 388ce663c..543c0f600 100644 --- a/tool/src/org/antlr/v4/codegen/model/RuleFunction.java +++ b/tool/src/org/antlr/v4/codegen/model/RuleFunction.java @@ -31,12 +31,17 @@ package org.antlr.v4.codegen.model; import org.antlr.v4.codegen.OutputModelFactory; import org.antlr.v4.codegen.model.decl.*; -import org.antlr.v4.misc.*; +import org.antlr.v4.misc.FrequencySet; +import org.antlr.v4.misc.Triple; import org.antlr.v4.misc.Utils; import org.antlr.v4.runtime.atn.ATNState; -import org.antlr.v4.runtime.misc.*; -import org.antlr.v4.tool.*; -import org.antlr.v4.tool.ast.*; +import org.antlr.v4.runtime.misc.IntervalSet; +import org.antlr.v4.runtime.misc.OrderedHashSet; +import org.antlr.v4.tool.Alternative; +import org.antlr.v4.tool.Attribute; +import org.antlr.v4.tool.Rule; +import org.antlr.v4.tool.ast.AltAST; +import org.antlr.v4.tool.ast.GrammarAST; import java.util.*; @@ -149,7 +154,6 @@ public class RuleFunction extends OutputModelObject { IntervalSet reftypes = new IntervalSet(RULE_REF, TOKEN_REF); List refs = altAST.getNodesWithType(reftypes); - System.out.println(refs); FrequencySet freq = new FrequencySet(); for (GrammarAST t : refs) { freq.add(t.getText()); @@ -193,10 +197,10 @@ public class RuleFunction extends OutputModelObject { CodeBlockForOuterMostAlt alt = d.getOuterMostAltCodeBlock(); // if we found code blk and might be alt label, try to add to that label ctx if ( alt!=null && altLabelCtxs!=null ) { - System.out.println(d.name+" lives in alt "+alt.alt.altNum); +// System.out.println(d.name+" lives in alt "+alt.alt.altNum); AltLabelStructDecl altCtx = altLabelCtxs.get(altLabel); if ( altCtx!=null ) { // we have an alt ctx - System.out.println("ctx is "+ altCtx.name); +// System.out.println("ctx is "+ altCtx.name); altCtx.addDecl(d); return; } diff --git a/tool/test/org/antlr/v4/test/TestATNSerialization.java b/tool/test/org/antlr/v4/test/TestATNSerialization.java index d106936e3..c3d2558e1 100644 --- a/tool/test/org/antlr/v4/test/TestATNSerialization.java +++ b/tool/test/org/antlr/v4/test/TestATNSerialization.java @@ -31,7 +31,9 @@ package org.antlr.v4.test; import org.antlr.v4.automata.ATNSerializer; import org.antlr.v4.runtime.atn.ATN; -import org.antlr.v4.tool.*; +import org.antlr.v4.tool.DOTGenerator; +import org.antlr.v4.tool.Grammar; +import org.antlr.v4.tool.LexerGrammar; import org.junit.Test; public class TestATNSerialization extends BaseTest { @@ -463,8 +465,7 @@ public class TestATNSerialization extends BaseTest { "13->15 ATOM 99,0,0\n" + "15->16 ACTION 2,1,0\n" + "16->6 EPSILON 0,0,0\n" + - "0:0 1\n" + - "\n"; + "0:0 1\n"; ATN atn = createATN(lg); String result = ATNSerializer.getDecoded(lg, atn); assertEquals(expecting, result); @@ -599,8 +600,7 @@ public class TestATNSerialization extends BaseTest { "22->7 EPSILON 0,0,0\n" + "0:0 1\n" + "1:1 1\n" + - "2:12 1\n" + - "\n"; + "2:12 1\n"; ATN atn = createATN(lg); String result = ATNSerializer.getDecoded(lg, atn); assertEquals(expecting, result); diff --git a/tool/test/org/antlr/v4/test/TestActionTranslation.java b/tool/test/org/antlr/v4/test/TestActionTranslation.java index 75a078fc4..20932f9a1 100644 --- a/tool/test/org/antlr/v4/test/TestActionTranslation.java +++ b/tool/test/org/antlr/v4/test/TestActionTranslation.java @@ -68,8 +68,9 @@ public class TestActionTranslation extends BaseTest { @Test public void testComplicatedArgParsingWithTranslation() throws Exception { String action = "x, $ID.text+\"3242\", (*$ID).foo(21,33), 3.2+1, '\\n', "+ "\"a,oo\\nick\", {bl, \"fdkj\"eck}"; - String expected = "x, (_localctx.ID!=null?_localctx.ID.getText():null)+\"3242\"," + - " (*_localctx.ID).foo(21,33), 3.2+1, '\\n', \"a,oo\\nick\", {bl, \"fdkj\"eck}"; + String expected = + "x, (((aContext)_localctx).ID!=null?((aContext)_localctx).ID.getText():null)+\"3242\", " + + "(*((aContext)_localctx).ID).foo(21,33), 3.2+1, '\\n', \"a,oo\\nick\", {bl, \"fdkj\"eck}"; testActions(attributeTemplate, "inline", action, expected); } @@ -99,25 +100,25 @@ public class TestActionTranslation extends BaseTest { @Test public void testReturnValues() throws Exception { String action = "$lab.e; $b.e;"; - String expected = "_localctx.lab.e; _localctx.b.e;"; + String expected = "((aContext)_localctx).lab.e; ((aContext)_localctx).b.e;"; testActions(attributeTemplate, "inline", action, expected); } @Test public void testReturnWithMultipleRuleRefs() throws Exception { String action = "$c.x; $c.y;"; - String expected = "_localctx.c.x; _localctx.c.y;"; + String expected = "((aContext)_localctx).c.x; ((aContext)_localctx).c.y;"; testActions(attributeTemplate, "inline", action, expected); } @Test public void testTokenRefs() throws Exception { String action = "$id; $ID; $id.text; $id.getText(); $id.line;"; - String expected = "_localctx.id; _localctx.ID; (_localctx.id!=null?_localctx.id.getText():null); _localctx.id.getText(); (_localctx.id!=null?_localctx.id.getLine():0);"; + String expected = "((aContext)_localctx).id; ((aContext)_localctx).ID; (((aContext)_localctx).id!=null?((aContext)_localctx).id.getText():null); ((aContext)_localctx).id.getText(); (((aContext)_localctx).id!=null?((aContext)_localctx).id.getLine():0);"; testActions(attributeTemplate, "inline", action, expected); } @Test public void testRuleRefs() throws Exception { String action = "$lab.start; $c.text;"; - String expected = "(_localctx.lab!=null?(_localctx.lab.start):null); (_localctx.c!=null?_input.toString(_localctx.c.start,_localctx.c.stop):null);"; + String expected = "(((aContext)_localctx).lab!=null?(((aContext)_localctx).lab.start):null); (((aContext)_localctx).c!=null?_input.toString(((aContext)_localctx).c.start,((aContext)_localctx).c.stop):null);"; testActions(attributeTemplate, "inline", action, expected); } diff --git a/tool/test/org/antlr/v4/test/TestToolSyntaxErrors.java b/tool/test/org/antlr/v4/test/TestToolSyntaxErrors.java index db879c70c..8c25bae88 100644 --- a/tool/test/org/antlr/v4/test/TestToolSyntaxErrors.java +++ b/tool/test/org/antlr/v4/test/TestToolSyntaxErrors.java @@ -135,7 +135,8 @@ public class TestToolSyntaxErrors extends BaseTest { "mode foo;\n" + "b : B ;", - "error(87): A.g:3:5: lexical modes are only allowed in lexer grammars\n" + ": A.g:4:0: 'b' came as a complete surprise to me\n" + + ": A.g:4:6: mismatched input ';' expecting COLON while matching a lexer rule\n" }; super.testErrors(pair, true); }