-
Notifications
You must be signed in to change notification settings - Fork 17
/
SingleQuoteStringLexer.flex
66 lines (51 loc) · 2.01 KB
/
SingleQuoteStringLexer.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
package com.github.kornilova203.matlab.lexer;
import com.intellij.lexer.FlexAdapter;
import com.intellij.lexer.FlexLexer;
import com.intellij.psi.tree.IElementType;
import static com.github.kornilova203.matlab.psi.MatlabTypes.SINGLE_QUOTE_STRING;
import static com.intellij.psi.StringEscapesTokenTypes.VALID_STRING_ESCAPE_TOKEN;
import static com.intellij.psi.TokenType.BAD_CHARACTER;
%%
%{
private boolean hasUnmatchedText = false;
public static FlexAdapter getAdapter() {
return new FlexAdapter(new SingleQuoteStringLexer());
}
private SingleQuoteStringLexer() {
this(null);
}
%}
%public
%class SingleQuoteStringLexer
%implements FlexLexer
%function advance
%type IElementType
%unicode
/* It is guaranteed that a string literal has following properties:
* - it starts with single quote (but may not end with quote)
* - it does not contain \n characters
* - stand alone single quote always may be only at the end of string */
%state AFTER_FIRST_QUOTE
%state AFTER_LAST_QUOTE
%state ESCAPE_SEQUENCE_STATE
%%
<YYINITIAL> {
' / (\\[nrbtf\\] | '') { yybegin(AFTER_FIRST_QUOTE); return SINGLE_QUOTE_STRING; } // string starts with escaped char
' { yybegin(AFTER_FIRST_QUOTE); hasUnmatchedText = true; }
}
<AFTER_FIRST_QUOTE> {
<<EOF>> { yybegin(AFTER_LAST_QUOTE);
if (hasUnmatchedText) return SINGLE_QUOTE_STRING;
else return null; }
\\[nrbtf\\] { hasUnmatchedText = false; return VALID_STRING_ESCAPE_TOKEN; }
'' { hasUnmatchedText = false; return VALID_STRING_ESCAPE_TOKEN; }
/* in the case of ''' group symbols: ('')' */
[^'] / '' { hasUnmatchedText = false; return SINGLE_QUOTE_STRING; }
/* in the case of \\\ group symbols: (\\)\ */
[^\\] / \\[nrbtf\\] { hasUnmatchedText = false; return SINGLE_QUOTE_STRING; }
. { hasUnmatchedText = true; }
}
<AFTER_LAST_QUOTE> {
. { return null; }
}
[^] { return BAD_CHARACTER; }