-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.js
150 lines (120 loc) · 3.18 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
const { optionalCommaSep1, commaSep1, sep1 } = require('./grammar/utils')
const { PREC, SEMICOLON } = require('./grammar/constants')
const imp = require('./grammar/imp')
const fun = require('./grammar/fun')
const common = require('./grammar/common')
// Grammar
// =======
module.exports = grammar({
name: 'bend',
rules: {
source_file: $ => repeat($._top_level_defs),
...common, // Common rules between syntaxes
...imp, // Imperative-like syntax
...fun, // Functional-like syntax
// Top-level definitions
// =====================
_top_level_defs: $ => choice(
$._import,
$._func_def,
$.object_definition,
$._type_definition,
$.hvm_definition
),
// Import definition
// =================
_import: $ => choice(
$.import_name,
$.import_from
),
import_name: $ => seq(
'import',
$.os_path
),
import_from: $ => seq(
'from',
$.os_path,
'import',
$.os_path
),
// Function definitions
// ====================
_func_def: $ => choice(
$.imp_function_definition, // Uses "Statements"
$.fun_function_definition, // Uses "Terms"
),
// Object definitions
// ==================
object_definition: $ => seq(
'object',
field('name', $.identifier),
$._object_def_body,
),
_object_def_body: $ => seq(
'{',
optional(commaSep1(field('field', $.object_field))),
optional(','),
'}',
),
object_field: $ => $.identifier,
// Type definitions
// ================
_type_definition: $ => choice(
$.imp_type_definition,
$.fun_type_definition
),
// HVM defintions
// ==============
hvm_definition: $ => seq(
'hvm',
field('name', $.identifier),
':',
// $._newline,
$._indent,
// TODO: multiple lines of `hvm_code` should be the same expression
repeat1(field('code', $.hvm_code)),
$._dedent
),
},
extras: $ => [
$.multiline_comment,
$.comment,
/[\s\f\uFEFF\u2060\u200B]|\r?\n/,
],
externals: $ => [
$._newline,
$._indent,
$._dedent,
// Mark comments as external tokens so that the external scanner is always
// invoked, even if no external token is expected. This allows for better
// error recovery, because the external scanner can maintain the overall
// structure by returning dedent tokens whenever a dedent occurs, even
// if no dedent is expected.
$.comment,
// Normal identifiers with a slash '/' after their last character.
$.path,
$.error_sentinel
],
inline: $ => [
$._simple_statement,
$._compound_statement,
$.expression,
$.simple_expression,
],
conflicts: $ => [
[$.for_clause],
[$.imp_eraser],
[$.fun_type_constructor],
[$.fun_type_constructor_fields],
[$.imp_constructor, $.imp_superposition],
[$.imp_lambda, $.imp_constructor],
[$._fun_eraser, $.operator],
[$.imp_tuple, $.arguments],
[$.imp_tuple, $._imp_arg],
[$.imp_tree_leaf, $.imp_constructor],
[$._function_pattern, $._terms],
[$._fun_args]
],
word: $ => $._normal_identifier,
supertypes: $ => [$._id],
});