This repository has been archived by the owner on Jan 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.js
224 lines (182 loc) · 4.46 KB
/
grammar.js
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
// Regex portions adapated from tree-sitter-regex
const quantifierRule = prefix => $ => prefix($)
const SYNTAX_CHARS = [
...'?*+|()^$.[]{}"\\'
]
const SYNTAX_CHARS_ESCAPED = SYNTAX_CHARS.map(
char => `\\${char}`
).join('')
module.exports = grammar({
name: 'jlex',
extras: $ => [
$.comment,
/[\s\uFEFF\u2060\u200B\u00A0]/, // whitespace
],
inline: $ => [
$._character_escape,
$._class_atom,
],
rules: {
source_file: $ => seq(
$.user_code,
$.directives,
$.rules,
),
user_code: $ => seq(
$.java_code,
$.section_separator,
),
directives: $ => seq(
repeat(choice(
$.code,
$.macro_def,
$.state_decl,
$.directive,
)),
$.section_separator,
),
// TODO: Any way to make sure that the two idents match?
code: $ => seq(
/%(\w)+?\{/,
$.java_code,
/%(\w)+?}/,
),
macro_def: $ => seq(
$.identifier,
'=',
$.regex,
),
state_decl: $ => seq(
'%state',
repeatWithDelimiter1($.identifier, ','),
),
directive: $ => seq(
/%\w+/,
optional(/.+/),
),
rules: $ => repeat1($.rule),
rule: $ => seq(
optional($.state),
$.regex,
$.action,
),
state: $ => seq(
'<',
repeatWithDelimiter($.identifier, ','),
'>',
),
regex: $ => choice(
$.disjunction,
$.term,
),
disjunction: $ => seq(
optional($.term),
repeat1(seq('|', optional($.term))),
),
term: $ => prec.right(repeat1(seq( // TODO: Is prec.right the correct choice here?
choice(
$.start_assertion,
$.end_assertion,
$.pattern_character,
$.character_class,
$.any_character,
$.verbatim,
$.macro,
$._character_escape,
$.group,
),
optional(choice(
$.zero_or_more,
$.one_or_more,
$.optional,
)),
))),
any_character: $ => '.',
start_assertion: $ => '^',
end_assertion: $ => '$',
pattern_character: $ => new RegExp(`[^${SYNTAX_CHARS_ESCAPED}\\n]`),
character_class: $ => seq(
'[',
optional('^'),
repeat(choice(
$.class_range,
$._class_atom
)),
']',
),
class_range: $ => prec.right(1,
seq($._class_atom, '-', $._class_atom),
),
_class_atom: $ => choice(
alias('-', $.class_character),
$.class_character,
$.verbatim,
$.macro,
alias('\\-', $.identity_escape),
$._character_escape,
),
class_character: $ => /[^\\\]\-]/, // NOT: \ ] or -
group: $ => seq('(', $.regex, ')'),
zero_or_more: quantifierRule($ => '*'),
one_or_more: quantifierRule($ => '+'),
optional: quantifierRule($ => '?'),
verbatim: $ => seq(
'"',
repeat(choice(
alias('\\"', $.identity_escape),
/./,
)),
'"',
),
macro: $ => seq(
'{',
$.identifier,
'}',
),
_character_escape: $ => choice(
$.control_escape,
$.control_letter_escape,
$.digit_escape,
$.identity_escape
),
control_escape: $ => /\\[bntfr]/,
control_letter_escape: $ => /\\\^C/,
digit_escape: $ => choice(
/\\\d{3}/,
/\\x\d{2}/,
/\\u\d{4}/,
),
identity_escape: $ => token(seq('\\', /[^bntfrdxu]/)),
action: $ => seq(
'{',
$.java_code,
'}'
),
// This should probably be /^%%/ but tree-sitter doesn't support assertions yet
section_separator: $ => seq(
/%%/,
optional($.comment),
),
identifier: $ => {
const alpha = /[A-Za-z_]/
const alpha_numeric = /[A-Za-z0-9_]/
return token(seq(alpha, repeat(alpha_numeric)))
},
java_code: $ => 'hello', // TODO
comment: $ => token(choice(
/\/\/.*/, // slash comment
// http://stackoverflow.com/questions/13014947/regex-to-match-a-c-style-multiline-comment/36328890#36328890
seq(
'/*',
/[^*]*\*+([^/*][^*]*\*+)*/,
'/',
),
)),
}
});
function repeatWithDelimiter1 (rule, delimiter) {
return seq(rule, repeat(seq(delimiter, rule)));
}
function repeatWithDelimiter (rule, delimiter) {
return optional(repeatWithDelimiter1(rule, delimiter));
}