SciNotes: handle comments /*(.*)*/, function...end, switch, otherwise, || and &&
[scilab.git] / scilab / modules / scinotes / src / java / jflex / indentation.jflex
1 //CHECKSTYLE:OFF 
2
3 package org.scilab.modules.scinotes;
4
5 import java.io.IOException;
6 import javax.swing.text.Element;
7 import javax.swing.text.BadLocationException;
8
9 @javax.annotation.Generated("JFlex")
10 @SuppressWarnings("fallthrough")
11 %% 
12
13 %public
14 %class IndentScanner
15 %final
16 %unicode
17 %char
18 %type int
19
20 %{
21     private ScilabDocument doc;
22     private Element elem;
23
24     public IndentScanner(ScilabDocument doc) {
25         this.doc = doc;
26         this.elem = doc.getDefaultRootElement();
27     }
28    
29     public int getIndentNumber(int lineNumber, char type) {
30         if (lineNumber < 0) {
31             return 0;
32         }
33         try {
34              Element line = elem.getElement(lineNumber);
35              int startL = line.getStartOffset();
36              int endL = line.getEndOffset();    
37              yyreset(new ScilabDocumentReader(doc, startL, endL));
38              switch (type) {
39                  case ' ':
40                  yybegin(WHITE);
41                  break;
42                  case '\t':
43                  yybegin(TAB);
44                  break;
45              default:
46                  yybegin(WHITE);
47              }
48              return yylex();
49         } catch (IOException e) {
50              return 0;
51         }
52     }   
53
54     public int getTabsAtBeginning(int pos) {
55         Element line = elem.getElement(pos);
56         int startL = line.getStartOffset();
57         int endL = line.getEndOffset();
58         try {
59             yyreset(new ScilabDocumentReader(doc, startL, endL));
60             yybegin(BEG);
61             if (yylex() == 1) {
62                 return yylength();
63             }
64         } catch (IOException e) {  
65             return 0;   
66         }
67         return 0;
68     }       
69
70     public void getIndentLevel(int pos, int[] level) {
71         Element line = elem.getElement(elem.getElementIndex(pos));
72         int startL = line.getStartOffset();
73         int endL = line.getEndOffset();
74         level[0] = 0;
75         level[1] = 0;
76         try {
77             yyreset(new ScilabDocumentReader(doc, startL, endL));
78             yybegin(INDENT);
79             do {
80                 switch (yylex()) {
81                 case 0:
82                       break;
83                 case 1:
84                      level[1]++;
85                      break;
86                 case 2:
87                      if (level[1] > 0) {
88                          level[1]--;
89                      } else {
90                          level[0]++;
91                      }
92                      break;
93                 case 3:
94                      if (level[1] == 0) {
95                          level[0]++;
96                          level[1]++;
97                      }
98                 }                
99             } while (zzMarkedPos != 0);
100         } catch (IOException e) { }       
101     }
102 %}
103
104 %eofval{
105   return 0;
106 %eofval}
107
108 /* main character classes */
109
110 eol = \n
111
112 comment = "//".*{eol}
113
114 spec = [a-zA-Z0-9%_#!$?0-9]
115
116 string = (([^\'\"\r\n]*)|([\'\"]{2}))+
117 qstring = (\"|\'){string}(\"|\')
118 transp = ({spec} | ")" | "]" | "}") "'"
119
120 indentP = ("function" | "if" | "for" | "while" | "try" | "select" | "switch")
121 indentPx = {indentP}{spec}+
122 xindentP = {spec}{indentP}
123
124 indentM = ("endfunction" | "end")
125 indentMx = {indentM}{spec}+
126 xindentM = {spec}{indentM}
127 indentMP = ("else" | "elseif" | "catch" | "case" | "otherwise")
128 indentMPx = {indentMP}{spec}+
129 xindentMP = {spec}{indentMP}
130
131 %x INDENT, WHITE, TAB, BEG
132
133 %%
134
135 <INDENT> {
136   {transp}                       |
137   {comment}                      |
138   {indentPx}                     |
139   {qstring}                      { }
140
141   {indentP}                      {
142                                    return 1;
143                                  }
144
145   {indentM}                      {
146                                    return 2;
147                                  }
148
149   {indentMP}                     {
150                                    return 3;
151                                  }
152
153   .                              |
154   {xindentP}                     |
155   {xindentM}                     |
156   {xindentMP}                    |
157   {indentMPx}                    |
158   {indentMx}                     { }
159
160   {eol}                          {
161                                    return 0;
162                                  }
163 }
164
165 <WHITE> {
166   " "*                           {
167                                    return yylength();
168                                  }
169
170   .                              |
171   {eol}                          {
172                                    return 0;
173                                  }
174 }
175
176 <TAB> {
177   "\t"*                          {
178                                    return yylength();
179                                  }
180
181   .                              |
182   {eol}                          {
183                                    return 0;
184                                  }
185 }
186
187 <BEG> {
188   ^[ \t]+                        {
189                                    return 1;
190                                  }
191
192   .                              |
193   {eol}                          { }
194 }