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