-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.js
100 lines (80 loc) · 1.86 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
const zero_to_255 = /(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])/;
const hex_digit = /[A-Za-z0-9]/;
const number = /\d+/;
const space = /[ \t]+/;
const maybe_space = optional(space);
module.exports = grammar({
name: "xtc",
extras: $ => [],
rules: {
program: $ => $._lines,
_lines: $ => repeat1($._line),
_line: $ => seq(
maybe_space,
optional(seq(
$.change_port,
maybe_space,
)),
optional(choice(
$.command,
$.comment,
)),
$._line_ending,
),
change_port: $ => /\d+\/\d+/,
command: $ => seq(
$.parameter,
optional(seq(space, $.indexes)),
repeat(seq(space, $._argument)),
),
parameter: $ => /([Pp][Ee]|[CMPcmp])[A-Za-z]?_\w+/,
indexes: $ => seq(
'[', maybe_space, $.index,
repeat(seq( maybe_space, ',', maybe_space, $.index )),
maybe_space,
']',
),
index: $ => /\d+/,
_argument: $ => choice(
$.hex_argument,
$.ipv4_argument,
$.numeric_argument,
$.string_literal_argument,
$.string_argument,
$.template,
),
hex_argument: $ => seq(
"0x",
repeat1(
choice(
$.template,
hex_digit,
))
),
template: $ => /<[\w_-]+>/,
numeric_argument: $ => /-?\d+/,
string_literal_argument: $ => /"[^"]*"/,
string_argument: $ => /[A-Za-f][A-Za-f0-9_]+/,
ipv4_argument: $ => token(seq(
zero_to_255, '.',
zero_to_255, '.',
zero_to_255, '.',
zero_to_255,
)),
comment: $ => seq(
";",
repeat(choice(
$.port_comment,
/[^\n\r]/,
))
),
port_comment: $ => token(seq(
"Port", maybe_space,
":", maybe_space,
number, maybe_space,
"/", maybe_space,
number
)),
_line_ending: $ => token(seq(maybe_space, /[\n\r]+/))
},
});