KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ParserTest.java
3  * JUnit based test
4  *
5  * Created on January 30, 2006, 9:32 PM
6  */

7
8 package org.netbeans.modules.languages.parser;
9
10 import java.util.Map JavaDoc;
11 import org.netbeans.api.languages.ASTToken;
12 import org.netbeans.api.languages.CharInput;
13 import org.netbeans.api.languages.ParseException;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16 import junit.framework.*;
17 import org.netbeans.modules.languages.Feature;
18 import org.netbeans.modules.languages.Language;
19 import org.netbeans.modules.languages.parser.Parser.Cookie;
20
21 /**
22  *
23  * @author Jan Jancura
24  */

25 public class ParserTest extends TestCase {
26     
27     public ParserTest (String JavaDoc testName) {
28         super (testName);
29     }
30     
31 // public void testRead123 () throws ParseException {
32
// Parser abc = Parser.create ("abc", "abc");
33
// Parser axd = Parser.create ("a.d", "axd");
34
// Parser p = abc.merge (axd);
35
// Input in = Input.create (
36
// "abd"
37
// );
38
// assertEquals ("state: " + p.getState () + " text: " + in, "axd", p.read (p.getState (), in));
39
// assertEquals (0, in.current ());
40
// assertEquals (true, in.eof ());
41
// }
42

43 // public void testRead () throws ParseException {
44
// Parser abc = Parser.create ("(abc)*|(ab)*", "abc");
45
// Parser abcd = Parser.create ("abcd", "abcd");
46
// Parser whitespace = Parser.create ("[ \n\r\t]+", "whitespace");
47
// Parser p = abc.merge (abcd).append (whitespace).star ();
48
// Input in = Input.create (
49
// "abab abcd abcabcd abcd"
50
// );
51
// assertEquals ("state: " + p.getState () + " text: " + in, "abc", p.read (p.getState (), in));
52
// assertEquals ("state: " + p.getState () + " text: " + in, "whitespace", p.read (p.getState (), in));
53
// assertEquals ("state: " + p.getState () + " text: " + in, "abcd", p.read (p.getState (), in));
54
// assertEquals ("state: " + p.getState () + " text: " + in, "whitespace", p.read (p.getState (), in));
55
// assertEquals ("state: " + p.getState () + " text: " + in, "abc", p.read (p.getState (), in));
56
// assertEquals ("state: " + p.getState () + " text: " + in, null, p.read (p.getState (), in));
57
// in.read ();
58
// assertEquals ("state: " + p.getState () + " text: " + in, "whitespace", p.read (p.getState (), in));
59
// assertEquals ("state: " + p.getState () + " text: " + in, "abcd", p.read (p.getState (), in));
60
// assertEquals (0, in.current ());
61
// assertEquals (true, in.eof ());
62
// }
63
//
64
// public void testRead1 () throws ParseException {
65
// Parser value = Parser.create ("[^\n]+", "VALUE");
66
// Parser eol = Parser.create ("[\n]", "EOL");
67
// Parser p = value.merge (eol).star ();
68
// Input in = Input.create (
69
// "test:ttt\n" +
70
// "test\n" +
71
// "#comment"
72
// );
73
// assertEquals ("state: " + p.getState () + " text: " + in, "VALUE", p.read (p.getState (), in));
74
// assertEquals ("state: " + p.getState () + " text: " + in, "EOL", p.read (p.getState (), in));
75
// assertEquals ("state: " + p.getState () + " text: " + in, "VALUE", p.read (p.getState (), in));
76
// assertEquals ("state: " + p.getState () + " text: " + in, "EOL", p.read (p.getState (), in));
77
// assertEquals ("state: " + p.getState () + " text: " + in, "VALUE", p.read (p.getState (), in));
78
// assertEquals (0, in.current ());
79
// assertEquals (true, in.eof ());
80
// }
81
//
82
// public void testReadManifest () throws ParseException {
83
// Parser p = Parser.create ();
84
// p.add ("LINE_COMMENT", "#[^\n\r]*");
85
// p.add ("EOL", "[\n\r][\n\r]?");
86
// p.add (p.DEFAULT_STATE, "NAME", "[^:\n\r]*", "STATE_COLON");
87
// p.add ("STATE_COLON", "COLON", ":", "STATE_VALUE");
88
// p.add ("STATE_COLON", "EOL", "[\n\r][\n\r]?", p.DEFAULT_STATE);
89
// p.add ("STATE_VALUE", "VALUE", "[^\n\r]*", p.DEFAULT_STATE);
90
//
91
// Input in = Input.create (
92
// "#comment\n" +
93
// "test:ttt\n" +
94
// "#comment\n" +
95
// "test\n" +
96
// "t:t"
97
// );
98
// System.out.println(p);
99
// assertEquals ("state: " + p.getState () + " text: " + in, "LINE_COMMENT", p.read (p.getState (), in));
100
// assertEquals ("state: " + p.getState () + " text: " + in, "EOL", p.read (p.getState (), in));
101
// assertEquals ("state: " + p.getState () + " text: " + in, "NAME", p.read (p.getState (), in));
102
// assertEquals ("state: " + p.getState () + " text: " + in, "COLON", p.read (p.getState (), in));
103
// assertEquals ("state: " + p.getState () + " text: " + in, "VALUE", p.read (p.getState (), in));
104
// assertEquals ("state: " + p.getState () + " text: " + in, "EOL", p.read (p.getState (), in));
105
// assertEquals ("state: " + p.getState () + " text: " + in, "LINE_COMMENT", p.read (p.getState (), in));
106
// assertEquals ("state: " + p.getState () + " text: " + in, "EOL", p.read (p.getState (), in));
107
// assertEquals ("state: " + p.getState () + " text: " + in, "NAME", p.read (p.getState (), in));
108
// assertEquals ("state: " + p.getState () + " text: " + in, "EOL", p.read (p.getState (), in));
109
// assertEquals ("state: " + p.getState () + " text: " + in, "NAME", p.read (p.getState (), in));
110
// assertEquals ("state: " + p.getState () + " text: " + in, "COLON", p.read (p.getState (), in));
111
// assertEquals ("state: " + p.getState () + " text: " + in, "VALUE", p.read (p.getState (), in));
112
// assertEquals (0, in.current ());
113
// assertEquals (true, in.eof ());
114
// }
115
//
116

117     
118     private static Parser create (String JavaDoc[] l) throws ParseException {
119         List JavaDoc rules = new ArrayList JavaDoc ();
120         int i, k = l.length;
121         for (i = 0; i < k; i+=4) {
122             String JavaDoc tokenName = (String JavaDoc) l [i + 1];
123             String JavaDoc pattern = (String JavaDoc) l [i + 2];
124             rules.add (Language.createTokenType (
125                 (String JavaDoc) l [i],
126                 Pattern.create (pattern),
127                 tokenName,
128                 (String JavaDoc) l [i + 3],
129                 i / 4,
130                 null
131             ));
132         }
133         return Parser.create (rules);
134     }
135     
136     public void testReadJava () throws ParseException {
137         Parser parser = create (new String JavaDoc[] {
138             null, "comment", "'/*'-'*/'", null,
139             null, "comment", "'//'[^'\n''\r']*", null,
140             null, "keyword", "'if'|'else'|'public'|'static'|'private'|'protected'|'class'|'extends'|'import'|'package'|'try'|'int'|'false'|'true'|'void'", null,
141             null, "operator", "['\\\\''/''*''-''+''.'',''=''{''}''('')''!''@''#''$''%''^''&''~''|'';']", null,
142             null, "string", "'\\\"'[^'\\\"']*'\\\"'", null,
143             null, "char", "'\\\''[^'\\\'']'\\\''", null,
144             null, "number", "['0'-'9']['0'-'9''.']*", null,
145             null, "whitespace", "[' ''\t''\n''\r']+", null,
146             null, "identifier", "['a'-'z''A'-'Z'][^' ''\\t''\\n''\\r''/''*''-''+''.'',''=''{''}''('')''!''@''#''$''%''^''&''~''|''\\\\'';']*", null
147         });
148         
149         System.out.println (parser);
150         CharInput in = new StringInput (
151             "/** dsfsf\n" +
152             " asd as * asdf */ " +
153             "package ifa.vv.b;\n" +
154             "import a.v;\n" +
155             "\n" +
156             "public class Asd extends sd {\n" +
157             " void main (String[] s) {\n" +
158             " return \"asd\" + \'a\' + 123;\n" +
159             " }\n" +
160             "}",
161             "test"
162         );
163         Cookie c = new MyCookie ();
164         assertEquals ("state: " + c.getState () + " text: " + in, "comment", ((ASTToken) parser.read (c, in, "text/test")).getType ());
165         assertEquals ("state: " + c.getState () + " text: " + in, "whitespace", ((ASTToken) parser.read (c, in, "text/test")).getType ());
166         assertEquals ("state: " + c.getState () + " text: " + in, "keyword", ((ASTToken) parser.read (c, in, "text/test")).getType ());
167         assertEquals ("state: " + c.getState () + " text: " + in, "whitespace", ((ASTToken) parser.read (c, in, "text/test")).getType ());
168         assertEquals ("state: " + c.getState () + " text: " + in, "identifier", ((ASTToken) parser.read (c, in, "text/test")).getType ());
169         assertEquals ("state: " + c.getState () + " text: " + in, "operator", ((ASTToken) parser.read (c, in, "text/test")).getType ());
170         assertEquals ("state: " + c.getState () + " text: " + in, "identifier", ((ASTToken) parser.read (c, in, "text/test")).getType ());
171         assertEquals ("state: " + c.getState () + " text: " + in, "operator", ((ASTToken) parser.read (c, in, "text/test")).getType ());
172         assertEquals ("state: " + c.getState () + " text: " + in, "identifier", ((ASTToken) parser.read (c, in, "text/test")).getType ());
173         assertEquals ("state: " + c.getState () + " text: " + in, "operator", ((ASTToken) parser.read (c, in, "text/test")).getType ());
174     }
175     
176     public void testReadHtml () throws ParseException {
177         Parser parser = create (new String JavaDoc[] {
178             null, "TEXT", "[^'<']*", null,
179             null, "COMMENT", "'<!'-'-->'", null,
180             Parser.DEFAULT_STATE, "ELEMENT", "'<'[^' ''>''\\t''\\n''\\r']+", "IN_ELEMENT",
181             null, "END_ELEMENT", "'</'[^' ''>''\\t''\\n''\\r']+'>'", null,
182             "IN_ELEMENT", "ELEMENT", "'/'?'>'", Parser.DEFAULT_STATE,
183             "IN_ELEMENT", "ATTRIBUTE_NAME", "[^' ''>''=''\\t''\\n''\\r']+", "IN_ATTRIBUTE",
184             "IN_ATTRIBUTE", "OPERATOR", "'='", "IN_ATTRIBUTE2",
185             "IN_ATTRIBUTE2", "ATTRIBUTE_VALUE", "'\\\"'[^'\\\"']*'\\\"'", "IN_ELEMENT",
186         
187             "IN_ATTRIBUTE", "ELEMENT", "'/'?'>'", Parser.DEFAULT_STATE,
188             "IN_ATTRIBUTE", "ATTRIBUTE_NAME", "[^' ''=''\\t''\\n''\\r']+", "IN_ATTRIBUTE",
189             "IN_ELEMENT", "WHITESPACE", "[' ''\\t''\\n''\\r']*", "IN_ELEMENT",
190             "IN_ATTRIBUTE", "WHITESPACE", "[' ''\\t''\\n''\\r']*", "IN_ATTRIBUTE",
191             "IN_ATTRIBUTE2", "WHITESPACE", "[' ''\\t''\\n''\\r']*", "IN_ATTRIBUTE2"
192         });
193         
194         CharInput in = new StringInput (
195             "<test a=\"asz\"> \n" +
196             " \" sdf\" = sdf" +
197             "<!-- NewPage" +
198             " ?!?@? -- > sdf -> -->\n" +
199             "<HTML><\\HTML> <test t>",
200             "test"
201         );
202         Cookie c = new MyCookie ();
203         assertEquals ("state: " + c + " text: " + in, "ELEMENT", ((ASTToken) parser.read (c, in, "text/test")).getType ());
204         assertEquals ("state: " + c + " text: " + in, "WHITESPACE", ((ASTToken) parser.read (c, in, "text/test")).getType ());
205         assertEquals ("state: " + c + " text: " + in, "ATTRIBUTE_NAME", ((ASTToken) parser.read (c, in, "text/test")).getType ());
206         assertEquals ("state: " + c + " text: " + in, "OPERATOR", ((ASTToken) parser.read (c, in, "text/test")).getType ());
207         assertEquals ("state: " + c + " text: " + in, "ATTRIBUTE_VALUE", ((ASTToken) parser.read (c, in, "text/test")).getType ());
208         assertEquals ("state: " + c + " text: " + in, "ELEMENT", ((ASTToken) parser.read (c, in, "text/test")).getType ());
209         assertEquals ("state: " + c + " text: " + in, "TEXT", ((ASTToken) parser.read (c, in, "text/test")).getType ());
210         assertEquals ("state: " + c + " text: " + in, "COMMENT", ((ASTToken) parser.read (c, in, "text/test")).getType ());
211         assertEquals ("state: " + c + " text: " + in, "TEXT", ((ASTToken) parser.read (c, in, "text/test")).getType ());
212         assertEquals ("state: " + c + " text: " + in, "ELEMENT", ((ASTToken) parser.read (c, in, "text/test")).getType ());
213         assertEquals ("state: " + c + " text: " + in, "ELEMENT", ((ASTToken) parser.read (c, in, "text/test")).getType ());
214         assertEquals ("state: " + c + " text: " + in, "ELEMENT", ((ASTToken) parser.read (c, in, "text/test")).getType ());
215         assertEquals ("state: " + c + " text: " + in, "ELEMENT", ((ASTToken) parser.read (c, in, "text/test")).getType ());
216         assertEquals ("state: " + c + " text: " + in, "TEXT", ((ASTToken) parser.read (c, in, "text/test")).getType ());
217         assertEquals ("state: " + c + " text: " + in, "ELEMENT", ((ASTToken) parser.read (c, in, "text/test")).getType ());
218         assertEquals ("state: " + c + " text: " + in, "WHITESPACE", ((ASTToken) parser.read (c, in, "text/test")).getType ());
219         assertEquals ("state: " + c + " text: " + in, "ATTRIBUTE_NAME", ((ASTToken) parser.read (c, in, "text/test")).getType ());
220         assertEquals ("state: " + c + " text: " + in, "ELEMENT", ((ASTToken) parser.read (c, in, "text/test")).getType ());
221         assertTrue (in.eof ());
222     }
223     
224     private static class MyCookie implements Parser.Cookie {
225         
226         private int state = -1;
227         
228         public int getState () {
229             return state;
230         }
231
232         public void setState (int state) {
233             this.state = state;
234         }
235     
236         public void setProperties (Feature properties) {
237         }
238     }
239 }
240
Popular Tags