forked from scrapscript/tree-sitter-scrapscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrammar.js
More file actions
198 lines (163 loc) · 4.05 KB
/
Copy pathgrammar.js
File metadata and controls
198 lines (163 loc) · 4.05 KB
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
/**
* @file a sharable programming language
* @author Taylor Troesh <taylor@troe.sh>
* @license MIT
*/
/// <reference types="tree-sitter-cli/dsl" />
// @ts-check
const PREC = {
COMMENT: 1,
STRING: 2,
FIELD_ACCESS: 1,
PART: 1,
FUNCTION: 3,
INFIX: 10,
APPLY: 20, // Higher precedence than infix operators
};
function sepBy(rule, sep) {
return optional(seq(rule, repeat(seq(sep, rule))));
}
module.exports = grammar({
name: "scrapscript",
conflicts: ($) => [
[$._expr, $._callable],
[$._expr, $.pattern],
],
extras: $ => [
$._whitespace,
$._line_comment,
],
rules: {
program: ($) => $._subprogram,
// The following two rules ensure the left-associativity of the
// "where" declaration syntax production
_subprogram: $ => choice($._expr, $.where),
where: $ => seq($._subprogram, ";", $.declaration),
declaration: $ => choice($._annotation, $._binding),
_annotation: $ => seq($.pattern, ":", $.id),
_binding: $ => choice(seq($.pattern, "=", $._expr), seq($._annotation, "=", $._expr)),
_whitespace: _ => /\s+/,
_line_comment: _ => /--.*/,
_expr: ($) =>
choice(
$.apply,
$.bytes,
$.fun,
$.hole,
$.id,
$.infix,
$.list,
$.match_fun,
$.number,
$.parens,
$.record,
$.tag,
$.text,
),
parens: ($) => seq("(", field("expr", $._subprogram), ")"),
_unary: $ =>
choice(
$.bytes,
$.hole,
$.id,
$.list,
$.number,
$.parens,
$.record,
$.tag,
$.text,
$.wildcard,
),
// The following two rules ensures the left-associativity of the
// partial function application syntax production
_callable: $ =>
choice(
// $._callable doesn't include the following syntactically ambiguous
// constructs like $.fun and $.match_fun.
// [TODO]: Support record access
$.apply,
$.id,
$.parens
),
apply: ($) =>
seq(field("caller", $._callable), field("callee", $._unary)),
infix: ($) =>
prec.left(
PREC.INFIX,
seq(field("left", $._expr), field("op", $.op), field("right", $._expr))
),
number: ($) => seq(optional("-"), choice(/[0-9]+/, /[0-9]+\.[0-9]+/)),
text: ($) => /"[^"]*"/,
bytes: ($) => /~~[A-Za-z0-9+/=]+/,
list: ($) => seq("[", sepBy($._expr, ","), "]"),
id: ($) => /[a-zA-Z_][a-zA-Z0-9_]*/,
hole: ($) => "()",
tag: ($) => prec.right(5, seq("#", $.id, optional($._unary))),
record: ($) => seq("{", optional(sepBy($.field, ",")), "}"),
field: ($) =>
choice(
field("name", $.id),
seq(field("name", $.id), "=", field("value", $._expr)),
seq("..", field("value", $._expr)),
field("record_wildcard", "...")
),
// [TODO]: Do we want nested where clauses within regular
// functions and match functions?
// If so, we can use $._subprogram instead of $._expr in the
// two productions below
fun: ($) =>
prec.right(PREC.FUNCTION, seq($.pattern, "->", $._expr)),
match_fun: ($) =>
prec.right(PREC.FUNCTION, seq("|", $.match_arm, repeat(seq("|", $.match_arm)))),
match_arm: ($) => prec.right(PREC.FUNCTION + 1, seq($.pattern, "->", $._expr)),
pattern: ($) =>
choice(
$.bytes,
$.cons,
$.hole,
$.id,
$.list,
$.number,
$.parens,
$.record,
$.tag,
$.text,
$.wildcard,
),
cons: $ => prec.right(seq($.pattern, ">+", $.pattern)),
wildcard: _ => "_",
op: ($) =>
choice(
"|>",
"==",
"/=",
"<",
">",
"<=",
">=",
"*",
"/",
"//",
"%",
"+",
"-",
"&&",
"||",
"::",
"..",
"@",
">>",
"<<",
"^",
">*",
"++",
">+",
"+<",
"'",
":",
"?",
"!",
".",
),
},
});