- Notifications
You must be signed in to change notification settings - Fork 150
/
Copy patherror-patterns.boo
242 lines (190 loc) · 6.12 KB
/
error-patterns.boo
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
"""
Defines a set of syntax examples producing *parsing errors*, recording the
generated parser exception and creating an ErrorPattern with a customized
error message.
Once these patterns are injected in the parser, when a similar error occurs
it will use the customized error message.
Use the -test switch to test the raw error generated by the parser for the
given code.
$ booi scripts/error-patterns.boo -test
(1, (2 <<< press ctrl+d >>>
The -debug switch allows to parse the given snippet and output the first
syntax error found after applying all the patterns defined.
"""
# If it gets difficult to write specific rules, perhaps we can include
# some additional matchers that take into account the tokens in the scanner
# buffer for example.
# For more advanced stuff we may even inspect the partially generated AST
# at the time of the error.
#
from System import Console, ConsoleColor
from System.IO import StringReader
from Boo.Lang.Parser import*
from Boo.Lang.Compiler.Ast import CompileUnit
from System.Collections.Generic import Stack, HashSet
from Boo.Lang.Compiler import Ast
importantlrfrom'Boo.Lang.Parser.dll'
classGeneratorParser(BooParser):
""" Specialized parser for capturing and validating errors examples
"""
Message asstring
CapturedPattern as ErrorPattern
IsValidating =false
Ignore =false
defconstructor(message as string, code as string):
super(CreateBooLexer(4, '<generator>', StringReader(code)))
Message =message
defCapture(patterns):
ErrorPatterns =patterns
start(CompileUnit())
return CapturedPattern
defValidate(patterns):
ErrorPatterns =patterns
IsValidating =true
cached_error as RecognitionException=null
Error =def (ex as RecognitionException):
cached_error=ex
start(CompileUnit())
returncached_error
override defreportError(ex as RecognitionException, rulename as string):
# Skip any other errors produced after the first one
returnif Ignore
Ignore =true
if IsValidating:
super(ex, rulename)
return
ifmismatch=ex as MismatchedTokenException:
CapturedPattern = MismatchedErrorPattern(Message, rulename, mismatch.expecting)
elifnoviable=ex as NoViableAltException:
CapturedPattern = NoViableAltErrorPattern(Message, rulename, noviable.token.Type)
else:
CapturedPattern = RecognitionErrorPattern(Message, rulename)
classAnyErrorPattern(ErrorPattern):
property Exception as RecognitionException
defconstructor():
super(null, null)
override defMatches(rule as string, ex as RecognitionException):
Rule =rule
Exception =ex
returntrue
PATTERNS = List[of ErrorPattern]()
deferror(msg as string, code as string):
indent=' '*8
fortestcodein /^-{3,}/m.Split(code):
testcode=testcode.Trim()
lines=testcode.Split(char('\n'))
comment=join(lines, '\n'+indent+' // ')
# Build error pattern from example code
pattern= GeneratorParser(msg, testcode).Capture(null)
ifnotpattern:
raise"ERROR: The following snippet did not produce any errors!\n$testcode"
PATTERNS.Add(pattern)
# Validate the pattern when actually used with the others
ex= GeneratorParser(msg, testcode).Validate(PATTERNS.ToArray())
ifnotex:
raise"ERROR: Validation for the following snippet did not produce any errors!\n$testcode"
ifmsg!=ex.Message:
Console.ForegroundColor = ConsoleColor.Yellow
Console.Error.WriteLine("expected: $msg")
Console.Error.WriteLine("produced: $(ex.Message)")
Console.Error.WriteLine(' '+testcode.Replace("\n", "\n "))
Console.Error.WriteLine()
Console.ResetColor()
iflen(argv) andargv[0] =='-test':
code=''
whilenull!= (ln= Console.ReadLine()):
code+=ln+"\n"
any= AnyErrorPattern()
ex= GeneratorParser('<test>', code).Validate(List[of ErrorPattern]() { any })
ifnotex:
print"No error generated from the snippet"
return
print'Rule:', any.Rule
print'Exception:', any.Exception
return
#####################################################################
## Error examples. Order them to disambiguate
#####################################################################
error"Unbalanced expression, closing paren not found", """
a = (bar, baz
def foo():
pass
---
a = (bar, baz
---
foo(
---
foo(bar, baz
qux
---
class Foo(Bar:
pass
"""
error"Unbalanced expression, opening paren not found", """
foo)
---
foo = a, b)
---
def fn():
foo)
"""
error"Either separate expressions with commas or make sure your parens are balanced", """
foo(bar baz, qux)
"""
error"Expressions must be separated by commas", """
foo(bar baz)
---
a = foo bar
"""
# error "Expressions must be separated by commas", """
# print foo bar
# """
error"Block must be indented", """
def foo():
return
---
block:
return
---
while true:
return
---
class Foo
pass
---
class Foo(Bar)
pass
"""
#####################################################################
## Code generation
#####################################################################
iflen(argv) andargv[0] =='-debug':
code=''
whilenull!= (ln= Console.ReadLine()):
code+=ln+"\n"
ex= GeneratorParser('<debug>', code).Validate(PATTERNS)
ifnotex:
print"No error generated from the snippet"
return
printex
return
print"""
// DO NOT EDIT! File automatically generated from parser error examples
namespace Boo.Lang.Parser
{
static class GeneratedErrorPatterns
{
public static readonly ErrorPattern[] Patterns = {
"""
level=3
defindent(*partsas (string)):
ind=' '*level
text=ind+join(parts, '').Replace('\n', '\n'+ind)
printtext
fori as int, pattern as ErrorPatterninenumerate(PATTERNS):
indentpattern.ToCodeString(), (',\n'ifi<len(PATTERNS) -1else'')
print"""
};
}
}
"""