cleaned up action splitting

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 8737]
This commit is contained in:
parrt 2011-06-22 16:40:42 -08:00
parent c607e66f1a
commit 5f95cde48d
7 changed files with 79 additions and 55 deletions

View File

@ -1,8 +1,6 @@
import org.antlr.runtime.debug.BlankDebugEventListener;
import org.antlr.v4.runtime.ANTLRFileStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.atn.LexerInterpreter;
import org.antlr.v4.runtime.atn.ParserInterpreter;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.atn.*;
import java.io.File;
@ -103,7 +101,7 @@ class TestJava {
}
parser.setTokenStream(tokens);
// start parsing at the compilationUnit rule
parser.compilationUnit(null);
parser.compilationUnit();
//System.err.println("finished "+f);
}
}

View File

@ -1,8 +1,6 @@
import org.antlr.runtime.debug.BlankDebugEventListener;
import org.antlr.v4.runtime.ANTLRFileStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.atn.LexerInterpreter;
import org.antlr.v4.runtime.atn.ParserInterpreter;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.atn.*;
import java.io.File;
@ -103,7 +101,7 @@ class TestYang {
}
parser.setTokenStream(tokens);
// start parsing at the compilationUnit rule
parser.compilationUnit(null);
parser.compilationUnit();
//System.err.println("finished "+f);
}
}

View File

@ -42,11 +42,6 @@ LINE_COMMENT
: '//' ~('\n'|'\r')* '\r'? '\n' {delegate.text($text);}
;
ESC
: '\\$' {delegate.text("$");}
| '\\%' {delegate.text("\%");}
;
SET_QUALIFIED_ATTR
: '$' x=ID '.' y=ID WS? '=' expr=ATTR_VALUE_EXPR ';'
{delegate.setQualifiedAttr($text, $x, $y, $expr);}
@ -138,8 +133,13 @@ UNKNOWN_SYNTAX
// Anything else is just random text
TEXT
@after {delegate.text($text);}
: ~('$'|'%') // can't do (...)+ here since it gobbles \$, \%
@init {StringBuilder buf = new StringBuilder();}
@after {delegate.text(buf.toString());}
: ( c=~('\\'| '$'|'%') {buf.append((char)$c);}
| '\\$' {buf.append("$");}
| '\\%' {buf.append("\%");}
| '\\' c=~('$'|'%') {buf.append("\\"+(char)$c);}
)+
;
fragment

View File

@ -1,11 +1,11 @@
package org.antlr.v4.test;
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import org.antlr.runtime.RuleReturnScope;
import org.antlr.runtime.tree.Tree;
import org.antlr.v4.gunit.gUnitBase;
import org.junit.Test;
import org.junit.Before;
import static org.junit.Assert.*;
import org.junit.*;
import static org.junit.Assert.assertEquals;
public class TestASTStructure extends gUnitBase {
@Before public void setup() {
@ -334,7 +334,7 @@ public class TestASTStructure extends gUnitBase {
// gunit test on line 206
RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x=~(A|B)", 206);
Object actual = ((Tree)rstruct.getTree()).toStringTree();
Object expecting = "(= x (~ (BLOCK (ALT A) (ALT B))))";
Object expecting = "(= x (~ (BLOCK A B)))";
assertEquals("testing rule element", expecting, actual);
}
@ -342,7 +342,7 @@ public class TestASTStructure extends gUnitBase {
// gunit test on line 207
RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x+=~(A|B)", 207);
Object actual = ((Tree)rstruct.getTree()).toStringTree();
Object expecting = "(+= x (~ (BLOCK (ALT A) (ALT B))))";
Object expecting = "(+= x (~ (BLOCK A B)))";
assertEquals("testing rule element", expecting, actual);
}
@ -350,7 +350,7 @@ public class TestASTStructure extends gUnitBase {
// gunit test on line 208
RuleReturnScope rstruct = (RuleReturnScope)execParser("element", "x+=~(A|B)+", 208);
Object actual = ((Tree)rstruct.getTree()).toStringTree();
Object expecting = "(+ (BLOCK (ALT (+= x (~ (BLOCK (ALT A) (ALT B)))))))";
Object expecting = "(+ (BLOCK (ALT (+= x (~ (BLOCK A B))))))";
assertEquals("testing rule element", expecting, actual);
}

View File

@ -1,33 +1,31 @@
package org.antlr.v4.test;
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.Token;
import org.antlr.runtime.*;
import org.antlr.v4.parse.ActionSplitter;
import org.antlr.v4.semantics.BlankActionSplitterListener;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
public class TestActionSplitter extends BaseTest {
static String[] exprs = {
"foo", "['foo'<29>]",
"$x", "['$x'<20>]",
"\\$x", "['\\$'<6>, 'x'<29>]",
"$x.y", "['$x.y'<11>]",
"$ID.text", "['$ID.text'<11>]",
"$ID", "['$ID'<20>]",
"$ID.getText()", "['$ID'<20>, '.getText()'<29>]",
"$ID.text = \"test\";", "['$ID.text = \"test\";'<10>]",
"$a.line == $b.line", "['$a.line'<11>, ' == '<29>, '$b.line'<11>]",
"$r.tree", "['$r.tree'<11>]",
"foo $a::n bar", "['foo '<29>, '$a::n'<13>, ' bar'<29>]",
"$Symbols[-1]::names.add($id.text);", "['$Symbols[-1]::names'<16>, '.add('<29>, '$id.text'<11>, ');'<29>]",
"$Symbols[0]::names.add($id.text);", "['$Symbols[0]::names'<18>, '.add('<29>, '$id.text'<11>, ');'<29>]",
"$Symbols::x;", "['$Symbols::x'<13>, ';'<29>]",
"$Symbols.size()>0", "['$Symbols'<20>, '.size()>0'<29>]",
"$field::x = $field.st;", "['$field::x = $field.st;'<12>]",
"$foo.get(\"ick\");", "['$foo'<20>, '.get(\"ick\");'<29>]",
"foo", "['foo'<26>]",
"$x", "['$x'<6>]",
"\\$x", "['\\$x'<26>]",
"$x.y", "['$x.y'<15>]",
"$ID.text", "['$ID.text'<15>]",
"$ID", "['$ID'<6>]",
"$ID.getText()", "['$ID'<6>, '.getText()'<26>]",
"$ID.text = \"test\";", "['$ID.text = \"test\";'<23>]",
"$a.line == $b.line", "['$a.line'<15>, ' == '<26>, '$b.line'<15>]",
"$r.tree", "['$r.tree'<15>]",
"foo $a::n bar", "['foo '<26>, '$a::n'<11>, ' bar'<26>]",
"$Symbols[-1]::names.add($id.text);", "['$Symbols[-1]::names'<10>, '.add('<26>, '$id.text'<15>, ');'<26>]",
"$Symbols[0]::names.add($id.text);", "['$Symbols[0]::names'<9>, '.add('<26>, '$id.text'<15>, ');'<26>]",
"$Symbols::x;", "['$Symbols::x'<11>, ';'<26>]",
"$Symbols.size()>0", "['$Symbols'<6>, '.size()>0'<26>]",
"$field::x = $field.st;", "['$field::x = $field.st;'<21>]",
"$foo.get(\"ick\");", "['$foo'<6>, '.get(\"ick\");'<26>]",
};
@Test public void testExprs() {

View File

@ -1,17 +1,12 @@
package org.antlr.v4.test;
import org.antlr.runtime.RecognitionException;
import org.antlr.v4.automata.ATNFactory;
import org.antlr.v4.automata.LexerATNFactory;
import org.antlr.v4.automata.ParserATNFactory;
import org.antlr.v4.automata.*;
import org.antlr.v4.codegen.CodeGenerator;
import org.antlr.v4.semantics.SemanticPipeline;
import org.antlr.v4.tool.Grammar;
import org.antlr.v4.tool.LexerGrammar;
import org.antlr.v4.tool.*;
import org.junit.Test;
import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.STGroupString;
import org.stringtemplate.v4.*;
/** */
public class TestActionTranslation extends BaseTest {
@ -64,7 +59,7 @@ public class TestActionTranslation extends BaseTest {
@Test public void testEscaped$InAction() throws Exception {
String action = "int \\$n; \"\\$in string\\$\"";
String expected = "int \\$n; \"\\$in string\\$\"";
String expected = "int $n; \"$in string$\"";
testActions(attributeTemplate, "members", action, expected);
testActions(attributeTemplate, "init", action, expected);
testActions(attributeTemplate, "inline", action, expected);
@ -72,6 +67,16 @@ public class TestActionTranslation extends BaseTest {
testActions(attributeTemplate, "inline2", action, expected);
}
@Test public void testEscapedSlash() throws Exception {
String action = "x = '\\n';"; // x = '\n'; -> x = '\n';
String expected = "x = '\\n';";
testActions(attributeTemplate, "members", action, expected);
testActions(attributeTemplate, "init", action, expected);
testActions(attributeTemplate, "inline", action, expected);
testActions(attributeTemplate, "finally", action, expected);
testActions(attributeTemplate, "inline2", action, expected);
}
@Test public void testComplicatedArgParsing() throws Exception {
String action = "x, (*a).foo(21,33), 3.2+1, '\\n', "+
"\"a,oo\\nick\", {bl, \"fdkj\"eck}";

View File

@ -0,0 +1,25 @@
package org.antlr.v4.test;
import org.antlr.runtime.RecognitionException;
import org.antlr.v4.Tool;
import org.antlr.v4.codegen.CodeGenerator;
import org.antlr.v4.tool.LexerGrammar;
import org.junit.Test;
import org.stringtemplate.v4.ST;
public class TestLexerAttributes extends BaseTest {
@Test
public void testTokenRef() throws RecognitionException {
LexerGrammar g = new LexerGrammar(
"lexer grammar T;\n" +
"A : 'a' {#$type=101;#} ;\n"
);
Tool antlr = new Tool();
antlr.process(g);
CodeGenerator gen = new CodeGenerator(g);
ST outputFileST = gen.generate();
String output = outputFileST.render();
assertEquals("type=101", output);
}
}