-
-
Notifications
You must be signed in to change notification settings - Fork 274
/
switchexpressionparser.go
106 lines (85 loc) · 2.74 KB
/
switchexpressionparser.go
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
package parser
import (
"strings"
"github.com/a-h/parse"
)
var switchExpression parse.Parser[Node] = switchExpressionParser{}
type switchExpressionParser struct{}
func (_ switchExpressionParser) Parse(pi *parse.Input) (n Node, ok bool, err error) {
// Check the prefix first.
if _, ok, err = parse.String("switch ").Parse(pi); err != nil || !ok {
return
}
// Once we've got a prefix, read until {\n.
var r SwitchExpression
until := parse.All(openBraceWithOptionalPadding, parse.NewLine)
endOfStatementExpression := ExpressionOf(parse.StringUntil(until))
if r.Expression, ok, err = endOfStatementExpression.Parse(pi); err != nil || !ok {
err = parse.Error("switch: "+unterminatedMissingCurly, pi.Position())
return
}
// Eat " {\n".
if _, ok, err = until.Parse(pi); err != nil || !ok {
err = parse.Error("switch: "+unterminatedMissingCurly, pi.Position())
return
}
// Once we've had the start of a switch block, we must conclude the block.
// Read the optional 'case' nodes.
for {
var ce CaseExpression
ce, ok, err = caseExpressionParser.Parse(pi)
if err != nil {
return
}
if !ok {
break
}
r.Cases = append(r.Cases, ce)
}
// Read the required closing brace.
if _, ok, err = closeBraceWithOptionalPadding.Parse(pi); err != nil || !ok {
err = parse.Error("switch: "+unterminatedMissingEnd, pi.Position())
return
}
return r, true, nil
}
var caseExpressionStartParser = parse.Func(func(in *parse.Input) (e Expression, ok bool, err error) {
start := in.Index()
// Optional whitespace.
if _, _, err = parse.OptionalWhitespace.Parse(in); err != nil {
return
}
// Read the line.
if e, ok, err = ExpressionOf(parse.StringUntil(parse.String("\n"))).Parse(in); err != nil || !ok {
in.Seek(start)
return
}
// Check the expected results.
ok = (strings.HasPrefix(e.Value, "case ") || strings.HasPrefix(e.Value, "default"))
if !ok {
in.Seek(start)
return
}
// Eat terminating newline.
_, _, _ = parse.String("\n").Parse(in)
return
})
var caseExpressionParser = parse.Func(func(pi *parse.Input) (r CaseExpression, ok bool, err error) {
if r.Expression, ok, err = caseExpressionStartParser.Parse(pi); err != nil || !ok {
return
}
// Read until the next case statement, default, or end of the block.
pr := newTemplateNodeParser(parse.Any(StripType(closeBraceWithOptionalPadding), StripType(caseExpressionStartParser)), "closing brace or case expression")
var nodes Nodes
if nodes, ok, err = pr.Parse(pi); err != nil || !ok {
err = parse.Error("case: expected nodes, but none were found", pi.Position())
return
}
r.Children = nodes.Nodes
r.Diagnostics = nodes.Diagnostics
// Optional whitespace.
if _, ok, err = parse.OptionalWhitespace.Parse(pi); err != nil || !ok {
return
}
return r, true, nil
})