-
-
Notifications
You must be signed in to change notification settings - Fork 231
/
packageparser.go
49 lines (39 loc) · 1.21 KB
/
packageparser.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
package parser
import (
"io"
"github.com/a-h/lexical/parse"
)
// Package.
func newPackageParser() packageParser {
return packageParser{}
}
type packageParser struct {
}
var packageExpressionStartParser = createStartParser("package")
func (p packageParser) Parse(pi parse.Input) parse.Result {
// Check the prefix first.
prefixResult := packageExpressionStartParser(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(newLine, expressionEnd))(pi)
if pr.Error != nil && pr.Error != io.EOF {
return pr
}
// If there's no tag end, the package literal wasn't terminated.
if !pr.Success {
return parse.Failure("packageParser", newParseError("package literal not terminated", from, NewPositionFromInput(pi)))
}
// Success!
to := NewPositionFromInput(pi)
r := Package{
Expression: NewExpression(pr.Item.(string), from, to),
}
// Eat the tag end.
if te := expressionEnd(pi); !te.Success {
return parse.Failure("packageParser", newParseError("package literal not terminated", from, NewPositionFromInput(pi)))
}
return parse.Success("packageParser", r, nil)
}