-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgrammer-initial.js
162 lines (131 loc) · 3.74 KB
/
grammer-initial.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
module.exports = grammar({
name: 'YOUR_LANGUAGE_NAME',
rules: {
// The external scanner (scanner.cc) allows us to inject "dummy" tokens into the grammar.
// These tokens are used to track the indentation-based scoping used in F#
externals: $ => [
$._virtual_open_section, // Signal the external scanner that a new indentation scope should be opened. Add the indetation size to the stack.
$._virtual_end_section, // end an indentation scope, popping the indentation off the stack.
$._virtual_end_decl, // end an indentation scope with equal alignment, popping the indentation off the stack.
$.block_comment_content
],
// TODO: add the actual grammar rules
source_file: $ => 'hello',
// string_escape: $ => choice('\t', '\\', '\n', '\"', ''),
string_escape: ($) => /\\(u\{[0-9A-Fa-f]{4,6}\}|[nrt\"'\\])/,
_string_content: $ => repeat1(choice($.string_escape, /[^\\"]+|\\/)),
string: $ => seq('"', optional($._string_content), '"'),
_identifier: $ => /[a-z][a-zA-Z0-9_]/,
_identifier_capital: $ => /[A-Z][a-zA-Z0-9_]/,
record: $ => choice(
$.empty_record,
seq(
'{',
$.assigned_fields,
'}'
)
),
empty_record: $ => seq('{', '}'),
assigned_fields: $ => seq(
repeat(seq(
$.assigned_field,
optional($.comma)
)),
optional($.assigned_field)
),
_arrow: $ => "->",
list: $ => seq("[", $.list_content, "]"),
module: $ => seq(
header(),
optional(module_defs()),
indented_end()
),
apply:$ =>
seq(
apply_start_pattern, apply_args
),
parens_around:$=>seq('(',$.full_expr,')'),
access_start:$=>choice($._identifier,$.record,$.parens_around),
op_expr:$=>$.pizza_expr,
//a pizza is this '|>' operator
//TODO: they use bool_or_expr
pizza_expr:$=> seq($.expr,),
pizza_end:$=>seq("|>",$.expr)
full_expr: $ => choice(
op_expr,
seq(
$._virtual_open_section,
op_expr(),
$._virtual_end_section, // end an indentation scope, popping the indentation off the stack.
close_or_end()
)
),
op_expr: $ => pizza_expr(),
expr:$=>choice($.common_expr,$.apply), // Assumption: common_expr is a single node with multiple possible child expressions
common_expr: $ => choice(
closure,
expect,
if_expr,
when,
backpass,
list,
record,
record_update,
parens_around,
token(Number),
token(NumberBase),
token(String),
module_var,
tag,
accessor_function,
defs,
annotation,
token(LowercaseIdent)
),
expr: $ => choice(
access,
apply,
common_expr
),
closure: $ => seq(
'\\',
$.args,
$._arrow,
$.closure_body
),
// Assumption: closure_body handles indentation
closure_body: $ => choice(
seq(
$._virtual_open_section,
full_expr(),
choice($._virtual_close_section, end_of_file(), not($.')'')
),
optional(token(SameIndent)),
full_expr()
),
args: $ => seq(
$.arg,
repeat(seq(
",",
$.arg,
))
),
arg: $ => choice(
'_',
$._identifier,
$.record_destructure
),
record_field: $ => $._identifier,
type: $ => $._identifier,
typed_record_field: $ => seq($.record_field, ":", $.type),
record_value: $ => choice($.record_field, $.typed_record_field),
record_destructure_body: $ => seq(optional(repeat1(seq($.record_value, ","))), $.record_value),
record_destructure: $ => seq("{", $.record_body, "}")
assigned_field: $ => {
choice(
$.required_value,
$._identifier
)
},
required_value: $ => seq($._identifier, ":", $._identifier)
});