001: package org.antlr.test;
002:
003: /** Test the set stuff in lexer and parser */
004: public class TestSets extends BaseTest {
005: protected boolean debug = false;
006:
007: /** Public default constructor used by TestRig */
008: public TestSets() {
009: }
010:
011: public void testSeqDoesNotBecomeSet() throws Exception {
012: // this must return A not I to the parser; calling a nonfragment rule
013: // from a nonfragment rule does not set the overall token.
014: String grammar = "grammar P;\n"
015: + "a : C {System.out.println(input);} ;\n"
016: + "fragment A : '1' | '2';\n"
017: + "fragment B : '3' '4';\n" + "C : A | B;\n";
018: String found = execParser("P.g", grammar, "PParser", "PLexer",
019: "a", "34", debug);
020: assertEquals("34\n", found);
021: }
022:
023: public void testParserSet() throws Exception {
024: String grammar = "grammar T;\n"
025: + "a : t=('x'|'y') {System.out.println($t.text);} ;\n";
026: String found = execParser("T.g", grammar, "TParser", "TLexer",
027: "a", "x", debug);
028: assertEquals("x\n", found);
029: }
030:
031: public void testParserNotSet() throws Exception {
032: String grammar = "grammar T;\n"
033: + "a : t=~('x'|'y') 'z' {System.out.println($t.text);} ;\n";
034: String found = execParser("T.g", grammar, "TParser", "TLexer",
035: "a", "zz", debug);
036: assertEquals("z\n", found);
037: }
038:
039: public void testParserNotToken() throws Exception {
040: String grammar = "grammar T;\n"
041: + "a : ~'x' 'z' {System.out.println(input);} ;\n";
042: String found = execParser("T.g", grammar, "TParser", "TLexer",
043: "a", "zz", debug);
044: assertEquals("zz\n", found);
045: }
046:
047: public void testParserNotTokenWithLabel() throws Exception {
048: String grammar = "grammar T;\n"
049: + "a : t=~'x' 'z' {System.out.println($t.text);} ;\n";
050: String found = execParser("T.g", grammar, "TParser", "TLexer",
051: "a", "zz", debug);
052: assertEquals("z\n", found);
053: }
054:
055: public void testRuleAsSet() throws Exception {
056: String grammar = "grammar T;\n"
057: + "a @after {System.out.println(input);} : 'a' | 'b' |'c' ;\n";
058: String found = execParser("T.g", grammar, "TParser", "TLexer",
059: "a", "b", debug);
060: assertEquals("b\n", found);
061: }
062:
063: public void testRuleAsSetAST() throws Exception {
064: String grammar = "grammar T;\n" + "options {output=AST;}\n"
065: + "a : 'a' | 'b' |'c' ;\n";
066: String found = execParser("T.g", grammar, "TParser", "TLexer",
067: "a", "b", debug);
068: assertEquals("b\n", found);
069: }
070:
071: public void testNotChar() throws Exception {
072: String grammar = "grammar T;\n"
073: + "a : A {System.out.println($A.text);} ;\n"
074: + "A : ~'b' ;\n";
075: String found = execParser("T.g", grammar, "TParser", "TLexer",
076: "a", "x", debug);
077: assertEquals("x\n", found);
078: }
079:
080: public void testOptionalSingleElement() throws Exception {
081: String grammar = "grammar T;\n"
082: + "a : A? 'c' {System.out.println(input);} ;\n"
083: + "A : 'b' ;\n";
084: String found = execParser("T.g", grammar, "TParser", "TLexer",
085: "a", "bc", debug);
086: assertEquals("bc\n", found);
087: }
088:
089: public void testOptionalLexerSingleElement() throws Exception {
090: String grammar = "grammar T;\n"
091: + "a : A {System.out.println(input);} ;\n"
092: + "A : 'b'? 'c' ;\n";
093: String found = execParser("T.g", grammar, "TParser", "TLexer",
094: "a", "bc", debug);
095: assertEquals("bc\n", found);
096: }
097:
098: public void testStarLexerSingleElement() throws Exception {
099: String grammar = "grammar T;\n"
100: + "a : A {System.out.println(input);} ;\n"
101: + "A : 'b'* 'c' ;\n";
102: String found = execParser("T.g", grammar, "TParser", "TLexer",
103: "a", "bbbbc", debug);
104: assertEquals("bbbbc\n", found);
105: found = execParser("T.g", grammar, "TParser", "TLexer", "a",
106: "c", debug);
107: assertEquals("c\n", found);
108: }
109:
110: public void testPlusLexerSingleElement() throws Exception {
111: String grammar = "grammar T;\n"
112: + "a : A {System.out.println(input);} ;\n"
113: + "A : 'b'+ 'c' ;\n";
114: String found = execParser("T.g", grammar, "TParser", "TLexer",
115: "a", "bbbbc", debug);
116: assertEquals("bbbbc\n", found);
117: }
118:
119: public void testOptionalSet() throws Exception {
120: String grammar = "grammar T;\n"
121: + "a : ('a'|'b')? 'c' {System.out.println(input);} ;\n";
122: String found = execParser("T.g", grammar, "TParser", "TLexer",
123: "a", "ac", debug);
124: assertEquals("ac\n", found);
125: }
126:
127: public void testStarSet() throws Exception {
128: String grammar = "grammar T;\n"
129: + "a : ('a'|'b')* 'c' {System.out.println(input);} ;\n";
130: String found = execParser("T.g", grammar, "TParser", "TLexer",
131: "a", "abaac", debug);
132: assertEquals("abaac\n", found);
133: }
134:
135: public void testPlusSet() throws Exception {
136: String grammar = "grammar T;\n"
137: + "a : ('a'|'b')+ 'c' {System.out.println(input);} ;\n";
138: String found = execParser("T.g", grammar, "TParser", "TLexer",
139: "a", "abaac", debug);
140: assertEquals("abaac\n", found);
141: }
142:
143: public void testLexerOptionalSet() throws Exception {
144: String grammar = "grammar T;\n"
145: + "a : A {System.out.println(input);} ;\n"
146: + "A : ('a'|'b')? 'c' ;\n";
147: String found = execParser("T.g", grammar, "TParser", "TLexer",
148: "a", "ac", debug);
149: assertEquals("ac\n", found);
150: }
151:
152: public void testLexerStarSet() throws Exception {
153: String grammar = "grammar T;\n"
154: + "a : A {System.out.println(input);} ;\n"
155: + "A : ('a'|'b')* 'c' ;\n";
156: String found = execParser("T.g", grammar, "TParser", "TLexer",
157: "a", "abaac", debug);
158: assertEquals("abaac\n", found);
159: }
160:
161: public void testLexerPlusSet() throws Exception {
162: String grammar = "grammar T;\n"
163: + "a : A {System.out.println(input);} ;\n"
164: + "A : ('a'|'b')+ 'c' ;\n";
165: String found = execParser("T.g", grammar, "TParser", "TLexer",
166: "a", "abaac", debug);
167: assertEquals("abaac\n", found);
168: }
169:
170: public void testNotCharSet() throws Exception {
171: String grammar = "grammar T;\n"
172: + "a : A {System.out.println($A.text);} ;\n"
173: + "A : ~('b'|'c') ;\n";
174: String found = execParser("T.g", grammar, "TParser", "TLexer",
175: "a", "x", debug);
176: assertEquals("x\n", found);
177: }
178:
179: public void testNotCharSetWithLabel() throws Exception {
180: // This doesn't work in lexer yet.
181: // Generates: h=input.LA(1); but h is defined as a Token
182: String grammar = "grammar T;\n"
183: + "a : A {System.out.println($A.text);} ;\n"
184: + "A : h=~('b'|'c') ;\n";
185: String found = execParser("T.g", grammar, "TParser", "TLexer",
186: "a", "x", debug);
187: assertEquals("x\n", found);
188: }
189:
190: public void testNotCharSetWithRuleRef() throws Exception {
191: String grammar = "grammar T;\n"
192: + "a : A {System.out.println($A.text);} ;\n"
193: + "A : ~('a'|B) ;\n" + "B : 'b' ;\n";
194: String found = execParser("T.g", grammar, "TParser", "TLexer",
195: "a", "x", debug);
196: assertEquals("x\n", found);
197: }
198:
199: public void testNotCharSetWithRuleRef2() throws Exception {
200: String grammar = "grammar T;\n"
201: + "a : A {System.out.println($A.text);} ;\n"
202: + "A : ~('a'|B) ;\n" + "B : 'b'|'c' ;\n";
203: String found = execParser("T.g", grammar, "TParser", "TLexer",
204: "a", "x", debug);
205: assertEquals("x\n", found);
206: }
207:
208: public void testNotCharSetWithRuleRef3() throws Exception {
209: String grammar = "grammar T;\n"
210: + "a : A {System.out.println($A.text);} ;\n"
211: + "A : ('a'|B) ;\n" + "fragment\n"
212: + "B : ~('a'|'c') ;\n";
213: String found = execParser("T.g", grammar, "TParser", "TLexer",
214: "a", "x", debug);
215: assertEquals("x\n", found);
216: }
217:
218: public void testNotCharSetWithRuleRef4() throws Exception {
219: String grammar = "grammar T;\n"
220: + "a : A {System.out.println($A.text);} ;\n"
221: + "A : ('a'|B) ;\n" + "fragment\n" + "B : ~('a'|C) ;\n"
222: + "fragment\n" + "C : 'c'|'d' ;\n ";
223: String found = execParser("T.g", grammar, "TParser", "TLexer",
224: "a", "x", debug);
225: assertEquals("x\n", found);
226: }
227:
228: }
|