Update jflex to 1.8.2
[scilab.git] / scilab / modules / scinotes / src / java / jflex / matchingblock.jflex
1 //CHECKSTYLE:OFF
2
3 package org.scilab.modules.scinotes;
4
5 import java.io.IOException;
6 import javax.swing.text.Element;
7
8 @javax.annotation.Generated("JFlex")
9 %%
10
11 %public
12 %class MatchingBlockScanner
13 %final
14 %unicode
15 %char
16 %type int
17 %buffer 256
18
19 %{
20     private ScilabDocument doc;
21     private Element elem;
22     private boolean transp = false;
23     private ScilabLexer scilabLexer;
24     private int start;
25     private int end;
26     private int savePos;
27
28     public MatchingBlockScanner(ScilabDocument doc) {
29         this.doc = doc;
30         this.elem = doc.getDefaultRootElement();
31         this.scilabLexer = new ScilabLexer(doc, null, false);
32     }
33
34     public int yychar() {
35         return (int) yychar;
36     }
37
38     public MatchingPositions getNextBlock(int pos, boolean lr) {
39         int s = 1;
40         transp = false;
41         try {
42             if (lr) {
43                 start =  pos;
44                 end = doc.getEndPosition().getOffset();
45                 yyreset(new ScilabDocumentReader(doc, start, end));
46                 yybegin(LR);
47             } else {
48                 start = pos - 1;
49                 end = 0;
50                 yyreset(new ScilabDocumentReader(doc, true, start, end));
51                 yybegin(RL);
52             }
53             do {
54                if (yylex() == 0) {
55                   s--;
56                } else {
57                   s++;
58                }
59             } while (zzMarkedPos != 0 && s != 0);
60         } catch (IOException e) {
61             return null;
62         }
63         if (s == 0) {
64             if (lr) {
65                 return new MatchingPositions(pos, pos, pos + yychar(), pos + yychar() + (transp?(yylength()-1):yylength()));
66             } else {
67                 return new MatchingPositions(pos, pos, pos - yychar() - yylength(), pos - yychar());
68             }
69         }
70
71         return null;
72     }
73
74     public MatchingPositions getMatchingBlock(int pos, boolean lr) {
75         int p1, s = 1;
76         transp = false;
77         try {
78             if (lr) {
79                 start =  pos;
80                 end = doc.getEndPosition().getOffset();
81                 yyreset(new ScilabDocumentReader(doc, start, end));
82                 yybegin(OPENCLOSE);
83                 if (yylex() != 1) {
84                    return null;
85                 }
86
87                 p1 = pos + yylength();
88                 yybegin(LR);
89             } else {
90                  start = pos - 1;
91                  end = 0;
92                  yyreset(new ScilabDocumentReader(doc, true, start, end));
93                  yybegin(CLOSEOPEN);
94                  if (yylex() != 1) {
95                     return null;
96                  }
97                  p1 = pos - yylength();
98                  yybegin(RL);
99             }
100
101             do {
102                if (yylex() == 0) {
103                   s--;
104                } else {
105                   s++;
106                }
107             } while (zzMarkedPos != 0 && s != 0);
108         } catch (IOException e) {
109             return null;
110         }
111         if (s == 0) {
112             if (lr) {
113                 return new MatchingPositions(pos, p1, pos + yychar(), pos + yychar() + (transp?(yylength()-1):yylength()));
114             } else {
115                 return new MatchingPositions(p1, pos, pos - yychar() - yylength(), pos - yychar());
116             }
117         }
118
119         return null;
120     }
121
122     public final class MatchingPositions {
123         public int firstB;
124         public int firstE;
125         public int secondB;
126         public int secondE;
127
128         private MatchingPositions(int x1, int x2, int y1, int y2) {
129             firstB = x1;
130             firstE = x2;
131             secondB = y1;
132             secondE = y2;
133         }
134     }
135 %}
136
137 %eofval{
138   return -1;
139 %eofval}
140
141 /* main character classes */
142 eol = \n
143
144 comment = "//".*{eol}
145
146 tnemmoc = {eol}([^\r\n]*"//")+
147
148 spec = [a-zA-Z0-9_#!$?]
149
150 id = [a-zA-Z%_#!?][a-zA-Z0-9_#!$?]*
151
152 string = (([^\'\"\r\n]*)|([\'\"]{2}))*
153 qstring = (\"|\'){string}(\"|\')
154 gnirtsq = \"{string}(\"|\')
155 transp = ({spec} | ")" | "]" | "}") "'"
156
157 openK = ("if" | "for" | "while" | "select" | "try" | "function" | "switch")
158 openKx = {openK}{spec}+
159
160 elseif = "elseif" | "else"
161
162 openS = "(" | "[" | "{"
163 closeK = ("end" | "endfunction")
164 closeKx = {closeK}{spec}+
165 xcloseK = ({spec} | "%" ){closeK}
166 closeS =  ")" | "]" | "}"
167
168 esolcK = ("fi" | "rof" | "elihw" | "tceles" | "yrt" | "noitcnuf" | "hctiws")
169 esolcKx = {spec}{esolcK}
170 xesolcK = {esolcK}{spec}
171 nepoK = ("dne" | "noitcnufdne")
172 nepoKx = {spec}{nepoK}
173
174 %x LR, RL, OPENCLOSE, CLOSEOPEN, PSNART, SPEC
175
176 %%
177
178 <LR> {
179   {transp}                       {
180                                    char c = yycharat(yylength() - 2);
181                                    if (c == ')' || c == ']' || c == '}') {
182                                       transp = true;
183                                       return 0;
184                                    }
185                                  }
186
187   "elseif"                       |
188   {comment}                      |
189   {openKx}                       |
190   {qstring}                      { }
191
192   {openS}                        |
193   {openK}                        {
194                                    return 1;
195                                  }
196
197   {closeS}                       |
198   {closeK}                       {
199                                    return 0;
200                                  }
201
202   {id}                           |
203   {closeKx}                      |
204   {xcloseK}                      |
205   .                              |
206   {eol}                          { }
207 }
208
209 <RL> {
210   \'                             {
211                                    yypushback(1);
212                                    yybegin(PSNART);
213                                  }
214
215   "fiesle"                       |
216   {tnemmoc}                      |
217   {esolcKx}                      |
218   {xesolcK}                      |
219   {gnirtsq}                      { }
220
221   {closeS}                       |
222   {nepoK}                        {
223                                    return 1;
224                                  }
225
226   {openS}                        |
227   {esolcK}                       {
228                                    return 0;
229                                  }
230
231   {nepoKx}                       |
232   .                              |
233   {eol}                          { }
234 }
235
236 <PSNART> {
237   \'                             {
238                                    if (scilabLexer.getKeyword(start - yychar(), false) == ScilabLexerConstants.STRING) {
239                                       savePos = start - yychar() - scilabLexer.beginString - scilabLexer.start;
240                                       yybegin(SPEC);
241                                    } else {
242                                       yybegin(RL);
243                                    }
244                                  }
245 }
246
247 <SPEC> {
248   .                              |
249   {eol}                          {
250                                    if (--savePos == 0) {
251                                       yybegin(RL);
252                                    }
253                                  }
254 }
255
256 <OPENCLOSE> {
257   {openKx}                       {
258                                    return 0;
259                                  }
260
261   {elseif}                       |
262   {openS}                        |
263   {openK}                        {
264                                    return 1;
265                                  }
266
267   .                              |
268   {eol}                          {
269                                    return 0;
270                                  }
271 }
272
273 <CLOSEOPEN> {
274   {nepoKx}                       {
275                                    return 0;
276                                  }
277
278   {closeS}                       |
279   {nepoK}                        {
280                                    return 1;
281                                  }
282
283   .                              |
284   {eol}                          {
285                                    return 0;
286                                  }
287 }