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
|
%{
#include "Common.h"
#include "Token.h"
#include "Highlighter.h"
IPLVector<Token> tokens;
%}
LineTerminator \n|\r|\r\n
Whitespace \t+
SingleLineComment \/\/.*
MultiLineComment \/\*{1,2}[^\*]+\*\/
Comment {SingleLineComment}|{MultiLineComment}
Keyword break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|export|extends|finally|for|function|if|import|in|of|instanceof|new|return|super|switch|this|throw|try|typeof|var|void|while|with|module
BooleanLiteral true|false
DecimalDigits [0-9]+
SignedInteger {DecimalDigits}|\+{DecimalDigits}|\-{DecimalDigits}
ExponentPart e{SignedInteger}|E{SignedInteger}
DecimalLiteral {DecimalDigits}\.{DecimalDigits}?{ExponentPart}?|\.{DecimalDigits}{ExponentPart}?|{DecimalDigits}{ExponentPart}?
NumericLiteral {DecimalLiteral}
SingleQuoteString '([^\']|(\\\'))*'
DoubleQuoteString \"([^\"]|(\\\"))*\"
StringLiteral {SingleQuoteString}|{DoubleQuoteString}
TemplateLiteral `([^`]|(\\`))*`
Regex \/[^\/]+\/[gimuy]*
Identifier [a-zA-Z_$][a-zA-Z0-9_$]*
Global Infinity|NaN|undefined|null|eval|isNaN|parseFloat|parseInt|Object|Function|Boolean|Symbol|Error|Number|Math|Date|String|RegExp|Array|Map|Set|JSON|Promise|arguments
%%
{Whitespace} { tokens.emplace_back(Whitespace, yytext); }
{LineTerminator} { tokens.emplace_back(NewLine, yytext); }
{Comment} { tokens.emplace_back(Comment, yytext); }
{Keyword} { tokens.emplace_back(Keyword, yytext); }
{BooleanLiteral} { tokens.emplace_back(BooleanLiteral, yytext); }
{NumericLiteral} { tokens.emplace_back(NumericLiteral, yytext); }
{StringLiteral} { tokens.emplace_back(StringLiteral, yytext); }
{TemplateLiteral} { tokens.emplace_back(TemplateLiteral, yytext); }
{Regex} { tokens.emplace_back(Regex, yytext); }
{Global} { tokens.emplace_back(Global, yytext); }
{Identifier} { tokens.emplace_back(Identifier, yytext); }
. { tokens.emplace_back(Raw, yytext); }
%%
int yywrap()
{
return 1;
}
int main(int argc, const char *argv[]) {
++argv, --argc;
if ( argc > 0 )
yyin = fopen( argv[0], "r" );
else
yyin = stdin;
yylex();
++argv, --argc;
const char *out = nullptr;
if (argc > 0) {
out = argv[0];
}
Highlighter hh(out);
hh.highlight(tokens);
return 0;
}
|