/
parse_errors.mlyl
136 lines (119 loc) · 4.45 KB
/
parse_errors.mlyl
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
{
let is_same_line prev token token_start =
match token with
| Grammar.EOF _ -> false
| _ -> prev.Lexing.pos_lnum = token_start.Lexing.pos_lnum
}
rule error_message = parse error
(* Match `expr() =` in tables: probably wanted `[expr()] = ...` *)
| [_ / table_entry: expr . ...] @ EQUALS
{ Error.Table_key_equals (token, $startloc(token), $endloc(token)) }
(* Match `expr() = ` in any other context: probably wanted `expr() == `. *)
| [expr] @ EQUALS
{ Error.Use_double_equals (token, $startloc(token), $endloc(token)) }
(* Suggest a missing comma in table entries. *)
| last = [_ / table_body: table_entry . ...] @ STRING, NUMBER, TRUE, FALSE, NIL, OSQUARE, OPAREN, IDENT
{
let token = (token, $startloc(token), $endloc(token)) in
Missing_table_comma { comma_pos = $endloc(last); token }
}
(* Suggest removing a comma in function calls. *)
| (_ / call_args: OPAREN . ...);
(expr | COMMA)* ;
(comma = COMMA / sep_list1(COMMA,expr): expr COMMA . sep_list1(COMMA,expr) ...)
@ CPAREN
{
Trailing_call_comma {
comma = (comma, $startloc(comma), $endloc(comma));
token = (token, $startloc(token), $endloc(token))
}
}
(* Unclosed parentheses. *)
| open_ = OPAREN; [_ / ... . CPAREN]
| open_ = OBRACE; [_ / ... . CBRACE]
| open_ = OSQUARE; [_ / ... . CSQUARE]
{
Unclosed_brackets {
open_ = (open_, $startloc(open_), $endloc(open_));
token = (token, $startloc(token), $endloc(token))
}
}
| start = DOUBLE_COLON; [_ / stmt: DOUBLE_COLON ident . DOUBLE_COLON]
{
Unfinished_label {
start = (start, $startloc(start), $endloc(start));
token = (token, $startloc(token), $endloc(token))
}
}
(* `local function x.f() end` *)
| local = LOCAL; FUNCTION; [var] @ DOT
{
Local_function_dot {
local = (local, $startloc(local), $endloc(local));
token = (token, $startloc(token), $endloc(token))
}
}
(* An `if`/`elseif` statement with a missing `then`. *)
| (if_ = IF | if_ = ELSEIF); expr = [expr]
{
let pos =
if is_same_line $endloc(expr) token $startloc(token) then ($startloc(token), $endloc(token))
else ($endloc(expr), $endloc(expr))
in
Expected_then {
if_ = (if_, $startloc(if_), $endloc(if_));
pos
}
}
(* Statements with a missing `end`. *)
| (start = IF | start = ELSEIF) ; expr ; THEN; [stmts]
| start = ELSE ; [stmts]
| start = WHILE ; expr ; DO; [stmts]
| (start = DO / stmt: DO . ...) ; [stmts]
| (start = FOR / stmt: FOR . ...) ; sep_list1(COMMA,var) ; IN ; sep_list1(COMMA,expr) ; DO ; [stmts]
| (start = FOR / stmt: FOR . ...) ; var ; EQUALS ; expr ; COMMA ; expr ; (COMMA ; expr)? ; DO ; [stmts]
| (start = FUNCTION / stmt: FUNCTION . ...) ; function_name; [args ; stmts]
| (start = FUNCTION / atom: FUNCTION . ...) ; [args ; stmts]
{
Expected_end {
start = ($startloc(start), $endloc(start));
token = (token, $startloc(token), $endloc(token))
}
}
(* We handle local function specially, to capture both loc and func. *)
| loc = LOCAL; func = FUNCTION; var; [args ; stmts]
{
Expected_end {
start = ($startloc(loc), $endloc(func));
token = (token, $startloc(token), $endloc(token))
}
}
(* A standalone name in a statement context *)
| (/ stmt: . call) ; x = [simple_expr]
partial {
if not (is_same_line $endloc(x) token $startloc(token)) then
Some (Standalone_name_call $endloc(x))
else
None
}
| (/ stmt: . sep_list1(COMMA,name) ...) ; [name]
{ Standalone_name (token, $startloc(token), $endloc(token)) }
| (/ stmt: . sep_list1(COMMA,name) ...) ; [sep_list1(COMMA,name)]
{ Standalone_names (token, $startloc(token), $endloc(token)) }
(* Fallback parse error for when we expected a statement. *)
| [_ / stmts: ... . ...]
partial {
match token with
| END _ -> Some (Unexpected_end (token, $startloc(token), $endloc(token)))
| EOF _ -> None
| _ -> Some (Expected_statement (token, $startloc(token), $endloc(token)))
}
(* Fallback parse error for function arguments. *)
| [_? / ... . args ...]
{ Expected_function_args (token, $startloc(token), $endloc(token)) }
(* Fallback parse error for when we expected an expression. *)
| [/ ... . expr ...] | [/ ... . expr_pow ...] | [/ ... . sep_list1(COMMA,expr) ...]
{ Expected_expression (token, $startloc(token), $endloc(token)) }
(* Fallback parse error for when we expected a variable name. *)
| [/ ... . var ...] | [/ ... . name ...] | [_ / ... . function_name ...]
{ Expected_var (token, $startloc(token), $endloc(token)) }