Update jflex to 1.8.2
[scilab.git] / scilab / modules / scinotes / src / java / jflex / function.jflex
1 //CHECKSTYLE:OFF
2
3 package org.scilab.modules.scinotes;
4
5 import java.io.IOException;
6 import java.util.List;
7 import java.util.ArrayList;
8 import javax.swing.text.BadLocationException;
9 import javax.swing.text.Element;
10
11 @javax.annotation.Generated("JFlex")
12 %%
13
14 %public
15 %class FunctionScanner
16 %final
17 %unicode
18 %char
19 %type int
20
21 %{
22     List<String> returnValues;
23     List<String> argsValues;
24     String functionName;
25     int end;
26     int start;
27
28     private ScilabDocument doc;
29     private String id;
30     private MatchingBlockScanner matchBlock;
31
32     public FunctionScanner(ScilabDocument doc) {
33         this.doc = doc;
34         returnValues = new ArrayList();
35         argsValues = new ArrayList();
36         this.matchBlock = new MatchingBlockScanner(doc);
37     }
38
39     public int yychar() {
40         return (int) yychar;
41     }
42
43     public String getFunctionName() {
44         return functionName;
45     }
46
47     public List<String> getReturnValues() {
48         return returnValues;
49     }
50
51     public List<String> getArgsValues() {
52         return argsValues;
53     }
54
55     public int getLineType(int p0, int p1) {
56         try {
57             returnValues = new ArrayList();
58             argsValues = new ArrayList();
59             start = p0;
60             end = p1;
61             yyreset(new ScilabDocumentReader(doc, p0, p1));
62             yybegin(BROKEN);
63             int broken = yylex();
64
65             yyreset(new ScilabDocumentReader(doc, p0, p1));
66             while (true) {
67                 int ret = yylex();
68                 if (ret != ScilabDocument.ScilabLeafElement.BROKEN) {
69                    return ret | broken;
70                 }
71                 Element elem = doc.getDefaultRootElement();
72                 elem = elem.getElement(elem.getElementIndex(end + 1));
73                 end = elem.getEndOffset();
74                 yyreset(new ScilabDocumentReader(doc, elem.getStartOffset(), end));
75                 yybegin(ARGS);
76             }
77         } catch (IOException e) {
78             return ScilabDocument.ScilabLeafElement.NOTHING;
79         }
80     }
81
82     public FunctionInfo getFunctionInfo() {
83         return new FunctionInfo(functionName, returnValues, argsValues);
84     }
85
86     public class FunctionInfo {
87         public String functionName;
88         public List<String> returnValues;
89         public List<String> argsValues;
90
91         public FunctionInfo(String name, List ret, List args) {
92             this.functionName = name;
93             this.returnValues = ret;
94             this.argsValues = args;
95         }
96     }
97 %}
98
99 %eofval{
100   return ScilabDocument.ScilabLeafElement.NOTHING;
101 %eofval}
102
103 /* main character classes */
104 white = [ \t]+
105 eol = \n
106
107 comments = {white}*("//".*)?{eol}
108 break = ".."(".")*{comments}
109
110 brokenline = ([^\.]* | ([^\.]*"."[^\.]+)+){break}
111
112 id = [a-zA-Z%_#!$?][a-zA-Z0-9_#!$?]*
113 spec = [^a-zA-Z0-9_#!$?]?
114
115 equal = {white}* "=" {white}*
116
117 rpar = ")"{comments}
118
119 fun = {white}* "function" {white}
120 funb = {white}* "function["
121 endfun = {white}* "endfunction" {spec}
122 end = "end"
123
124 %x FUNCTION, TYPEID, FUNNAME, RETS, ARGS, BROKEN
125
126 %%
127
128 <YYINITIAL> {
129   {funb}                         {
130                                    yybegin(RETS);
131                                  }
132
133   {fun}                          {
134                                    yybegin(FUNCTION);
135                                  }
136
137   {endfun}                       {
138                                    return ScilabDocument.ScilabLeafElement.ENDFUN;
139                                  }
140
141   {end}                          {
142                                    MatchingBlockScanner.MatchingPositions pos = matchBlock.getMatchingBlock(start + yychar() + yylength(), false);
143                                    if (pos != null) {
144                                       try {
145                                          String match = doc.getText(pos.secondB, pos.secondE - pos.secondB);
146                                          if (match.equals("function")) {
147                                             return ScilabDocument.ScilabLeafElement.ENDFUN;
148                                          }
149                                       } catch (BadLocationException e) { }
150                                    }
151                                  }
152
153   .                              |
154   {eol}                          {
155                                    return ScilabDocument.ScilabLeafElement.NOTHING;
156                                  }
157 }
158
159 <BROKEN> {
160   {brokenline}                   {
161                                    return ScilabDocument.ScilabLeafElement.BROKEN;
162                                  }
163
164   .                              |
165   {eol}                          {
166                                    return 0;
167                                  }
168 }
169
170 <FUNCTION> {
171   "["                            {
172                                    yybegin(RETS);
173                                  }
174
175   {id}                           {
176                                    id = yytext();
177                                    yybegin(TYPEID);
178                                  }
179
180   .                              |
181   {eol}                          {
182                                    return ScilabDocument.ScilabLeafElement.NOTHING;
183                                  }
184 }
185
186 <TYPEID> {
187   {equal}                        {
188                                    returnValues.add(id);
189                                    yybegin(FUNNAME);
190                                  }
191
192   "("                            {
193                                    functionName = id;
194                                    yybegin(ARGS);
195                                  }
196
197   {white}                        { }
198
199   {comments}                     {
200                                    functionName = id;
201                                    return ScilabDocument.ScilabLeafElement.FUN;
202                                  }
203
204   .                              {
205                                    return ScilabDocument.ScilabLeafElement.NOTHING;
206                                  }
207 }
208
209 <FUNNAME> {
210   {id}                           {
211                                    functionName = yytext();
212                                  }
213
214   [ \t]*"("                      {
215                                    yybegin(ARGS);
216                                  }
217
218   {comments}                     {
219                                    return ScilabDocument.ScilabLeafElement.FUN;
220                                  }
221
222   .                              {
223                                    return ScilabDocument.ScilabLeafElement.NOTHING;
224                                  }
225 }
226
227 <RETS> {
228   {id}                           {
229                                    returnValues.add(yytext());
230                                  }
231
232   ","                            |
233   {white}                        { }
234
235   "]"{equal}                     {
236                                    yybegin(FUNNAME);
237                                  }
238
239   .                              |
240   {eol}                          {
241                                    return ScilabDocument.ScilabLeafElement.NOTHING;
242                                  }
243 }
244
245 <ARGS> {
246   {id}                           {
247                                    argsValues.add(yytext());
248                                  }
249
250   ","                            |
251   {white}                        { }
252
253   {break}                        {
254                                    return ScilabDocument.ScilabLeafElement.BROKEN;
255                                  }
256
257   {rpar}                         {
258                                    return ScilabDocument.ScilabLeafElement.FUN;
259                                  }
260
261   .                              |
262   {eol}                          {
263                                    return ScilabDocument.ScilabLeafElement.NOTHING;
264                                  }
265 }