-
Notifications
You must be signed in to change notification settings - Fork 17
/
MatlabLexer.flex
263 lines (224 loc) · 10.6 KB
/
MatlabLexer.flex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package com.github.kornilova203.matlab.lexer;
import com.intellij.lexer.FlexAdapter;
import com.intellij.lexer.FlexLexer;
import com.intellij.psi.tree.IElementType;
import java.util.Stack;
import static com.intellij.psi.TokenType.BAD_CHARACTER;
import static com.intellij.psi.TokenType.WHITE_SPACE;
import static com.github.kornilova203.matlab.psi.MatlabTypes.*;
%%
%{
private Stack<Integer> stack = new Stack<>();
public static FlexAdapter getAdapter() {
return new FlexAdapter(new MatlabLexer());
}
private MatlabLexer() {
this(null);
}
private void yypushState(int newState) {
stack.push(yystate());
yybegin(newState);
}
private void yypopState() {
if (stack.isEmpty()) return;
yybegin(stack.pop());
}
private void yyclearStack() {
while (!stack.isEmpty()) stack.pop();
}
private void stopLookForCtrans() {
if (yystate() == LOOK_FOR_CTRANS) yypopState();
}
private void lookForCtrans() {
if (yystate() != LOOK_FOR_CTRANS) yypushState(LOOK_FOR_CTRANS);
}
private void startWsDoesNotMatter() {
if (yystate() == LOOK_FOR_CTRANS) yypopState();
yypushState(YYINITIAL);
}
private void stopWsDoesNotMatter() {
if (yystate() == LOOK_FOR_CTRANS) yypopState();
if (yystate() == YYINITIAL) yypopState();
}
private void startWsMatters() {
if (yystate() == LOOK_FOR_CTRANS) yypopState();
yypushState(WS_MATTERS);
}
private void stopWsMatters() {
if (yystate() == LOOK_FOR_CTRANS) yypopState();
if (yystate() == WS_MATTERS) yypopState();
}
%}
%public
%class MatlabLexer
%implements FlexLexer
%function advance
%type IElementType
%unicode
NEWLINE=(\R( \t)*)
WHITE_SPACE=[ \t\x0B\f]+ // do not match new line
SINGLE_QUOTE='
LINE_COMMENT=%.*
BLOCK_COMMENT_PREFIX={WHITE_SPACE}* "%{" {WHITE_SPACE}*
BLOCK_COMMENT_SUFFIX={WHITE_SPACE}* "%}" {WHITE_SPACE}*
LINE_HASH_COMMENT=#.*
BLOCK_HASH_COMMENT_PREFIX={WHITE_SPACE}* "#{" {WHITE_SPACE}*
BLOCK_HASH_COMMENT_SUFFIX={WHITE_SPACE}* "#}" {WHITE_SPACE}*
FLOAT=(([\d]*\.[\d]+)|([\d]+\.))i?
FLOAT_EXPONENTIAL=(([\d]*\.[\d]+)|([\d]+\.)|\d+)e[\+-]?[\d]+i?
IDENTIFIER = [:jletter:] [:jletterdigit:]*
INTEGER=[0-9]+[ijIJ]?
/* double quote literal does not allow single \ character. Sequence \" gives double quote */
ESCAPE_SEQUENCE = \\[^\r\n]
DOUBLE_QUOTE_STRING = \" ([^\\\"\r\n] | {ESCAPE_SEQUENCE})* \"?
/* single quote literal allows single \ character. Sequence '' gives single quote */
SINGLE_QUOTE_EXCAPE_SEQUENCE=\\[\\bfnrt]|''
%state WS_MATTERS
%state LOOK_FOR_CTRANS
%state SINGLE_QOUTE_STRING_STATE
%state BLOCKCOMMENT_STATE
%state BLOCK_HASH_COMMENT_STATE
%state LOOK_FOR_LINECOMMENT
%state LINECOMMENT_STATE
%state FILE_NAME_STATE
%%
<YYINITIAL,WS_MATTERS,LOOK_FOR_CTRANS> {
{WHITE_SPACE} { if (yystate() == LOOK_FOR_CTRANS &&
!stack.isEmpty() &&
stack.peek() == WS_MATTERS) {
yypopState();
}
return WHITE_SPACE;
}
{SINGLE_QUOTE} / \n { if (yystate() == LOOK_FOR_CTRANS) {
return TRANS;
} else {
return SINGLE_QUOTE_STRING;
}
}
{SINGLE_QUOTE} { if (yystate() == LOOK_FOR_CTRANS) {
return CTRANS;
} else {
yypushState(SINGLE_QOUTE_STRING_STATE);
}
}
function { stopLookForCtrans(); return FUNCTION; }
elseif { stopLookForCtrans(); return ELSEIF; }
else { stopLookForCtrans(); return ELSE; }
end { stopLookForCtrans(); return END; }
if { stopLookForCtrans(); return IF; }
for { stopLookForCtrans(); return FOR; }
while { stopLookForCtrans(); return WHILE; }
parfor { stopLookForCtrans(); return PARFOR; }
spmd { stopLookForCtrans(); return SPMD; }
classdef { stopLookForCtrans(); return CLASSDEF; }
switch { stopLookForCtrans(); return SWITCH; }
case { stopLookForCtrans(); return CASE; }
otherwise { stopLookForCtrans(); return OTHERWISE; }
try { stopLookForCtrans(); return TRY; }
catch { stopLookForCtrans(); return CATCH; }
global { stopLookForCtrans(); return GLOBAL; }
return { stopLookForCtrans(); return RETURN; }
continue { stopLookForCtrans(); return CONTINUE; }
break { stopLookForCtrans(); return BREAK; }
load/" "+[^ (] { stopLookForCtrans(); yypushState(FILE_NAME_STATE); return LOAD; }
dir/" "+[^ (] { stopLookForCtrans(); yypushState(FILE_NAME_STATE); return DIR; }
ls/" "+[^ (] { stopLookForCtrans(); yypushState(FILE_NAME_STATE); return LS; }
cd/" "+[^ (] { stopLookForCtrans(); yypushState(FILE_NAME_STATE); return CD; }
"(" { startWsDoesNotMatter(); return LPARENTH; }
")" { stopWsDoesNotMatter(); lookForCtrans(); return RPARENTH; }
"." { stopLookForCtrans(); return DOT; }
"<=" { stopLookForCtrans(); return LESS_OR_EQUAL; }
"-" { stopLookForCtrans(); return MINUS; }
"--" { stopLookForCtrans(); return MINUSMINUS; }
"+" { stopLookForCtrans(); return PLUS; }
"++" { stopLookForCtrans(); return PLUSPLUS; }
"./" { stopLookForCtrans(); return DOT_RDIV; }
"/" { stopLookForCtrans(); return RDIV; }
"\\" { stopLookForCtrans(); return LDIV; }
".\\" { stopLookForCtrans(); return DOT_LDIV; }
".*" { stopLookForCtrans(); return DOT_MUL; }
"*" { stopLookForCtrans(); return MUL; }
".^" { stopLookForCtrans(); return DOT_POW; }
"^" { stopLookForCtrans(); return POW; }
"&&" { stopLookForCtrans(); return AND; }
"&" { stopLookForCtrans(); return MATRIX_AND; }
"||" { stopLookForCtrans(); return OR; }
"|" { stopLookForCtrans(); return MATRIX_OR; }
".'" { stopLookForCtrans(); return TRANS; }
"~" { stopLookForCtrans(); return TILDA; }
"=" { stopLookForCtrans(); return ASSIGN; }
">=" { stopLookForCtrans(); return MORE_OR_EQUAL; }
">" { stopLookForCtrans(); return MORE; }
"<" { stopLookForCtrans(); return LESS; }
"==" { stopLookForCtrans(); return EQUAL; }
"~=" { stopLookForCtrans(); return NOT_EQUAL; }
"," { stopLookForCtrans(); return COMMA; }
":" { stopLookForCtrans(); return COLON; }
";" { stopLookForCtrans(); return SEMICOLON; }
"[" { startWsMatters(); return LBRACKET; }
"]" { stopWsMatters(); lookForCtrans(); return RBRACKET; }
"{" { startWsMatters(); return LBRACE; }
"}" { stopWsMatters(); lookForCtrans(); return RBRACE; }
"..." / {WHITE_SPACE}? \n { stopLookForCtrans(); return ELLIPSIS; }
"..." { stopLookForCtrans(); yypushState(LOOK_FOR_LINECOMMENT); return ELLIPSIS; }
"@" { stopLookForCtrans(); return AT; }
"?" { stopLookForCtrans(); return QUESTION_MARK; }
{NEWLINE} { stopLookForCtrans(); return NEWLINE; }
{LINE_COMMENT} { stopLookForCtrans(); return COMMENT; }
{LINE_HASH_COMMENT} { stopLookForCtrans(); return COMMENT; }
^{BLOCK_COMMENT_PREFIX}$ { stopLookForCtrans(); yypushState(BLOCKCOMMENT_STATE); }
^{BLOCK_HASH_COMMENT_PREFIX}$ { stopLookForCtrans(); yypushState(BLOCK_HASH_COMMENT_STATE); }
{FLOAT_EXPONENTIAL} { lookForCtrans(); return FLOAT_EXPONENTIAL; }
{FLOAT} { lookForCtrans(); return FLOAT; }
{INTEGER} { lookForCtrans(); return INTEGER; }
{IDENTIFIER} { lookForCtrans(); return IDENTIFIER; }
{DOUBLE_QUOTE_STRING} { lookForCtrans(); return DOUBLE_QUOTE_STRING; }
<<EOF>> { return null; }
}
<SINGLE_QOUTE_STRING_STATE> {
{SINGLE_QUOTE_EXCAPE_SEQUENCE} / \n { yypopState(); return SINGLE_QUOTE_STRING; }
{SINGLE_QUOTE_EXCAPE_SEQUENCE} { }
"'" { yypopState(); lookForCtrans(); return SINGLE_QUOTE_STRING; }
<<EOF>> { yyclearStack(); yybegin(YYINITIAL); return SINGLE_QUOTE_STRING; }
/* line should not consume \n character */
. / \n { yypopState(); return SINGLE_QUOTE_STRING; }
. { }
}
<LOOK_FOR_LINECOMMENT,LINECOMMENT_STATE> {
<<EOF>> { yyclearStack(); yybegin(YYINITIAL); return COMMENT; }
}
<LOOK_FOR_LINECOMMENT> {
{WHITE_SPACE} { return WHITE_SPACE; }
. / \n { yypopState(); return COMMENT; }
. { yypopState(); yypushState(LINECOMMENT_STATE); }
}
<LINECOMMENT_STATE> {
/* comment should not consume \n character */
. / \n { yypopState(); return COMMENT; }
. { }
}
<BLOCKCOMMENT_STATE> {
^{BLOCK_COMMENT_PREFIX}$ { yypushState(BLOCKCOMMENT_STATE); }
^{BLOCK_COMMENT_SUFFIX}$ { yypopState();
if (yystate() != BLOCKCOMMENT_STATE) return COMMENT;
}
<<EOF>> { yyclearStack(); yybegin(YYINITIAL); return COMMENT; }
[^] { }
}
<BLOCK_HASH_COMMENT_STATE> {
^{BLOCK_HASH_COMMENT_PREFIX}$ { yypushState(BLOCK_HASH_COMMENT_STATE); }
^{BLOCK_HASH_COMMENT_SUFFIX}$ { yypopState();
if (yystate() != BLOCK_HASH_COMMENT_STATE) return COMMENT;
}
<<EOF>> { yyclearStack(); yybegin(YYINITIAL); return COMMENT; }
[^] { }
}
<FILE_NAME_STATE> {
/* stop consuming filename when find newline */
[^(]/[\n ] { yypopState(); return FILE_NAME; }
"(" { yypopState(); }
<<EOF>> { yyclearStack(); yybegin(YYINITIAL); return FILE_NAME; }
. { }
}
[^] { return BAD_CHARACTER; }