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: import org.antlr.runtime.tree.ParseTree;
034:
035: public class TestInterpretedParsing extends BaseTest {
036:
037: /** Public default constructor used by TestRig */
038: public TestInterpretedParsing() {
039: }
040:
041: public void testSimpleParse() throws Exception {
042: Grammar pg = new Grammar("parser grammar p;\n"
043: + "prog : WHILE ID LCURLY (assign)* RCURLY EOF;\n"
044: + "assign : ID ASSIGN expr SEMI ;\n"
045: + "expr : INT | FLOAT | ID ;\n");
046: Grammar g = new Grammar();
047: g.importTokenVocabulary(pg);
048: g.setFileName(Grammar.IGNORE_STRING_IN_GRAMMAR_FILE_NAME
049: + "string");
050: g.setGrammarContent("lexer grammar t;\n" + "WHILE : 'while';\n"
051: + "LCURLY : '{';\n" + "RCURLY : '}';\n"
052: + "ASSIGN : '=';\n" + "SEMI : ';';\n"
053: + "ID : ('a'..'z')+ ;\n" + "INT : (DIGIT)+ ;\n"
054: + "FLOAT : (DIGIT)+ '.' (DIGIT)* ;\n"
055: + "fragment DIGIT : '0'..'9';\n" + "WS : (' ')+ ;\n");
056: CharStream input = new ANTLRStringStream(
057: "while x { i=1; y=3.42; z=y; }");
058: Interpreter lexEngine = new Interpreter(g, input);
059:
060: CommonTokenStream tokens = new CommonTokenStream(lexEngine);
061: tokens.setTokenTypeChannel(g.getTokenType("WS"), 99);
062: //System.out.println("tokens="+tokens.toString());
063: Interpreter parseEngine = new Interpreter(pg, tokens);
064: ParseTree t = parseEngine.parse("prog");
065: String result = t.toStringTree();
066: String expecting = "(<grammar p> (prog while x { (assign i = (expr 1) ;) (assign y = (expr 3.42) ;) (assign z = (expr y) ;) } <EOF>))";
067: assertEquals(expecting, result);
068: }
069:
070: public void testMismatchedTokenError() throws Exception {
071: Grammar pg = new Grammar("parser grammar p;\n"
072: + "prog : WHILE ID LCURLY (assign)* RCURLY;\n"
073: + "assign : ID ASSIGN expr SEMI ;\n"
074: + "expr : INT | FLOAT | ID ;\n");
075: Grammar g = new Grammar();
076: g.setFileName(Grammar.IGNORE_STRING_IN_GRAMMAR_FILE_NAME
077: + "string");
078: g.importTokenVocabulary(pg);
079: g.setGrammarContent("lexer grammar t;\n" + "WHILE : 'while';\n"
080: + "LCURLY : '{';\n" + "RCURLY : '}';\n"
081: + "ASSIGN : '=';\n" + "SEMI : ';';\n"
082: + "ID : ('a'..'z')+ ;\n" + "INT : (DIGIT)+ ;\n"
083: + "FLOAT : (DIGIT)+ '.' (DIGIT)* ;\n"
084: + "fragment DIGIT : '0'..'9';\n" + "WS : (' ')+ ;\n");
085: CharStream input = new ANTLRStringStream(
086: "while x { i=1 y=3.42; z=y; }");
087: Interpreter lexEngine = new Interpreter(g, input);
088:
089: CommonTokenStream tokens = new CommonTokenStream(lexEngine);
090: tokens.setTokenTypeChannel(g.getTokenType("WS"), 99);
091: //System.out.println("tokens="+tokens.toString());
092: Interpreter parseEngine = new Interpreter(pg, tokens);
093: ParseTree t = parseEngine.parse("prog");
094: String result = t.toStringTree();
095: String expecting = "(<grammar p> (prog while x { (assign i = (expr 1) MismatchedTokenException(5!=9))))";
096: assertEquals(expecting, result);
097: }
098:
099: public void testMismatchedSetError() throws Exception {
100: Grammar pg = new Grammar("parser grammar p;\n"
101: + "prog : WHILE ID LCURLY (assign)* RCURLY;\n"
102: + "assign : ID ASSIGN expr SEMI ;\n"
103: + "expr : INT | FLOAT | ID ;\n");
104: Grammar g = new Grammar();
105: g.importTokenVocabulary(pg);
106: g.setFileName("<string>");
107: g.setGrammarContent("lexer grammar t;\n" + "WHILE : 'while';\n"
108: + "LCURLY : '{';\n" + "RCURLY : '}';\n"
109: + "ASSIGN : '=';\n" + "SEMI : ';';\n"
110: + "ID : ('a'..'z')+ ;\n" + "INT : (DIGIT)+ ;\n"
111: + "FLOAT : (DIGIT)+ '.' (DIGIT)* ;\n"
112: + "fragment DIGIT : '0'..'9';\n" + "WS : (' ')+ ;\n");
113: CharStream input = new ANTLRStringStream(
114: "while x { i=; y=3.42; z=y; }");
115: Interpreter lexEngine = new Interpreter(g, input);
116:
117: CommonTokenStream tokens = new CommonTokenStream(lexEngine);
118: tokens.setTokenTypeChannel(g.getTokenType("WS"), 99);
119: //System.out.println("tokens="+tokens.toString());
120: Interpreter parseEngine = new Interpreter(pg, tokens);
121: ParseTree t = parseEngine.parse("prog");
122: String result = t.toStringTree();
123: String expecting = "(<grammar p> (prog while x { (assign i = (expr MismatchedSetException(9!={5,10,11})))))";
124: assertEquals(expecting, result);
125: }
126:
127: public void testNoViableAltError() throws Exception {
128: Grammar pg = new Grammar("parser grammar p;\n"
129: + "prog : WHILE ID LCURLY (assign)* RCURLY;\n"
130: + "assign : ID ASSIGN expr SEMI ;\n"
131: + "expr : {;}INT | FLOAT | ID ;\n");
132: Grammar g = new Grammar();
133: g.importTokenVocabulary(pg);
134: g.setFileName("<string>");
135: g.setGrammarContent("lexer grammar t;\n" + "WHILE : 'while';\n"
136: + "LCURLY : '{';\n" + "RCURLY : '}';\n"
137: + "ASSIGN : '=';\n" + "SEMI : ';';\n"
138: + "ID : ('a'..'z')+ ;\n" + "INT : (DIGIT)+ ;\n"
139: + "FLOAT : (DIGIT)+ '.' (DIGIT)* ;\n"
140: + "fragment DIGIT : '0'..'9';\n" + "WS : (' ')+ ;\n");
141: CharStream input = new ANTLRStringStream(
142: "while x { i=; y=3.42; z=y; }");
143: Interpreter lexEngine = new Interpreter(g, input);
144:
145: CommonTokenStream tokens = new CommonTokenStream(lexEngine);
146: tokens.setTokenTypeChannel(g.getTokenType("WS"), 99);
147: //System.out.println("tokens="+tokens.toString());
148: Interpreter parseEngine = new Interpreter(pg, tokens);
149: ParseTree t = parseEngine.parse("prog");
150: String result = t.toStringTree();
151: String expecting = "(<grammar p> (prog while x { (assign i = (expr NoViableAltException(9!=[4:1: expr : ( INT | FLOAT | ID );])))))";
152: assertEquals(expecting, result);
153: }
154:
155: }
|