-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
importparser.go
45 lines (36 loc) · 1.17 KB
/
importparser.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
package parser
import (
"io"
"github.com/a-h/lexical/parse"
)
// Import.
func newImportParser() importParser {
return importParser{}
}
type importParser struct {
}
var importExpressionStartParser = createStartParser("import")
func (p importParser) Parse(pi parse.Input) parse.Result {
var r Import
// Check the prefix first.
prefixResult := importExpressionStartParser(pi)
if !prefixResult.Success {
return prefixResult
}
// Once we have the prefix, we must have an expression and tag end on the same line.
from := NewPositionFromInput(pi)
pr := parse.StringUntil(parse.Or(expressionEnd, newLine))(pi)
if pr.Error != nil && pr.Error != io.EOF {
return pr
}
// If there's no tag end, the import literal wasn't terminated.
if !pr.Success {
return parse.Failure("importParser", newParseError("import: not terminated", from, NewPositionFromInput(pi)))
}
r.Expression = NewExpression(pr.Item.(string), from, NewPositionFromInput(pi))
// Eat the tag end.
if te := expressionEnd(pi); !te.Success {
return parse.Failure("importParser", newParseError("import: missing end of block (' %}')", from, NewPositionFromInput(pi)))
}
return parse.Success("importParser", r, nil)
}