-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseResult.nim
63 lines (52 loc) · 2.29 KB
/
parseResult.nim
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
from errors import nil
from nodes import nil
from lexer import init_pos, no_error
type
ParseResult* = ref object of RootObj
error*: errors.Error
node*: nodes.Node
advance_count*: int
to_reverse_count*: int
last_registered_advance_count*: int
internal_else_case*: (nodes.Node, bool)
if_cases*: (seq[(nodes.Node, nodes.Node, bool)], (nodes.Node, bool))
proc register_advancement*(this: ParseResult) =
this.advance_count += 1
proc register*(this: ParseResult, res: ParseResult) : nodes.Node =
this.last_registered_advance_count = res.advance_count
this.advance_count += res.advance_count
if res.error.name != "NoError": this.error = res.error
return res.node
proc register_else_case*(this: ParseResult, res: ParseResult) : (nodes.Node, bool) =
this.last_registered_advance_count = res.advance_count
this.advance_count += res.advance_count
if res.error.name != "NoError": this.error = res.error
return res.internal_else_case
proc register_if_cases*(this: ParseResult, res: ParseResult) : (seq[(nodes.Node, nodes.Node, bool)], (nodes.Node, bool)) =
this.last_registered_advance_count = res.advance_count
this.advance_count += res.advance_count
if res.error.name != "NoError": this.error = res.error
return res.if_cases
proc try_register*(this: ParseResult, res : ParseResult) : nodes.Node =
if (res.error.name != "NoError"):
this.to_reverse_count = res.advance_count
return nodes.emptyNode
else:
return this.register(res)
proc success*(this: ParseResult, node: nodes.Node) : ParseResult =
this.node = node
return this
proc success_else_case*(this: ParseResult, else_case: (nodes.Node, bool)) : ParseResult =
this.internal_else_case = else_case
return this
proc success_if_cases*(this: ParseResult, if_cases: (seq[(nodes.Node, nodes.Node, bool)], (nodes.Node, bool))) : ParseResult =
this.if_cases = if_cases
return this
proc failure*(this: ParseResult, error: errors.Error) : ParseResult =
if (this.error.name == "NoError") or (this.advance_count == 0):
this.error = error
return this
proc newParseResult*(): ParseResult =
ParseResult(error: no_error, node: nodes.emptyNode, advance_count: 0, to_reverse_count: 0, last_registered_advance_count: 0,
internal_else_case: (nodes.emptyNode, false), if_cases:(@[], (nodes.emptyNode, false))
)