parser: fix all tests, comments are still WIP
This commit is contained in:
parent
1a63f537eb
commit
6b5bd170f6
17
ast/ast.go
17
ast/ast.go
@ -8,8 +8,6 @@ type Node interface {
|
||||
Pos() token.Pos
|
||||
}
|
||||
|
||||
func (NodeList) node() {}
|
||||
|
||||
func (ObjectList) node() {}
|
||||
func (ObjectKey) node() {}
|
||||
func (ObjectItem) node() {}
|
||||
@ -19,21 +17,6 @@ func (ObjectType) node() {}
|
||||
func (LiteralType) node() {}
|
||||
func (ListType) node() {}
|
||||
|
||||
// ObjectList represents a list of ObjectItems. An HCL file itself is an
|
||||
// ObjectList.
|
||||
type NodeList struct {
|
||||
Nodes []Node
|
||||
}
|
||||
|
||||
func (n *NodeList) Add(node Node) {
|
||||
n.Nodes = append(n.Nodes, node)
|
||||
}
|
||||
|
||||
func (n *NodeList) Pos() token.Pos {
|
||||
// always returns the uninitiliazed position
|
||||
return n.Nodes[0].Pos()
|
||||
}
|
||||
|
||||
// ObjectList represents a list of ObjectItems. An HCL file itself is an
|
||||
// ObjectList.
|
||||
type ObjectList struct {
|
||||
|
@ -29,12 +29,12 @@ var errEofToken = errors.New("EOF token found")
|
||||
|
||||
// Parse returns the fully parsed source and returns the abstract syntax tree.
|
||||
func (p *Parser) Parse() (ast.Node, error) {
|
||||
return p.parseNodeList()
|
||||
return p.parseObjectList()
|
||||
}
|
||||
|
||||
func (p *Parser) parseNodeList() (*ast.NodeList, error) {
|
||||
func (p *Parser) parseObjectList() (*ast.ObjectList, error) {
|
||||
defer un(trace(p, "ParseObjectList"))
|
||||
node := &ast.NodeList{}
|
||||
node := &ast.ObjectList{}
|
||||
|
||||
for {
|
||||
n, err := p.next()
|
||||
@ -48,31 +48,11 @@ func (p *Parser) parseNodeList() (*ast.NodeList, error) {
|
||||
return node, err
|
||||
}
|
||||
|
||||
// we successfully parsed a node, add it to the final source node
|
||||
node.Add(n)
|
||||
}
|
||||
|
||||
return node, nil
|
||||
}
|
||||
|
||||
func (p *Parser) parseObjectList() (*ast.ObjectList, error) {
|
||||
defer un(trace(p, "ParseObjectList"))
|
||||
node := &ast.ObjectList{}
|
||||
|
||||
for {
|
||||
n, err := p.parseObjectItem()
|
||||
if err == errEofToken {
|
||||
break // we are finished
|
||||
if item, ok := n.(*ast.ObjectItem); ok {
|
||||
// we successfully parsed a node, add it to the final source node
|
||||
node.Add(item)
|
||||
}
|
||||
|
||||
// we don't return a nil, because might want to use already collected
|
||||
// items.
|
||||
if err != nil {
|
||||
return node, err
|
||||
}
|
||||
|
||||
// we successfully parsed a node, add it to the final source node
|
||||
node.Add(n)
|
||||
}
|
||||
|
||||
return node, nil
|
||||
@ -83,11 +63,10 @@ func (p *Parser) next() (ast.Node, error) {
|
||||
defer un(trace(p, "ParseNode"))
|
||||
|
||||
tok := p.scan()
|
||||
if tok.Type == token.EOF {
|
||||
return nil, errEofToken
|
||||
}
|
||||
|
||||
switch tok.Type {
|
||||
case token.EOF:
|
||||
return nil, errEofToken
|
||||
case token.IDENT, token.STRING:
|
||||
p.unscan()
|
||||
return p.parseObjectItem()
|
||||
@ -240,6 +219,9 @@ func (p *Parser) parseListType() (*ast.ListType, error) {
|
||||
case token.COMMA:
|
||||
// get next list item or we are at the end
|
||||
continue
|
||||
case token.COMMENT:
|
||||
// TODO(arslan): parse comment
|
||||
continue
|
||||
case token.BOOL:
|
||||
// TODO(arslan) should we support? not supported by HCL yet
|
||||
case token.LBRACK:
|
||||
|
@ -20,6 +20,7 @@ func TestType(t *testing.T) {
|
||||
{token.STRING, `foo = "foo"`},
|
||||
{token.NUMBER, `foo = 123`},
|
||||
{token.FLOAT, `foo = 123.12`},
|
||||
{token.FLOAT, `foo = -123.12`},
|
||||
{token.BOOL, `foo = true`},
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user