01: /*
02: [The "BSD licence"]
03: Copyright (c) 2005-2006 Terence Parr
04: All rights reserved.
05:
06: Redistribution and use in source and binary forms, with or without
07: modification, are permitted provided that the following conditions
08: are met:
09: 1. Redistributions of source code must retain the above copyright
10: notice, this list of conditions and the following disclaimer.
11: 2. Redistributions in binary form must reproduce the above copyright
12: notice, this list of conditions and the following disclaimer in the
13: documentation and/or other materials provided with the distribution.
14: 3. The name of the author may not be used to endorse or promote products
15: derived from this software without specific prior written permission.
16:
17: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27: */
28: package org.antlr.test;
29:
30: import org.antlr.analysis.DFA;
31: import org.antlr.analysis.NFA;
32: import org.antlr.runtime.ANTLRStringStream;
33: import org.antlr.tool.Grammar;
34:
35: public class TestDFAMatching extends BaseTest {
36:
37: /** Public default constructor used by TestRig */
38: public TestDFAMatching() {
39: }
40:
41: public void testSimpleAltCharTest() throws Exception {
42: Grammar g = new Grammar("lexer grammar t;\n"
43: + "A : {;}'a' | 'b' | 'c';");
44: g.createNFAs();
45: g.createLookaheadDFAs();
46: DFA dfa = g.getLookaheadDFA(1);
47: checkPrediction(dfa, "a", 1);
48: checkPrediction(dfa, "b", 2);
49: checkPrediction(dfa, "c", 3);
50: checkPrediction(dfa, "d", NFA.INVALID_ALT_NUMBER);
51: }
52:
53: public void testSets() throws Exception {
54: Grammar g = new Grammar("lexer grammar t;\n"
55: + "A : {;}'a'..'z' | ';' | '0'..'9' ;");
56: g.createNFAs();
57: g.createLookaheadDFAs();
58: DFA dfa = g.getLookaheadDFA(1);
59: checkPrediction(dfa, "a", 1);
60: checkPrediction(dfa, "q", 1);
61: checkPrediction(dfa, "z", 1);
62: checkPrediction(dfa, ";", 2);
63: checkPrediction(dfa, "9", 3);
64: }
65:
66: public void testFiniteCommonLeftPrefixes() throws Exception {
67: Grammar g = new Grammar("lexer grammar t;\n"
68: + "A : 'a' 'b' | 'a' 'c' | 'd' 'e' ;");
69: g.createNFAs();
70: g.createLookaheadDFAs();
71: DFA dfa = g.getLookaheadDFA(1);
72: checkPrediction(dfa, "ab", 1);
73: checkPrediction(dfa, "ac", 2);
74: checkPrediction(dfa, "de", 3);
75: checkPrediction(dfa, "q", NFA.INVALID_ALT_NUMBER);
76: }
77:
78: public void testSimpleLoops() throws Exception {
79: Grammar g = new Grammar("lexer grammar t;\n"
80: + "A : (DIGIT)+ '.' DIGIT | (DIGIT)+ ;\n"
81: + "fragment DIGIT : '0'..'9' ;\n");
82: g.createNFAs();
83: g.createLookaheadDFAs();
84: DFA dfa = g.getLookaheadDFA(3);
85: checkPrediction(dfa, "32", 2);
86: checkPrediction(dfa, "999.2", 1);
87: checkPrediction(dfa, ".2", NFA.INVALID_ALT_NUMBER);
88: }
89:
90: protected void checkPrediction(DFA dfa, String input, int expected)
91: throws Exception {
92: ANTLRStringStream stream = new ANTLRStringStream(input);
93: assertEquals(dfa.predict(stream), expected);
94: }
95:
96: }
|