Update jflex to 1.8.2
[scilab.git] / scilab / modules / helptools / src / java / jflex / xml.jflex
1 //CHECKSTYLE:OFF
2
3 package org.scilab.modules.helptools.XML;
4
5 import java.io.Reader;
6 import java.io.StringReader;
7 import java.io.IOException;
8
9 @javax.annotation.Generated("JFlex")
10 %%
11
12 %public
13 %class XMLLexer
14 %final
15 %unicode
16 %char
17 %type void
18
19 %{
20
21     private int whitesOnFirstLine;
22     private int saveLexState;
23     private AbstractXMLCodeHandler handler;
24
25     public XMLLexer() { }
26
27     private String trimEnd(String str) {
28         int end = str.length() - 1;
29         int i = end;
30         for (; i >= 0; i--) {
31            char c = str.charAt(i);
32            if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
33               break;
34            }
35         }
36         if (i != end) {
37            return str.substring(0, i + 1);
38         }
39
40         return str;
41     }
42
43     public String convert(AbstractXMLCodeHandler h, String code) {
44         Reader reader = new StringReader(trimEnd(code));
45         String str = convert(h, reader, true);
46         try {
47           reader.close();
48         } catch (IOException e) { }
49
50         return str;
51     }
52
53     public String convert(AbstractXMLCodeHandler h, Reader code, boolean ret) {
54         if (code == null) {
55            return null;
56         } else {
57            handler = h;
58            whitesOnFirstLine = 0;
59            yyreset(code);
60            yybegin(CLEANFIRST);
61            try {
62                yylex();
63            } catch (IOException e) {
64                return null;
65            }
66            if (ret) {
67                return h.toString();
68            }
69            return "";
70         }
71     }
72 %}
73
74 %x COMMENT, CDATA, TAG, PI, CLEAN, CLEANFIRST
75
76 /* main character classes */
77 /* http://www.w3.org/TR/REC-xml/ */
78
79 white = [ \t]+
80
81 entity = "&"[#a-zA-Z0-9]*";"
82
83 nameStartChar = ":" | [A-Z] | "_" | [a-z] | [\u00C0-\u00D6] | [\u00D8-\u00F6] | [\u00F8-\u02FF] | [\u0370-\u037D] | [\u037F-\u1FFF] | [\u200C-\u200D] | [\u2070-\u218F] | [\u2C00-\u2FEF] | [\u3001-\uD7FF] | [\uF900-\uFDCF] | [\uFDF0-\uFFFD]
84 nameChar = {nameStartChar} | "-" | "." | [0-9] | \u00B7 | [\u0300-\u036F] | [\u0203F-\u2040]
85 name = {nameStartChar} {nameChar}*
86
87 commentBeg = "<!--"
88 commentEnd = "-->"
89 comment = ([^\r\n-]* | [^\r\n-]* "-" [^\r\n-] | [^\r\n-]* "--" [^\r\n>])+
90
91 cdataBeg = "<![CDATA["
92 cdataEnd = "]]>"
93 cdata = ([^\r\n\]]* | [^\r\n\]]* "]" [^\r\n\]] | [^\r\n\]]* "]]" [^\r\n>])+
94
95 piBeg = "<?" {name}
96 piEnd = "?>"
97
98 openTagBeg = "<" {name}
99 autocloseTag = "/>"
100 openTagEnd = ">"
101 closeTag = "</" {name} {white}* ">"
102
103 attribute = {name} "="
104 value = ("\"" [^\r\n\"]* "\"") | ("\'" [^\r\n\']* "\'")
105
106 eol = [\r\n]+
107
108 %%
109 <CLEANFIRST> {
110   {white}*{eol}+                 { }
111
112   {white}                        {
113                                      whitesOnFirstLine = yylength();
114                                      yybegin(YYINITIAL);
115                                  }
116
117   .                              {
118                                      yypushback(1);
119                                      yybegin(YYINITIAL);
120                                  }
121 }
122
123 <CLEAN> {
124   {eol}+                         {
125                                      handler.handleNothing("\n");
126                                  }
127
128   {white}                        {
129                                      int len = yylength() - whitesOnFirstLine;
130                                      if (len > 0) {
131                                         yypushback(len);
132                                      }
133                                      yybegin(saveLexState);
134                                  }
135
136   .                              {
137                                      yypushback(1);
138                                      yybegin(saveLexState);
139                                  }
140 }
141
142 <YYINITIAL> {
143   {entity}                       {
144                                      handler.handleEntity("&amp;" + yytext().substring(1));
145                                  }
146
147   {piBeg}                        {
148                                      yybegin(PI);
149                                      handler.handleOpenInstr("&#0060;?");
150                                      handler.handleInstrName(yytext().substring(2));
151                                  }
152
153   {openTagBeg}                   {
154                                      yybegin(TAG);
155                                      handler.handleLow("&#0060;");
156                                      handler.handleOpenTagName(yytext().substring(1));
157                                  }
158
159   {closeTag}                     {
160                                      handler.handleLowClose("&#0060;/");
161                                      String str = yytext();
162                                      handler.handleOpenTagName(str.substring(2, str.length() - 1).trim());
163                                      handler.handleGreat("&#0062;");
164                                  }
165
166   {commentBeg}                   {
167                                      yybegin(COMMENT);
168                                      handler.handleOpenComment("&#0060;!--");
169                                  }
170
171   {cdataBeg}                     {
172                                      yybegin(CDATA);
173                                      handler.handleOpenCdata("&#0060;![CDATA[");
174                                  }
175
176   [^<\r\n]*                      {
177                                      handler.handleDefault(yytext());
178                                  }
179
180   {eol}                          {
181                                      handler.handleNothing("\n");
182                                      saveLexState = YYINITIAL;
183                                      yybegin(CLEAN);
184                                  }
185 }
186
187 <PI> {
188   {attribute}                    {
189                                      String str = yytext();
190                                      handler.handleAttributeName(str.substring(0, str.length() - 1));
191                                      handler.handleEqual("=");
192                                  }
193
194   {value}                        {
195                                      handler.handleAttributeValue(yytext());
196                                  }
197
198   {piEnd}                        {
199                                      yybegin(YYINITIAL);
200                                      handler.handleCloseInstr("?&#0062;");
201                                  }
202
203   {white}                        {
204                                      handler.handleNothing(" ");
205                                  }
206
207   {eol}                          {
208                                      handler.handleNothing("\n");
209                                      saveLexState = PI;
210                                      yybegin(CLEAN);
211                                  }
212
213   .                              { }
214 }
215
216 <TAG> {
217   {attribute}                    {
218                                      String str = yytext();
219                                      handler.handleAttributeName(str.substring(0, str.length() - 1));
220                                      handler.handleEqual("=");
221                                  }
222
223   {value}                        {
224                                      handler.handleAttributeValue(yytext());
225                                  }
226
227   {autocloseTag}                 {
228                                      yybegin(YYINITIAL);
229                                      handler.handleAutoClose("/&#0062;");
230                                  }
231
232   {openTagEnd}                   {
233                                      yybegin(YYINITIAL);
234                                      handler.handleGreat("&#0062;");
235                                  }
236
237   {white}                        {
238                                      handler.handleNothing(" ");
239                                  }
240
241   {eol}                          {
242                                      handler.handleNothing("\n");
243                                      saveLexState = TAG;
244                                      yybegin(CLEAN);
245                                  }
246
247   .                              { }
248 }
249
250 <COMMENT> {
251   {commentEnd}                   {
252                                      yybegin(YYINITIAL);
253                                      handler.handleCommentEnd("--&#0062;");
254                                  }
255
256   {eol}                          {
257                                      handler.handleNothing("\n");
258                                      saveLexState = COMMENT;
259                                      yybegin(CLEAN);
260                                  }
261
262   {comment}                      {
263                                      handler.handleComment(yytext());
264                                  }
265 }
266
267 <CDATA> {
268   {cdataEnd}                     {
269                                      yybegin(YYINITIAL);
270                                      handler.handleCdataEnd("]]&#0062;");
271                                  }
272
273   {eol}                          {
274                                      handler.handleNothing("\n");
275                                      saveLexState = CDATA;
276                                      yybegin(CLEAN);
277                                  }
278
279   {cdata}                        {
280                                      handler.handleCdata(yytext());
281                                  }
282 }
283
284 <<EOF>>                          {
285                                      return;
286                                  }