itemEOF
itemField // alphanumeric identifier, starting with '.', possibly chained ('.x.y')
itemIdentifier // alphanumeric identifier
- itemLeftMeta // left meta-string
+ itemLeftDelim // left action delimiter
itemNumber // simple number, including imaginary
itemPipe // pipe symbol
itemRawString // raw quoted string (includes quotes)
- itemRightMeta // right meta-string
+ itemRightDelim // right action delimiter
itemString // quoted string (includes quotes)
itemText // plain text
// Keywords appear after all the rest.
itemEOF: "EOF",
itemField: "field",
itemIdentifier: "identifier",
- itemLeftMeta: "left meta",
+ itemLeftDelim: "left delim",
itemNumber: "number",
itemPipe: "pipe",
itemRawString: "raw string",
- itemRightMeta: "rightMeta",
+ itemRightDelim: "right delim",
itemString: "string",
// keywords
itemDot: ".",
// state functions
const (
- leftMeta = "{{"
- rightMeta = "}}"
+ leftDelim = "{{"
+ rightDelim = "}}"
leftComment = "{{/*"
rightComment = "*/}}"
)
-// lexText scans until a metacharacter
+// lexText scans until an opening action delimiter, "{{".
func lexText(l *lexer) stateFn {
for {
- if strings.HasPrefix(l.input[l.pos:], leftMeta) {
+ if strings.HasPrefix(l.input[l.pos:], leftDelim) {
if l.pos > l.start {
l.emit(itemText)
}
- return lexLeftMeta
+ return lexLeftDelim
}
if l.next() == eof {
break
return nil
}
-// leftMeta scans the left "metacharacter", which is known to be present.
-func lexLeftMeta(l *lexer) stateFn {
+// lexLeftDelim scans the left delimiter, which is known to be present.
+func lexLeftDelim(l *lexer) stateFn {
if strings.HasPrefix(l.input[l.pos:], leftComment) {
return lexComment
}
- l.pos += len(leftMeta)
- l.emit(itemLeftMeta)
+ l.pos += len(leftDelim)
+ l.emit(itemLeftDelim)
return lexInsideAction
}
return lexText
}
-// rightMeta scans the right "metacharacter", which is known to be present.
-func lexRightMeta(l *lexer) stateFn {
- l.pos += len(rightMeta)
- l.emit(itemRightMeta)
+// lexRightDelim scans the right delimiter, which is known to be present.
+func lexRightDelim(l *lexer) stateFn {
+ l.pos += len(rightDelim)
+ l.emit(itemRightDelim)
return lexText
}
-// lexInsideAction scans the elements inside "metacharacters".
+// lexInsideAction scans the elements inside action delimiters.
func lexInsideAction(l *lexer) stateFn {
// Either number, quoted string, or identifier.
// Spaces separate and are ignored.
// Pipe symbols separate and are emitted.
for {
- if strings.HasPrefix(l.input[l.pos:], rightMeta) {
- return lexRightMeta
+ if strings.HasPrefix(l.input[l.pos:], rightDelim) {
+ return lexRightDelim
}
switch r := l.next(); {
case r == eof || r == '\n':
var (
tEOF = item{itemEOF, ""}
- tLeft = item{itemLeftMeta, "{{"}
- tRight = item{itemRightMeta, "}}"}
+ tLeft = item{itemLeftDelim, "{{"}
+ tRight = item{itemRightDelim, "}}"}
tRange = item{itemRange, "range"}
tPipe = item{itemPipe, "|"}
tFor = item{itemIdentifier, "for"}
return fmt.Sprintf("(text: %q)", t.text)
}
-// actionNode holds an action (something bounded by metacharacters).
+// actionNode holds an action (something bounded by delimiters).
type actionNode struct {
nodeType
line int
switch token := t.next(); token.typ {
case itemText:
return newText(token.val)
- case itemLeftMeta:
+ case itemLeftDelim:
return t.action()
default:
t.unexpected(token, "input")
// Action:
// control
// command ("|" command)*
-// Left meta is past. Now get actions.
+// Left delim is past. Now get actions.
// First word could be a keyword such as range.
func (t *Template) action() (n node) {
switch token := t.next(); token.typ {
func (t *Template) pipeline(context string) (pipe []*commandNode) {
for {
switch token := t.next(); token.typ {
- case itemRightMeta:
+ case itemRightDelim:
if len(pipe) == 0 {
t.errorf("missing value for %s", context)
}
// {{end}}
// End keyword is past.
func (t *Template) endControl() node {
- t.expect(itemRightMeta, "end")
+ t.expect(itemRightDelim, "end")
return newEnd()
}
// {{else}}
// Else keyword is past.
func (t *Template) elseControl() node {
- t.expect(itemRightMeta, "else")
+ t.expect(itemRightDelim, "else")
return newElse(t.lex.lineNumber())
}
}
// command:
-// space-separated arguments up to a pipeline character or right metacharacter.
-// we consume the pipe character but leave the right meta to terminate the action.
+// space-separated arguments up to a pipeline character or right delimiter.
+// we consume the pipe character but leave the right delim to terminate the action.
func (t *Template) command() *commandNode {
cmd := newCommand()
Loop:
for {
switch token := t.next(); token.typ {
- case itemRightMeta:
+ case itemRightDelim:
t.backup()
break Loop
case itemPipe:
`[(text: " \t\n")]`},
{"text", "some text", noError,
`[(text: "some text")]`},
- {"emptyMeta", "{{}}", hasError,
+ {"emptyAction", "{{}}", hasError,
`[(action: [])]`},
{"field", "{{.X}}", noError,
`[(action: [(command: [F=[X]])])]`},
if t.atEOF() {
return
}
- t.expect(itemLeftMeta, context)
+ t.expect(itemLeftDelim, context)
t.expect(itemDefine, context)
name := t.expect(itemString, context)
t.name, err = strconv.Unquote(name.val)
if err != nil {
t.error(err)
}
- t.expect(itemRightMeta, context)
+ t.expect(itemRightDelim, context)
end := t.parse(false)
if end == nil {
t.errorf("unexpected EOF in %s", context)