-
Notifications
You must be signed in to change notification settings - Fork 7
/
grammar.js
159 lines (144 loc) · 4.45 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
//const base = require('./grammar/base');
module.exports = grammar({
name: "groovy",
extras: ($) => [$.line_comment, $.block_comment, /\s/],
conflicts: ($) => [[$._unit, $.func]],
rules: {
source_file: ($) => seq(optional($.shebang), repeat($.command)),
block: ($) =>
seq(
$._command_unit,
"{",
repeat($.command),
optional($.end_command),
"}"
),
command: ($) => seq(repeat1($._command_unit), choice("\n", ";")),
end_command: ($) => repeat1($._command_unit),
_command_unit: ($) =>
choice(
$.decorate,
$.list,
$.unit,
$.operators,
$.string,
$.number,
$.arg_spliter,
$.block
),
// ---- split the args -----
arg_spliter: ($) => choice(",", ":"),
// ---- leading_key -----
leading_key: ($) => "$",
// ---- operators ---------
operators: ($) =>
choice("+=", "=", "-=", "+", "-", "~", "->", "&", "|", "!=", "!"),
// ----- unit ----------------
unit: ($) => seq($._unit, repeat(seq(".", choice($._unit, $.string)))),
decorate: ($) => seq("@", $.identifier),
_unit: ($) => choice(choice($.identifier, $.generics_class), $.func),
generics_class: ($) =>
seq($.identifier, "<", field("generics_type", $.identifier), ">"),
func: ($) => seq(choice($.identifier, $.generics_class), $.arg_block),
arg_block: ($) => seq("(", optional($._args), ")"),
// ------------ string -------------
string: ($) =>
choice(
seq(
"'",
optional(
repeat(
choice(token.immediate(prec(1, /[^'\\]+/)), $.escape_sequence)
)
),
"'"
),
seq(
'"',
optional(
repeat(
choice(
token.immediate(prec(1, /[^"\\^$]+/)),
$.escape_sequence,
seq(
$.leading_key,
choice($.identifier, seq("{", $._command_unit, "}"))
)
)
)
),
'"'
)
),
// ---------- list -----------------
list: ($) => seq("[", optional($._args), "]"),
_args: ($) =>
seq(
choice($.string, $.unit, $.list, $.operators, $.arg_block, $.number),
repeat(
seq(
optional($.arg_spliter),
choice($.string, $.unit, $.list, $.operators, $.arg_block, $.number)
)
)
),
// NOTE: both boolean ,type, def and valuename
// and fucntionname, is the base type
identifier: (_) =>
/[A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u00FF\u0100-\uFFFE$_][A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u00FF\u0100-\uFFFE\d_]*/,
escape_sequence: ($) =>
token.immediate(
seq(
"\\",
choice(/[^xu0-7]/, /[0-7]{1,3}/, /x[0-9a-fA-F]{2}/, /u[0-9a-fA-F]{4}/)
)
),
number: ($) => {
const hex_literal = seq(choice("0x", "0X"), /[\da-fA-F](_?[\da-fA-F])*/);
const decimal_digits = /\d(_?\d)*/;
const signed_integer = seq(optional(choice("-", "+")), decimal_digits);
const exponent_part = seq(choice("e", "E"), signed_integer);
const binary_literal = seq(choice("0b", "0B"), /[0-1](_?[0-1])*/);
const octal_literal = seq(choice("0o", "0O"), /[0-7](_?[0-7])*/);
const bigint_literal = seq(
choice(hex_literal, binary_literal, octal_literal, decimal_digits),
"n"
);
const decimal_integer_literal = choice(
"0",
seq(
optional("0"),
/[1-9]/,
optional(seq(optional("_"), decimal_digits))
)
);
const decimal_literal = choice(
seq(
decimal_integer_literal,
".",
optional(decimal_digits),
optional(exponent_part)
),
seq(".", decimal_digits, optional(exponent_part)),
seq(decimal_integer_literal, exponent_part),
seq(decimal_digits)
);
return token(
choice(
// TODO maybe better solution
signed_integer,
hex_literal,
decimal_literal,
binary_literal,
octal_literal,
bigint_literal
)
);
},
comment: ($) => choice($.line_comment, $.block_comment, $.shebang),
line_comment: ($) => token(prec(0, seq("//", /[^\n]*/))),
block_comment: ($) =>
token(prec(0, seq("/*", /[^*]*\*+([^/*][^*]*\*+)*/, "/"))),
shebang: ($) => seq("#!", /.*/),
},
});