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