fixed 8 unit tests; only 9 fail now.

This commit is contained in:
Terence Parr 2012-02-13 17:47:26 -08:00
parent 0031075da2
commit 203ad46820
6 changed files with 42 additions and 27 deletions

View File

@ -31,12 +31,14 @@ package org.antlr.v4.analysis;
import org.antlr.runtime.*; import org.antlr.runtime.*;
import org.antlr.v4.Tool; 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.parse.*;
import org.antlr.v4.tool.*; import org.antlr.v4.tool.*;
import org.antlr.v4.tool.ast.*; 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. /** Remove left-recursive rule refs, add precedence args to recursive rule refs.
* Rewrite rule so we can create ATN. * Rewrite rule so we can create ATN.
@ -192,7 +194,7 @@ public class LeftRecursiveRuleTransformer {
*/ */
public void setAltASTPointers(LeftRecursiveRule r, RuleAST t) { 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); BlockAST ruleBlk = (BlockAST)t.getFirstChildWithType(ANTLRParser.BLOCK);
AltAST mainAlt = (AltAST)ruleBlk.getChild(0); AltAST mainAlt = (AltAST)ruleBlk.getChild(0);
BlockAST primaryBlk = (BlockAST)mainAlt.getChild(0); BlockAST primaryBlk = (BlockAST)mainAlt.getChild(0);

View File

@ -29,16 +29,24 @@
package org.antlr.v4.codegen; 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.RuleFunction;
import org.antlr.v4.codegen.model.chunk.*; import org.antlr.v4.codegen.model.chunk.*;
import org.antlr.v4.codegen.model.decl.StructDecl; import org.antlr.v4.codegen.model.decl.StructDecl;
import org.antlr.v4.parse.*; import org.antlr.v4.parse.ActionSplitter;
import org.antlr.v4.tool.*; 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 org.antlr.v4.tool.ast.ActionAST;
import java.lang.reflect.Constructor; 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 { public class ActionTranslator implements ActionSplitterListener {
@ -111,7 +119,6 @@ public class ActionTranslator implements ActionSplitterListener {
translator.rf = rf; translator.rf = rf;
factory.getGrammar().tool.log("action-translator", "translate " + action); factory.getGrammar().tool.log("action-translator", "translate " + action);
String altLabel = node.getAltLabel(); String altLabel = node.getAltLabel();
System.out.println("label="+altLabel);
if ( rf!=null ) translator.nodeContext = rf.ruleCtx; if ( rf!=null ) translator.nodeContext = rf.ruleCtx;
if ( altLabel!=null ) translator.nodeContext = rf.altLabelCtxs.get(altLabel); if ( altLabel!=null ) translator.nodeContext = rf.altLabelCtxs.get(altLabel);
ANTLRStringStream in = new ANTLRStringStream(action); ANTLRStringStream in = new ANTLRStringStream(action);

View File

@ -31,12 +31,17 @@ package org.antlr.v4.codegen.model;
import org.antlr.v4.codegen.OutputModelFactory; import org.antlr.v4.codegen.OutputModelFactory;
import org.antlr.v4.codegen.model.decl.*; 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.misc.Utils;
import org.antlr.v4.runtime.atn.ATNState; import org.antlr.v4.runtime.atn.ATNState;
import org.antlr.v4.runtime.misc.*; import org.antlr.v4.runtime.misc.IntervalSet;
import org.antlr.v4.tool.*; import org.antlr.v4.runtime.misc.OrderedHashSet;
import org.antlr.v4.tool.ast.*; 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.*; import java.util.*;
@ -149,7 +154,6 @@ public class RuleFunction extends OutputModelObject {
IntervalSet reftypes = new IntervalSet(RULE_REF, IntervalSet reftypes = new IntervalSet(RULE_REF,
TOKEN_REF); TOKEN_REF);
List<GrammarAST> refs = altAST.getNodesWithType(reftypes); List<GrammarAST> refs = altAST.getNodesWithType(reftypes);
System.out.println(refs);
FrequencySet<String> freq = new FrequencySet<String>(); FrequencySet<String> freq = new FrequencySet<String>();
for (GrammarAST t : refs) { for (GrammarAST t : refs) {
freq.add(t.getText()); freq.add(t.getText());
@ -193,10 +197,10 @@ public class RuleFunction extends OutputModelObject {
CodeBlockForOuterMostAlt alt = d.getOuterMostAltCodeBlock(); CodeBlockForOuterMostAlt alt = d.getOuterMostAltCodeBlock();
// if we found code blk and might be alt label, try to add to that label ctx // if we found code blk and might be alt label, try to add to that label ctx
if ( alt!=null && altLabelCtxs!=null ) { 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); AltLabelStructDecl altCtx = altLabelCtxs.get(altLabel);
if ( altCtx!=null ) { // we have an alt ctx 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); altCtx.addDecl(d);
return; return;
} }

View File

@ -31,7 +31,9 @@ package org.antlr.v4.test;
import org.antlr.v4.automata.ATNSerializer; import org.antlr.v4.automata.ATNSerializer;
import org.antlr.v4.runtime.atn.ATN; 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; import org.junit.Test;
public class TestATNSerialization extends BaseTest { public class TestATNSerialization extends BaseTest {
@ -463,8 +465,7 @@ public class TestATNSerialization extends BaseTest {
"13->15 ATOM 99,0,0\n" + "13->15 ATOM 99,0,0\n" +
"15->16 ACTION 2,1,0\n" + "15->16 ACTION 2,1,0\n" +
"16->6 EPSILON 0,0,0\n" + "16->6 EPSILON 0,0,0\n" +
"0:0 1\n" + "0:0 1\n";
"\n";
ATN atn = createATN(lg); ATN atn = createATN(lg);
String result = ATNSerializer.getDecoded(lg, atn); String result = ATNSerializer.getDecoded(lg, atn);
assertEquals(expecting, result); assertEquals(expecting, result);
@ -599,8 +600,7 @@ public class TestATNSerialization extends BaseTest {
"22->7 EPSILON 0,0,0\n" + "22->7 EPSILON 0,0,0\n" +
"0:0 1\n" + "0:0 1\n" +
"1:1 1\n" + "1:1 1\n" +
"2:12 1\n" + "2:12 1\n";
"\n";
ATN atn = createATN(lg); ATN atn = createATN(lg);
String result = ATNSerializer.getDecoded(lg, atn); String result = ATNSerializer.getDecoded(lg, atn);
assertEquals(expecting, result); assertEquals(expecting, result);

View File

@ -68,8 +68,9 @@ public class TestActionTranslation extends BaseTest {
@Test public void testComplicatedArgParsingWithTranslation() throws Exception { @Test public void testComplicatedArgParsingWithTranslation() throws Exception {
String action = "x, $ID.text+\"3242\", (*$ID).foo(21,33), 3.2+1, '\\n', "+ String action = "x, $ID.text+\"3242\", (*$ID).foo(21,33), 3.2+1, '\\n', "+
"\"a,oo\\nick\", {bl, \"fdkj\"eck}"; "\"a,oo\\nick\", {bl, \"fdkj\"eck}";
String expected = "x, (_localctx.ID!=null?_localctx.ID.getText():null)+\"3242\"," + String expected =
" (*_localctx.ID).foo(21,33), 3.2+1, '\\n', \"a,oo\\nick\", {bl, \"fdkj\"eck}"; "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); testActions(attributeTemplate, "inline", action, expected);
} }
@ -99,25 +100,25 @@ public class TestActionTranslation extends BaseTest {
@Test public void testReturnValues() throws Exception { @Test public void testReturnValues() throws Exception {
String action = "$lab.e; $b.e;"; 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); testActions(attributeTemplate, "inline", action, expected);
} }
@Test public void testReturnWithMultipleRuleRefs() throws Exception { @Test public void testReturnWithMultipleRuleRefs() throws Exception {
String action = "$c.x; $c.y;"; 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); testActions(attributeTemplate, "inline", action, expected);
} }
@Test public void testTokenRefs() throws Exception { @Test public void testTokenRefs() throws Exception {
String action = "$id; $ID; $id.text; $id.getText(); $id.line;"; 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); testActions(attributeTemplate, "inline", action, expected);
} }
@Test public void testRuleRefs() throws Exception { @Test public void testRuleRefs() throws Exception {
String action = "$lab.start; $c.text;"; 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); testActions(attributeTemplate, "inline", action, expected);
} }

View File

@ -135,7 +135,8 @@ public class TestToolSyntaxErrors extends BaseTest {
"mode foo;\n" + "mode foo;\n" +
"b : B ;", "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); super.testErrors(pair, true);
} }