source: contrib/MailArchiver/sources/vendor/mime4j/apache-mime4j-0.7-SNAPSHOT-20110327.010440-17/dom/src/main/jjtree/org/apache/james/mime4j/field/address/AddressListParser.jjt @ 6785

Revision 6785, 7.6 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2946 - Liberado codigo do MailArchiver?. Documentação na subpasta DOCS.

Line 
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
28options {
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
40PARSER_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 ****************************************************************/
59package org.apache.james.mime4j.field.address;
60
61public 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
111PARSER_END(AddressListParser)
112
113void parseLine() #void :
114{}
115{
116        address_list() ["\r"] "\n"
117}
118
119void parseAddressList0() #void :
120{}
121{
122        address_list() <EOF>
123}
124
125void parseAddress0() #void :
126{}
127{
128        address() <EOF>
129}
130
131void parseMailbox0() #void :
132{}
133{
134        mailbox() <EOF>
135}
136
137void address_list() :
138{}
139{
140        [ address() ]
141        (
142                ","
143                [ address() ]
144        )*
145}
146
147void address() :
148{}
149{
150        LOOKAHEAD(2147483647)
151        addr_spec()
152|       angle_addr()
153|       ( phrase() (group_body() | angle_addr()) )
154}
155
156void mailbox() :
157{}
158{
159        LOOKAHEAD(2147483647)
160        addr_spec()
161|       angle_addr()
162|       name_addr()
163}
164
165void name_addr() :
166{}
167{
168        phrase() angle_addr()
169}
170
171void group_body() :
172{}
173{
174        ":"
175        [ mailbox() ]
176        (
177                ","
178                [ mailbox() ]
179        )*
180        ";"
181}
182
183void angle_addr() :
184{}
185{
186        "<" [ route() ] addr_spec() ">"
187}
188
189void route() :
190{}
191{
192        "@" domain() ( (",")* "@" domain() )* ":"
193}
194
195void phrase() :
196{}
197{
198(       <DOTATOM>
199|       <QUOTEDSTRING>
200)+
201}
202
203void addr_spec() :
204{}
205{
206        ( local_part() "@" domain() )
207}
208
209void local_part() :
210{ Token t; }
211{
212        ( t=<DOTATOM> | t=<QUOTEDSTRING> )
213        (       [t="."]
214                {
215                        if ( t.kind == AddressListParserConstants.QUOTEDSTRING || t.image.charAt(t.image.length() - 1) != '.')
216                                throw new ParseException("Words in local part must be separated by '.'");
217                }
218                (       t=<DOTATOM> | t=<QUOTEDSTRING> )
219        )*
220}
221
222void domain() :
223{ Token t; }
224{
225        (       t=<DOTATOM>
226                (       [t="."]
227                        {
228                                if (t.image.charAt(t.image.length() - 1) != '.')
229                                        throw new ParseException("Atoms in domain names must be separated by '.'");
230                        }
231                        t=<DOTATOM>
232                )*
233        )
234|       <DOMAINLITERAL>
235}
236
237SPECIAL_TOKEN :
238{
239        < WS: ( [" ", "\t"] )+ >
240}
241
242TOKEN :
243{
244        < #ALPHA: ["a" - "z", "A" - "Z"] >
245|       < #DIGIT: ["0" - "9"] >
246|       < #ATEXT: ( <ALPHA> | <DIGIT>
247                          | "!" | "#" | "$" | "%"
248                          | "&" | "'" | "*" | "+"
249                          | "-" | "/" | "=" | "?"
250                          | "^" | "_" | "`" | "{"
251                          | "|" | "}" | "~"
252                          )>
253|       < DOTATOM: <ATEXT> ( <ATEXT> | "." )* >
254}
255
256TOKEN_MGR_DECLS :
257{
258        // Keeps track of how many levels of comment nesting
259        // we've encountered.  This is only used when the 2nd
260        // level is reached, for example ((this)), not (this).
261        // This is because the outermost level must be treated
262        // specially anyway, because the outermost ")" has a
263        // different token type than inner ")" instances.
264        static int commentNest;
265}
266
267MORE :
268{
269        // domain literal
270        "[" : INDOMAINLITERAL
271}
272
273<INDOMAINLITERAL>
274MORE :
275{
276        < <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); }
277|       < ~["[", "]", "\\"] >
278}
279
280<INDOMAINLITERAL>
281TOKEN :
282{
283        < DOMAINLITERAL: "]" > { matchedToken.image = image.toString(); }: DEFAULT
284}
285
286MORE :
287{
288        // starts a comment
289        "(" : INCOMMENT
290}
291
292<INCOMMENT>
293SKIP :
294{
295        // ends a comment
296        < COMMENT: ")" > : DEFAULT
297        // if this is ever changed to not be a SKIP, need
298        // to make sure matchedToken.token = token.toString()
299        // is called.
300}
301
302<INCOMMENT>
303MORE :
304{
305        < <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); }
306|       "(" { commentNest = 1; } : NESTED_COMMENT
307|       < <ANY>>
308}
309
310<NESTED_COMMENT>
311MORE :
312{
313        < <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); }
314|       "(" { ++commentNest; }
315|       ")" { --commentNest; if (commentNest == 0) SwitchTo(INCOMMENT); }
316|       < <ANY>>
317}
318
319
320// QUOTED STRINGS
321
322MORE :
323{
324        "\"" { image.deleteCharAt(image.length() - 1); } : INQUOTEDSTRING
325}
326
327<INQUOTEDSTRING>
328MORE :
329{
330        < <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); }
331|       < (~["\"", "\\"])+ >
332}
333
334<INQUOTEDSTRING>
335TOKEN :
336{
337        < QUOTEDSTRING: "\"" > { matchedToken.image = image.substring(0, image.length() - 1); } : DEFAULT
338}
339
340// GLOBALS
341
342<*>
343TOKEN :
344{
345        < #QUOTEDPAIR: "\\" <ANY> >
346|       < #ANY: ~[] >
347}
348
349// ERROR!
350/*
351
352<*>
353TOKEN :
354{
355        < UNEXPECTED_CHAR: <ANY> >
356}
357
358*/
Note: See TracBrowser for help on using the repository browser.