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