added unit tests for scope parser

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 6680]
This commit is contained in:
parrt 2010-02-10 10:42:57 -08:00
parent 6f2a5f3c57
commit 56a0940360
3 changed files with 445 additions and 15 deletions

View File

@ -19,20 +19,6 @@ import java.util.List;
* use "int i" but ruby/python would use "i". * use "int i" but ruby/python would use "i".
*/ */
public class ScopeParser { public class ScopeParser {
public static void main(String[] args) {
System.out.println(parseTypeList("int i"));
System.out.println(parseTypeList("int[] i, int j[]"));
System.out.println(parseTypeList("Map<A\\,B>[] i, int j[]"));
System.out.println(parseTypeList("int i = 34+a[3], int j[] = new int[34]"));
System.out.println(parseTypeList("char *foo32[3] = {1\\,2\\,3}"));
System.out.println();
System.out.println(parseDynamicScope("int i;"));
System.out.println(parseDynamicScope("int[] i; int j[];"));
System.out.println(parseDynamicScope("Map<A,B>[] i; int j[];"));
System.out.println(parseDynamicScope("int i = 34+a[3]; int j[] = new int[34];"));
System.out.println(parseDynamicScope("char *foo32[] = {1,2,3};"));
}
/** Given an arg or retval scope definition list like /** Given an arg or retval scope definition list like
* *
* Map<String, String>, int[] j3, char *foo32[3] * Map<String, String>, int[] j3, char *foo32[3]
@ -67,7 +53,7 @@ public class ScopeParser {
i++; i++;
} }
i++; // skip separator i++; // skip separator
System.out.println("def="+buf.toString()); // System.out.println("def="+buf.toString());
Attribute a = parseAttributeDef(buf.toString()); Attribute a = parseAttributeDef(buf.toString());
scope.attributes.put(a.name, a); scope.attributes.put(a.name, a);
} }

View File

@ -0,0 +1,383 @@
package org.antlr.v4.test;
import org.junit.Test;
/** */
public class TestAttributes extends BaseTest {
@Test public void testEscapedLessThanInAction() throws Exception {
String action = "i<3; '<xmltag>'";
}
@Test public void testEscaped$InAction() throws Exception {
String action = "int \\$n; \"\\$in string\\$\"";
}
@Test public void testArguments() throws Exception {
String action = "$i; $i.x; $u; $u.x";
}
@Test public void testComplicatedArgParsing() throws Exception {
String action = "x, (*a).foo(21,33), 3.2+1, '\\n', "+
"\"a,oo\\nick\", {bl, \"fdkj\"eck}";
}
@Test public void testBracketArgParsing() throws Exception {
}
@Test public void testStringArgParsing() throws Exception {
String action = "34, '{', \"it's<\", '\"', \"\\\"\", 19";
}
@Test public void testComplicatedSingleArgParsing() throws Exception {
String action = "(*a).foo(21,33,\",\")";
}
@Test public void testArgWithLT() throws Exception {
String action = "34<50";
}
@Test public void testGenericsAsArgumentDefinition() throws Exception {
String action = "$foo.get(\"ick\");";
}
@Test public void testGenericsAsArgumentDefinition2() throws Exception {
String action = "$foo.get(\"ick\"); x=3;";
}
@Test public void testGenericsAsReturnValue() throws Exception {
}
@Test public void testComplicatedArgParsingWithTranslation() throws Exception {
String action = "x, $A.text+\"3242\", (*$A).foo(21,33), 3.2+1, '\\n', "+
"\"a,oo\\nick\", {bl, \"fdkj\"eck}";
}
@Test public void testRefToReturnValueBeforeRefToPredefinedAttr() throws Exception {
String action = "$x.foo";
}
@Test public void testRuleLabelBeforeRefToPredefinedAttr() throws Exception {
String action = "$x.text";
}
@Test public void testInvalidArguments() throws Exception {
String action = "$x";
}
@Test public void testReturnValue() throws Exception {
String action = "$x.i";
}
@Test public void testReturnValueWithNumber() throws Exception {
String action = "$x.i1";
}
@Test public void testReturnValues() throws Exception {
String action = "$i; $i.x; $u; $u.x";
}
@Test public void testReturnWithMultipleRuleRefs() throws Exception {
String action1 = "$obj = $rule2.obj;";
String action2 = "$obj = $rule3.obj;";
String expecting1 = "obj = rule21;";
String expecting2 = "obj = rule32;";
String action = action1;
}
@Test public void testInvalidReturnValues() throws Exception {
String action = "$x";
}
@Test public void testTokenLabels() throws Exception {
String action = "$id; $f; $id.text; $id.getText(); $id.dork " +
"$id.type; $id.line; $id.pos; " +
"$id.channel; $id.index;";
}
@Test public void testRuleLabels() throws Exception {
String action = "$r.x; $r.start;\n $r.stop;\n $r.tree; $a.x; $a.stop;";
}
@Test public void testAmbiguRuleRef() throws Exception {
}
@Test public void testRuleLabelsWithSpecialToken() throws Exception {
String action = "$r.x; $r.start; $r.stop; $r.tree; $a.x; $a.stop;";
}
@Test public void testForwardRefRuleLabels() throws Exception {
String action = "$r.x; $r.start; $r.stop; $r.tree; $a.x; $a.tree;";
}
@Test public void testInvalidRuleLabelAccessesParameter() throws Exception {
String action = "$r.z";
}
@Test public void testInvalidRuleLabelAccessesScopeAttribute() throws Exception {
String action = "$r.n";
}
@Test public void testInvalidRuleAttribute() throws Exception {
String action = "$r.blort";
}
@Test public void testMissingRuleAttribute() throws Exception {
String action = "$r";
}
@Test public void testMissingUnlabeledRuleAttribute() throws Exception {
String action = "$a";
}
@Test public void testNonDynamicAttributeOutsideRule() throws Exception {
String action = "public void foo() { $x; }";
}
@Test public void testNonDynamicAttributeOutsideRule2() throws Exception {
String action = "public void foo() { $x.y; }";
}
@Test public void testBasicGlobalScope() throws Exception {
String action = "$Symbols::names.add($id.text);";
}
@Test public void testUnknownGlobalScope() throws Exception {
String action = "$Symbols::names.add($id.text);";
}
@Test public void testIndexedGlobalScope() throws Exception {
String action = "$Symbols[-1]::names.add($id.text);";
}
@Test public void test0IndexedGlobalScope() throws Exception {
String action = "$Symbols[0]::names.add($id.text);";
}
@Test public void testAbsoluteIndexedGlobalScope() throws Exception {
String action = "$Symbols[3]::names.add($id.text);";
}
@Test public void testScopeAndAttributeWithUnderscore() throws Exception {
String action = "$foo_bar::a_b;";
}
@Test public void testSharedGlobalScope() throws Exception {
String action = "$Symbols::x;";
}
@Test public void testGlobalScopeOutsideRule() throws Exception {
String action = "public void foo() {$Symbols::names.add('foo');}";
}
@Test public void testRuleScopeOutsideRule() throws Exception {
String action = "public void foo() {$a::name;}";
}
@Test public void testBasicRuleScope() throws Exception {
String action = "$a::n;";
}
@Test public void testUnqualifiedRuleScopeAccessInsideRule() throws Exception {
String action = "$n;";
}
@Test public void testIsolatedDynamicRuleScopeRef() throws Exception {
String action = "$a;"; // refers to stack not top of stack
}
@Test public void testDynamicRuleScopeRefInSubrule() throws Exception {
String action = "$a::n;";
}
@Test public void testIsolatedGlobalScopeRef() throws Exception {
String action = "$Symbols;";
}
@Test public void testRuleScopeFromAnotherRule() throws Exception {
String action = "$a::n;"; // must be qualified
}
@Test public void testFullyQualifiedRefToCurrentRuleParameter() throws Exception {
String action = "$a.i;";
}
@Test public void testFullyQualifiedRefToCurrentRuleRetVal() throws Exception {
String action = "$a.i;";
}
@Test public void testSetFullyQualifiedRefToCurrentRuleRetVal() throws Exception {
String action = "$a.i = 1;";
}
@Test public void testIsolatedRefToCurrentRule() throws Exception {
String action = "$a;";
}
@Test public void testIsolatedRefToRule() throws Exception {
String action = "$x;";
}
@Test public void testFullyQualifiedRefToLabelInCurrentRule() throws Exception {
String action = "$a.x;";
}
@Test public void testFullyQualifiedRefToListLabelInCurrentRule() throws Exception {
String action = "$a.x;"; // must be qualified
}
@Test public void testFullyQualifiedRefToTemplateAttributeInCurrentRule() throws Exception {
String action = "$a.st;"; // can be qualified
}
@Test public void testRuleRefWhenRuleHasScope() throws Exception {
String action = "$b.start;";
}
@Test public void testDynamicScopeRefOkEvenThoughRuleRefExists() throws Exception {
String action = "$b::n;";
}
@Test public void testRefToTemplateAttributeForCurrentRule() throws Exception {
String action = "$st=null;";
}
@Test public void testRefToTextAttributeForCurrentRule() throws Exception {
String action = "$text";
}
@Test public void testRefToStartAttributeForCurrentRule() throws Exception {
String action = "$start;";
}
@Test public void testTokenLabelFromMultipleAlts() throws Exception {
String action = "$ID.text;"; // must be qualified
}
@Test public void testRuleLabelFromMultipleAlts() throws Exception {
String action = "$b.text;"; // must be qualified
}
@Test public void testUnknownDynamicAttribute() throws Exception {
String action = "$a::x";
}
@Test public void testUnknownGlobalDynamicAttribute() throws Exception {
String action = "$Symbols::x";
}
@Test public void testUnqualifiedRuleScopeAttribute() throws Exception {
String action = "$n;"; // must be qualified
}
@Test public void testRuleAndTokenLabelTypeMismatch() throws Exception {
}
@Test public void testListAndTokenLabelTypeMismatch() throws Exception {
}
@Test public void testListAndRuleLabelTypeMismatch() throws Exception {
}
@Test public void testArgReturnValueMismatch() throws Exception {
}
@Test public void testSimplePlusEqualLabel() throws Exception {
String action = "$ids.size();"; // must be qualified
}
@Test public void testPlusEqualStringLabel() throws Exception {
String action = "$ids.size();"; // must be qualified
}
@Test public void testPlusEqualSetLabel() throws Exception {
String action = "$ids.size();"; // must be qualified
}
@Test public void testPlusEqualWildcardLabel() throws Exception {
String action = "$ids.size();"; // must be qualified
}
@Test public void testImplicitTokenLabel() throws Exception {
String action = "$ID; $ID.text; $ID.getText()";
}
@Test public void testImplicitRuleLabel() throws Exception {
String action = "$r.start;";
}
@Test public void testReuseExistingLabelWithImplicitRuleLabel() throws Exception {
String action = "$r.start;";
}
@Test public void testReuseExistingListLabelWithImplicitRuleLabel() throws Exception {
String action = "$r.start;";
}
@Test public void testReuseExistingLabelWithImplicitTokenLabel() throws Exception {
String action = "$ID.text;";
}
@Test public void testReuseExistingListLabelWithImplicitTokenLabel() throws Exception {
String action = "$ID.text;";
}
@Test public void testRuleLabelWithoutOutputOption() throws Exception {
}
@Test public void testRuleLabelOnTwoDifferentRulesAST() throws Exception {
}
@Test public void testRuleLabelOnTwoDifferentRulesTemplate() throws Exception {
}
@Test public void testMissingArgs() throws Exception {
}
@Test public void testArgsWhenNoneDefined() throws Exception {
}
@Test public void testReturnInitValue() throws Exception {
}
@Test public void testMultipleReturnInitValue() throws Exception {
}
@Test public void testCStyleReturnInitValue() throws Exception {
}
@Test public void testArgsWithInitValues() throws Exception {
}
@Test public void testArgsOnToken() throws Exception {
}
@Test public void testArgsOnTokenInLexer() throws Exception {
}
@Test public void testLabelOnRuleRefInLexer() throws Exception {
String action = "$i.text";
}
@Test public void testRefToRuleRefInLexer() throws Exception {
String action = "$ID.text";
}
@Test public void testRefToRuleRefInLexerNoAttribute() throws Exception {
String action = "$ID";
}
@Test public void testCharLabelInLexer() throws Exception {
}
@Test public void testCharListLabelInLexer() throws Exception {
}
@Test public void testWildcardCharLabelInLexer() throws Exception {
}
@Test public void testWildcardCharListLabelInLexer() throws Exception {
}
@Test public void testMissingArgsInLexer() throws Exception {
}
@Test public void testLexerRulePropertyRefs() throws Exception {
String action = "$text $type $line $pos $channel $index $start $stop";
}
@Test public void testLexerLabelRefs() throws Exception {
String action = "$a $b.text $c $d.text";
}
@Test public void testSettingLexerRulePropertyRefs() throws Exception {
String action = "$text $type=1 $line=1 $pos=1 $channel=1 $index";
}
@Test public void testArgsOnTokenInLexerRuleOfCombined() throws Exception {
}
@Test public void testMissingArgsOnTokenInLexerRuleOfCombined() throws Exception {
}
@Test public void testTokenLabelTreeProperty() throws Exception {
String action = "$id.tree;";
}
@Test public void testTokenRefTreeProperty() throws Exception {
String action = "$ID.tree;";
}
@Test public void testAmbiguousTokenRef() throws Exception {
String action = "$ID;";
}
@Test public void testAmbiguousTokenRefWithProp() throws Exception {
String action = "$ID.text;";
}
@Test public void testRuleRefWithDynamicScope() throws Exception {
String action = "$field::x = $field.st;";
}
@Test public void testAssignToOwnRulenameAttr() throws Exception {
String action = "$rule.tree = null;";
}
@Test public void testAssignToOwnParamAttr() throws Exception {
String action = "$rule.i = 42; $i = 23;";
}
@Test public void testIllegalAssignToOwnRulenameAttr() throws Exception {
String action = "$rule.stop = 0;";
}
@Test public void testIllegalAssignToLocalAttr() throws Exception {
String action = "$tree = null; $st = null; $start = 0; $stop = 0; $text = 0;";
}
@Test public void testIllegalAssignRuleRefAttr() throws Exception {
String action = "$other.tree = null;";
}
@Test public void testIllegalAssignTokenRefAttr() throws Exception {
String action = "$ID.text = \"test\";";
}
@Test public void testAssignToTreeNodeAttribute() throws Exception {
String action = "$tree.scope = localScope;";
}
@Test public void testDoNotTranslateAttributeCompare() throws Exception {
String action = "$a.line == $b.line";
}
@Test public void testDoNotTranslateScopeAttributeCompare() throws Exception {
String action = "if ($rule::foo == \"foo\" || 1) { System.out.println(\"ouch\"); }";
}
@Test public void testTreeRuleStopAttributeIsInvalid() throws Exception {
String action = "$r.x; $r.start; $r.stop";
}
@Test public void testRefToTextAttributeForCurrentTreeRule() throws Exception {
String action = "$text";
}
@Test public void testTypeOfGuardedAttributeRefIsCorrect() throws Exception {
String action = "int x = $b::n;";
}
}

View File

@ -0,0 +1,61 @@
package org.antlr.v4.test;
import org.antlr.v4.parse.ScopeParser;
import org.junit.Test;
public class TestScopeParsing extends BaseTest {
String[] argPairs = {
"int i", "{i=int i}",
"int[] i, int j[]", "{i=int[] i, j=int [] j}",
"Map<A\\,B>[] i, int j[]", "{i=Map<A,B>[] i, j=int [] j}",
"int i = 34+a[3], int j[] = new int[34]",
"{i=int i= 34+a[3], j=int [] j= new int[34]}",
"char *foo32[3] = {1\\,2\\,3}", "{3=char *foo32[] 3= {1,2,3}}",
// python/ruby style
"i", "{i=null i}",
"i,j", "{i=null i, j=null j}",
"i,j, k", "{i=null i, j=null j, k=null k}",
};
String[] scopePairs = {
"int i;", "{i=int i}",
"int[] i; int j[];", "{i=int[] i, j=int [] j}",
"Map<A,B>[] i; int j[];", "{i=Map<A,B>[] i, j=int [] j}",
"int i = 34+a[3]; int j[] = new int[34];",
"{i=int i= 34+a[3], j=int [] j= new int[34]}",
"char *foo32[] = {1,2,3};", "{foo32=char *[] foo32= {1,2,3}}",
// python/ruby style
"i", "{i=null i}",
"i; j;", "{i=null i, j=null j}",
"i; j; k;", "{i=null i, j=null j, k=null k}",
};
public void testInputOutputPairs(String[] pairs) {
for (int i = 0; i < pairs.length; i+=2) {
String input = pairs[i];
String expected = pairs[i+1];
String actual = ScopeParser.parseTypeList(input).attributes.toString();
assertEquals(expected, actual);
}
}
@Test public void testArgs() {
for (int i = 0; i < argPairs.length; i+=2) {
String input = argPairs[i];
String expected = argPairs[i+1];
String actual = ScopeParser.parseTypeList(input).attributes.toString();
assertEquals(expected, actual);
}
}
@Test public void testScopes() {
for (int i = 0; i < scopePairs.length; i+=2) {
String input = scopePairs[i];
String expected = scopePairs[i+1];
String actual = ScopeParser.parseDynamicScope(input).attributes.toString();
assertEquals(expected, actual);
}
}
}