-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrammar.js
52 lines (50 loc) · 1.11 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
module.exports = grammar({
name: "csv",
rules: {
source_file: $ => repeat($.record),
record: $ => seq(
optional($._field),
repeat($._pair),
$._eor,
),
_eor: $ => choice('\n', '\r\n'),
_pair: $ => prec(1, seq(
field('sep', $.delimiter),
optional($._field),
)),
delimiter: $ => ',',
_field: $ => choice(
$.null,
$.na,
$.boolean,
$._number,
$.string,
),
null: $ => /null|NULL/,
na: $ => /na|NA/,
boolean: $ => /true|TRUE|false|FALSE/,
_number: $ => choice(
$.hex,
$.float,
$.integer,
),
integer: $ => /-?\d+/,
hex: $ => /0[xX][\da-fA-F]+/,
float: $ => /-?(0|[1-9]\d*)\.\d+/,
string: $ => choice(
seq($.quote, $.quote),
seq($.quote, $.escaped, $.quote),
$.non_escaped,
),
quote: $ => '"',
escaped: $ =>
repeat1(prec.left(1, choice($.escape_sequence, $.text))),
non_escaped: $ => /[^\\"\r\n\t,]+/,
text: $ => /[^\\"\r\n]+/,
escape_sequence: $ => token.immediate(prec(10, choice(
'""',
'\n',
'\r\n',
))),
}
});