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
|
Pos() token.Pos
|
||||||
}
|
}
|
||||||
|
|
||||||
func (NodeList) node() {}
|
|
||||||
|
|
||||||
func (ObjectList) node() {}
|
func (ObjectList) node() {}
|
||||||
func (ObjectKey) node() {}
|
func (ObjectKey) node() {}
|
||||||
func (ObjectItem) node() {}
|
func (ObjectItem) node() {}
|
||||||
@ -19,21 +17,6 @@ func (ObjectType) node() {}
|
|||||||
func (LiteralType) node() {}
|
func (LiteralType) node() {}
|
||||||
func (ListType) 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 represents a list of ObjectItems. An HCL file itself is an
|
||||||
// ObjectList.
|
// ObjectList.
|
||||||
type ObjectList struct {
|
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.
|
// Parse returns the fully parsed source and returns the abstract syntax tree.
|
||||||
func (p *Parser) Parse() (ast.Node, error) {
|
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"))
|
defer un(trace(p, "ParseObjectList"))
|
||||||
node := &ast.NodeList{}
|
node := &ast.ObjectList{}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
n, err := p.next()
|
n, err := p.next()
|
||||||
@ -48,31 +48,11 @@ func (p *Parser) parseNodeList() (*ast.NodeList, error) {
|
|||||||
return node, err
|
return node, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// we successfully parsed a node, add it to the final source node
|
if item, ok := n.(*ast.ObjectItem); ok {
|
||||||
node.Add(n)
|
// we successfully parsed a node, add it to the final source node
|
||||||
}
|
node.Add(item)
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
return node, nil
|
||||||
@ -83,11 +63,10 @@ func (p *Parser) next() (ast.Node, error) {
|
|||||||
defer un(trace(p, "ParseNode"))
|
defer un(trace(p, "ParseNode"))
|
||||||
|
|
||||||
tok := p.scan()
|
tok := p.scan()
|
||||||
if tok.Type == token.EOF {
|
|
||||||
return nil, errEofToken
|
|
||||||
}
|
|
||||||
|
|
||||||
switch tok.Type {
|
switch tok.Type {
|
||||||
|
case token.EOF:
|
||||||
|
return nil, errEofToken
|
||||||
case token.IDENT, token.STRING:
|
case token.IDENT, token.STRING:
|
||||||
p.unscan()
|
p.unscan()
|
||||||
return p.parseObjectItem()
|
return p.parseObjectItem()
|
||||||
@ -240,6 +219,9 @@ func (p *Parser) parseListType() (*ast.ListType, error) {
|
|||||||
case token.COMMA:
|
case token.COMMA:
|
||||||
// get next list item or we are at the end
|
// get next list item or we are at the end
|
||||||
continue
|
continue
|
||||||
|
case token.COMMENT:
|
||||||
|
// TODO(arslan): parse comment
|
||||||
|
continue
|
||||||
case token.BOOL:
|
case token.BOOL:
|
||||||
// TODO(arslan) should we support? not supported by HCL yet
|
// TODO(arslan) should we support? not supported by HCL yet
|
||||||
case token.LBRACK:
|
case token.LBRACK:
|
||||||
|
@ -20,6 +20,7 @@ func TestType(t *testing.T) {
|
|||||||
{token.STRING, `foo = "foo"`},
|
{token.STRING, `foo = "foo"`},
|
||||||
{token.NUMBER, `foo = 123`},
|
{token.NUMBER, `foo = 123`},
|
||||||
{token.FLOAT, `foo = 123.12`},
|
{token.FLOAT, `foo = 123.12`},
|
||||||
|
{token.FLOAT, `foo = -123.12`},
|
||||||
{token.BOOL, `foo = true`},
|
{token.BOOL, `foo = true`},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user