From 6d9db3139c6494641bd1171798c12b58eb5c7bb9 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 13 Jan 2015 08:50:28 -0800 Subject: [PATCH] config/lang: AST String() methods --- config/lang/ast/ast.go | 2 +- config/lang/ast/call.go | 14 ++++++++++++++ config/lang/ast/literal.go | 4 ++++ config/lang/ast/type_string.go | 26 ++++++++++++++++++++++---- config/lang/ast/variable_access.go | 4 ++++ config/lang/parse_test.go | 10 ++++++++++ 6 files changed, 55 insertions(+), 5 deletions(-) diff --git a/config/lang/ast/ast.go b/config/lang/ast/ast.go index 2476098e8..0d7000da6 100644 --- a/config/lang/ast/ast.go +++ b/config/lang/ast/ast.go @@ -37,7 +37,7 @@ type Type uint const ( TypeInvalid Type = 0 - TypeString = 1 << iota + TypeString Type = 1 << iota TypeInt TypeFloat ) diff --git a/config/lang/ast/call.go b/config/lang/ast/call.go index d70219983..40b0773e1 100644 --- a/config/lang/ast/call.go +++ b/config/lang/ast/call.go @@ -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, ", ")) +} diff --git a/config/lang/ast/literal.go b/config/lang/ast/literal.go index a20316ac1..1fd7669ff 100644 --- a/config/lang/ast/literal.go +++ b/config/lang/ast/literal.go @@ -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) +} diff --git a/config/lang/ast/type_string.go b/config/lang/ast/type_string.go index 55e2af017..fd0e9e355 100644 --- a/config/lang/ast/type_string.go +++ b/config/lang/ast/type_string.go @@ -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]] } diff --git a/config/lang/ast/variable_access.go b/config/lang/ast/variable_access.go index 943ee5140..1f86a260d 100644 --- a/config/lang/ast/variable_access.go +++ b/config/lang/ast/variable_access.go @@ -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) +} diff --git a/config/lang/parse_test.go b/config/lang/parse_test.go index d8e2c7236..eb4670644 100644 --- a/config/lang/parse_test.go +++ b/config/lang/parse_test.go @@ -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,