mirror of
https://github.com/danog/liquid.git
synced 2024-12-02 17:28:38 +01:00
53 lines
1.4 KiB
Go
53 lines
1.4 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
|
|
SourceInfo SourceInfo
|
|
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
|
|
)
|
|
|
|
// SourceInfo contains a Chunk's source information
|
|
type SourceInfo struct {
|
|
Pathname string
|
|
lineNo int
|
|
}
|
|
|
|
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 SourceInfo) String() string {
|
|
if s.Pathname != "" {
|
|
return fmt.Sprintf("%s:%d", s.Pathname, s.lineNo)
|
|
}
|
|
return fmt.Sprintf("line %d", s.lineNo)
|
|
}
|