config/lang: AST String() methods

This commit is contained in:
Mitchell Hashimoto 2015-01-13 08:50:28 -08:00
parent abca82a84e
commit 6d9db3139c
6 changed files with 55 additions and 5 deletions

View File

@ -37,7 +37,7 @@ type Type uint
const (
TypeInvalid Type = 0
TypeString = 1 << iota
TypeString Type = 1 << iota
TypeInt
TypeFloat
)

View File

@ -1,5 +1,10 @@
package ast
import (
"fmt"
"strings"
)
// Call represents a function call.
type Call struct {
Func string
@ -18,3 +23,12 @@ func (n *Call) Accept(v Visitor) {
func (n *Call) Pos() Pos {
return n.Posx
}
func (n *Call) String() string {
args := make([]string, len(n.Args))
for i, arg := range n.Args {
args[i] = fmt.Sprintf("%s", arg)
}
return fmt.Sprintf("Call(%s, %s)", n.Func, strings.Join(args, ", "))
}

View File

@ -23,3 +23,7 @@ func (n *LiteralNode) Pos() Pos {
func (n *LiteralNode) GoString() string {
return fmt.Sprintf("*%#v", *n)
}
func (n *LiteralNode) String() string {
return fmt.Sprintf("Literal(%s, %v)", n.Type, n.Value)
}

View File

@ -4,13 +4,31 @@ package ast
import "fmt"
const _Type_name = "TypeInvalid"
const (
_Type_name_0 = "TypeInvalid"
_Type_name_1 = "TypeString"
_Type_name_2 = "TypeInt"
_Type_name_3 = "TypeFloat"
)
var _Type_index = [...]uint8{0, 11}
var (
_Type_index_0 = [...]uint8{0, 11}
_Type_index_1 = [...]uint8{0, 10}
_Type_index_2 = [...]uint8{0, 7}
_Type_index_3 = [...]uint8{0, 9}
)
func (i Type) String() string {
if i+1 >= Type(len(_Type_index)) {
switch {
case i == 0:
return _Type_name_0
case i == 2:
return _Type_name_1
case i == 4:
return _Type_name_2
case i == 8:
return _Type_name_3
default:
return fmt.Sprintf("Type(%d)", i)
}
return _Type_name[_Type_index[i]:_Type_index[i+1]]
}

View File

@ -21,3 +21,7 @@ func (n *VariableAccess) Pos() Pos {
func (n *VariableAccess) GoString() string {
return fmt.Sprintf("*%#v", *n)
}
func (n *VariableAccess) String() string {
return fmt.Sprintf("Variable(%s)", n.Name)
}

View File

@ -23,6 +23,16 @@ func TestParse(t *testing.T) {
},
},
{
"$${var.foo}",
false,
&ast.LiteralNode{
Value: "${var.foo}",
Type: ast.TypeString,
Posx: ast.Pos{Column: 1, Line: 1},
},
},
{
"foo ${var.bar}",
false,