001: /*
002: [The "BSD licence"]
003: Copyright (c) 2005-2006 Terence Parr
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011: 2. Redistributions in binary form must reproduce the above copyright
012: notice, this list of conditions and the following disclaimer in the
013: documentation and/or other materials provided with the distribution.
014: 3. The name of the author may not be used to endorse or promote products
015: derived from this software without specific prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
018: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
019: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
020: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
021: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028: package org.antlr.test;
029:
030: import org.antlr.tool.Grammar;
031: import org.antlr.tool.Interpreter;
032: import org.antlr.runtime.*;
033:
034: public class TestInterpretedLexing extends BaseTest {
035:
036: /*
037: static class Tracer implements ANTLRDebugInterface {
038: Grammar g;
039: public DebugActions(Grammar g) {
040: this.g = g;
041: }
042: public void enterRule(String ruleName) {
043: System.out.println("enterRule("+ruleName+")");
044: }
045:
046: public void exitRule(String ruleName) {
047: System.out.println("exitRule("+ruleName+")");
048: }
049:
050: public void matchElement(int type) {
051: System.out.println("matchElement("+g.getTokenName(type)+")");
052: }
053:
054: public void mismatchedElement(MismatchedTokenException e) {
055: System.out.println(e);
056: e.printStackTrace(System.out);
057: }
058:
059: public void mismatchedSet(MismatchedSetException e) {
060: System.out.println(e);
061: e.printStackTrace(System.out);
062: }
063:
064: public void noViableAlt(NoViableAltException e) {
065: System.out.println(e);
066: e.printStackTrace(System.out);
067: }
068: }
069: */
070:
071: /** Public default constructor used by TestRig */
072: public TestInterpretedLexing() {
073: }
074:
075: public void testSimpleAltCharTest() throws Exception {
076: Grammar g = new Grammar("lexer grammar t;\n"
077: + "A : 'a' | 'b' | 'c';");
078: final int Atype = g.getTokenType("A");
079: Interpreter engine = new Interpreter(g, new ANTLRStringStream(
080: "a"));
081: engine = new Interpreter(g, new ANTLRStringStream("b"));
082: Token result = engine.scan("A");
083: assertEquals(result.getType(), Atype);
084: engine = new Interpreter(g, new ANTLRStringStream("c"));
085: result = engine.scan("A");
086: assertEquals(result.getType(), Atype);
087: }
088:
089: public void testSingleRuleRef() throws Exception {
090: Grammar g = new Grammar("lexer grammar t;\n"
091: + "A : 'a' B 'c' ;\n" + "B : 'b' ;\n");
092: final int Atype = g.getTokenType("A");
093: Interpreter engine = new Interpreter(g, new ANTLRStringStream(
094: "abc")); // should ignore the x
095: Token result = engine.scan("A");
096: assertEquals(result.getType(), Atype);
097: }
098:
099: public void testSimpleLoop() throws Exception {
100: Grammar g = new Grammar("lexer grammar t;\n"
101: + "INT : (DIGIT)+ ;\n" + "fragment DIGIT : '0'..'9';\n");
102: final int INTtype = g.getTokenType("INT");
103: Interpreter engine = new Interpreter(g, new ANTLRStringStream(
104: "12x")); // should ignore the x
105: Token result = engine.scan("INT");
106: assertEquals(result.getType(), INTtype);
107: engine = new Interpreter(g, new ANTLRStringStream("1234"));
108: result = engine.scan("INT");
109: assertEquals(result.getType(), INTtype);
110: }
111:
112: public void testMultAltLoop() throws Exception {
113: Grammar g = new Grammar("lexer grammar t;\n"
114: + "A : ('0'..'9'|'a'|'b')+ ;\n");
115: final int Atype = g.getTokenType("A");
116: Interpreter engine = new Interpreter(g, new ANTLRStringStream(
117: "a"));
118: Token result = engine.scan("A");
119: engine = new Interpreter(g, new ANTLRStringStream("a"));
120: result = engine.scan("A");
121: assertEquals(result.getType(), Atype);
122: engine = new Interpreter(g, new ANTLRStringStream("1234"));
123: result = engine.scan("A");
124: assertEquals(result.getType(), Atype);
125: engine = new Interpreter(g, new ANTLRStringStream("aaa"));
126: result = engine.scan("A");
127: assertEquals(result.getType(), Atype);
128: engine = new Interpreter(g, new ANTLRStringStream("aaaa9"));
129: result = engine.scan("A");
130: assertEquals(result.getType(), Atype);
131: engine = new Interpreter(g, new ANTLRStringStream("b"));
132: result = engine.scan("A");
133: assertEquals(result.getType(), Atype);
134: engine = new Interpreter(g, new ANTLRStringStream("baa"));
135: result = engine.scan("A");
136: assertEquals(result.getType(), Atype);
137: }
138:
139: public void testSimpleLoops() throws Exception {
140: Grammar g = new Grammar("lexer grammar t;\n"
141: + "A : ('0'..'9')+ '.' ('0'..'9')* | ('0'..'9')+ ;\n");
142: final int Atype = g.getTokenType("A");
143: CharStream input = new ANTLRStringStream("1234.5");
144: Interpreter engine = new Interpreter(g, input);
145: Token result = engine.scan("A");
146: assertEquals(result.getType(), Atype);
147: }
148:
149: public void testTokensRules() throws Exception {
150: Grammar pg = new Grammar("grammar p;\n"
151: + "a : (INT|FLOAT|WS)+;\n");
152: Grammar g = new Grammar();
153: g.importTokenVocabulary(pg);
154: g.setFileName("<string>");
155: g.setGrammarContent("lexer grammar t;\n" + "INT : (DIGIT)+ ;\n"
156: + "FLOAT : (DIGIT)+ '.' (DIGIT)* ;\n"
157: + "fragment DIGIT : '0'..'9';\n"
158: + "WS : (' ')+ {channel=99;};\n");
159: CharStream input = new ANTLRStringStream("123 139.52");
160: Interpreter lexEngine = new Interpreter(g, input);
161:
162: CommonTokenStream tokens = new CommonTokenStream(lexEngine);
163: String result = tokens.toString();
164: //System.out.println(result);
165: String expecting = "123 139.52";
166: assertEquals(result, expecting);
167: }
168:
169: }
|