1
0
mirror of https://github.com/danog/liquid.git synced 2024-12-02 16:27:45 +01:00
liquid/parser/token.go
2017-07-10 11:49:14 -04:00

59 lines
1.7 KiB
Go

package parser
import "fmt"
// A Token is either 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.
}
// 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 }
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)
}