1// Copyright 2011 The Go Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style 3// license that can be found in the LICENSE file. 4 5// Parse nodes. 6 7package parse 8 9import ( 10 "bytes" 11 "fmt" 12 "strconv" 13 "strings" 14) 15 16// A Node is an element in the parse tree. The interface is trivial. 17// The interface contains an unexported method so that only 18// types local to this package can satisfy it. 19type Node interface { 20 Type() NodeType 21 String() string 22 // Copy does a deep copy of the Node and all its components. 23 // To avoid type assertions, some XxxNodes also have specialized 24 // CopyXxx methods that return *XxxNode. 25 Copy() Node 26 Position() Pos // byte position of start of node in full original input string 27 // Make sure only functions in this package can create Nodes. 28 unexported() 29} 30 31// NodeType identifies the type of a parse tree node. 32type NodeType int 33 34// Pos represents a byte position in the original input text from which 35// this template was parsed. 36type Pos int 37 38func (p Pos) Position() Pos { 39 return p 40} 41 42// unexported keeps Node implementations local to the package. 43// All implementations embed Pos, so this takes care of it. 44func (Pos) unexported() { 45} 46 47// Type returns itself and provides an easy default implementation 48// for embedding in a Node. Embedded in all non-trivial Nodes. 49func (t NodeType) Type() NodeType { 50 return t 51} 52 53const ( 54 NodeText NodeType = iota // Plain text. 55 NodeAction // A non-control action such as a field evaluation. 56 NodeBool // A boolean constant. 57 NodeChain // A sequence of field accesses. 58 NodeCommand // An element of a pipeline. 59 NodeDot // The cursor, dot. 60 nodeElse // An else action. Not added to tree. 61 nodeEnd // An end action. Not added to tree. 62 NodeField // A field or method name. 63 NodeIdentifier // An identifier; always a function name. 64 NodeIf // An if action. 65 NodeList // A list of Nodes. 66 NodeNil // An untyped nil constant. 67 NodeNumber // A numerical constant. 68 NodePipe // A pipeline of commands. 69 NodeRange // A range action. 70 NodeString // A string constant. 71 NodeTemplate // A template invocation action. 72 NodeVariable // A $ variable. 73 NodeWith // A with action. 74) 75 76// Nodes. 77 78// ListNode holds a sequence of nodes. 79type ListNode struct { 80 NodeType 81 Pos 82 Nodes []Node // The element nodes in lexical order. 83} 84 85func newList(pos Pos) *ListNode { 86 return &ListNode{NodeType: NodeList, Pos: pos} 87} 88 89func (l *ListNode) append(n Node) { 90 l.Nodes = append(l.Nodes, n) 91} 92 93func (l *ListNode) String() string { 94 b := new(bytes.Buffer) 95 for _, n := range l.Nodes { 96 fmt.Fprint(b, n) 97 } 98 return b.String() 99} 100 101func (l *ListNode) CopyList() *ListNode { 102 if l == nil { 103 return l 104 } 105 n := newList(l.Pos) 106 for _, elem := range l.Nodes { 107 n.append(elem.Copy()) 108 } 109 return n 110} 111 112func (l *ListNode) Copy() Node { 113 return l.CopyList() 114} 115 116// TextNode holds plain text. 117type TextNode struct { 118 NodeType 119 Pos 120 Text []byte // The text; may span newlines. 121} 122 123func newText(pos Pos, text string) *TextNode { 124 return &TextNode{NodeType: NodeText, Pos: pos, Text: []byte(text)} 125} 126 127func (t *TextNode) String() string { 128 return fmt.Sprintf("%q", t.Text) 129} 130 131func (t *TextNode) Copy() Node { 132 return &TextNode{NodeType: NodeText, Text: append([]byte{}, t.Text...)} 133} 134 135// PipeNode holds a pipeline with optional declaration 136type PipeNode struct { 137 NodeType 138 Pos 139 Line int // The line number in the input (deprecated; kept for compatibility) 140 Decl []*VariableNode // Variable declarations in lexical order. 141 Cmds []*CommandNode // The commands in lexical order. 142} 143 144func newPipeline(pos Pos, line int, decl []*VariableNode) *PipeNode { 145 return &PipeNode{NodeType: NodePipe, Pos: pos, Line: line, Decl: decl} 146} 147 148func (p *PipeNode) append(command *CommandNode) { 149 p.Cmds = append(p.Cmds, command) 150} 151 152func (p *PipeNode) String() string { 153 s := "" 154 if len(p.Decl) > 0 { 155 for i, v := range p.Decl { 156 if i > 0 { 157 s += ", " 158 } 159 s += v.String() 160 } 161 s += " := " 162 } 163 for i, c := range p.Cmds { 164 if i > 0 { 165 s += " | " 166 } 167 s += c.String() 168 } 169 return s 170} 171 172func (p *PipeNode) CopyPipe() *PipeNode { 173 if p == nil { 174 return p 175 } 176 var decl []*VariableNode 177 for _, d := range p.Decl { 178 decl = append(decl, d.Copy().(*VariableNode)) 179 } 180 n := newPipeline(p.Pos, p.Line, decl) 181 for _, c := range p.Cmds { 182 n.append(c.Copy().(*CommandNode)) 183 } 184 return n 185} 186 187func (p *PipeNode) Copy() Node { 188 return p.CopyPipe() 189} 190 191// ActionNode holds an action (something bounded by delimiters). 192// Control actions have their own nodes; ActionNode represents simple 193// ones such as field evaluations and parenthesized pipelines. 194type ActionNode struct { 195 NodeType 196 Pos 197 Line int // The line number in the input (deprecated; kept for compatibility) 198 Pipe *PipeNode // The pipeline in the action. 199} 200 201func newAction(pos Pos, line int, pipe *PipeNode) *ActionNode { 202 return &ActionNode{NodeType: NodeAction, Pos: pos, Line: line, Pipe: pipe} 203} 204 205func (a *ActionNode) String() string { 206 return fmt.Sprintf("{{%s}}", a.Pipe) 207 208} 209 210func (a *ActionNode) Copy() Node { 211 return newAction(a.Pos, a.Line, a.Pipe.CopyPipe()) 212 213} 214 215// CommandNode holds a command (a pipeline inside an evaluating action). 216type CommandNode struct { 217 NodeType 218 Pos 219 Args []Node // Arguments in lexical order: Identifier, field, or constant. 220} 221 222func newCommand(pos Pos) *CommandNode { 223 return &CommandNode{NodeType: NodeCommand, Pos: pos} 224} 225 226func (c *CommandNode) append(arg Node) { 227 c.Args = append(c.Args, arg) 228} 229 230func (c *CommandNode) String() string { 231 s := "" 232 for i, arg := range c.Args { 233 if i > 0 { 234 s += " " 235 } 236 if arg, ok := arg.(*PipeNode); ok { 237 s += "(" + arg.String() + ")" 238 continue 239 } 240 s += arg.String() 241 } 242 return s 243} 244 245func (c *CommandNode) Copy() Node { 246 if c == nil { 247 return c 248 } 249 n := newCommand(c.Pos) 250 for _, c := range c.Args { 251 n.append(c.Copy()) 252 } 253 return n 254} 255 256// IdentifierNode holds an identifier. 257type IdentifierNode struct { 258 NodeType 259 Pos 260 Ident string // The identifier's name. 261} 262 263// NewIdentifier returns a new IdentifierNode with the given identifier name. 264func NewIdentifier(ident string) *IdentifierNode { 265 return &IdentifierNode{NodeType: NodeIdentifier, Ident: ident} 266} 267 268// SetPos sets the position. NewIdentifier is a public method so we can't modify its signature. 269// Chained for convenience. 270// TODO: fix one day? 271func (i *IdentifierNode) SetPos(pos Pos) *IdentifierNode { 272 i.Pos = pos 273 return i 274} 275 276func (i *IdentifierNode) String() string { 277 return i.Ident 278} 279 280func (i *IdentifierNode) Copy() Node { 281 return NewIdentifier(i.Ident).SetPos(i.Pos) 282} 283 284// VariableNode holds a list of variable names, possibly with chained field 285// accesses. The dollar sign is part of the (first) name. 286type VariableNode struct { 287 NodeType 288 Pos 289 Ident []string // Variable name and fields in lexical order. 290} 291 292func newVariable(pos Pos, ident string) *VariableNode { 293 return &VariableNode{NodeType: NodeVariable, Pos: pos, Ident: strings.Split(ident, ".")} 294} 295 296func (v *VariableNode) String() string { 297 s := "" 298 for i, id := range v.Ident { 299 if i > 0 { 300 s += "." 301 } 302 s += id 303 } 304 return s 305} 306 307func (v *VariableNode) Copy() Node { 308 return &VariableNode{NodeType: NodeVariable, Pos: v.Pos, Ident: append([]string{}, v.Ident...)} 309} 310 311// DotNode holds the special identifier '.'. 312type DotNode struct { 313 Pos 314} 315 316func newDot(pos Pos) *DotNode { 317 return &DotNode{Pos: pos} 318} 319 320func (d *DotNode) Type() NodeType { 321 return NodeDot 322} 323 324func (d *DotNode) String() string { 325 return "." 326} 327 328func (d *DotNode) Copy() Node { 329 return newDot(d.Pos) 330} 331 332// NilNode holds the special identifier 'nil' representing an untyped nil constant. 333type NilNode struct { 334 Pos 335} 336 337func newNil(pos Pos) *NilNode { 338 return &NilNode{Pos: pos} 339} 340 341func (n *NilNode) Type() NodeType { 342 return NodeNil 343} 344 345func (n *NilNode) String() string { 346 return "nil" 347} 348 349func (n *NilNode) Copy() Node { 350 return newNil(n.Pos) 351} 352 353// FieldNode holds a field (identifier starting with '.'). 354// The names may be chained ('.x.y'). 355// The period is dropped from each ident. 356type FieldNode struct { 357 NodeType 358 Pos 359 Ident []string // The identifiers in lexical order. 360} 361 362func newField(pos Pos, ident string) *FieldNode { 363 return &FieldNode{NodeType: NodeField, Pos: pos, Ident: strings.Split(ident[1:], ".")} // [1:] to drop leading period 364} 365 366func (f *FieldNode) String() string { 367 s := "" 368 for _, id := range f.Ident { 369 s += "." + id 370 } 371 return s 372} 373 374func (f *FieldNode) Copy() Node { 375 return &FieldNode{NodeType: NodeField, Pos: f.Pos, Ident: append([]string{}, f.Ident...)} 376} 377 378// ChainNode holds a term followed by a chain of field accesses (identifier starting with '.'). 379// The names may be chained ('.x.y'). 380// The periods are dropped from each ident. 381type ChainNode struct { 382 NodeType 383 Pos 384 Node Node 385 Field []string // The identifiers in lexical order. 386} 387 388func newChain(pos Pos, node Node) *ChainNode { 389 return &ChainNode{NodeType: NodeChain, Pos: pos, Node: node} 390} 391 392// Add adds the named field (which should start with a period) to the end of the chain. 393func (c *ChainNode) Add(field string) { 394 if len(field) == 0 || field[0] != '.' { 395 panic("no dot in field") 396 } 397 field = field[1:] // Remove leading dot. 398 if field == "" { 399 panic("empty field") 400 } 401 c.Field = append(c.Field, field) 402} 403 404func (c *ChainNode) String() string { 405 s := c.Node.String() 406 if _, ok := c.Node.(*PipeNode); ok { 407 s = "(" + s + ")" 408 } 409 for _, field := range c.Field { 410 s += "." + field 411 } 412 return s 413} 414 415func (c *ChainNode) Copy() Node { 416 return &ChainNode{NodeType: NodeChain, Pos: c.Pos, Node: c.Node, Field: append([]string{}, c.Field...)} 417} 418 419// BoolNode holds a boolean constant. 420type BoolNode struct { 421 NodeType 422 Pos 423 True bool // The value of the boolean constant. 424} 425 426func newBool(pos Pos, true bool) *BoolNode { 427 return &BoolNode{NodeType: NodeBool, Pos: pos, True: true} 428} 429 430func (b *BoolNode) String() string { 431 if b.True { 432 return "true" 433 } 434 return "false" 435} 436 437func (b *BoolNode) Copy() Node { 438 return newBool(b.Pos, b.True) 439} 440 441// NumberNode holds a number: signed or unsigned integer, float, or complex. 442// The value is parsed and stored under all the types that can represent the value. 443// This simulates in a small amount of code the behavior of Go's ideal constants. 444type NumberNode struct { 445 NodeType 446 Pos 447 IsInt bool // Number has an integral value. 448 IsUint bool // Number has an unsigned integral value. 449 IsFloat bool // Number has a floating-point value. 450 IsComplex bool // Number is complex. 451 Int64 int64 // The signed integer value. 452 Uint64 uint64 // The unsigned integer value. 453 Float64 float64 // The floating-point value. 454 Complex128 complex128 // The complex value. 455 Text string // The original textual representation from the input. 456} 457 458func newNumber(pos Pos, text string, typ itemType) (*NumberNode, error) { 459 n := &NumberNode{NodeType: NodeNumber, Pos: pos, Text: text} 460 switch typ { 461 case itemCharConstant: 462 rune, _, tail, err := strconv.UnquoteChar(text[1:], text[0]) 463 if err != nil { 464 return nil, err 465 } 466 if tail != "'" { 467 return nil, fmt.Errorf("malformed character constant: %s", text) 468 } 469 n.Int64 = int64(rune) 470 n.IsInt = true 471 n.Uint64 = uint64(rune) 472 n.IsUint = true 473 n.Float64 = float64(rune) // odd but those are the rules. 474 n.IsFloat = true 475 return n, nil 476 case itemComplex: 477 // fmt.Sscan can parse the pair, so let it do the work. 478 if _, err := fmt.Sscan(text, &n.Complex128); err != nil { 479 return nil, err 480 } 481 n.IsComplex = true 482 n.simplifyComplex() 483 return n, nil 484 } 485 // Imaginary constants can only be complex unless they are zero. 486 if len(text) > 0 && text[len(text)-1] == 'i' { 487 f, err := strconv.ParseFloat(text[:len(text)-1], 64) 488 if err == nil { 489 n.IsComplex = true 490 n.Complex128 = complex(0, f) 491 n.simplifyComplex() 492 return n, nil 493 } 494 } 495 // Do integer test first so we get 0x123 etc. 496 u, err := strconv.ParseUint(text, 0, 64) // will fail for -0; fixed below. 497 if err == nil { 498 n.IsUint = true 499 n.Uint64 = u 500 } 501 i, err := strconv.ParseInt(text, 0, 64) 502 if err == nil { 503 n.IsInt = true 504 n.Int64 = i 505 if i == 0 { 506 n.IsUint = true // in case of -0. 507 n.Uint64 = u 508 } 509 } 510 // If an integer extraction succeeded, promote the float. 511 if n.IsInt { 512 n.IsFloat = true 513 n.Float64 = float64(n.Int64) 514 } else if n.IsUint { 515 n.IsFloat = true 516 n.Float64 = float64(n.Uint64) 517 } else { 518 f, err := strconv.ParseFloat(text, 64) 519 if err == nil { 520 n.IsFloat = true 521 n.Float64 = f 522 // If a floating-point extraction succeeded, extract the int if needed. 523 if !n.IsInt && float64(int64(f)) == f { 524 n.IsInt = true 525 n.Int64 = int64(f) 526 } 527 if !n.IsUint && float64(uint64(f)) == f { 528 n.IsUint = true 529 n.Uint64 = uint64(f) 530 } 531 } 532 } 533 if !n.IsInt && !n.IsUint && !n.IsFloat { 534 return nil, fmt.Errorf("illegal number syntax: %q", text) 535 } 536 return n, nil 537} 538 539// simplifyComplex pulls out any other types that are represented by the complex number. 540// These all require that the imaginary part be zero. 541func (n *NumberNode) simplifyComplex() { 542 n.IsFloat = imag(n.Complex128) == 0 543 if n.IsFloat { 544 n.Float64 = real(n.Complex128) 545 n.IsInt = float64(int64(n.Float64)) == n.Float64 546 if n.IsInt { 547 n.Int64 = int64(n.Float64) 548 } 549 n.IsUint = float64(uint64(n.Float64)) == n.Float64 550 if n.IsUint { 551 n.Uint64 = uint64(n.Float64) 552 } 553 } 554} 555 556func (n *NumberNode) String() string { 557 return n.Text 558} 559 560func (n *NumberNode) Copy() Node { 561 nn := new(NumberNode) 562 *nn = *n // Easy, fast, correct. 563 return nn 564} 565 566// StringNode holds a string constant. The value has been "unquoted". 567type StringNode struct { 568 NodeType 569 Pos 570 Quoted string // The original text of the string, with quotes. 571 Text string // The string, after quote processing. 572} 573 574func newString(pos Pos, orig, text string) *StringNode { 575 return &StringNode{NodeType: NodeString, Pos: pos, Quoted: orig, Text: text} 576} 577 578func (s *StringNode) String() string { 579 return s.Quoted 580} 581 582func (s *StringNode) Copy() Node { 583 return newString(s.Pos, s.Quoted, s.Text) 584} 585 586// endNode represents an {{end}} action. 587// It does not appear in the final parse tree. 588type endNode struct { 589 Pos 590} 591 592func newEnd(pos Pos) *endNode { 593 return &endNode{Pos: pos} 594} 595 596func (e *endNode) Type() NodeType { 597 return nodeEnd 598} 599 600func (e *endNode) String() string { 601 return "{{end}}" 602} 603 604func (e *endNode) Copy() Node { 605 return newEnd(e.Pos) 606} 607 608// elseNode represents an {{else}} action. Does not appear in the final tree. 609type elseNode struct { 610 NodeType 611 Pos 612 Line int // The line number in the input (deprecated; kept for compatibility) 613} 614 615func newElse(pos Pos, line int) *elseNode { 616 return &elseNode{NodeType: nodeElse, Pos: pos, Line: line} 617} 618 619func (e *elseNode) Type() NodeType { 620 return nodeElse 621} 622 623func (e *elseNode) String() string { 624 return "{{else}}" 625} 626 627func (e *elseNode) Copy() Node { 628 return newElse(e.Pos, e.Line) 629} 630 631// BranchNode is the common representation of if, range, and with. 632type BranchNode struct { 633 NodeType 634 Pos 635 Line int // The line number in the input (deprecated; kept for compatibility) 636 Pipe *PipeNode // The pipeline to be evaluated. 637 List *ListNode // What to execute if the value is non-empty. 638 ElseList *ListNode // What to execute if the value is empty (nil if absent). 639} 640 641func (b *BranchNode) String() string { 642 name := "" 643 switch b.NodeType { 644 case NodeIf: 645 name = "if" 646 case NodeRange: 647 name = "range" 648 case NodeWith: 649 name = "with" 650 default: 651 panic("unknown branch type") 652 } 653 if b.ElseList != nil { 654 return fmt.Sprintf("{{%s %s}}%s{{else}}%s{{end}}", name, b.Pipe, b.List, b.ElseList) 655 } 656 return fmt.Sprintf("{{%s %s}}%s{{end}}", name, b.Pipe, b.List) 657} 658 659// IfNode represents an {{if}} action and its commands. 660type IfNode struct { 661 BranchNode 662} 663 664func newIf(pos Pos, line int, pipe *PipeNode, list, elseList *ListNode) *IfNode { 665 return &IfNode{BranchNode{NodeType: NodeIf, Pos: pos, Line: line, Pipe: pipe, List: list, ElseList: elseList}} 666} 667 668func (i *IfNode) Copy() Node { 669 return newIf(i.Pos, i.Line, i.Pipe.CopyPipe(), i.List.CopyList(), i.ElseList.CopyList()) 670} 671 672// RangeNode represents a {{range}} action and its commands. 673type RangeNode struct { 674 BranchNode 675} 676 677func newRange(pos Pos, line int, pipe *PipeNode, list, elseList *ListNode) *RangeNode { 678 return &RangeNode{BranchNode{NodeType: NodeRange, Pos: pos, Line: line, Pipe: pipe, List: list, ElseList: elseList}} 679} 680 681func (r *RangeNode) Copy() Node { 682 return newRange(r.Pos, r.Line, r.Pipe.CopyPipe(), r.List.CopyList(), r.ElseList.CopyList()) 683} 684 685// WithNode represents a {{with}} action and its commands. 686type WithNode struct { 687 BranchNode 688} 689 690func newWith(pos Pos, line int, pipe *PipeNode, list, elseList *ListNode) *WithNode { 691 return &WithNode{BranchNode{NodeType: NodeWith, Pos: pos, Line: line, Pipe: pipe, List: list, ElseList: elseList}} 692} 693 694func (w *WithNode) Copy() Node { 695 return newWith(w.Pos, w.Line, w.Pipe.CopyPipe(), w.List.CopyList(), w.ElseList.CopyList()) 696} 697 698// TemplateNode represents a {{template}} action. 699type TemplateNode struct { 700 NodeType 701 Pos 702 Line int // The line number in the input (deprecated; kept for compatibility) 703 Name string // The name of the template (unquoted). 704 Pipe *PipeNode // The command to evaluate as dot for the template. 705} 706 707func newTemplate(pos Pos, line int, name string, pipe *PipeNode) *TemplateNode { 708 return &TemplateNode{NodeType: NodeTemplate, Line: line, Pos: pos, Name: name, Pipe: pipe} 709} 710 711func (t *TemplateNode) String() string { 712 if t.Pipe == nil { 713 return fmt.Sprintf("{{template %q}}", t.Name) 714 } 715 return fmt.Sprintf("{{template %q %s}}", t.Name, t.Pipe) 716} 717 718func (t *TemplateNode) Copy() Node { 719 return newTemplate(t.Pos, t.Line, t.Name, t.Pipe.CopyPipe()) 720} 721