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 Error handling (generating an error while parsing).
32
33 This is not that complete or well thought through; it needs to be revised.
34 '''
35
36 from lepl.support.node import Node
37 from lepl.support.lib import fmt
38 from lepl.stream.core import s_kargs
39
40
42 '''
43 Create an error node using a fmt string.
44
45 Invoke as ``** make_error('bad results: {results}')``, for example.
46 '''
47 def fun(stream_in, stream_out, results):
48 '''
49 Create the error node when results are available.
50 '''
51 kargs = syntax_error_kargs(stream_in, stream_out, results)
52 return Error(fmt(msg, **kargs), kargs)
53 return fun
54
55
57 '''
58 Helper function for constructing fmt dictionary.
59 '''
60 kargs = s_kargs(stream_in, prefix='in_')
61 kargs = s_kargs(stream_out, prefix='out_', kargs=kargs)
62 kargs['results'] = results
63 return kargs
64
65
67 '''
68 As `make_error()`, but also raise the result.
69 '''
70 def fun(stream_in, stream_out, results):
71 '''
72 Delay raising the error until called in the parser.
73 '''
74 raise make_error(msg)(stream_in, stream_out, results)
75 return fun
76
77
78 -class Error(Node, SyntaxError):
79 '''
80 Subclass `Node` and Python's SyntaxError to provide an AST
81 node that can be raised as an error via `node_throw` or `sexpr_throw`.
82
83 Create with `make_error()`.
84 '''
85
87
88 Node.__init__(self, msg, kargs)
89 if 'in_all' in kargs:
90 SyntaxError.__init__(self, msg,
91 (kargs.get('in_filename', ''),
92 int(kargs.get('in_lineno', 0)),
93 int(kargs.get('in_char', 0)),
94 kargs.get('in_all')))
95 else:
96 SyntaxError.__init__(self, msg,
97 (kargs.get('in_filename', ''),
98 int(kargs.get('in_lineno', -1)),
99 int(kargs.get('in_offset', 1)),
100 kargs.get('in_rest', '')))
101
103 return SyntaxError.__str__(self)
104