validated Sets tests

This commit is contained in:
ericvergnaud 2014-10-24 22:46:12 +08:00
parent f99acb7202
commit e03c7d44e5
15 changed files with 93 additions and 307 deletions

View File

@ -1,4 +1,4 @@
grammar <grammarName>;
a : (A {<writeln("$A.text")>})+ ;
a : [AaBb] ;
A : [AaBb] ;
WS : (' '|'\n')+ -> skip ;

View File

@ -1,3 +1,3 @@
grammar <grammarName>;
a : A {<InputText():writeln()>} ;
a : ('a'|'b')? 'c' ;
A : ('a'|'b')? 'c' ;

View File

@ -1,3 +1,3 @@
grammar <grammarName>;
a : A {<InputText():writeln()>} ;
a : ('a'|'b')+ 'c' ;
A : ('a'|'b')+ 'c' ;

View File

@ -1,3 +1,3 @@
grammar <grammarName>;
a : A {<InputText():writeln()>} ;
a : ('a'|'b')* 'c' ;
A : ('a'|'b')* 'c' ;

View File

@ -1,3 +1,3 @@
grammar <grammarName>;
a : A {<writeln("$A.text")>} ;
a : ~'b' ;
A : ~'b' ;

View File

@ -1,3 +1,3 @@
grammar <grammarName>;
a : A {<writeln("$A.text")>} ;
a : ~('b'|'c') ;
A : ~('b'|'c') ;

View File

@ -1,3 +1,3 @@
grammar <grammarName>;
a : A {<writeln("$A.text")>} ;
a : h=~('b'|'c') ;
A : h=~('b'|'c') ;

View File

@ -1,5 +1,5 @@
grammar <grammarName>;
a : A {<writeln("$A.text")>} ;
a : ('a'|B) ; // this doesn't collapse to set but works
A : ('a'|B) ; // this doesn't collapse to set but works
fragment
B : ~('a'|'c') ;

View File

@ -1,3 +1,3 @@
grammar <grammarName>;
a : A {<InputText():writeln()>} ;
a : 'b'? 'c' ;
A : 'b'? 'c' ;

View File

@ -1,3 +1,3 @@
grammar <grammarName>;
a : A? 'c' {<InputText():writeln()>} ;
a : 'b' ;
A : 'b' ;

View File

@ -1,3 +1,3 @@
grammar <grammarName>;
a : A {<InputText():writeln()>} ;
a : 'b'+ 'c' ;
A : 'b'+ 'c' ;

View File

@ -1,3 +1,3 @@
grammar <grammarName>;
a : A {<InputText():writeln()>} ;
a : 'b'* 'c' ;
A : 'b'* 'c' ;

View File

@ -66,7 +66,7 @@ public class TestSets extends BaseTest {
public void testNotChar() throws Exception {
String grammar = "grammar T;\n" +
"a : A {System.out.println($A.text);} ;\n" +
"a : ~'b' ;";
"A : ~'b' ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "x", false);
assertEquals("x\n", found);
assertNull(this.stderrDuringParse);
@ -76,7 +76,7 @@ public class TestSets extends BaseTest {
public void testOptionalSingleElement() throws Exception {
String grammar = "grammar T;\n" +
"a : A? 'c' {System.out.println(this._input.getText());} ;\n" +
"a : 'b' ;";
"A : 'b' ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "bc", false);
assertEquals("bc\n", found);
assertNull(this.stderrDuringParse);
@ -86,7 +86,7 @@ public class TestSets extends BaseTest {
public void testOptionalLexerSingleElement() throws Exception {
String grammar = "grammar T;\n" +
"a : A {System.out.println(this._input.getText());} ;\n" +
"a : 'b'? 'c' ;";
"A : 'b'? 'c' ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "bc", false);
assertEquals("bc\n", found);
assertNull(this.stderrDuringParse);
@ -95,7 +95,7 @@ public class TestSets extends BaseTest {
String testStarLexerSingleElement(String input) throws Exception {
String grammar = "grammar T;\n" +
"a : A {System.out.println(this._input.getText());} ;\n" +
"a : 'b'* 'c' ;";
"A : 'b'* 'c' ;";
return execParser("T.g4", grammar, "TParser", "TLexer", "a", input, false);
}
@ -117,7 +117,7 @@ public class TestSets extends BaseTest {
public void testPlusLexerSingleElement() throws Exception {
String grammar = "grammar T;\n" +
"a : A {System.out.println(this._input.getText());} ;\n" +
"a : 'b'+ 'c' ;";
"A : 'b'+ 'c' ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "bbbbc", false);
assertEquals("bbbbc\n", found);
assertNull(this.stderrDuringParse);
@ -154,7 +154,7 @@ public class TestSets extends BaseTest {
public void testLexerOptionalSet() throws Exception {
String grammar = "grammar T;\n" +
"a : A {System.out.println(this._input.getText());} ;\n" +
"a : ('a'|'b')? 'c' ;";
"A : ('a'|'b')? 'c' ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "ac", false);
assertEquals("ac\n", found);
assertNull(this.stderrDuringParse);
@ -164,7 +164,7 @@ public class TestSets extends BaseTest {
public void testLexerStarSet() throws Exception {
String grammar = "grammar T;\n" +
"a : A {System.out.println(this._input.getText());} ;\n" +
"a : ('a'|'b')* 'c' ;";
"A : ('a'|'b')* 'c' ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "abaac", false);
assertEquals("abaac\n", found);
assertNull(this.stderrDuringParse);
@ -174,7 +174,7 @@ public class TestSets extends BaseTest {
public void testLexerPlusSet() throws Exception {
String grammar = "grammar T;\n" +
"a : A {System.out.println(this._input.getText());} ;\n" +
"a : ('a'|'b')+ 'c' ;";
"A : ('a'|'b')+ 'c' ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "abaac", false);
assertEquals("abaac\n", found);
assertNull(this.stderrDuringParse);
@ -184,7 +184,7 @@ public class TestSets extends BaseTest {
public void testNotCharSet() throws Exception {
String grammar = "grammar T;\n" +
"a : A {System.out.println($A.text);} ;\n" +
"a : ~('b'|'c') ;";
"A : ~('b'|'c') ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "x", false);
assertEquals("x\n", found);
assertNull(this.stderrDuringParse);
@ -194,7 +194,7 @@ public class TestSets extends BaseTest {
public void testNotCharSetWithLabel() throws Exception {
String grammar = "grammar T;\n" +
"a : A {System.out.println($A.text);} ;\n" +
"a : h=~('b'|'c') ;";
"A : h=~('b'|'c') ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "x", false);
assertEquals("x\n", found);
assertNull(this.stderrDuringParse);
@ -204,7 +204,7 @@ public class TestSets extends BaseTest {
public void testNotCharSetWithRuleRef3() throws Exception {
String grammar = "grammar T;\n" +
"a : A {System.out.println($A.text);} ;\n" +
"a : ('a'|B) ; // this doesn't collapse to set but works\n" +
"A : ('a'|B) ; // this doesn't collapse to set but works\n" +
"fragment\n" +
"B : ~('a'|'c') ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "x", false);
@ -216,7 +216,7 @@ public class TestSets extends BaseTest {
public void testCharSetLiteral() throws Exception {
String grammar = "grammar T;\n" +
"a : (A {System.out.println($A.text);})+ ;\n" +
"a : [AaBb] ;\n" +
"A : [AaBb] ;\n" +
"WS : (' '|'\\n')+ -> skip ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "A a B b", false);
assertEquals("A\na\nB\nb\n", found);

View File

@ -0,0 +1,69 @@
/*
* [The "BSD license"]
* Copyright (c) 2012 Terence Parr
* Copyright (c) 2012 Sam Harwell
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.antlr.v4.test.tool;
import org.antlr.v4.tool.ErrorType;
import org.junit.Test;
/** Test errors with the set stuff in lexer and parser */
public class TestErrorSets extends BaseTest {
protected boolean debug = false;
/** Public default constructor used by TestRig */
public TestErrorSets() {
}
@Test public void testNotCharSetWithRuleRef() throws Exception {
// might be a useful feature to add someday
String[] pair = new String[] {
"grammar T;\n" +
"a : A {System.out.println($A.text);} ;\n" +
"A : ~('a'|B) ;\n" +
"B : 'b' ;\n",
"error(" + ErrorType.UNSUPPORTED_REFERENCE_IN_LEXER_SET.code + "): T.g4:3:10: rule reference B is not currently supported in a set\n"
};
super.testErrors(pair, true);
}
@Test public void testNotCharSetWithString() throws Exception {
// might be a useful feature to add someday
String[] pair = new String[] {
"grammar T;\n" +
"a : A {System.out.println($A.text);} ;\n" +
"A : ~('a'|'aa') ;\n" +
"B : 'b' ;\n",
"error(" + ErrorType.INVALID_LITERAL_IN_LEXER_SET.code + "): T.g4:3:10: multi-character literals are not allowed in lexer sets: 'aa'\n"
};
super.testErrors(pair, true);
}
}

View File

@ -1,283 +0,0 @@
/*
* [The "BSD license"]
* Copyright (c) 2012 Terence Parr
* Copyright (c) 2012 Sam Harwell
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.antlr.v4.test.tool;
import org.antlr.v4.tool.ErrorType;
import org.junit.Test;
import static org.junit.Assert.*;
/** Test the set stuff in lexer and parser */
public class TestSets extends BaseTest {
protected boolean debug = false;
/** Public default constructor used by TestRig */
public TestSets() {
}
@Test public void testSeqDoesNotBecomeSet() throws Exception {
// this must return A not I to the parser; calling a nonfragment rule
// from a nonfragment rule does not set the overall token.
String grammar =
"grammar P;\n" +
"a : C {System.out.println(_input.getText());} ;\n" +
"fragment A : '1' | '2';\n" +
"fragment B : '3' '4';\n" +
"C : A | B;\n";
String found = execParser("P.g4", grammar, "PParser", "PLexer",
"a", "34", debug);
assertEquals("34\n", found);
}
@Test public void testParserSet() throws Exception {
String grammar =
"grammar T;\n" +
"a : t=('x'|'y') {System.out.println($t.text);} ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer",
"a", "x", debug);
assertEquals("x\n", found);
}
@Test public void testParserNotSet() throws Exception {
String grammar =
"grammar T;\n" +
"a : t=~('x'|'y') 'z' {System.out.println($t.text);} ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer",
"a", "zz", debug);
assertEquals("z\n", found);
}
@Test public void testParserNotToken() throws Exception {
String grammar =
"grammar T;\n" +
"a : ~'x' 'z' {System.out.println(_input.getText());} ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer",
"a", "zz", debug);
assertEquals("zz\n", found);
}
@Test public void testParserNotTokenWithLabel() throws Exception {
String grammar =
"grammar T;\n" +
"a : t=~'x' 'z' {System.out.println($t.text);} ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer",
"a", "zz", debug);
assertEquals("z\n", found);
}
@Test public void testRuleAsSet() throws Exception {
String grammar =
"grammar T;\n" +
"a @after {System.out.println(_input.getText());} : 'a' | 'b' |'c' ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer",
"a", "b", debug);
assertEquals("b\n", found);
}
@Test public void testNotChar() throws Exception {
String grammar =
"grammar T;\n" +
"a : A {System.out.println($A.text);} ;\n" +
"A : ~'b' ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer",
"a", "x", debug);
assertEquals("x\n", found);
}
@Test public void testOptionalSingleElement() throws Exception {
String grammar =
"grammar T;\n" +
"a : A? 'c' {System.out.println(_input.getText());} ;\n" +
"A : 'b' ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer",
"a", "bc", debug);
assertEquals("bc\n", found);
}
@Test public void testOptionalLexerSingleElement() throws Exception {
String grammar =
"grammar T;\n" +
"a : A {System.out.println(_input.getText());} ;\n" +
"A : 'b'? 'c' ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer",
"a", "bc", debug);
assertEquals("bc\n", found);
}
@Test public void testStarLexerSingleElement() throws Exception {
String grammar =
"grammar T;\n" +
"a : A {System.out.println(_input.getText());} ;\n" +
"A : 'b'* 'c' ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer",
"a", "bbbbc", debug);
assertEquals("bbbbc\n", found);
found = execParser("T.g4", grammar, "TParser", "TLexer",
"a", "c", debug);
assertEquals("c\n", found);
}
@Test public void testPlusLexerSingleElement() throws Exception {
String grammar =
"grammar T;\n" +
"a : A {System.out.println(_input.getText());} ;\n" +
"A : 'b'+ 'c' ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer",
"a", "bbbbc", debug);
assertEquals("bbbbc\n", found);
}
@Test public void testOptionalSet() throws Exception {
String grammar =
"grammar T;\n" +
"a : ('a'|'b')? 'c' {System.out.println(_input.getText());} ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer",
"a", "ac", debug);
assertEquals("ac\n", found);
}
@Test public void testStarSet() throws Exception {
String grammar =
"grammar T;\n" +
"a : ('a'|'b')* 'c' {System.out.println(_input.getText());} ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer",
"a", "abaac", debug);
assertEquals("abaac\n", found);
}
@Test public void testPlusSet() throws Exception {
String grammar =
"grammar T;\n" +
"a : ('a'|'b')+ 'c' {System.out.println(_input.getText());} ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer",
"a", "abaac", debug);
assertEquals("abaac\n", found);
}
@Test public void testLexerOptionalSet() throws Exception {
String grammar =
"grammar T;\n" +
"a : A {System.out.println(_input.getText());} ;\n" +
"A : ('a'|'b')? 'c' ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer",
"a", "ac", debug);
assertEquals("ac\n", found);
}
@Test public void testLexerStarSet() throws Exception {
String grammar =
"grammar T;\n" +
"a : A {System.out.println(_input.getText());} ;\n" +
"A : ('a'|'b')* 'c' ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer",
"a", "abaac", debug);
assertEquals("abaac\n", found);
}
@Test public void testLexerPlusSet() throws Exception {
String grammar =
"grammar T;\n" +
"a : A {System.out.println(_input.getText());} ;\n" +
"A : ('a'|'b')+ 'c' ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer",
"a", "abaac", debug);
assertEquals("abaac\n", found);
}
@Test public void testNotCharSet() throws Exception {
String grammar =
"grammar T;\n" +
"a : A {System.out.println($A.text);} ;\n" +
"A : ~('b'|'c') ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer",
"a", "x", debug);
assertEquals("x\n", found);
}
@Test public void testNotCharSetWithLabel() throws Exception {
String grammar =
"grammar T;\n" +
"a : A {System.out.println($A.text);} ;\n" +
"A : h=~('b'|'c') ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer",
"a", "x", debug);
assertEquals("x\n", found);
}
@Test public void testNotCharSetWithRuleRef() throws Exception {
// might be a useful feature to add someday
String[] pair = new String[] {
"grammar T;\n" +
"a : A {System.out.println($A.text);} ;\n" +
"A : ~('a'|B) ;\n" +
"B : 'b' ;\n",
"error(" + ErrorType.UNSUPPORTED_REFERENCE_IN_LEXER_SET.code + "): T.g4:3:10: rule reference B is not currently supported in a set\n"
};
super.testErrors(pair, true);
}
@Test public void testNotCharSetWithString() throws Exception {
// might be a useful feature to add someday
String[] pair = new String[] {
"grammar T;\n" +
"a : A {System.out.println($A.text);} ;\n" +
"A : ~('a'|'aa') ;\n" +
"B : 'b' ;\n",
"error(" + ErrorType.INVALID_LITERAL_IN_LEXER_SET.code + "): T.g4:3:10: multi-character literals are not allowed in lexer sets: 'aa'\n"
};
super.testErrors(pair, true);
}
@Test public void testNotCharSetWithRuleRef3() throws Exception {
String grammar =
"grammar T;\n" +
"a : A {System.out.println($A.text);} ;\n" +
"A : ('a'|B) ;\n" + // this doesn't collapse to set but works
"fragment\n" +
"B : ~('a'|'c') ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer",
"a", "x", debug);
assertEquals("x\n", found);
}
@Test public void testCharSetLiteral() throws Exception {
String grammar =
"grammar T;\n" +
"a : (A {System.out.println($A.text);})+ ;\n" +
"A : [AaBb] ;\n" +
"WS : (' '|'\\n')+ -> skip ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer",
"a", "A a B b", debug);
assertEquals("A\n" +
"a\n" +
"B\n" +
"b\n", found);
}
}