-
-
Notifications
You must be signed in to change notification settings - Fork 264
/
stringexpressionparser.go
50 lines (40 loc) · 1.45 KB
/
stringexpressionparser.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
package parser
import (
"fmt"
"io"
"github.com/a-h/lexical/parse"
)
// StringExpression.
func newStringExpressionParser() stringExpressionParser {
return stringExpressionParser{}
}
type stringExpressionParser struct {
}
var stringExpressionStartParser = parse.Or(parse.String("{%= "), parse.String("{%="))
func (p stringExpressionParser) Parse(pi parse.Input) parse.Result {
// Check the prefix first.
prefixResult := stringExpressionStartParser(pi)
if !prefixResult.Success {
return prefixResult
}
// Once we've seen a string expression prefix, read until the tag end.
from := NewPositionFromInput(pi)
pr := parse.StringUntil(expressionEnd)(pi)
if pr.Error != nil && pr.Error != io.EOF {
return parse.Failure("stringExpressionParser", fmt.Errorf("stringExpressionParser: failed to read until tag end: %w", pr.Error))
}
// If there's no tag end, the string expression parser wasn't terminated.
if !pr.Success {
return parse.Failure("stringExpressionParser", newParseError("string expression not terminated", from, NewPositionFromInput(pi)))
}
// Success! Create the expression.
to := NewPositionFromInput(pi)
r := StringExpression{
Expression: NewExpression(pr.Item.(string), from, to),
}
// Eat the tag end.
if te := expressionEnd(pi); !te.Success {
return parse.Failure("stringExpressionParser", newParseError("could not terminate string expression", from, NewPositionFromInput(pi)))
}
return parse.Success("stringExpressionParser", r, nil)
}