forked from osteele/liquid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
64 lines (53 loc) · 2.01 KB
/
token.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
package parser
import "fmt"
// A Token is an object {{ a.b }}, a tag {% if a>b %}, or a text chunk (anything outside of {{}} and {%%}.)
type Token struct {
Type TokenType
SourceLoc SourceLoc
Name string // Name is the tag name of a tag Chunk. E.g. the tag name of "{% if 1 %}" is "if".
Args string // Parameters is the tag arguments of a tag Chunk. E.g. the tag arguments of "{% if 1 %}" is "1".
Source string // Source is the entirety of the token, including the "{{", "{%", etc. markers.
TrimLeft, TrimRight bool // Trim whitespace left or right of this token; from {{- tag -}} and {%- expr -%}
}
// TokenType is the type of a Chunk
type TokenType int
////go:generate stringer -type=TokenType
const (
// TextTokenType is the type of a text Chunk
TextTokenType TokenType = iota
// TagTokenType is the type of a tag Chunk "{%…%}"
TagTokenType
// ObjTokenType is the type of an object Chunk "{{…}}"
ObjTokenType
)
// SourceLoc contains a Token's source location.
type SourceLoc struct {
Pathname string
LineNo int
}
// SourceLocation returns the token's source location, for use in error reporting.
func (c Token) SourceLocation() SourceLoc { return c.SourceLoc }
// SourceText returns the token's source text, for use in error reporting.
func (c Token) SourceText() string { return c.Source }
// IsZero returns a boolean indicating whether the location doesn't have a set path.
func (s SourceLoc) IsZero() bool {
return s.Pathname == "" && s.LineNo == 0
}
func (c Token) String() string {
switch c.Type {
case TextTokenType:
return fmt.Sprintf("%v{%#v}", c.Type, c.Source)
case TagTokenType:
return fmt.Sprintf("%v{Tag:%#v, Args:%#v}", c.Type, c.Name, c.Args)
case ObjTokenType:
return fmt.Sprintf("%v{%#v}", c.Type, c.Args)
default:
return fmt.Sprintf("%v{%#v}", c.Type, c.Source)
}
}
func (s SourceLoc) String() string {
if s.Pathname != "" {
return fmt.Sprintf("%s:%d", s.Pathname, s.LineNo)
}
return fmt.Sprintf("line %d", s.LineNo)
}