-
-
Notifications
You must be signed in to change notification settings - Fork 238
/
parser.go
100 lines (81 loc) · 2.37 KB
/
parser.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
package parser
import (
"fmt"
"io"
"unicode"
"github.com/a-h/lexical/parse"
)
// Constants.
// {
var expressionEnd = parse.Or(parse.String(" {"), parse.String("{"))
// ) {
var expressionFuncEnd = parse.All(asNil, parse.Rune(')'), expressionEnd)
func asNil(inputs []interface{}) (interface{}, bool) {
return nil, true
}
var newLine = parse.Or(parse.String("\r\n"), parse.Rune('\n'))
// Whitespace.
func asWhitespace(parts []interface{}) (result interface{}, ok bool) {
var w Whitespace
for _, ip := range parts {
w.Value += string(ip.(rune))
}
return w, true
}
var whitespaceParser = parse.AtLeast(asWhitespace, 1, parse.RuneInRanges(unicode.White_Space))
var optionalWhitespaceParser = parse.AtLeast(asWhitespace, 0, parse.RuneInRanges(unicode.White_Space))
var optionalWhitespaceAsString = parse.AtLeast(parse.WithStringConcatCombiner, 0, parse.RuneInRanges(unicode.White_Space))
// Template
var template templateParser
type templateParser struct {
}
func (p templateParser) Parse(pi parse.Input) parse.Result {
var r HTMLTemplate
// templ FuncName(p Person, other Other) {
tepr := newTemplateExpressionParser().Parse(pi)
if !tepr.Success {
return tepr
}
te := tepr.Item.(templateExpression)
r.Expression = te.Expression
// Once we're in a template, we should expect some template whitespace, if/switch/for,
// or node string expressions etc.
from := NewPositionFromInput(pi)
tnpr := newTemplateNodeParser(closeBraceWithOptionalPadding, "closing brace").Parse(pi)
if tnpr.Error != nil && tnpr.Error != io.EOF {
return tnpr
}
// If there's no match, there's no template elements.
if !tnpr.Success {
return parse.Failure("templateParser", newParseError("templ: expected nodes in templ body, but found none", from, NewPositionFromInput(pi)))
}
r.Children = tnpr.Item.([]Node)
// Eat any whitespace.
pr := optionalWhitespaceParser(pi)
if pr.Error != nil {
return pr
}
// Try for }
pr, ok := chompBrace(pi)
if !ok {
return pr
}
return parse.Success("templ", r, nil)
}
// Parse error.
func newParseError(msg string, from Position, to Position) ParseError {
return ParseError{
Message: msg,
From: from,
To: to,
}
}
// ParseError details where the error occurred in the file.
type ParseError struct {
Message string
From Position
To Position
}
func (pe ParseError) Error() string {
return fmt.Sprintf("%v at %v", pe.Message, pe.From)
}