summaryrefslogtreecommitdiffstats
path: root/src/org/apache/james/mime4j/field/address/parser/AddressListParser.jj
blob: 685988634129ed5a76a60cba0a1e5dd15e0e52c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/*@bgen(jjtree) Generated By:JJTree: Do not edit this line. /Users/jason/Projects/apache-mime4j-0.3/target/generated-sources/jjtree/org/apache/james/mime4j/field/address/parser/AddressListParser.jj */
/*@egen*//****************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one   *
 * or more contributor license agreements.  See the NOTICE file *
 * distributed with this work for additional information        *
 * regarding copyright ownership.  The ASF licenses this file   *
 * to you under the Apache License, Version 2.0 (the            *
 * "License"); you may not use this file except in compliance   *
 * with the License.  You may obtain a copy of the License at   *
 *                                                              *
 *   http://www.apache.org/licenses/LICENSE-2.0                 *
 *                                                              *
 * Unless required by applicable law or agreed to in writing,   *
 * software distributed under the License is distributed on an  *
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
 * KIND, either express or implied.  See the License for the    *
 * specific language governing permissions and limitations      *
 * under the License.                                           *
 ****************************************************************/


/**
 * RFC2822 address list parser.
 *
 * Created 9/17/2004
 * by Joe Cheng <code@joecheng.com>
 */

options {
	STATIC=false;
	LOOKAHEAD=1;                                                                                                                               
	//DEBUG_PARSER=true;
	//DEBUG_TOKEN_MANAGER=true;
}

PARSER_BEGIN(AddressListParser)
/*
 *  Copyright 2004 the mime4j project
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.james.mime4j.field.address.parser;

public class AddressListParser/*@bgen(jjtree)*/implements AddressListParserTreeConstants/*@egen*/ {/*@bgen(jjtree)*/
  protected JJTAddressListParserState jjtree = new JJTAddressListParserState();

/*@egen*/
    public static void main(String args[]) throws ParseException {
		while (true) {
		    try {
				AddressListParser parser = new AddressListParser(System.in);
		    	parser.parseLine();
		    	((SimpleNode)parser.jjtree.rootNode()).dump("> ");
		    } catch (Exception x) {
				x.printStackTrace();
				return;
		    }
		}
    }
    
    private static void log(String msg) {
    	System.out.print(msg);
    }
    
    public ASTaddress_list parse() throws ParseException {
        try {
    	    parseAll();
    	    return (ASTaddress_list)jjtree.rootNode();
    	} catch (TokenMgrError tme) {
    	    throw new ParseException(tme.getMessage());
    	}
    }
    
    
    void jjtreeOpenNodeScope(Node n) {
    	((SimpleNode)n).firstToken = getToken(1);
    }
    
    void jjtreeCloseNodeScope(Node n) {
    	((SimpleNode)n).lastToken = getToken(0);
    }
}

PARSER_END(AddressListParser)

void parseLine()       :
{}
{
	address_list() ["\r"] "\n"
}

void parseAll()       :
{}
{
	address_list() <EOF>
}

void address_list() :
{/*@bgen(jjtree) address_list */
  ASTaddress_list jjtn000 = new ASTaddress_list(JJTADDRESS_LIST);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);
  jjtreeOpenNodeScope(jjtn000);
/*@egen*/}
{/*@bgen(jjtree) address_list */
        try {
/*@egen*/
	[ address() ]
	(
		","
		[ address() ]
	)*/*@bgen(jjtree)*/
        } catch (Throwable jjte000) {
          if (jjtc000) {
            jjtree.clearNodeScope(jjtn000);
            jjtc000 = false;
          } else {
            jjtree.popNode();
          }
          if (jjte000 instanceof RuntimeException) {
            throw (RuntimeException)jjte000;
          }
          if (jjte000 instanceof ParseException) {
            throw (ParseException)jjte000;
          }
          throw (Error)jjte000;
        } finally {
          if (jjtc000) {
            jjtree.closeNodeScope(jjtn000, true);
            jjtreeCloseNodeScope(jjtn000);
          }
        }
/*@egen*/
}

void address() :
{/*@bgen(jjtree) address */
  ASTaddress jjtn000 = new ASTaddress(JJTADDRESS);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);
  jjtreeOpenNodeScope(jjtn000);
/*@egen*/}
{/*@bgen(jjtree) address */
        try {
/*@egen*/
	LOOKAHEAD(2147483647)
	addr_spec()
|	angle_addr()
|	( phrase() (group_body() | angle_addr()) )/*@bgen(jjtree)*/
        } catch (Throwable jjte000) {
          if (jjtc000) {
            jjtree.clearNodeScope(jjtn000);
            jjtc000 = false;
          } else {
            jjtree.popNode();
          }
          if (jjte000 instanceof RuntimeException) {
            throw (RuntimeException)jjte000;
          }
          if (jjte000 instanceof ParseException) {
            throw (ParseException)jjte000;
          }
          throw (Error)jjte000;
        } finally {
          if (jjtc000) {
            jjtree.closeNodeScope(jjtn000, true);
            jjtreeCloseNodeScope(jjtn000);
          }
        }
/*@egen*/
}

void mailbox() :
{/*@bgen(jjtree) mailbox */
  ASTmailbox jjtn000 = new ASTmailbox(JJTMAILBOX);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);
  jjtreeOpenNodeScope(jjtn000);
/*@egen*/}
{/*@bgen(jjtree) mailbox */
        try {
/*@egen*/
	LOOKAHEAD(2147483647)
	addr_spec()
|	angle_addr()
|	name_addr()/*@bgen(jjtree)*/
        } catch (Throwable jjte000) {
          if (jjtc000) {
            jjtree.clearNodeScope(jjtn000);
            jjtc000 = false;
          } else {
            jjtree.popNode();
          }
          if (jjte000 instanceof RuntimeException) {
            throw (RuntimeException)jjte000;
          }
          if (jjte000 instanceof ParseException) {
            throw (ParseException)jjte000;
          }
          throw (Error)jjte000;
        } finally {
          if (jjtc000) {
            jjtree.closeNodeScope(jjtn000, true);
            jjtreeCloseNodeScope(jjtn000);
          }
        }
/*@egen*/
}

void name_addr() :
{/*@bgen(jjtree) name_addr */
  ASTname_addr jjtn000 = new ASTname_addr(JJTNAME_ADDR);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);
  jjtreeOpenNodeScope(jjtn000);
/*@egen*/}
{/*@bgen(jjtree) name_addr */
        try {
/*@egen*/
	phrase() angle_addr()/*@bgen(jjtree)*/
        } catch (Throwable jjte000) {
          if (jjtc000) {
            jjtree.clearNodeScope(jjtn000);
            jjtc000 = false;
          } else {
            jjtree.popNode();
          }
          if (jjte000 instanceof RuntimeException) {
            throw (RuntimeException)jjte000;
          }
          if (jjte000 instanceof ParseException) {
            throw (ParseException)jjte000;
          }
          throw (Error)jjte000;
        } finally {
          if (jjtc000) {
            jjtree.closeNodeScope(jjtn000, true);
            jjtreeCloseNodeScope(jjtn000);
          }
        }
/*@egen*/
}

void group_body() :
{/*@bgen(jjtree) group_body */
  ASTgroup_body jjtn000 = new ASTgroup_body(JJTGROUP_BODY);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);
  jjtreeOpenNodeScope(jjtn000);
/*@egen*/}
{/*@bgen(jjtree) group_body */
        try {
/*@egen*/
	":"
	[ mailbox() ]
	(
		","
		[ mailbox() ]
	)*
	";"/*@bgen(jjtree)*/
        } catch (Throwable jjte000) {
          if (jjtc000) {
            jjtree.clearNodeScope(jjtn000);
            jjtc000 = false;
          } else {
            jjtree.popNode();
          }
          if (jjte000 instanceof RuntimeException) {
            throw (RuntimeException)jjte000;
          }
          if (jjte000 instanceof ParseException) {
            throw (ParseException)jjte000;
          }
          throw (Error)jjte000;
        } finally {
          if (jjtc000) {
            jjtree.closeNodeScope(jjtn000, true);
            jjtreeCloseNodeScope(jjtn000);
          }
        }
/*@egen*/
}

void angle_addr() :
{/*@bgen(jjtree) angle_addr */
  ASTangle_addr jjtn000 = new ASTangle_addr(JJTANGLE_ADDR);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);
  jjtreeOpenNodeScope(jjtn000);
/*@egen*/}
{/*@bgen(jjtree) angle_addr */
        try {
/*@egen*/
	"<" [ route() ] addr_spec() ">"/*@bgen(jjtree)*/
        } catch (Throwable jjte000) {
          if (jjtc000) {
            jjtree.clearNodeScope(jjtn000);
            jjtc000 = false;
          } else {
            jjtree.popNode();
          }
          if (jjte000 instanceof RuntimeException) {
            throw (RuntimeException)jjte000;
          }
          if (jjte000 instanceof ParseException) {
            throw (ParseException)jjte000;
          }
          throw (Error)jjte000;
        } finally {
          if (jjtc000) {
            jjtree.closeNodeScope(jjtn000, true);
            jjtreeCloseNodeScope(jjtn000);
          }
        }
/*@egen*/
}

void route() :
{/*@bgen(jjtree) route */
  ASTroute jjtn000 = new ASTroute(JJTROUTE);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);
  jjtreeOpenNodeScope(jjtn000);
/*@egen*/}
{/*@bgen(jjtree) route */
        try {
/*@egen*/
	"@" domain() ( (",")* "@" domain() )* ":"/*@bgen(jjtree)*/
        } catch (Throwable jjte000) {
          if (jjtc000) {
            jjtree.clearNodeScope(jjtn000);
            jjtc000 = false;
          } else {
            jjtree.popNode();
          }
          if (jjte000 instanceof RuntimeException) {
            throw (RuntimeException)jjte000;
          }
          if (jjte000 instanceof ParseException) {
            throw (ParseException)jjte000;
          }
          throw (Error)jjte000;
        } finally {
          if (jjtc000) {
            jjtree.closeNodeScope(jjtn000, true);
            jjtreeCloseNodeScope(jjtn000);
          }
        }
/*@egen*/
}

void phrase() :
{/*@bgen(jjtree) phrase */
  ASTphrase jjtn000 = new ASTphrase(JJTPHRASE);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);
  jjtreeOpenNodeScope(jjtn000);
/*@egen*/}
{/*@bgen(jjtree) phrase */
try {
/*@egen*/
(	<DOTATOM>
|	<QUOTEDSTRING>
)+/*@bgen(jjtree)*/
} finally {
  if (jjtc000) {
    jjtree.closeNodeScope(jjtn000, true);
    jjtreeCloseNodeScope(jjtn000);
  }
}
/*@egen*/
}

void addr_spec() :
{/*@bgen(jjtree) addr_spec */
  ASTaddr_spec jjtn000 = new ASTaddr_spec(JJTADDR_SPEC);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);
  jjtreeOpenNodeScope(jjtn000);
/*@egen*/}
{/*@bgen(jjtree) addr_spec */
        try {
/*@egen*/
	( local_part() "@" domain() )/*@bgen(jjtree)*/
        } catch (Throwable jjte000) {
          if (jjtc000) {
            jjtree.clearNodeScope(jjtn000);
            jjtc000 = false;
          } else {
            jjtree.popNode();
          }
          if (jjte000 instanceof RuntimeException) {
            throw (RuntimeException)jjte000;
          }
          if (jjte000 instanceof ParseException) {
            throw (ParseException)jjte000;
          }
          throw (Error)jjte000;
        } finally {
          if (jjtc000) {
            jjtree.closeNodeScope(jjtn000, true);
            jjtreeCloseNodeScope(jjtn000);
          }
        }
/*@egen*/
}

void local_part() :
{/*@bgen(jjtree) local_part */
  ASTlocal_part jjtn000 = new ASTlocal_part(JJTLOCAL_PART);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);
  jjtreeOpenNodeScope(jjtn000);
/*@egen*/ Token t; }
{/*@bgen(jjtree) local_part */
        try {
/*@egen*/
	( t=<DOTATOM> | t=<QUOTEDSTRING> )
	(	[t="."]
		{
			if (t.image.charAt(t.image.length() - 1) != '.' || t.kind == AddressListParserConstants.QUOTEDSTRING)
				throw new ParseException("Words in local part must be separated by '.'");
		}
		(	t=<DOTATOM> | t=<QUOTEDSTRING> )
	)*/*@bgen(jjtree)*/
        } finally {
          if (jjtc000) {
            jjtree.closeNodeScope(jjtn000, true);
            jjtreeCloseNodeScope(jjtn000);
          }
        }
/*@egen*/
}

void domain() :
{/*@bgen(jjtree) domain */
  ASTdomain jjtn000 = new ASTdomain(JJTDOMAIN);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);
  jjtreeOpenNodeScope(jjtn000);
/*@egen*/ Token t; }
{/*@bgen(jjtree) domain */
        try {
/*@egen*/
	(	t=<DOTATOM>
		(	[t="."]
			{
				if (t.image.charAt(t.image.length() - 1) != '.')
					throw new ParseException("Atoms in domain names must be separated by '.'");
			}
			t=<DOTATOM>
		)*
	)
|	<DOMAINLITERAL>/*@bgen(jjtree)*/
        } finally {
          if (jjtc000) {
            jjtree.closeNodeScope(jjtn000, true);
            jjtreeCloseNodeScope(jjtn000);
          }
        }
/*@egen*/
}

SPECIAL_TOKEN :
{
 	< WS: ( [" ", "\t"] )+ >
}

TOKEN :
{
	< #ALPHA: ["a" - "z", "A" - "Z"] >
|	< #DIGIT: ["0" - "9"] >
|	< #ATEXT: ( <ALPHA> | <DIGIT>
			  | "!" | "#" | "$" | "%"
			  | "&" | "'" | "*" | "+"
			  | "-" | "/" | "=" | "?"
			  | "^" | "_" | "`" | "{"
			  | "|" | "}" | "~"
			  )>
|	< DOTATOM: <ATEXT> ( <ATEXT> | "." )* >
}

TOKEN_MGR_DECLS :
{
	// Keeps track of how many levels of comment nesting
	// we've encountered.  This is only used when the 2nd
	// level is reached, for example ((this)), not (this).
	// This is because the outermost level must be treated
	// specially anyway, because the outermost ")" has a 
	// different token type than inner ")" instances.
	static int commentNest;
}

MORE :
{
	// domain literal
	"[" : INDOMAINLITERAL
}

<INDOMAINLITERAL>
MORE :
{
	< <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); }
|	< ~["[", "]", "\\"] >
}

<INDOMAINLITERAL>
TOKEN :
{
	< DOMAINLITERAL: "]" > { matchedToken.image = image.toString(); }: DEFAULT
}

MORE :
{
	// starts a comment
	"(" : INCOMMENT
}

<INCOMMENT>
SKIP :
{
	// ends a comment
	< COMMENT: ")" > : DEFAULT
	// if this is ever changed to not be a SKIP, need
	// to make sure matchedToken.token = token.toString()
	// is called.
}

<INCOMMENT>
MORE :
{
	< <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); }
|	"(" { commentNest = 1; } : NESTED_COMMENT
|	< <ANY>>
}

<NESTED_COMMENT>
MORE :
{
	< <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); }
|	"(" { ++commentNest; }
|	")" { --commentNest; if (commentNest == 0) SwitchTo(INCOMMENT); }
|	< <ANY>>
}


// QUOTED STRINGS

MORE :
{
	"\"" { image.deleteCharAt(image.length() - 1); } : INQUOTEDSTRING
}

<INQUOTEDSTRING>
MORE :
{
	< <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); }
|	< (~["\"", "\\"])+ >
}

<INQUOTEDSTRING>
TOKEN :
{
	< QUOTEDSTRING: "\"" > { matchedToken.image = image.substring(0, image.length() - 1); } : DEFAULT
}

// GLOBALS

<*>
TOKEN :
{
	< #QUOTEDPAIR: "\\" <ANY> >
|	< #ANY: ~[] >
}

// ERROR!
/*

<*>
TOKEN :
{
	< UNEXPECTED_CHAR: <ANY> >
}

*/