-
Notifications
You must be signed in to change notification settings - Fork 5
/
grammar.js
254 lines (220 loc) · 7.71 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
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
/**
* @file OpenFOAM grammar for tree-sitter, supports OF9 and FE4
* @author Mohammed Elwardi Fadeli <[email protected]>
* @author Amaan Qureshi <[email protected]>
* @license MIT
*/
/* eslint-disable arrow-parens */
/* eslint-disable camelcase */
/* eslint-disable-next-line spaced-comment */
/// <reference types="tree-sitter-cli/dsl" />
// @ts-check
module.exports = grammar({
name: 'foam',
// Resolve conflicting rules
conflicts: $ => [
[$._statement, $._non_uniform_list],
[$._non_uniform_list],
[$.dict],
],
// Tell the external scanner to figure out identifiers
externals: $ => [
$.identifier,
$.boolean,
$._eof,
],
// Our extras are C's extras
extras: $ => [
/\s/, // Treating whitespace as white space;
$.comment, // Treating comments as white space;
$.pyfoam_template, // PyFoam stuff is whitespace;
],
word: $ => $.identifier,
rules: {
foam: $ => seq(repeat($._statement), $._eof),
// Global statements in a dictionary
_statement: $ => choice(
// Canonical statements
$.preproc_call,
$.key_value,
$.dict,
// Special support for uncommon things which may appear as statements
$._non_uniform_list,
$._uniform_list,
$.number_literal,
$.pyfoam_variable,
),
// OpenFOAM Dictionaries
dict: $ => seq(
field('key', choice($.identifier, $.string_literal, $.list)),
'{',
optional($.dict_core),
alias(token(prec(-1, '}')), '}'),
optional(';'), // This shouldn't be here; but oh well; FE4 tutorials had some
),
dict_core: $ => prec.left(3, seq(
field('dict_body', repeat1(choice($._statement, seq($.macro, optional(';'))))),
)),
// OpenFOAM Key-Value pairs
key_value: $ => seq(
field('keyword', choice($.identifier, $.string_literal)),
optional(field('value', repeat1($._value))), // Optional values
';',
),
// Directives (#include*, #calc and the like, let's call them preprocessor calls)
preproc_call: $ => choice(
$._generic_preproc_call,
$._special_preproc_call,
),
_special_preproc_call: $ => prec(2, choice(
$._cond_preproc_call,
field('directive', token(seq('#', 'else'))),
field('directive', token(seq('#', 'endif'))),
)),
// TODO: "diretive" does not show up as a field for "#ifeq" call
_cond_preproc_call: $ => prec.left(2, seq(
field('directive', token(seq('#', 'ifeq'))),
field('argument', seq(choice(
$.identifier,
$.macro,
$.string_literal,
), choice(
$.identifier,
$.macro,
$.string_literal,
),
)))),
_generic_preproc_call: $ => prec.left(2, seq(
field('directive', seq('#', $.identifier)),
field('argument', choice(
$.preproc_call,
$.string_literal,
$.identifier,
$.macro,
$.dict_headless,
)),
optional(';'),
)),
// OpenFOAM basic values (What commonly goes into the value part of a key-value pair)
_value: $ => prec.left(2, choice(
$.dimensions,
$.preproc_call,
$.dict,
$.list,
$.code,
$.macro,
$.boolean,
$.string_literal,
$.number_literal,
$.pyfoam_expression,
$.identifier,
)),
// OpenFOAM dimensions data
// TODO: $.identifier is overkill; here to support [m^2 s^-2] kind of dimensions
dimensions: $ => seq(
'[',
repeat(field('dimension', choice($.identifier, $.number_literal))),
alias(token(prec(1, ']')), ']'),
),
// OpenFOAM list of items
list: $ => choice($._uniform_list, $._non_uniform_list),
_uniform_list: $ => seq(
field('size', $.number_literal),
'{',
field('item', $._list_item),
alias(token(prec(-1, '}')), '}'),
),
_non_uniform_list: $ => seq(
optional(field('type', $.identifier)),
optional(field('size', $.number_literal)),
'(',
field('item', repeat($._list_item)),
alias(token(prec(-1, ')')), ')'),
),
_list_item: $ => choice(
$._value, $.dict_headless,
),
dict_headless: $ => seq(
'{',
field('dict_body', repeat(choice($._statement, seq($.macro, optional(';'))))),
alias(token(prec(-1, '}')), '}'),
),
// C++ code blocks in OpenFOAM dictionaries
// C++ Comments will be treated as white space even inside the C++ block
// which is good, protecting against #} appearing a comment and messing things up
code: $ => seq(
'#{',
optional($.code_body),
'#}',
),
code_body: $ => repeat1(choice(
token(/([^#\n"]|#[^{}\n]|"([^"\\]|\\.)+")+/),
$.string_literal,
)),
// OpenFOAM macros; currently, also reads file paths
macro: $ => choice($._macro_braces, $._macro_no_braces),
_macro_braces: $ => seq(
'${',
optional($.prev_scope),
$.identifier,
token.immediate('}'),
),
_macro_no_braces: $ => seq(
'$',
optional($.prev_scope),
$.identifier,
),
// Scope indication for the reference entry
prev_scope: _ => /[:!\.\/]+/,
// OpenFOAM boolean-like values
// boolean: _ => choice(
// 'on',
// 'off',
// 'true',
// 'false',
// 'yes',
// 'no',
// ),
// Primitive floating number
number_literal: _ => token(seq(
/[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?/, // "0." will not get match
token.immediate(optional('.')), // hence add a sketchy & optional "."
)),
// C-style strings
string_literal: $ => seq(
choice('L"', 'u"', 'U"', 'u8"', '"'),
repeat(choice(
token.immediate(prec(1, /[^\\"\n]+/)),
$.escape_sequence,
)),
'"',
),
escape_sequence: _ => token(prec(1, seq(
'\\',
choice(
/[^xuU]/,
/\d{2,3}/,
/x[0-9a-fA-F]{2,}/,
/u[0-9a-fA-F]{4}/,
/U[0-9a-fA-F]{8}/,
),
))),
// C-like comments, not as good as using a real preprocessor,
// but does the job just fine
comment: _ => prec.left(4, token(choice(
seq('//', /(\\(.|\r?\n)|[^\\\n])*/),
seq(
'/*',
/[^*]*\*+([^/*][^*]*\*+)*/,
'/',
)))),
// Basic PyFoam support
single_python_line: _ => repeat1(token.immediate(/[^\n]/)),
pyfoam_template: $ => seq('<!--(', field('code_body', $.single_python_line), ')-->'),
pyfoam_expression: $ => seq('|-', field('code_body', $.single_python_line), '-|'),
pyfoam_variable: $ => seq('$$', field('code_body', $.single_python_line), '\n'),
// OpenFOAM identifiers
// This pretty much matches anything a crazy programmer can thinkup for a keyword name;
// The only requirement is the 1st character, just to avoid conflicts with other rules
},
});