This repository has been archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathres_grammar.ml
325 lines (294 loc) · 9.98 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
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
let toString = 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.toString 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"
let isSignatureItemStart = function
| Token.At | Let | Typ | External | Exception | Open | Include | Module | AtAt
| PercentPercent ->
true
| _ -> false
let isAtomicPatternStart = function
| Token.Int _ | String _ | Codepoint _ | Backtick | Lparen | Lbracket | Lbrace
| Underscore | Lident _ | Uident _ | List | Exception | Lazy | Percent ->
true
| _ -> false
let isAtomicExprStart = function
| Token.True | False | Int _ | String _ | Float _ | Codepoint _ | Backtick
| Uident _ | Lident _ | Hash | Lparen | List | Lbracket | Lbrace | LessThan
| Module | Percent ->
true
| _ -> false
let isAtomicTypExprStart = function
| Token.SingleQuote | Underscore | Lparen | Lbrace | Uident _ | Lident _
| Percent ->
true
| _ -> false
let isExprStart = function
| Token.Assert | At | Await | Backtick | Bang | Codepoint _ | False | Float _
| For | Hash | If | Int _ | Lazy | Lbrace | Lbracket | LessThan | Lident _
| List | Lparen | Minus | MinusDot | Module | Percent | Plus | PlusDot
| String _ | Switch | True | Try | Uident _ | Underscore (* _ => doThings() *)
| While ->
true
| _ -> false
let isJsxAttributeStart = function
| Token.Lident _ | Question | Lbrace -> true
| _ -> false
let isStructureItemStart = function
| Token.Open | Let | Typ | External | Exception | Include | Module | AtAt
| PercentPercent | At ->
true
| t when isExprStart t -> true
| _ -> false
let isPatternStart = function
| Token.Int _ | Float _ | String _ | Codepoint _ | Backtick | True | False
| Minus | Plus | Lparen | Lbracket | Lbrace | List | Underscore | Lident _
| Uident _ | Hash | Exception | Lazy | Percent | Module | At ->
true
| _ -> false
let isParameterStart = function
| Token.Typ | Tilde | Dot -> true
| token when isPatternStart token -> true
| _ -> false
(* TODO: overparse Uident ? *)
let isStringFieldDeclStart = function
| Token.String _ | Lident _ | At | DotDotDot -> true
| _ -> false
(* TODO: overparse Uident ? *)
let isFieldDeclStart = function
| Token.At | Mutable | Lident _ -> true
(* recovery, TODO: this is not ideal… *)
| Uident _ -> true
| t when Token.isKeyword t -> true
| _ -> false
let isRecordDeclStart = function
| Token.At | Mutable | Lident _ -> true
| _ -> false
let isTypExprStart = function
| Token.At | SingleQuote | Underscore | Lparen | Lbracket | Uident _
| Lident _ | Module | Percent | Lbrace ->
true
| _ -> false
let isTypeParameterStart = function
| Token.Tilde | Dot -> true
| token when isTypExprStart token -> true
| _ -> false
let isTypeParamStart = function
| Token.Plus | Minus | SingleQuote | Underscore -> true
| _ -> false
let isFunctorArgStart = function
| Token.At | Uident _ | Underscore | Percent | Lbrace | Lparen -> true
| _ -> false
let isModExprStart = function
| Token.At | Percent | Uident _ | Lbrace | Lparen | Lident "unpack" -> true
| _ -> false
let isRecordRowStart = function
| Token.DotDotDot -> true
| Token.Uident _ | Lident _ -> true
(* TODO *)
| t when Token.isKeyword t -> true
| _ -> false
let isRecordRowStringKeyStart = function
| Token.String _ -> true
| _ -> false
let isArgumentStart = function
| Token.Tilde | Dot | Underscore -> true
| t when isExprStart t -> true
| _ -> false
let isPatternMatchStart = function
| Token.Bar -> true
| t when isPatternStart t -> true
| _ -> false
let isPatternOcamlListStart = function
| Token.DotDotDot -> true
| t when isPatternStart t -> true
| _ -> false
let isPatternRecordItemStart = function
| Token.DotDotDot | Uident _ | Lident _ | Underscore -> true
| _ -> false
let isAttributeStart = function
| Token.At -> true
| _ -> false
let isJsxChildStart = isAtomicExprStart
let isBlockExprStart = function
| Token.Assert | At | Await | Backtick | Bang | Codepoint _ | Exception
| False | Float _ | For | Forwardslash | Hash | If | Int _ | Lazy | Lbrace
| Lbracket | LessThan | Let | Lident _ | List | Lparen | Minus | MinusDot
| Module | Open | Percent | Plus | PlusDot | String _ | Switch | True | Try
| Uident _ | Underscore | While ->
true
| _ -> false
let isListElement grammar token =
match grammar with
| ExprList -> token = Token.DotDotDot || isExprStart token
| ListExpr -> token = DotDotDot || isExprStart token
| PatternList -> token = DotDotDot || isPatternStart token
| ParameterList -> isParameterStart token
| StringFieldDeclarations -> isStringFieldDeclStart token
| FieldDeclarations -> isFieldDeclStart token
| RecordDecl -> isRecordDeclStart token
| TypExprList -> isTypExprStart token || token = Token.LessThan
| TypeParams -> isTypeParamStart token
| FunctorArgs -> isFunctorArgStart token
| ModExprList -> isModExprStart token
| TypeParameters -> isTypeParameterStart token
| RecordRows -> isRecordRowStart token
| RecordRowsStringKey -> isRecordRowStringKeyStart token
| ArgumentList -> isArgumentStart token
| Signature | Specification -> isSignatureItemStart token
| Structure | Implementation -> isStructureItemStart token
| PatternMatching -> isPatternMatchStart token
| PatternOcamlList -> isPatternOcamlListStart token
| PatternRecord -> isPatternRecordItemStart token
| Attribute -> isAttributeStart token
| TypeConstraint -> token = Constraint
| PackageConstraint -> token = And
| ConstructorDeclaration -> token = Bar
| JsxAttribute -> isJsxAttributeStart token
| AttributePayload -> token = Lparen
| TagNames -> token = Hash
| _ -> false
let isListTerminator grammar token =
match (grammar, token) with
| _, Token.Eof
| ExprList, (Rparen | Forwardslash | Rbracket)
| ListExpr, Rparen
| ArgumentList, Rparen
| 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 isPartOfList grammar token =
isListElement grammar token || isListTerminator grammar token