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 | options { |
---|
21 | static=false; |
---|
22 | JDK_VERSION = "1.5"; |
---|
23 | OUTPUT_DIRECTORY = "../../../../../../../../../target/generated-sources/javacc"; |
---|
24 | } |
---|
25 | |
---|
26 | PARSER_BEGIN(ContentLanguageParser) |
---|
27 | /**************************************************************** |
---|
28 | * Licensed to the Apache Software Foundation (ASF) under one * |
---|
29 | * or more contributor license agreements. See the NOTICE file * |
---|
30 | * distributed with this work for additional information * |
---|
31 | * regarding copyright ownership. The ASF licenses this file * |
---|
32 | * to you under the Apache License, Version 2.0 (the * |
---|
33 | * "License"); you may not use this file except in compliance * |
---|
34 | * with the License. You may obtain a copy of the License at * |
---|
35 | * * |
---|
36 | * http://www.apache.org/licenses/LICENSE-2.0 * |
---|
37 | * * |
---|
38 | * Unless required by applicable law or agreed to in writing, * |
---|
39 | * software distributed under the License is distributed on an * |
---|
40 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * |
---|
41 | * KIND, either express or implied. See the License for the * |
---|
42 | * specific language governing permissions and limitations * |
---|
43 | * under the License. * |
---|
44 | ****************************************************************/ |
---|
45 | package org.apache.james.mime4j.field.language.parser; |
---|
46 | |
---|
47 | import java.util.ArrayList; |
---|
48 | import java.util.List; |
---|
49 | |
---|
50 | public class ContentLanguageParser { |
---|
51 | private List<String> languages = new ArrayList<String>(); |
---|
52 | |
---|
53 | /** |
---|
54 | * Parses the input into a list of language tags. |
---|
55 | * @return list of language tag Strings |
---|
56 | */ |
---|
57 | public List<String> parse() throws ParseException { |
---|
58 | try { |
---|
59 | return doParse(); |
---|
60 | } catch (TokenMgrError e) { |
---|
61 | // An issue with the TOKENiser |
---|
62 | // but it's not polite to throw an Error |
---|
63 | // when executing on a server |
---|
64 | throw new ParseException(e); |
---|
65 | } |
---|
66 | } |
---|
67 | } |
---|
68 | PARSER_END(ContentLanguageParser) |
---|
69 | |
---|
70 | private List<String> doParse() : |
---|
71 | { |
---|
72 | } |
---|
73 | { |
---|
74 | language() ( "," language() )* |
---|
75 | {return languages;} |
---|
76 | } |
---|
77 | |
---|
78 | String language() : |
---|
79 | { |
---|
80 | Token token; |
---|
81 | StringBuffer languageTag = new StringBuffer(); |
---|
82 | String result; |
---|
83 | } |
---|
84 | { |
---|
85 | token = <ALPHA> |
---|
86 | { |
---|
87 | languageTag.append(token.image); |
---|
88 | } |
---|
89 | ( |
---|
90 | "-" |
---|
91 | // This keeps TOKENising simple |
---|
92 | token = <ALPHA> |
---|
93 | { |
---|
94 | languageTag.append('-'); |
---|
95 | languageTag.append(token.image); |
---|
96 | } |
---|
97 | | |
---|
98 | token = <ALPHANUM> |
---|
99 | { |
---|
100 | languageTag.append('-'); |
---|
101 | languageTag.append(token.image); |
---|
102 | } |
---|
103 | )* |
---|
104 | |
---|
105 | { |
---|
106 | result = languageTag.toString(); |
---|
107 | languages.add(result); |
---|
108 | return result; |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | |
---|
114 | |
---|
115 | SPECIAL_TOKEN : |
---|
116 | { |
---|
117 | < WS: ( [" ", "\t", "\r", "\n"] )+ > |
---|
118 | } |
---|
119 | |
---|
120 | TOKEN_MGR_DECLS : |
---|
121 | { |
---|
122 | // Keeps track of how many levels of comment nesting |
---|
123 | // we've encountered. This is only used when the 2nd |
---|
124 | // level is reached, for example ((this)), not (this). |
---|
125 | // This is because the outermost level must be treated |
---|
126 | // specially anyway, because the outermost ")" has a |
---|
127 | // different token type than inner ")" instances. |
---|
128 | int commentNest; |
---|
129 | } |
---|
130 | |
---|
131 | |
---|
132 | MORE : |
---|
133 | { |
---|
134 | // starts a comment |
---|
135 | "(" : INCOMMENT |
---|
136 | } |
---|
137 | |
---|
138 | <INCOMMENT> |
---|
139 | SKIP : |
---|
140 | { |
---|
141 | // ends a comment |
---|
142 | < COMMENT: ")" > : DEFAULT |
---|
143 | // if this is ever changed to not be a SKIP, need |
---|
144 | // to make sure matchedToken.token = token.toString() |
---|
145 | // is called. |
---|
146 | } |
---|
147 | |
---|
148 | <INCOMMENT> |
---|
149 | MORE : |
---|
150 | { |
---|
151 | < <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); } |
---|
152 | | "(" { commentNest = 1; } : NESTED_COMMENT |
---|
153 | | < <ANY>> |
---|
154 | } |
---|
155 | |
---|
156 | <NESTED_COMMENT> |
---|
157 | MORE : |
---|
158 | { |
---|
159 | < <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); } |
---|
160 | | "(" { ++commentNest; } |
---|
161 | | ")" { --commentNest; if (commentNest == 0) SwitchTo(INCOMMENT); } |
---|
162 | | < <ANY>> |
---|
163 | } |
---|
164 | // QUOTED STRINGS |
---|
165 | |
---|
166 | MORE : |
---|
167 | { |
---|
168 | "\"" { image.deleteCharAt(image.length() - 1); } : INQUOTEDSTRING |
---|
169 | } |
---|
170 | |
---|
171 | <INQUOTEDSTRING> |
---|
172 | MORE : |
---|
173 | { |
---|
174 | < <QUOTEDPAIR>> { image.deleteCharAt(image.length() - 2); } |
---|
175 | | < (~["\"", "\\"])+ > |
---|
176 | } |
---|
177 | |
---|
178 | <INQUOTEDSTRING> |
---|
179 | TOKEN : |
---|
180 | { |
---|
181 | < QUOTEDSTRING: "\"" > { matchedToken.image = image.substring(0, image.length() - 1); } : DEFAULT |
---|
182 | } |
---|
183 | |
---|
184 | TOKEN : |
---|
185 | { |
---|
186 | < DIGITS: ( ["0"-"9"] )+ > |
---|
187 | } |
---|
188 | |
---|
189 | TOKEN : |
---|
190 | { |
---|
191 | < ALPHA: ( ["a"-"z"] | ["A"-"Z"] )+ > |
---|
192 | } |
---|
193 | |
---|
194 | TOKEN : |
---|
195 | { |
---|
196 | <ALPHANUM : ( ["0"-"9"] | ["a"-"z"] | ["A"-"Z"] )+> |
---|
197 | } |
---|
198 | |
---|
199 | TOKEN : |
---|
200 | { |
---|
201 | < DOT: "." > |
---|
202 | } |
---|
203 | |
---|
204 | <*> |
---|
205 | TOKEN : |
---|
206 | { |
---|
207 | < #QUOTEDPAIR: "\\" <ANY> > |
---|
208 | | < #ANY: ~[] > |
---|
209 | } |
---|