1 | /**************************************************************** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one * |
---|
3 | * or more contributor license agreements. See the NOTICE file * |
---|
4 | * distributed with this work for additional information * |
---|
5 | * regarding copyright ownership. The ASF licenses this file * |
---|
6 | * to you under the Apache License, Version 2.0 (the * |
---|
7 | * "License"); you may not use this file except in compliance * |
---|
8 | * with the License. You may obtain a copy of the License at * |
---|
9 | * * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 * |
---|
11 | * * |
---|
12 | * Unless required by applicable law or agreed to in writing, * |
---|
13 | * software distributed under the License is distributed on an * |
---|
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * |
---|
15 | * KIND, either express or implied. See the License for the * |
---|
16 | * specific language governing permissions and limitations * |
---|
17 | * under the License. * |
---|
18 | ****************************************************************/ |
---|
19 | |
---|
20 | |
---|
21 | /** |
---|
22 | * RFC2822 address list parser. |
---|
23 | * |
---|
24 | * Created 9/17/2004 |
---|
25 | * by Joe Cheng <code@joecheng.com> |
---|
26 | */ |
---|
27 | |
---|
28 | options { |
---|
29 | STATIC=false; |
---|
30 | LOOKAHEAD=1; |
---|
31 | JDK_VERSION = "1.5"; |
---|
32 | VISITOR=true; |
---|
33 | MULTI=true; |
---|
34 | NODE_SCOPE_HOOK=true; |
---|
35 | NODE_EXTENDS="org.apache.james.mime4j.field.address.BaseNode"; |
---|
36 | //DEBUG_PARSER=true; |
---|
37 | //DEBUG_TOKEN_MANAGER=true; |
---|
38 | } |
---|
39 | |
---|
40 | PARSER_BEGIN(AddressListParser) |
---|
41 | /**************************************************************** |
---|
42 | * Licensed to the Apache Software Foundation (ASF) under one * |
---|
43 | * or more contributor license agreements. See the NOTICE file * |
---|
44 | * distributed with this work for additional information * |
---|
45 | * regarding copyright ownership. The ASF licenses this file * |
---|
46 | * to you under the Apache License, Version 2.0 (the * |
---|
47 | * "License"); you may not use this file except in compliance * |
---|
48 | * with the License. You may obtain a copy of the License at * |
---|
49 | * * |
---|
50 | * http://www.apache.org/licenses/LICENSE-2.0 * |
---|
51 | * * |
---|
52 | * Unless required by applicable law or agreed to in writing, * |
---|
53 | * software distributed under the License is distributed on an * |
---|
54 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * |
---|
55 | * KIND, either express or implied. See the License for the * |
---|
56 | * specific language governing permissions and limitations * |
---|
57 | * under the License. * |
---|
58 | ****************************************************************/ |
---|
59 | package org.apache.james.mime4j.field.address; |
---|
60 | |
---|
61 | public class AddressListParser { |
---|
62 | public static void main(String args[]) throws ParseException { |
---|
63 | while (true) { |
---|
64 | try { |
---|
65 | AddressListParser parser = new AddressListParser(System.in); |
---|
66 | parser.parseLine(); |
---|
67 | ((SimpleNode) parser.jjtree.rootNode()).dump("> "); |
---|
68 | } catch (Exception x) { |
---|
69 | x.printStackTrace(); |
---|
70 | return; |
---|
71 | } |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | public ASTaddress_list parseAddressList() throws ParseException { |
---|
76 | try { |
---|
77 | parseAddressList0(); |
---|
78 | return (ASTaddress_list) jjtree.rootNode(); |
---|
79 | } catch (TokenMgrError tme) { |
---|
80 | throw new ParseException(tme.getMessage()); |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | public ASTaddress parseAddress() throws ParseException { |
---|
85 | try { |
---|
86 | parseAddress0(); |
---|
87 | return (ASTaddress) jjtree.rootNode(); |
---|
88 | } catch (TokenMgrError tme) { |
---|
89 | throw new ParseException(tme.getMessage()); |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | public ASTmailbox parseMailbox() throws ParseException { |
---|
94 | try { |
---|
95 | parseMailbox0(); |
---|
96 | return (ASTmailbox) jjtree.rootNode(); |
---|
97 | } catch (TokenMgrError tme) { |
---|
98 | throw new ParseException(tme.getMessage()); |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | void jjtreeOpenNodeScope(Node n) { |
---|
103 | ((SimpleNode) n).firstToken = getToken(1); |
---|
104 | } |
---|
105 | |
---|
106 | void jjtreeCloseNodeScope(Node n) { |
---|
107 | ((SimpleNode) n).lastToken = getToken(0); |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | PARSER_END(AddressListParser) |
---|
112 | |
---|
113 | void parseLine() #void : |
---|
114 | {} |
---|
115 | { |
---|
116 | address_list() ["\r"] "\n" |
---|
117 | } |
---|
118 | |
---|
119 | void parseAddressList0() #void : |
---|
120 | {} |
---|
121 | { |
---|
122 | address_list() <EOF> |
---|
123 | } |
---|
124 | |
---|
125 | void parseAddress0() #void : |
---|
126 | {} |
---|
127 | { |
---|
128 | address() <EOF> |
---|
129 | } |
---|
130 | |
---|
131 | void parseMailbox0() #void : |
---|
132 | {} |
---|
133 | { |
---|
134 | mailbox() <EOF> |
---|
135 | } |
---|
136 | |
---|
137 | void address_list() : |
---|
138 | {} |
---|
139 | { |
---|
140 | [ address() ] |
---|
141 | ( |
---|
142 | "," |
---|
143 | [ address() ] |
---|
144 | )* |
---|
145 | } |
---|
146 | |
---|
147 | void address() : |
---|
148 | {} |
---|
149 | { |
---|
150 | LOOKAHEAD(2147483647) |
---|
151 | ( phrase() (group_body() | angle_addr()) ) |
---|
152 | | angle_addr() |
---|
153 | | addr_spec() |
---|
154 | } |
---|
155 | |
---|
156 | void mailbox() : |
---|
157 | {} |
---|
158 | { |
---|
159 | LOOKAHEAD(2147483647) |
---|
160 | name_addr() |
---|
161 | | angle_addr() |
---|
162 | | addr_spec() |
---|
163 | } |
---|
164 | |
---|
165 | void name_addr() : |
---|
166 | {} |
---|
167 | { |
---|
168 | phrase() angle_addr() |
---|
169 | } |
---|
170 | |
---|
171 | void group_body() : |
---|
172 | {} |
---|
173 | { |
---|
174 | ":" |
---|
175 | [ mailbox() ] |
---|
176 | ( |
---|
177 | "," |
---|
178 | [ mailbox() ] |
---|
179 | )* |
---|
180 | ";" |
---|
181 | } |
---|
182 | |
---|
183 | void angle_addr() : |
---|
184 | {} |
---|
185 | { |
---|
186 | "<" [ route() ] addr_spec() ">" |
---|
187 | } |
---|
188 | |
---|
189 | void route() : |
---|
190 | {} |
---|
191 | { |
---|
192 | "@" domain() ( (",")* "@" domain() )* ":" |
---|
193 | } |
---|
194 | |
---|
195 | void phrase() : |
---|
196 | {} |
---|
197 | { |
---|
198 | ( <DOTATOM> |
---|
199 | | <QUOTEDSTRING> |
---|
200 | )+ |
---|
201 | } |
---|
202 | |
---|
203 | void addr_spec() : |
---|
204 | {} |
---|
205 | { |
---|
206 | ( local_part() [ "@" domain() ] ) |
---|
207 | { |
---|
208 | if(jjtThis.jjtGetNumChildren() == 1) { |
---|
209 | |
---|
210 | org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(getClass()) ; |
---|
211 | log.debug("Fake domain"); |
---|
212 | |
---|
213 | ASTdomain dom = new ASTdomain(JJTDOMAIN); |
---|
214 | Token tk = new Token(); |
---|
215 | tk.kind = AddressListParserConstants.DOTATOM; |
---|
216 | tk.image = ""; |
---|
217 | dom.firstToken = tk; |
---|
218 | dom.lastToken = tk; |
---|
219 | jjtThis.jjtAddChild(dom, 1); |
---|
220 | } |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | void local_part() : |
---|
225 | { Token t; } |
---|
226 | { |
---|
227 | ( t=<DOTATOM> | t=<QUOTEDSTRING> ) |
---|
228 | ( [t="."] |
---|
229 | { |
---|
230 | if ( t.kind == AddressListParserConstants.QUOTEDSTRING || t.image.charAt(t.image.length() - 1) != '.') |
---|
231 | throw new ParseException("Words in local part must be separated by '.'"); |
---|
232 | } |
---|
233 | ( t=<DOTATOM> | t=<QUOTEDSTRING> ) |
---|
234 | )* |
---|
235 | } |
---|
236 | |
---|
237 | void domain() : |
---|
238 | { Token t; } |
---|
239 | { |
---|
240 | ( t=<DOTATOM> |
---|
241 | ( [t="."] |
---|
242 | { |
---|
243 | if (t.image.charAt(t.image.length() - 1) != '.') |
---|
244 | throw new ParseException("Atoms in domain names must be separated by '.'"); |
---|
245 | } |
---|
246 | t=<DOTATOM> |
---|
247 | )* |
---|
248 | ) |
---|
249 | | <DOMAINLITERAL> |
---|
250 | } |
---|
251 | |
---|
252 | SPECIAL_TOKEN : |
---|
253 | { |
---|
254 | < WS: ( [" ", "\t"] )+ > |
---|
255 | } |
---|
256 | |
---|
257 | TOKEN : |
---|
258 | { |
---|
259 | < #ALPHA: ["a" - "z", "A" - "Z"] > |
---|
260 | | < #DIGIT: ["0" - "9"] > |
---|
261 | | < #ATEXT: ( <ALPHA> | <DIGIT> |
---|
262 | | "!" | "#" | "$" | "%" |
---|
263 | | "&" | "'" | "*" | "+" |
---|
264 | | "-" | "/" | "=" | "?" |
---|
265 | | "^" | "_" | "`" | "{" |
---|
266 | | "|" | "}" | "~" |
---|
267 | )> |
---|
268 | | < DOTATOM: <ATEXT> ( <ATEXT> | "." )* > |
---|
269 | } |
---|
270 | |
---|
271 | TOKEN_MGR_DECLS : |
---|
272 | { |
---|
273 | // Keeps track of how many levels of comment nesting |
---|
274 | // we've encountered. This is only used when the 2nd |
---|
275 | // level is reached, for example ((this)), not (this). |
---|
276 | // This is because the outermost level must be treated |
---|
277 | // specially anyway, because the outermost ")" has a |
---|
278 | // different token type than inner ")" instances. |
---|
279 | static int commentNest; |
---|
280 | } |
---|
281 | |
---|
282 | MORE : |
---|
283 | { |
---|
284 | // domain literal |
---|
285 | "[" : INDOMAINLITERAL |
---|
286 | } |
---|
287 | |
---|
288 | <INDOMAINLITERAL> |
---|
289 | MORE : |
---|
290 | { |
---|
291 | < <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); } |
---|
292 | | < ~["[", "]", "\\"] > |
---|
293 | } |
---|
294 | |
---|
295 | <INDOMAINLITERAL> |
---|
296 | TOKEN : |
---|
297 | { |
---|
298 | < DOMAINLITERAL: "]" > { matchedToken.image = image.toString(); }: DEFAULT |
---|
299 | } |
---|
300 | |
---|
301 | MORE : |
---|
302 | { |
---|
303 | // starts a comment |
---|
304 | "(" : INCOMMENT |
---|
305 | } |
---|
306 | |
---|
307 | <INCOMMENT> |
---|
308 | SKIP : |
---|
309 | { |
---|
310 | // ends a comment |
---|
311 | < COMMENT: ")" > : DEFAULT |
---|
312 | // if this is ever changed to not be a SKIP, need |
---|
313 | // to make sure matchedToken.token = token.toString() |
---|
314 | // is called. |
---|
315 | } |
---|
316 | |
---|
317 | <INCOMMENT> |
---|
318 | MORE : |
---|
319 | { |
---|
320 | < <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); } |
---|
321 | | "(" { commentNest = 1; } : NESTED_COMMENT |
---|
322 | | < <ANY>> |
---|
323 | } |
---|
324 | |
---|
325 | <NESTED_COMMENT> |
---|
326 | MORE : |
---|
327 | { |
---|
328 | < <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); } |
---|
329 | | "(" { ++commentNest; } |
---|
330 | | ")" { --commentNest; if (commentNest == 0) SwitchTo(INCOMMENT); } |
---|
331 | | < <ANY>> |
---|
332 | } |
---|
333 | |
---|
334 | |
---|
335 | // QUOTED STRINGS |
---|
336 | |
---|
337 | MORE : |
---|
338 | { |
---|
339 | "\"" { image.deleteCharAt(image.length() - 1); } : INQUOTEDSTRING |
---|
340 | } |
---|
341 | |
---|
342 | <INQUOTEDSTRING> |
---|
343 | MORE : |
---|
344 | { |
---|
345 | < <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); } |
---|
346 | | < (~["\"", "\\"])+ > |
---|
347 | } |
---|
348 | |
---|
349 | <INQUOTEDSTRING> |
---|
350 | TOKEN : |
---|
351 | { |
---|
352 | < QUOTEDSTRING: "\"" > { matchedToken.image = image.substring(0, image.length() - 1); } : DEFAULT |
---|
353 | } |
---|
354 | |
---|
355 | // GLOBALS |
---|
356 | |
---|
357 | <*> |
---|
358 | TOKEN : |
---|
359 | { |
---|
360 | < #QUOTEDPAIR: "\\" <ANY> > |
---|
361 | | < #ANY: ~[] > |
---|
362 | } |
---|
363 | |
---|
364 | // ERROR! |
---|
365 | /* |
---|
366 | |
---|
367 | <*> |
---|
368 | TOKEN : |
---|
369 | { |
---|
370 | < UNEXPECTED_CHAR: <ANY> > |
---|
371 | } |
---|
372 | |
---|
373 | */ |
---|