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