-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathres_grammar.ml
334 lines (302 loc) · 10.5 KB
/
res_grammar.ml
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
module Token = Res_token
type t =
| OpenDescription (* open Belt *)
| ModuleLongIdent (* Foo or Foo.Bar *) [@live]
| Ternary (* condExpr ? trueExpr : falseExpr *)
| Es6ArrowExpr
| Jsx
| JsxAttribute
| JsxChild [@live]
| ExprOperand
| ExprUnary
| ExprSetField
| ExprBinaryAfterOp of Token.t
| ExprBlock
| ExprCall
| ExprList
| ExprArrayAccess
| ExprArrayMutation
| ExprIf
| ExprFor
| IfCondition
| IfBranch
| ElseBranch
| TypeExpression
| External
| PatternMatching
| PatternMatchCase
| LetBinding
| PatternList
| PatternOcamlList
| PatternRecord
| TypeDef
| TypeConstrName
| TypeParams
| TypeParam [@live]
| PackageConstraint
| TypeRepresentation
| RecordDecl
| ConstructorDeclaration
| ParameterList
| StringFieldDeclarations
| FieldDeclarations
| TypExprList
| FunctorArgs
| ModExprList
| TypeParameters
| RecordRows
| RecordRowsStringKey
| ArgumentList
| Signature
| Specification
| Structure
| Implementation
| Attribute
| TypeConstraint
| AtomicTypExpr
| ListExpr
| Pattern
| AttributePayload
| TagNames
| DictRows
let to_string = function
| OpenDescription -> "an open description"
| ModuleLongIdent -> "a module path"
| Ternary -> "a ternary expression"
| Es6ArrowExpr -> "an es6 arrow function"
| Jsx -> "a jsx expression"
| JsxAttribute -> "a jsx attribute"
| ExprOperand -> "a basic expression"
| ExprUnary -> "a unary expression"
| ExprBinaryAfterOp op ->
"an expression after the operator \"" ^ Token.to_string op ^ "\""
| ExprIf -> "an if expression"
| IfCondition -> "the condition of an if expression"
| IfBranch -> "the true-branch of an if expression"
| ElseBranch -> "the else-branch of an if expression"
| TypeExpression -> "a type"
| External -> "an external"
| PatternMatching -> "the cases of a pattern match"
| ExprBlock -> "a block with expressions"
| ExprSetField -> "a record field mutation"
| ExprCall -> "a function application"
| ExprArrayAccess -> "an array access expression"
| ExprArrayMutation -> "an array mutation"
| LetBinding -> "a let binding"
| TypeDef -> "a type definition"
| TypeParams -> "type parameters"
| TypeParam -> "a type parameter"
| TypeConstrName -> "a type-constructor name"
| TypeRepresentation -> "a type representation"
| RecordDecl -> "a record declaration"
| PatternMatchCase -> "a pattern match case"
| ConstructorDeclaration -> "a constructor declaration"
| ExprList -> "multiple expressions"
| PatternList -> "multiple patterns"
| PatternOcamlList -> "a list pattern"
| PatternRecord -> "a record pattern"
| ParameterList -> "parameters"
| StringFieldDeclarations -> "string field declarations"
| FieldDeclarations -> "field declarations"
| TypExprList -> "list of types"
| FunctorArgs -> "functor arguments"
| ModExprList -> "list of module expressions"
| TypeParameters -> "list of type parameters"
| RecordRows -> "rows of a record"
| RecordRowsStringKey -> "rows of a record with string keys"
| ArgumentList -> "arguments"
| Signature -> "signature"
| Specification -> "specification"
| Structure -> "structure"
| Implementation -> "implementation"
| Attribute -> "an attribute"
| TypeConstraint -> "constraints on a type"
| AtomicTypExpr -> "a type"
| ListExpr -> "an ocaml list expr"
| PackageConstraint -> "a package constraint"
| JsxChild -> "jsx child"
| Pattern -> "pattern"
| ExprFor -> "a for expression"
| AttributePayload -> "an attribute payload"
| TagNames -> "tag names"
| DictRows -> "rows of a dict"
let is_signature_item_start = function
| Token.At | Let | Typ | External | Exception | Open | Include | Module | AtAt
| PercentPercent ->
true
| _ -> false
let is_atomic_pattern_start = function
| Token.Int _ | String _ | Codepoint _ | Backtick | Lparen | Lbracket | Lbrace
| Underscore | Lident _ | Uident _ | List | Dict | Exception | Percent ->
true
| _ -> false
let is_atomic_expr_start = function
| Token.True | False | Int _ | String _ | Float _ | Codepoint _ | Backtick
| Uident _ | Lident _ | Hash | Lparen | List | Lbracket | Lbrace | LessThan
| Module | Percent | Forwardslash | ForwardslashDot | Dict ->
true
| _ -> false
let is_atomic_typ_expr_start = function
| Token.SingleQuote | Underscore | Lparen | Lbrace | Uident _ | Lident _
| Percent ->
true
| _ -> false
let is_expr_start = function
| Token.Assert | At | Await | Backtick | Bang | Codepoint _ | False | Float _
| For | Hash | If | Int _ | Lbrace | Lbracket | LessThan | Lident _ | List
| Lparen | Minus | MinusDot | Module | Percent | Plus | PlusDot | String _
| Switch | True | Try | Uident _ | Underscore (* _ => doThings() *)
| While | Forwardslash | ForwardslashDot | Dict ->
true
| _ -> false
let is_jsx_attribute_start = function
| Token.Lident _ | Question | Lbrace -> true
| _ -> false
let is_structure_item_start = function
| Token.Open | Let | Typ | External | Exception | Include | Module | AtAt
| PercentPercent | At ->
true
| t when is_expr_start t -> true
| _ -> false
let is_pattern_start = function
| Token.Int _ | Float _ | String _ | Codepoint _ | Backtick | True | False
| Minus | Plus | Lparen | Lbracket | Lbrace | List | Dict | Underscore
| DotDotDot | Lident _ | Uident _ | Hash | Exception | Percent | Module | At
->
true
| _ -> false
let is_parameter_start = function
| Token.Typ | Tilde | Dot -> true
| token when is_pattern_start token -> true
| _ -> false
(* TODO: overparse Uident ? *)
let is_string_field_decl_start = function
| Token.String _ | Lident _ | At | DotDotDot -> true
| _ -> false
(* TODO: overparse Uident ? *)
let is_field_decl_start = function
| Token.At | Mutable | Lident _ -> true
(* recovery, TODO: this is not ideal… *)
| Uident _ -> true
| t when Token.is_keyword t -> true
| _ -> false
let is_record_decl_start = function
| Token.At | Mutable | Lident _ | DotDotDot | String _ -> true
| _ -> false
let is_typ_expr_start = function
| Token.At | SingleQuote | Underscore | Lparen | Lbracket | Uident _
| Lident _ | Module | Percent | Lbrace ->
true
| _ -> false
let is_type_parameter_start = function
| Token.Tilde | Dot -> true
| token when is_typ_expr_start token -> true
| _ -> false
let is_type_param_start = function
| Token.Plus | Minus | SingleQuote | Underscore -> true
| _ -> false
let is_functor_arg_start = function
| Token.At | Uident _ | Underscore | Percent | Lbrace | Lparen -> true
| _ -> false
let is_mod_expr_start = function
| Token.At | Percent | Uident _ | Lbrace | Lparen | Lident "unpack" | Await ->
true
| _ -> false
let is_dict_row_start = function
| Token.String _ -> true
| _ -> false
let is_record_row_start = function
| Token.DotDotDot -> true
| Token.Uident _ | Lident _ -> true
(* TODO *)
| t when Token.is_keyword t -> true
| _ -> false
let is_record_row_string_key_start = function
| Token.String _ -> true
| _ -> false
let is_argument_start = function
| Token.Tilde | Dot | Underscore -> true
| t when is_expr_start t -> true
| _ -> false
let is_pattern_match_start = function
| Token.Bar -> true
| t when is_pattern_start t -> true
| _ -> false
let is_pattern_ocaml_list_start = function
| Token.DotDotDot -> true
| t when is_pattern_start t -> true
| _ -> false
let is_pattern_record_item_start = function
| Token.DotDotDot | Uident _ | Lident _ | Underscore -> true
| _ -> false
let is_attribute_start = function
| Token.At -> true
| _ -> false
let is_jsx_child_start = is_atomic_expr_start
let is_block_expr_start = function
| Token.Assert | At | Await | Backtick | Bang | Codepoint _ | Exception
| False | Float _ | For | Forwardslash | ForwardslashDot | Hash | If | Int _
| Lbrace | Lbracket | LessThan | Let | Lident _ | List | Lparen | Minus
| MinusDot | Module | Open | Percent | Plus | PlusDot | String _ | Switch
| True | Try | Uident _ | Underscore | While | Dict ->
true
| _ -> false
let is_list_element grammar token =
match grammar with
| ExprList -> token = Token.DotDotDot || is_expr_start token
| ListExpr -> token = DotDotDot || is_expr_start token
| PatternList -> token = DotDotDot || is_pattern_start token
| ParameterList -> is_parameter_start token
| StringFieldDeclarations -> is_string_field_decl_start token
| FieldDeclarations -> is_field_decl_start token
| RecordDecl -> is_record_decl_start token
| TypExprList -> is_typ_expr_start token || token = Token.LessThan
| TypeParams -> is_type_param_start token
| FunctorArgs -> is_functor_arg_start token
| ModExprList -> is_mod_expr_start token
| TypeParameters -> is_type_parameter_start token
| DictRows -> is_dict_row_start token
| RecordRows -> is_record_row_start token
| RecordRowsStringKey -> is_record_row_string_key_start token
| ArgumentList -> is_argument_start token
| Signature | Specification -> is_signature_item_start token
| Structure | Implementation -> is_structure_item_start token
| PatternMatching -> is_pattern_match_start token
| PatternOcamlList -> is_pattern_ocaml_list_start token
| PatternRecord -> is_pattern_record_item_start token
| Attribute -> is_attribute_start token
| TypeConstraint -> token = Constraint
| PackageConstraint -> token = And
| ConstructorDeclaration -> token = Bar
| JsxAttribute -> is_jsx_attribute_start token
| AttributePayload -> token = Lparen
| TagNames -> token = Hash
| _ -> false
let is_list_terminator grammar token =
match (grammar, token) with
| _, Token.Eof
| ExprList, (Rparen | Forwardslash | Rbracket)
| ListExpr, Rparen
| ArgumentList, (Rparen | DotDotDot)
| TypExprList, (Rparen | Forwardslash | GreaterThan | Equal)
| ModExprList, Rparen
| ( (PatternList | PatternOcamlList | PatternRecord),
( Forwardslash | Rbracket | Rparen | EqualGreater (* pattern matching => *)
| In (* for expressions *)
| Equal (* let {x} = foo *) ) )
| ExprBlock, Rbrace
| (Structure | Signature), Rbrace
| TypeParams, Rparen
| ParameterList, (EqualGreater | Lbrace)
| JsxAttribute, (Forwardslash | GreaterThan)
| StringFieldDeclarations, Rbrace ->
true
| Attribute, token when token <> At -> true
| TypeConstraint, token when token <> Constraint -> true
| PackageConstraint, token when token <> And -> true
| ConstructorDeclaration, token when token <> Bar -> true
| AttributePayload, Rparen -> true
| TagNames, Rbracket -> true
| _ -> false
let is_part_of_list grammar token =
is_list_element grammar token || is_list_terminator grammar token