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