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