KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > languages > parser > AnalyserTest


1 /*
2  * LLAnalyserTest.java
3  * JUnit based test
4  *
5  * Created on March 26, 2006, 9:57 AM
6  */

7
8 package org.netbeans.modules.languages.parser;
9
10 import junit.framework.TestCase;
11 import org.netbeans.api.languages.CharInput;
12 import org.netbeans.api.languages.ParseException;
13 import org.netbeans.api.languages.ASTToken;
14 import org.netbeans.modules.languages.parser.TokenInput;
15 import org.netbeans.api.languages.ASTNode;
16
17 import java.util.Arrays JavaDoc;
18 import java.util.Collections JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import org.netbeans.modules.languages.Language;
22 import org.netbeans.modules.languages.NBSLanguageReader;
23
24
25 /**
26  *
27  * @author Jan Jancura
28  */

29 public class AnalyserTest extends TestCase {
30     
31     public AnalyserTest (String JavaDoc testName) {
32         super (testName);
33     }
34
35     private static String JavaDoc mimeType = "text/test";
36
37     public void test1 () throws ParseException {
38         Language l = new Language (mimeType);
39         l.addRule (LLSyntaxAnalyser.Rule.create ("S", Arrays.asList (new Object JavaDoc[] {
40             ASTToken.create (mimeType, "identifier", null, 0),
41             "S"
42         })));
43         l.addRule (LLSyntaxAnalyser.Rule.create ("S", Arrays.asList (new Object JavaDoc[] {
44             ASTToken.create (mimeType, "operator", "{", 0),
45             "S",
46             ASTToken.create (mimeType, "operator", "}", 0),
47             "S"
48         })));
49         l.addRule (LLSyntaxAnalyser.Rule.create ("S", Arrays.asList (new Object JavaDoc[] {
50         })));
51         LLSyntaxAnalyser a = l.getAnalyser ();
52         //PetraTest.print (Petra.first (r, 5));
53
TokenInput input = TokenInput.create (new ASTToken[] {
54             ASTToken.create (mimeType, "identifier", "asd", 0),
55             ASTToken.create (mimeType, "identifier", "ss", 0),
56             ASTToken.create (mimeType, "operator", "{", 0),
57             ASTToken.create (mimeType, "identifier", "a", 0),
58             ASTToken.create (mimeType, "operator", "{", 0),
59             ASTToken.create (mimeType, "operator", "}", 0),
60             ASTToken.create (mimeType, "identifier", "asd", 0),
61             ASTToken.create (mimeType, "operator", "}", 0),
62         });
63         assertNotNull (a.read (input, false));
64         assert (input.eof ());
65     }
66
67     public void test2 () throws ParseException {
68         Language l = NBSLanguageReader.readLanguage (
69             "test",
70             "TOKEN:operator:( '{' | '}' | '.' | ';' | ',' | '(' | ')' )" +
71             "TOKEN:whitespace:( ['\\n' '\\r' ' ' '\\t']+ )" +
72             "TOKEN:keyword:( 'package' | 'class' | 'import' | 'static' | 'synchronized' | 'final' | 'abstract' | 'native' | 'import' | 'extends' | 'implements' | 'public' | 'protected' | 'private' )" +
73             "TOKEN:identifier:( ['a'-'z' 'A'-'Z' '0'-'9' '_' '-' '$' '%']+ )" +
74             "SKIP:whitespace " +
75             "S = packageDeclaration imports classOrInterface otherClasses;" +
76             "packageDeclaration = ;" +
77             "packageDeclaration = <keyword,'package'> dottedName <operator,';'>;" +
78             "dottedName = <identifier> dottedName1;" +
79             "dottedName1 = <operator,'.'> <identifier> dottedName1;" +
80             "dottedName1 = ;" +
81             "imports = <keyword,'import'> dottedName <operator,';'> imports;" +
82             "imports = ;" +
83             "modifiers = <keyword,'public'> modifiers;" +
84             "modifiers = <keyword,'private'> modifiers;" +
85             "modifiers = <keyword,'protected'> modifiers;" +
86             "modifiers = <keyword,'static'> modifiers;" +
87             "modifiers = <keyword,'synchronized'> modifiers;" +
88             "modifiers = <keyword,'final'> modifiers;" +
89             "modifiers = <keyword,'abstract'> modifiers;" +
90             "modifiers = <keyword,'native'> modifiers;" +
91             "modifiers = ;" +
92             "classOrInterface = modifiers classOrInterface1;" +
93             "classOrInterface1 = <keyword,'class'> class;" +
94             "classOrInterface1 = <keyword,'interface'> interface;" +
95             "class = <identifier> extendsList implementsList classOrInterfaceBody;" +
96             "interface = <identifier> extendsList implementsList classOrInterfaceBody;" +
97             "extendsList = <keyword,'extends'> dottedName extendsList1;" +
98             "extendsList1 = <operator,','> dottedName;" +
99             "extendsList1 = ;" +
100             "implementsList = <keyword,'implements'> dottedName implementsList1;" +
101             "implementsList1 = <operator,','> dottedName;" +
102             "implementsList1 = ;" +
103             "classOrInterfaceBody = <operator,'{'> members <operator,'}'>;" +
104             "members = modifiers members1;" +
105             "members1 = block members;" +
106             "members1 = <identifier, type> <identifier, name> <operator,'('> parametersList <operator,')'> block members;" +
107             "members = ;" +
108             "parametersList =;" +
109             "block = <operator,'{'> body <operator,'}'>;" +
110             "body = <identifier> body;" +
111             "body = block;" +
112             "body = ;" +
113             "otherClasses = ;",
114             mimeType
115         );
116         CharInput input = new StringInput (
117             "package org.test.foo;" +
118             "import a.bb.ccc;" +
119             "import qq.ww.ee;" +
120             "public static class Hanz extends aaa.Text implements a.XXX, b.YYY {" +
121             " public static { aaa}" +
122             " public final int test () {" +
123             " test test" +
124             " }" +
125             "}",
126             "source"
127         );
128         ASTNode n = l.getAnalyser ().read (
129             TokenInput.create (
130                 "text/test",
131                 l.getParser (),
132                 input,
133                 l.getSkipTokenTypes ()
134             ),
135             false
136         );
137         //System.out.println(n.print ());
138
assertNotNull (n);
139     }
140
141     public void test4 () throws ParseException {
142         Language l = NBSLanguageReader.readLanguage (
143             "test",
144             "TOKEN:operator:( '{' | '}' | '.' | ',' | '(' | ')' )" +
145             "TOKEN:separator:( ';' )" +
146             "TOKEN:whitespace:( ['\\n' '\\r' ' ' '\\t']+ )" +
147             "TOKEN:keyword:( 'void' | 'public' )" +
148             "TOKEN:identifier:( ['a'-'z' 'A'-'Z' '0'-'9' '_' '-' '$' '%']+ )" +
149             "SKIP:whitespace " +
150             "S = variable S;" +
151             "S = ;" +
152             "variable = modifiers <keyword> <identifier,name> <separator,';'>;" +
153             "variable = modifiers <identifier,type> <identifier,name> <separator,';'>;" +
154             "modifiers = <keyword,'public'> modifiers;" +
155             "modifiers = ;",
156             mimeType
157         );
158         CharInput input = new StringInput (
159             "void a;" +
160             "public ii name;",
161             "source"
162         );
163         ASTNode n = l.getAnalyser ().read (
164             TokenInput.create (
165                 "text/test",
166                 l.getParser (),
167                 input,
168                 l.getSkipTokenTypes ()
169             ),
170             false
171         );
172         assertNotNull (n);
173         assertTrue (input.eof ());
174     }
175 //
176
// public void testFollow1 () throws ParseException {
177
// Language l = LanguageReader.readLanguage (
178
// "test",
179
// "S = <identifier,'a'> B C <identifier,'b'>;" +
180
// "B = <identifier,'b'>;" +
181
// "B =;" +
182
// "C = <identifier,'c'>;" +
183
// "C =;",
184
// mimeType
185
// );
186
// Analyser a = l.getAnalyser ();
187
// Parser p = Parser.create ();
188
// p.add ("operator", "'{' | '}' | '.' | ';' | ',' | '(' | ')'");
189
// p.add ("whitespace", "['\\n' '\\r' ' ' '\\t']+");
190
// p.add ("keyword", "'package' | 'class' | 'import' | 'static' | 'synchronized' | 'final' | 'abstract' | 'native' | 'import' | 'extends' | 'implements' | 'public' | 'protected' | 'private'");
191
// p.add ("identifier", "['a'-'z' 'A'-'Z' '0'-'9' '_' '-' '$' '%']+");
192
// p.addSkipToken (Token.create ("whitespace", null, false));
193
// Input input = Input.create (
194
// "b d"
195
// );
196
// Input input1 = p.readAll (input);
197
// List ast = new ArrayList ();
198
// try {
199
// a.read (input1);
200
// assertTrue (true);
201
// } catch (ParseException ex) {
202
// System.out.println(ex);
203
// }
204
// print (ast, "");
205
// }
206
//
207
// public void testFollow2 () throws ParseException {
208
// Language l = LanguageReader.readLanguage (
209
// "test",
210
// "S = <identifier,'a'> B C <identifier,'c'>;" +
211
// "B = <identifier,'b'>;" +
212
// "B =;" +
213
// "C = <identifier,'c'>;" +
214
// "C =;",
215
// mimeType
216
// );
217
// Analyser a = l.getAnalyser ();
218
// Parser p = Parser.create ();
219
// p.add ("operator", "'{' | '}' | '.' | ';' | ',' | '(' | ')'");
220
// p.add ("whitespace", "['\\n' '\\r' ' ' '\\t']+");
221
// p.add ("keyword", "'package' | 'class' | 'import' | 'static' | 'synchronized' | 'final' | 'abstract' | 'native' | 'import' | 'extends' | 'implements' | 'public' | 'protected' | 'private'");
222
// p.add ("identifier", "['a'-'z' 'A'-'Z' '0'-'9' '_' '-' '$' '%']+");
223
// p.addSkipToken (Token.create ("whitespace", null, false));
224
// Input input = Input.create (
225
// "b d"
226
// );
227
// Input input1 = p.readAll (input);
228
// List ast = new ArrayList ();
229
// try {
230
// a.read (input1);
231
// assertTrue (true);
232
// } catch (ParseException ex) {
233
// System.out.println(ex);
234
// }
235
// print (ast, "");
236
// }
237
//
238
// public void testFollow3 () throws ParseException {
239
// Language l = LanguageReader.readLanguage (
240
// "test",
241
// "S = <identifier,'a'> B C B1 <identifier,'c'>;" +
242
// "B = <identifier,'b'>;" +
243
// "B =;" +
244
// "C = <identifier,'c'>;" +
245
// "C =;" +
246
// "B1 = <identifier,'b'>;",
247
// mimeType
248
// );
249
// Analyser a = l.getAnalyser ();
250
// Parser p = Parser.create ();
251
// p.add ("operator", "'{' | '}' | '.' | ';' | ',' | '(' | ')'");
252
// p.add ("whitespace", "['\\n' '\\r' ' ' '\\t']+");
253
// p.add ("keyword", "'package' | 'class' | 'import' | 'static' | 'synchronized' | 'final' | 'abstract' | 'native' | 'import' | 'extends' | 'implements' | 'public' | 'protected' | 'private'");
254
// p.add ("identifier", "['a'-'z' 'A'-'Z' '0'-'9' '_' '-' '$' '%']+");
255
// p.addSkipToken (Token.create ("whitespace", null, false));
256
// Input input = Input.create (
257
// "b d"
258
// );
259
// Input input1 = p.readAll (input);
260
// List ast = new ArrayList ();
261
// try {
262
// a.read (input1);
263
// assertTrue (true);
264
// } catch (ParseException ex) {
265
// System.out.println(ex);
266
// }
267
// print (ast, "");
268
// }
269

270     
271     public void test3 () throws ParseException {
272         Language l = NBSLanguageReader.readLanguage (
273             "test",
274             "TOKEN:operator:( '{' | '}' | '.' | ';' | ',' | '(' | ')' )" +
275             "TOKEN:whitespace:( ['\\n' '\\r' ' ' '\\t']+ )" +
276             "TOKEN:keyword:( 'package' | 'class' | 'import' | 'static' | 'synchronized' | 'final' | 'abstract' | 'native' | 'import' | 'extends' | 'implements' | 'public' | 'protected' | 'private' )" +
277             "TOKEN:identifier:( ['a'-'z' 'A'-'Z' '0'-'9' '_' '-' '$' '%']+ )" +
278             "SKIP:whitespace " +
279             "S = <identifier,'a'> SS <identifier,'b'>;" +
280             "SS = <identifier,'if'> E <identifier,'then'> SS;" +
281             "SS = <identifier,'if'> E <identifier,'then'> SS <identifier,'else'> SS;" +
282             "SS = <identifier, 'b'>;" +
283             "E = <identifier,'e'>;",
284             mimeType
285         );
286         CharInput input = new StringInput (
287             "a if e then if e then b else b b",
288             "source"
289         );
290         ASTNode n = l.getAnalyser ().read (
291             TokenInput.create (
292                 "text/test",
293                 l.getParser (),
294                 input,
295                 l.getSkipTokenTypes ()
296             ),
297             false
298         );
299         assertTrue (input.eof ());
300         assertEquals (3, n.getChildren ().size ());
301         n = (ASTNode) n.getChildren ().get (1);
302         assertEquals (4, n.getChildren ().size ());
303         n = (ASTNode) n.getChildren ().get (3);
304         assertEquals (6, n.getChildren ().size ());
305     }
306     
307     public void test5 () throws ParseException {
308         Language l = NBSLanguageReader.readLanguage (
309             "test",
310             "TOKEN:operator:( '{' | '}' | '.' | ';' | ',' | '(' | ')' )" +
311             "TOKEN:whitespace:( ['\\n' '\\r' ' ' '\\t']+ )" +
312             "TOKEN:keyword:( 'package' | 'class' | 'import' | 'static' | 'synchronized' | 'final' | 'abstract' | 'native' | 'import' | 'extends' | 'implements' | 'public' | 'protected' | 'private' )" +
313             "TOKEN:identifier:( ['a'-'z' 'A'-'Z' '0'-'9' '_' '-' '$' '%']+ )" +
314             "SKIP:whitespace " +
315             "S = <identifier,'a'> SS <identifier,'b'>;" +
316             "SS = <identifier,'if'> E <identifier,'then'> SS;" +
317             "SS = <identifier,'if'> E <identifier,'then'> SS <identifier,'else'> SS;" +
318             "SS = <identifier, 'b'>;" +
319             "E = <identifier,'e'>;",
320             mimeType
321         );
322         CharInput input = new StringInput (
323             "a if e then if e then b else b b",
324             "source"
325         );
326         ASTNode n = l.getAnalyser ().read (
327             TokenInput.create (
328                 "text/test",
329                 l.getParser (),
330                 input,
331                 l.getSkipTokenTypes ()
332             ),
333             false
334         );
335         System.out.println(n.print ());
336         assertTrue (input.eof ());
337         assertEquals (3, n.getChildren ().size ());
338         n = (ASTNode) n.getChildren ().get (1);
339         assertEquals (4, n.getChildren ().size ());
340         n = (ASTNode) n.getChildren ().get (3);
341         assertEquals (6, n.getChildren ().size ());
342     }
343     
344     private void print (List JavaDoc l, String JavaDoc indent) {
345         Iterator JavaDoc it = l.iterator ();
346         while (it.hasNext ()) {
347             Object JavaDoc next = it.next ();
348             System.out.println (indent + next);
349             if (next instanceof ASTToken) continue;
350             print ((List JavaDoc) it.next (), indent + " ");
351         }
352     }
353 }
354
Popular Tags