tweak tests

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9108]
This commit is contained in:
parrt 2011-10-04 13:26:18 -08:00
parent e293cbbdf5
commit 6179d7586b
8 changed files with 119 additions and 32 deletions

View File

@ -94,7 +94,7 @@ public interface ParseTree extends SyntaxTree {
super(token);
}
@Override
public String toString() { return "<ERROR: "+super.toString()+">"; }
public String toString() { return "<ERROR:"+super.toString()+">"; }
}
// the following methods narrow the return type; they are not additional methods

View File

@ -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();

View File

@ -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));
}
}

View File

@ -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'+ ;

View File

@ -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; */

View File

@ -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,

View File

@ -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) {

View File

@ -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 <ERROR:z> 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 <ERROR:z>)\n";
assertEquals(expecting, result);
}
}