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 date parser. |
---|
23 | * |
---|
24 | * Created 9/28/2004 |
---|
25 | * by Joe Cheng <code@joecheng.com> |
---|
26 | */ |
---|
27 | |
---|
28 | options { |
---|
29 | STATIC=false; |
---|
30 | LOOKAHEAD=1; |
---|
31 | JDK_VERSION = "1.5"; |
---|
32 | OUTPUT_DIRECTORY = "../../../../../../../../../target/generated-sources/javacc"; |
---|
33 | //DEBUG_PARSER=true; |
---|
34 | //DEBUG_TOKEN_MANAGER=true; |
---|
35 | } |
---|
36 | |
---|
37 | PARSER_BEGIN(DateTimeParser) |
---|
38 | /**************************************************************** |
---|
39 | * Licensed to the Apache Software Foundation (ASF) under one * |
---|
40 | * or more contributor license agreements. See the NOTICE file * |
---|
41 | * distributed with this work for additional information * |
---|
42 | * regarding copyright ownership. The ASF licenses this file * |
---|
43 | * to you under the Apache License, Version 2.0 (the * |
---|
44 | * "License"); you may not use this file except in compliance * |
---|
45 | * with the License. You may obtain a copy of the License at * |
---|
46 | * * |
---|
47 | * http://www.apache.org/licenses/LICENSE-2.0 * |
---|
48 | * * |
---|
49 | * Unless required by applicable law or agreed to in writing, * |
---|
50 | * software distributed under the License is distributed on an * |
---|
51 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * |
---|
52 | * KIND, either express or implied. See the License for the * |
---|
53 | * specific language governing permissions and limitations * |
---|
54 | * under the License. * |
---|
55 | ****************************************************************/ |
---|
56 | package org.apache.james.mime4j.field.datetime.parser; |
---|
57 | |
---|
58 | import org.apache.james.mime4j.dom.datetime.DateTime; |
---|
59 | |
---|
60 | public class DateTimeParser { |
---|
61 | private static final boolean ignoreMilitaryZoneOffset = true; |
---|
62 | |
---|
63 | public static void main(String args[]) throws ParseException { |
---|
64 | while (true) { |
---|
65 | try { |
---|
66 | DateTimeParser parser = new DateTimeParser(System.in); |
---|
67 | parser.parseLine(); |
---|
68 | } catch (Exception x) { |
---|
69 | x.printStackTrace(); |
---|
70 | return; |
---|
71 | } |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | private static int parseDigits(Token token) { |
---|
76 | return Integer.parseInt(token.image, 10); |
---|
77 | } |
---|
78 | |
---|
79 | private static int getMilitaryZoneOffset(char c) { |
---|
80 | if (ignoreMilitaryZoneOffset) |
---|
81 | return 0; |
---|
82 | |
---|
83 | c = Character.toUpperCase(c); |
---|
84 | |
---|
85 | switch (c) { |
---|
86 | case 'A': return 1; |
---|
87 | case 'B': return 2; |
---|
88 | case 'C': return 3; |
---|
89 | case 'D': return 4; |
---|
90 | case 'E': return 5; |
---|
91 | case 'F': return 6; |
---|
92 | case 'G': return 7; |
---|
93 | case 'H': return 8; |
---|
94 | case 'I': return 9; |
---|
95 | case 'K': return 10; |
---|
96 | case 'L': return 11; |
---|
97 | case 'M': return 12; |
---|
98 | |
---|
99 | case 'N': return -1; |
---|
100 | case 'O': return -2; |
---|
101 | case 'P': return -3; |
---|
102 | case 'Q': return -4; |
---|
103 | case 'R': return -5; |
---|
104 | case 'S': return -6; |
---|
105 | case 'T': return -7; |
---|
106 | case 'U': return -8; |
---|
107 | case 'V': return -9; |
---|
108 | case 'W': return -10; |
---|
109 | case 'X': return -11; |
---|
110 | case 'Y': return -12; |
---|
111 | |
---|
112 | case 'Z': return 0; |
---|
113 | default: return 0; |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | private static class Time { |
---|
118 | private int hour; |
---|
119 | private int minute; |
---|
120 | private int second; |
---|
121 | private int zone; |
---|
122 | |
---|
123 | public Time(int hour, int minute, int second, int zone) { |
---|
124 | this.hour = hour; |
---|
125 | this.minute = minute; |
---|
126 | this.second = second; |
---|
127 | this.zone = zone; |
---|
128 | } |
---|
129 | |
---|
130 | public int getHour() { return hour; } |
---|
131 | public int getMinute() { return minute; } |
---|
132 | public int getSecond() { return second; } |
---|
133 | public int getZone() { return zone; } |
---|
134 | } |
---|
135 | |
---|
136 | private static class Date { |
---|
137 | private String year; |
---|
138 | private int month; |
---|
139 | private int day; |
---|
140 | |
---|
141 | public Date(String year, int month, int day) { |
---|
142 | this.year = year; |
---|
143 | this.month = month; |
---|
144 | this.day = day; |
---|
145 | } |
---|
146 | |
---|
147 | public String getYear() { return year; } |
---|
148 | public int getMonth() { return month; } |
---|
149 | public int getDay() { return day; } |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | PARSER_END(DateTimeParser) |
---|
154 | |
---|
155 | DateTime parseLine() : |
---|
156 | {DateTime dt;} |
---|
157 | { |
---|
158 | dt=date_time() ["\r"] "\n" |
---|
159 | { return dt; } |
---|
160 | } |
---|
161 | |
---|
162 | DateTime parseAll() : |
---|
163 | {DateTime dt;} |
---|
164 | { |
---|
165 | dt=date_time() <EOF> |
---|
166 | { return dt; } |
---|
167 | } |
---|
168 | |
---|
169 | DateTime date_time() : |
---|
170 | {Date d; Time t;} |
---|
171 | { |
---|
172 | [ day_of_week() "," ] |
---|
173 | d=date() |
---|
174 | t=time() |
---|
175 | { |
---|
176 | return new DateTime( |
---|
177 | d.getYear(), |
---|
178 | d.getMonth(), |
---|
179 | d.getDay(), |
---|
180 | t.getHour(), |
---|
181 | t.getMinute(), |
---|
182 | t.getSecond(), |
---|
183 | t.getZone()); // time zone offset |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | String day_of_week() : |
---|
188 | {} |
---|
189 | { |
---|
190 | ( "Mon" | "Tue" | "Wed" | "Thu" | "Fri" | "Sat" | "Sun" |
---|
191 | ) |
---|
192 | { return token.image; } |
---|
193 | } |
---|
194 | |
---|
195 | Date date() : |
---|
196 | {int d, m; String y;} |
---|
197 | { |
---|
198 | d=day() m=month() y=year() |
---|
199 | { return new Date(y, m, d); } |
---|
200 | } |
---|
201 | |
---|
202 | int day() : |
---|
203 | {Token t;} |
---|
204 | { |
---|
205 | t=<DIGITS> { return parseDigits(t); } |
---|
206 | } |
---|
207 | |
---|
208 | int month() : |
---|
209 | {} |
---|
210 | { |
---|
211 | "Jan" { return 1; } |
---|
212 | | "Feb" { return 2; } |
---|
213 | | "Mar" { return 3; } |
---|
214 | | "Apr" { return 4; } |
---|
215 | | "May" { return 5; } |
---|
216 | | "Jun" { return 6; } |
---|
217 | | "Jul" { return 7; } |
---|
218 | | "Aug" { return 8; } |
---|
219 | | "Sep" { return 9; } |
---|
220 | | "Oct" { return 10; } |
---|
221 | | "Nov" { return 11; } |
---|
222 | | "Dec" { return 12; } |
---|
223 | } |
---|
224 | |
---|
225 | String year() : |
---|
226 | {Token t;} |
---|
227 | { |
---|
228 | t=<DIGITS> { return t.image; } |
---|
229 | } |
---|
230 | |
---|
231 | Time time() : |
---|
232 | {int h, m, s=0, z;} |
---|
233 | { |
---|
234 | h=hour() ":" m=minute() [ ":" s=second() ] z=zone() |
---|
235 | { return new Time(h, m, s, z); } |
---|
236 | } |
---|
237 | |
---|
238 | int hour() : |
---|
239 | {Token t;} |
---|
240 | { |
---|
241 | t=<DIGITS> { return parseDigits(t); } |
---|
242 | } |
---|
243 | |
---|
244 | int minute() : |
---|
245 | {Token t;} |
---|
246 | { |
---|
247 | t=<DIGITS> { return parseDigits(t); } |
---|
248 | } |
---|
249 | |
---|
250 | int second() : |
---|
251 | {Token t;} |
---|
252 | { |
---|
253 | t=<DIGITS> { return parseDigits(t); } |
---|
254 | } |
---|
255 | |
---|
256 | int zone() : |
---|
257 | { Token t, u; int z; } |
---|
258 | { |
---|
259 | ( t=< OFFSETDIR: ["+", "-"] > u=<DIGITS> { z=parseDigits(u)*(t.image.equals("-") ? -1 : 1); } |
---|
260 | | z=obs_zone() |
---|
261 | ) |
---|
262 | { return z; } |
---|
263 | } |
---|
264 | |
---|
265 | int obs_zone() : |
---|
266 | {Token t; int z;} |
---|
267 | { |
---|
268 | ( "UT" { z=0; } |
---|
269 | | "GMT" { z=0; } |
---|
270 | | "EST" { z=-5; } |
---|
271 | | "EDT" { z=-4; } |
---|
272 | | "CST" { z=-6; } |
---|
273 | | "CDT" { z=-5; } |
---|
274 | | "MST" { z=-7; } |
---|
275 | | "MDT" { z=-6; } |
---|
276 | | "PST" { z=-8; } |
---|
277 | | "PDT" { z=-7; } |
---|
278 | | t=< MILITARY_ZONE: ["A"-"I","a"-"i","K"-"Z","k"-"z"] > { z=getMilitaryZoneOffset(t.image.charAt(0)); } |
---|
279 | ) |
---|
280 | { return z * 100; } |
---|
281 | } |
---|
282 | |
---|
283 | SPECIAL_TOKEN : |
---|
284 | { |
---|
285 | < WS: ( [" ", "\t"] )+ > |
---|
286 | } |
---|
287 | |
---|
288 | TOKEN_MGR_DECLS : |
---|
289 | { |
---|
290 | // Keeps track of how many levels of comment nesting |
---|
291 | // we've encountered. This is only used when the 2nd |
---|
292 | // level is reached, for example ((this)), not (this). |
---|
293 | // This is because the outermost level must be treated |
---|
294 | // specially anyway, because the outermost ")" has a |
---|
295 | // different token type than inner ")" instances. |
---|
296 | static int commentNest; |
---|
297 | } |
---|
298 | |
---|
299 | MORE : |
---|
300 | { |
---|
301 | // starts a comment |
---|
302 | "(" : INCOMMENT |
---|
303 | } |
---|
304 | |
---|
305 | <INCOMMENT> |
---|
306 | SKIP : |
---|
307 | { |
---|
308 | // ends a comment |
---|
309 | < COMMENT: ")" > : DEFAULT |
---|
310 | // if this is ever changed to not be a SKIP, need |
---|
311 | // to make sure matchedToken.token = token.toString() |
---|
312 | // is called. |
---|
313 | } |
---|
314 | |
---|
315 | <INCOMMENT> |
---|
316 | MORE : |
---|
317 | { |
---|
318 | < <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); } |
---|
319 | | "(" { commentNest = 1; } : NESTED_COMMENT |
---|
320 | | < <ANY>> |
---|
321 | } |
---|
322 | |
---|
323 | <NESTED_COMMENT> |
---|
324 | MORE : |
---|
325 | { |
---|
326 | < <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); } |
---|
327 | | "(" { ++commentNest; } |
---|
328 | | ")" { --commentNest; if (commentNest == 0) SwitchTo(INCOMMENT); } |
---|
329 | | < <ANY>> |
---|
330 | } |
---|
331 | |
---|
332 | TOKEN : |
---|
333 | { |
---|
334 | < DIGITS: ( ["0"-"9"] )+ > |
---|
335 | } |
---|
336 | |
---|
337 | // GLOBALS |
---|
338 | |
---|
339 | <*> |
---|
340 | TOKEN : |
---|
341 | { |
---|
342 | < #QUOTEDPAIR: "\\" <ANY> > |
---|
343 | | < #ANY: ~[] > |
---|
344 | } |
---|