parser: add ListType support

This commit is contained in:
Fatih Arslan 2015-10-17 00:00:05 +03:00
parent 32f4e84345
commit 4e690ec67d
3 changed files with 51 additions and 13 deletions

View File

@ -82,6 +82,10 @@ func (l *ListType) Pos() token.Pos {
return l.Lbrack
}
func (l *ListType) Add(node Node) {
l.List = append(l.List, node)
}
// ObjectType represents a HCL Object Type
type ObjectType struct {
Lbrace token.Pos // position of "{"

View File

@ -154,6 +154,41 @@ func (p *Parser) parseObjectKey() ([]*ast.ObjectKey, error) {
}
}
// parseListType parses a list type and returns a ListType AST
func (p *Parser) parseListType() (*ast.ListType, error) {
defer un(trace(p, "ParseListType"))
l := &ast.ListType{
Lbrack: p.tok.Pos,
}
for {
tok := p.scan()
switch tok.Type {
case token.NUMBER, token.FLOAT, token.STRING:
node, err := p.parseLiteralType()
if err != nil {
return nil, err
}
l.Add(node)
case token.COMMA:
// get next list item or we are at the end
continue
case token.BOOL:
// TODO(arslan) should we support? not supported by HCL yet
case token.LBRACK:
// TODO(arslan) should we support nested lists?
case token.RBRACK:
// finished
l.Rbrack = p.tok.Pos
return l, nil
default:
return nil, fmt.Errorf("unexpected token while parsing list: %s", tok.Type)
}
}
}
// parseLiteralType parses a literal type and returns a LiteralType AST
func (p *Parser) parseLiteralType() (*ast.LiteralType, error) {
defer un(trace(p, "ParseLiteral"))
@ -170,13 +205,6 @@ func (p *Parser) parseObjectType() (*ast.ObjectType, error) {
return nil, errors.New("ObjectType is not implemented yet")
}
// parseListType parses a list type and returns a ListType AST
func (p *Parser) parseListType() (*ast.ListType, error) {
defer un(trace(p, "ParseListType"))
return nil, errors.New("ListType is not implemented yet")
}
// scan returns the next token from the underlying scanner.
// If a token has been unscanned then read that instead.
func (p *Parser) scan() token.Token {

View File

@ -12,19 +12,25 @@ import (
)
func TestParseType(t *testing.T) {
src := `foo = true`
src := `foo = ["fatih", "arslan", 1224]`
p := New([]byte(src))
p.enableTrace = true
n, err := p.Parse()
node, err := p.Parse()
if err != nil {
t.Fatal(err)
}
fmt.Printf("n = %+v\n", n)
ast.Walk(n, func(node ast.Node) bool {
fmt.Printf("node = %+v\n", node)
ast.Walk(node, func(n ast.Node) bool {
if list, ok := n.(*ast.ObjectList); ok {
for _, l := range list.Items {
for _, k := range l.Keys {
fmt.Printf("key = %+v\n", k)
}
fmt.Printf("val = %+v\n", l.Val)
}
return false
}
return true
})
}