KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > test > ut > TestParser


1 package org.antlr.works.test.ut;
2
3 import junit.framework.TestSuite;
4 import junit.textui.TestRunner;
5 import org.antlr.works.ate.syntax.misc.ATEToken;
6 import org.antlr.works.editor.EditorInspector;
7 import org.antlr.works.editor.EditorRules;
8 import org.antlr.works.syntax.GrammarSyntax;
9 import org.antlr.works.syntax.GrammarSyntaxDelegate;
10 import org.antlr.works.syntax.GrammarSyntaxEngine;
11 import org.antlr.works.syntax.element.ElementBlock;
12 import org.antlr.works.syntax.element.ElementGrammarName;
13 import org.antlr.works.syntax.element.ElementReference;
14 import org.antlr.works.syntax.element.ElementRule;
15 import org.antlr.works.test.AbstractTest;
16 import org.antlr.works.test.TestConstants;
17
18 import java.util.*;
19 /*
20
21 [The "BSD licence"]
22 Copyright (c) 2005-2006 Jean Bovet
23 All rights reserved.
24
25 Redistribution and use in source and binary forms, with or without
26 modification, are permitted provided that the following conditions
27 are met:
28
29 1. Redistributions of source code must retain the above copyright
30 notice, this list of conditions and the following disclaimer.
31 2. Redistributions in binary form must reproduce the above copyright
32 notice, this list of conditions and the following disclaimer in the
33 documentation and/or other materials provided with the distribution.
34 3. The name of the author may not be used to endorse or promote products
35 derived from this software without specific prior written permission.
36
37 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
38 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
40 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
41 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
43 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
44 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
45 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
46 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47
48 */

49
50 public class TestParser extends AbstractTest implements GrammarSyntaxDelegate {
51
52     private String JavaDoc vocabFile;
53
54     public static void main(String JavaDoc[] args) {
55         new TestRunner().doRun(new TestSuite(TestParser.class));
56     }
57
58     public void testDoubleQuoteStringInArgument() throws Exception JavaDoc {
59         parseFile(TestConstants.PREFIX+"arguments.g");
60         assertInspector(0);
61     }
62
63     public void testEmptyRewriteSyntax() throws Exception JavaDoc {
64         parseFile(TestConstants.PREFIX+"empty_rewrite.g");
65         assertInspector(0);
66     }
67
68     public void testGroups() throws Exception JavaDoc {
69         parseFile(TestConstants.PREFIX+"groups.g");
70         assertInspector(0);
71     }
72
73     public void testIgnoreRules() throws Exception JavaDoc {
74         parseFile(TestConstants.PREFIX+"ignore_rules.g");
75         EditorRules.findTokensToIgnore(getParser().rules, true);
76         int ignored = 0;
77         for(ElementRule r : getParser().rules) {
78             if(r.ignored) ignored++;
79         }
80         assertEquals("ignored rules", 3, ignored);
81     }
82
83     public void testGrammarType() throws Exception JavaDoc {
84         parseFile(TestConstants.PREFIX+"type/combined.g");
85         assertEquals("combined grammar", ElementGrammarName.COMBINED, getParser().name.getType());
86
87         parseFile(TestConstants.PREFIX+"type/parser.g");
88         assertEquals("parser grammar", ElementGrammarName.PARSER, getParser().name.getType());
89
90         parseFile(TestConstants.PREFIX+"type/lexer.g");
91         assertEquals("lexer grammar", ElementGrammarName.LEXER, getParser().name.getType());
92
93         parseFile(TestConstants.PREFIX+"type/tree.g");
94         assertEquals("tree grammar", ElementGrammarName.TREEPARSER, getParser().name.getType());
95     }
96
97     public void testSyntaxBlock() throws Exception JavaDoc {
98         parseFile(TestConstants.BLOCKS);
99         assertInspector(0);
100
101         assertEquals("grammar name", "demo", getParser().name.getName());
102
103         ElementBlock tokensBlock = getParser().blocks.get(0);
104         assertEquals("tokens block", Arrays.asList("FOO", "OTHER", "LAST"), tokensBlock.getDeclaredTokensAsString());
105
106         ElementBlock optionsBlock = getParser().blocks.get(1);
107         assertEquals("tokenVocab", "DataViewExpressions", optionsBlock.getTokenVocab());
108     }
109
110     public void testReferences() throws Exception JavaDoc {
111         parseFile(TestConstants.REFERENCES);
112         assertInspector(0);
113
114         assertEquals("grammar name", "references", getParser().name.getName());
115
116         assertEquals("declarations", Arrays.asList("FOO", "OTHER", "LAST", "rule_a", "BAR"), getDeclsAsString(getParser().decls));
117         assertEquals("references", Arrays.asList("FOO", "BAR", "OTHER"), getRefsAsString(getParser().references));
118     }
119
120     public void testMantra() throws Exception JavaDoc {
121         parseFile(TestConstants.MANTRA);
122         assertInspector(0);
123
124         assertParserProperties(65, 32, 28, 115, 274); // verified by hand
125
}
126
127     public void testCodeGenPhase() throws Exception JavaDoc {
128         parseFile(TestConstants.CODE_GEN_PHASE);
129         assertInspector(76);
130
131         assertParserProperties(40, 18, 7, 40, 199); // verified by hand
132

133         // now add the remaining token as if they were read from a tokenVocab file
134
Set<String JavaDoc> names = new HashSet<String JavaDoc>();
135         GrammarSyntax.readTokenVocabFromFile(vocabFile = getResourceFile(TestConstants.PREFIX+"mantra/Mantra.tokens"), names);
136         getParser().resolveReferencesWithExternalNames(names);
137         assertParserProperties(40, 18, 7, 40, 199+4); // verified by hand
138

139         assertInspector(0);
140     }
141
142     public void testResolvePhase() throws Exception JavaDoc {
143         parseFile(TestConstants.RESOLVE_PHASE);
144         assertInspector(69);
145
146         Set<String JavaDoc> names = new HashSet<String JavaDoc>();
147         GrammarSyntax.readTokenVocabFromFile(vocabFile = getResourceFile(TestConstants.PREFIX+"mantra/Mantra.tokens"), names);
148         getParser().resolveReferencesWithExternalNames(names);
149
150         assertParserProperties(36, 14, 7, 36, 170); // verified by hand
151
assertInspector(0);
152     }
153
154     public void testSemanticPhase() throws Exception JavaDoc {
155         parseFile(TestConstants.SEMANTIC_PHASE);
156         assertInspector(69);
157
158         Set<String JavaDoc> names = new HashSet<String JavaDoc>();
159         //todo handle vocabfile in a better way
160
GrammarSyntax.readTokenVocabFromFile(vocabFile = getResourceFile(TestConstants.PREFIX+"mantra/Mantra.tokens"), names);
161         getParser().resolveReferencesWithExternalNames(names);
162
163         assertParserProperties(36, 37, 23, 36, 177); // verified by hand
164
assertInspector(0);
165     }
166
167     /*********************** HELPER ***************************************/
168
169     private void printParserProperties() {
170         System.out.println("Rules="+getParser().rules.size());
171         System.out.println("Actions="+getParser().actions.size());
172         System.out.println("Blocks="+getParser().blocks.size());
173         System.out.println("Decls="+getParser().decls.size());
174         System.out.println("Refs="+getParser().references.size());
175     }
176
177     private void assertParserProperties(int rules, int actions, int blocks, int decls, int references) {
178         assertEquals("Number of rules", rules, getParser().rules.size());
179         assertEquals("Number of actions", actions, getParser().actions.size());
180         assertEquals("Number of blocks", blocks, getParser().blocks.size());
181         assertEquals("Number of declarations", decls, getParser().decls.size());
182         assertEquals("Number of references", references, getParser().references.size());
183     }
184
185     private void assertInspector(int errors) {
186         GrammarSyntax syntax = new GrammarSyntax(this);
187         syntax.rebuildAll();
188         EditorInspector inspector = new EditorInspector(syntax, null, new MockInspectorDelegate());
189         assertEquals("Errors", errors, inspector.getErrors().size());
190     }
191
192     private List<String JavaDoc> getDeclsAsString(List<ATEToken> tokens) {
193         List<String JavaDoc> names = new ArrayList<String JavaDoc>();
194         for(ATEToken token : tokens) {
195             names.add(token.getAttribute());
196         }
197         return names;
198     }
199
200     private List<String JavaDoc> getRefsAsString(List<ElementReference> tokens) {
201         List<String JavaDoc> names = new ArrayList<String JavaDoc>();
202         for(ElementReference token : tokens) {
203             names.add(token.token.getAttribute());
204         }
205         return names;
206     }
207
208     public String JavaDoc getTokenVocabFile(String JavaDoc tokenVocabName) {
209         return vocabFile;
210     }
211
212     public GrammarSyntaxEngine getParserEngine() {
213         return engine;
214     }
215
216 }
217
Popular Tags