Commit bedfa4e1 authored by Daniel Theophanes's avatar Daniel Theophanes

text/template/parse: undo breaking API changes

golang.org/cl/84480 altered the API for the parse package for
clarity and consistency. However, the changes also broke the
API for consumers of the package. This CL reverts the API
to the previous spelling, adding only a single new exported
symbol.

Fixes #25968

Change-Id: Ieb81054b61eeac7df3bc3864ef446df43c26b80f
Reviewed-on: https://go-review.googlesource.com/120355Reviewed-by: default avatarDaniel Martí <mvdan@mvdan.cc>
Reviewed-by: default avatarRob Pike <r@golang.org>
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 4991bc62
...@@ -362,15 +362,6 @@ pkg syscall (openbsd-386-cgo), const SYS_KILL = 37 ...@@ -362,15 +362,6 @@ pkg syscall (openbsd-386-cgo), const SYS_KILL = 37
pkg syscall (openbsd-amd64), const SYS_KILL = 37 pkg syscall (openbsd-amd64), const SYS_KILL = 37
pkg syscall (openbsd-amd64-cgo), const SYS_KILL = 37 pkg syscall (openbsd-amd64-cgo), const SYS_KILL = 37
pkg unicode, const Version = "9.0.0" pkg unicode, const Version = "9.0.0"
pkg text/template/parse, method (*VariableNode) Copy() Node
pkg text/template/parse, method (*VariableNode) String() string
pkg text/template/parse, method (VariableNode) Position() Pos
pkg text/template/parse, method (VariableNode) Type() NodeType
pkg text/template/parse, type PipeNode struct, Decl []*VariableNode
pkg text/template/parse, type VariableNode struct
pkg text/template/parse, type VariableNode struct, Ident []string
pkg text/template/parse, type VariableNode struct, embedded NodeType
pkg text/template/parse, type VariableNode struct, embedded Pos
pkg syscall (windows-386), type CertChainPolicyPara struct, ExtraPolicyPara uintptr pkg syscall (windows-386), type CertChainPolicyPara struct, ExtraPolicyPara uintptr
pkg syscall (windows-386), type CertChainPolicyStatus struct, ExtraPolicyStatus uintptr pkg syscall (windows-386), type CertChainPolicyStatus struct, ExtraPolicyStatus uintptr
pkg syscall (windows-386), type CertContext struct, CertInfo uintptr pkg syscall (windows-386), type CertContext struct, CertInfo uintptr
......
...@@ -142,7 +142,7 @@ func (e *escaper) escape(c context, n parse.Node) context { ...@@ -142,7 +142,7 @@ func (e *escaper) escape(c context, n parse.Node) context {
// escapeAction escapes an action template node. // escapeAction escapes an action template node.
func (e *escaper) escapeAction(c context, n *parse.ActionNode) context { func (e *escaper) escapeAction(c context, n *parse.ActionNode) context {
if len(n.Pipe.Vars) != 0 { if len(n.Pipe.Decl) != 0 {
// A local variable assignment, not an interpolation. // A local variable assignment, not an interpolation.
return c return c
} }
......
...@@ -252,7 +252,7 @@ func (s *state) walk(dot reflect.Value, node parse.Node) { ...@@ -252,7 +252,7 @@ func (s *state) walk(dot reflect.Value, node parse.Node) {
// Do not pop variables so they persist until next end. // Do not pop variables so they persist until next end.
// Also, if the action declares variables, don't print the result. // Also, if the action declares variables, don't print the result.
val := s.evalPipeline(dot, node.Pipe) val := s.evalPipeline(dot, node.Pipe)
if len(node.Pipe.Vars) == 0 { if len(node.Pipe.Decl) == 0 {
s.printValue(node, val) s.printValue(node, val)
} }
case *parse.IfNode: case *parse.IfNode:
...@@ -339,11 +339,11 @@ func (s *state) walkRange(dot reflect.Value, r *parse.RangeNode) { ...@@ -339,11 +339,11 @@ func (s *state) walkRange(dot reflect.Value, r *parse.RangeNode) {
mark := s.mark() mark := s.mark()
oneIteration := func(index, elem reflect.Value) { oneIteration := func(index, elem reflect.Value) {
// Set top var (lexically the second if there are two) to the element. // Set top var (lexically the second if there are two) to the element.
if len(r.Pipe.Vars) > 0 { if len(r.Pipe.Decl) > 0 {
s.setTopVar(1, elem) s.setTopVar(1, elem)
} }
// Set next var (lexically the first if there are two) to the index. // Set next var (lexically the first if there are two) to the index.
if len(r.Pipe.Vars) > 1 { if len(r.Pipe.Decl) > 1 {
s.setTopVar(2, index) s.setTopVar(2, index)
} }
s.walk(elem, r.List) s.walk(elem, r.List)
...@@ -432,11 +432,11 @@ func (s *state) evalPipeline(dot reflect.Value, pipe *parse.PipeNode) (value ref ...@@ -432,11 +432,11 @@ func (s *state) evalPipeline(dot reflect.Value, pipe *parse.PipeNode) (value ref
value = reflect.ValueOf(value.Interface()) // lovely! value = reflect.ValueOf(value.Interface()) // lovely!
} }
} }
for _, variable := range pipe.Vars { for _, variable := range pipe.Decl {
if pipe.Decl { if pipe.IsAssign {
s.push(variable.Ident[0], value)
} else {
s.setVar(variable.Ident[0], value) s.setVar(variable.Ident[0], value)
} else {
s.push(variable.Ident[0], value)
} }
} }
return value return value
...@@ -461,7 +461,7 @@ func (s *state) evalCommand(dot reflect.Value, cmd *parse.CommandNode, final ref ...@@ -461,7 +461,7 @@ func (s *state) evalCommand(dot reflect.Value, cmd *parse.CommandNode, final ref
case *parse.PipeNode: case *parse.PipeNode:
// Parenthesized pipeline. The arguments are all inside the pipeline; final is ignored. // Parenthesized pipeline. The arguments are all inside the pipeline; final is ignored.
return s.evalPipeline(dot, n) return s.evalPipeline(dot, n)
case *parse.AssignNode: case *parse.VariableNode:
return s.evalVariableNode(dot, n, cmd.Args, final) return s.evalVariableNode(dot, n, cmd.Args, final)
} }
s.at(firstWord) s.at(firstWord)
...@@ -530,7 +530,7 @@ func (s *state) evalChainNode(dot reflect.Value, chain *parse.ChainNode, args [] ...@@ -530,7 +530,7 @@ func (s *state) evalChainNode(dot reflect.Value, chain *parse.ChainNode, args []
return s.evalFieldChain(dot, pipe, chain, chain.Field, args, final) return s.evalFieldChain(dot, pipe, chain, chain.Field, args, final)
} }
func (s *state) evalVariableNode(dot reflect.Value, variable *parse.AssignNode, args []parse.Node, final reflect.Value) reflect.Value { func (s *state) evalVariableNode(dot reflect.Value, variable *parse.VariableNode, args []parse.Node, final reflect.Value) reflect.Value {
// $x.Field has $x as the first ident, Field as the second. Eval the var, then the fields. // $x.Field has $x as the first ident, Field as the second. Eval the var, then the fields.
s.at(variable) s.at(variable)
value := s.varValue(variable.Ident[0]) value := s.varValue(variable.Ident[0])
...@@ -771,7 +771,7 @@ func (s *state) evalArg(dot reflect.Value, typ reflect.Type, n parse.Node) refle ...@@ -771,7 +771,7 @@ func (s *state) evalArg(dot reflect.Value, typ reflect.Type, n parse.Node) refle
s.errorf("cannot assign nil to %s", typ) s.errorf("cannot assign nil to %s", typ)
case *parse.FieldNode: case *parse.FieldNode:
return s.validateType(s.evalFieldNode(dot, arg, []parse.Node{n}, missingVal), typ) return s.validateType(s.evalFieldNode(dot, arg, []parse.Node{n}, missingVal), typ)
case *parse.AssignNode: case *parse.VariableNode:
return s.validateType(s.evalVariableNode(dot, arg, nil, missingVal), typ) return s.validateType(s.evalVariableNode(dot, arg, nil, missingVal), typ)
case *parse.PipeNode: case *parse.PipeNode:
return s.validateType(s.evalPipeline(dot, arg), typ) return s.validateType(s.evalPipeline(dot, arg), typ)
...@@ -889,7 +889,7 @@ func (s *state) evalEmptyInterface(dot reflect.Value, n parse.Node) reflect.Valu ...@@ -889,7 +889,7 @@ func (s *state) evalEmptyInterface(dot reflect.Value, n parse.Node) reflect.Valu
return s.idealConstant(n) return s.idealConstant(n)
case *parse.StringNode: case *parse.StringNode:
return reflect.ValueOf(n.Text) return reflect.ValueOf(n.Text)
case *parse.AssignNode: case *parse.VariableNode:
return s.evalVariableNode(dot, n, nil, missingVal) return s.evalVariableNode(dot, n, nil, missingVal)
case *parse.PipeNode: case *parse.PipeNode:
return s.evalPipeline(dot, n) return s.evalPipeline(dot, n)
......
...@@ -144,15 +144,15 @@ func (t *TextNode) Copy() Node { ...@@ -144,15 +144,15 @@ func (t *TextNode) Copy() Node {
type PipeNode struct { type PipeNode struct {
NodeType NodeType
Pos Pos
tr *Tree tr *Tree
Line int // The line number in the input. Deprecated: Kept for compatibility. Line int // The line number in the input. Deprecated: Kept for compatibility.
Decl bool // The variables are being declared, not assigned IsAssign bool // The variables are being assigned, not declared.
Vars []*AssignNode // Variables in lexical order. Decl []*VariableNode // Variables in lexical order.
Cmds []*CommandNode // The commands in lexical order. Cmds []*CommandNode // The commands in lexical order.
} }
func (t *Tree) newPipeline(pos Pos, line int, vars []*AssignNode) *PipeNode { func (t *Tree) newPipeline(pos Pos, line int, vars []*VariableNode) *PipeNode {
return &PipeNode{tr: t, NodeType: NodePipe, Pos: pos, Line: line, Vars: vars} return &PipeNode{tr: t, NodeType: NodePipe, Pos: pos, Line: line, Decl: vars}
} }
func (p *PipeNode) append(command *CommandNode) { func (p *PipeNode) append(command *CommandNode) {
...@@ -161,8 +161,8 @@ func (p *PipeNode) append(command *CommandNode) { ...@@ -161,8 +161,8 @@ func (p *PipeNode) append(command *CommandNode) {
func (p *PipeNode) String() string { func (p *PipeNode) String() string {
s := "" s := ""
if len(p.Vars) > 0 { if len(p.Decl) > 0 {
for i, v := range p.Vars { for i, v := range p.Decl {
if i > 0 { if i > 0 {
s += ", " s += ", "
} }
...@@ -187,12 +187,12 @@ func (p *PipeNode) CopyPipe() *PipeNode { ...@@ -187,12 +187,12 @@ func (p *PipeNode) CopyPipe() *PipeNode {
if p == nil { if p == nil {
return p return p
} }
var vars []*AssignNode var vars []*VariableNode
for _, d := range p.Vars { for _, d := range p.Decl {
vars = append(vars, d.Copy().(*AssignNode)) vars = append(vars, d.Copy().(*VariableNode))
} }
n := p.tr.newPipeline(p.Pos, p.Line, vars) n := p.tr.newPipeline(p.Pos, p.Line, vars)
n.Decl = p.Decl n.IsAssign = p.IsAssign
for _, c := range p.Cmds { for _, c := range p.Cmds {
n.append(c.Copy().(*CommandNode)) n.append(c.Copy().(*CommandNode))
} }
...@@ -321,18 +321,18 @@ func (i *IdentifierNode) Copy() Node { ...@@ -321,18 +321,18 @@ func (i *IdentifierNode) Copy() Node {
// AssignNode holds a list of variable names, possibly with chained field // AssignNode holds a list of variable names, possibly with chained field
// accesses. The dollar sign is part of the (first) name. // accesses. The dollar sign is part of the (first) name.
type AssignNode struct { type VariableNode struct {
NodeType NodeType
Pos Pos
tr *Tree tr *Tree
Ident []string // Variable name and fields in lexical order. Ident []string // Variable name and fields in lexical order.
} }
func (t *Tree) newVariable(pos Pos, ident string) *AssignNode { func (t *Tree) newVariable(pos Pos, ident string) *VariableNode {
return &AssignNode{tr: t, NodeType: NodeVariable, Pos: pos, Ident: strings.Split(ident, ".")} return &VariableNode{tr: t, NodeType: NodeVariable, Pos: pos, Ident: strings.Split(ident, ".")}
} }
func (v *AssignNode) String() string { func (v *VariableNode) String() string {
s := "" s := ""
for i, id := range v.Ident { for i, id := range v.Ident {
if i > 0 { if i > 0 {
...@@ -343,12 +343,12 @@ func (v *AssignNode) String() string { ...@@ -343,12 +343,12 @@ func (v *AssignNode) String() string {
return s return s
} }
func (v *AssignNode) tree() *Tree { func (v *VariableNode) tree() *Tree {
return v.tr return v.tr
} }
func (v *AssignNode) Copy() Node { func (v *VariableNode) Copy() Node {
return &AssignNode{tr: v.tr, NodeType: NodeVariable, Pos: v.Pos, Ident: append([]string{}, v.Ident...)} return &VariableNode{tr: v.tr, NodeType: NodeVariable, Pos: v.Pos, Ident: append([]string{}, v.Ident...)}
} }
// DotNode holds the special identifier '.'. // DotNode holds the special identifier '.'.
......
...@@ -384,7 +384,7 @@ func (t *Tree) action() (n Node) { ...@@ -384,7 +384,7 @@ func (t *Tree) action() (n Node) {
// declarations? command ('|' command)* // declarations? command ('|' command)*
func (t *Tree) pipeline(context string) (pipe *PipeNode) { func (t *Tree) pipeline(context string) (pipe *PipeNode) {
decl := false decl := false
var vars []*AssignNode var vars []*VariableNode
token := t.peekNonSpace() token := t.peekNonSpace()
pos := token.pos pos := token.pos
// Are there declarations or assignments? // Are there declarations or assignments?
...@@ -422,7 +422,7 @@ func (t *Tree) pipeline(context string) (pipe *PipeNode) { ...@@ -422,7 +422,7 @@ func (t *Tree) pipeline(context string) (pipe *PipeNode) {
break break
} }
pipe = t.newPipeline(pos, token.line, vars) pipe = t.newPipeline(pos, token.line, vars)
pipe.Decl = decl pipe.IsAssign = !decl
for { for {
switch token := t.nextNonSpace(); token.typ { switch token := t.nextNonSpace(); token.typ {
case itemRightDelim, itemRightParen: case itemRightDelim, itemRightParen:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment