parser: add Walk scanner: fix tests
This commit is contained in:
parent
378bec0cf4
commit
38490ad4dc
@ -9,8 +9,8 @@ type Node interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ObjectList) node() {}
|
func (ObjectList) node() {}
|
||||||
func (ObjectItem) node() {}
|
|
||||||
func (ObjectKey) node() {}
|
func (ObjectKey) node() {}
|
||||||
|
func (ObjectItem) node() {}
|
||||||
|
|
||||||
func (ObjectType) node() {}
|
func (ObjectType) node() {}
|
||||||
func (LiteralType) node() {}
|
func (LiteralType) node() {}
|
||||||
|
@ -28,7 +28,7 @@ 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() (Node, error) {
|
func (p *Parser) Parse() (Node, error) {
|
||||||
defer un(trace(p, "ParseSource"))
|
defer un(trace(p, "ParseObjectList"))
|
||||||
node := &ObjectList{}
|
node := &ObjectList{}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@ -80,39 +80,37 @@ func (p *Parser) parseObjectItem() (*ObjectItem, error) {
|
|||||||
fmt.Println("object")
|
fmt.Println("object")
|
||||||
}
|
}
|
||||||
|
|
||||||
switch len(keys) {
|
return nil, fmt.Errorf("not yet implemented: %s", p.tok.Type)
|
||||||
case 1:
|
}
|
||||||
// assignment or object
|
|
||||||
default:
|
|
||||||
// nested object
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// parseType parses any type of Type, such as number, bool, string, object or
|
||||||
|
// list.
|
||||||
|
func (p *Parser) parseType() (Node, error) {
|
||||||
|
defer un(trace(p, "ParseType"))
|
||||||
tok := p.scan()
|
tok := p.scan()
|
||||||
fmt.Println(tok) // debug
|
|
||||||
|
|
||||||
switch tok.Type {
|
switch tok.Type {
|
||||||
case scanner.LBRACK:
|
case scanner.NUMBER, scanner.FLOAT, scanner.BOOL, scanner.STRING:
|
||||||
// return p.parseListType()
|
return p.parseLiteralType()
|
||||||
case scanner.LBRACE:
|
case scanner.LBRACE:
|
||||||
// return p.parseObjectTpe()
|
return p.parseObjectType()
|
||||||
|
case scanner.LBRACK:
|
||||||
|
return p.parseListType()
|
||||||
case scanner.COMMENT:
|
case scanner.COMMENT:
|
||||||
// implement comment
|
// implement comment
|
||||||
case scanner.EOF:
|
case scanner.EOF:
|
||||||
return nil, errEofToken
|
return nil, errEofToken
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, fmt.Errorf("not yet implemented: %s", tok.Type)
|
|
||||||
}
|
|
||||||
|
|
||||||
// parseType parses any type of Type, such as number, bool, string, object or
|
|
||||||
// list.
|
|
||||||
func (p *Parser) parseType() (Node, error) {
|
|
||||||
return nil, errors.New("ParseType is not implemented yet")
|
return nil, errors.New("ParseType is not implemented yet")
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseObjectKey parses an object key and returns a ObjectKey AST
|
// parseObjectKey parses an object key and returns a ObjectKey AST
|
||||||
func (p *Parser) parseObjectKey() ([]*ObjectKey, error) {
|
func (p *Parser) parseObjectKey() ([]*ObjectKey, error) {
|
||||||
tok := p.scan()
|
tok := p.scan()
|
||||||
|
if tok.Type == scanner.EOF {
|
||||||
|
return nil, errEofToken
|
||||||
|
}
|
||||||
|
|
||||||
keys := make([]*ObjectKey, 0)
|
keys := make([]*ObjectKey, 0)
|
||||||
|
|
||||||
@ -165,11 +163,15 @@ func (p *Parser) parseLiteralType() (*LiteralType, error) {
|
|||||||
|
|
||||||
// parseObjectType parses an object type and returns a ObjectType AST
|
// parseObjectType parses an object type and returns a ObjectType AST
|
||||||
func (p *Parser) parseObjectType() (*ObjectType, error) {
|
func (p *Parser) parseObjectType() (*ObjectType, error) {
|
||||||
|
defer un(trace(p, "ParseObjectYpe"))
|
||||||
|
|
||||||
return nil, errors.New("ObjectType is not implemented yet")
|
return nil, errors.New("ObjectType is not implemented yet")
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseListType parses a list type and returns a ListType AST
|
// parseListType parses a list type and returns a ListType AST
|
||||||
func (p *Parser) parseListType() (*ListType, error) {
|
func (p *Parser) parseListType() (*ListType, error) {
|
||||||
|
defer un(trace(p, "ParseListType"))
|
||||||
|
|
||||||
return nil, errors.New("ListType is not implemented yet")
|
return nil, errors.New("ListType is not implemented yet")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,6 +10,24 @@ import (
|
|||||||
"github.com/fatih/hcl/scanner"
|
"github.com/fatih/hcl/scanner"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestParseType(t *testing.T) {
|
||||||
|
src := `foo = true`
|
||||||
|
p := New([]byte(src))
|
||||||
|
p.enableTrace = true
|
||||||
|
|
||||||
|
n, err := p.Parse()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("n = %+v\n", n)
|
||||||
|
|
||||||
|
Walk(n, func(node Node) bool {
|
||||||
|
fmt.Printf("node = %+v\n", node)
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestObjectKey(t *testing.T) {
|
func TestObjectKey(t *testing.T) {
|
||||||
keys := []struct {
|
keys := []struct {
|
||||||
exp []scanner.TokenType
|
exp []scanner.TokenType
|
||||||
|
@ -336,7 +336,7 @@ func TestRealExample(t *testing.T) {
|
|||||||
t.Errorf("got: %s want %s for %s\n", tok, l.tokenType, tok.String())
|
t.Errorf("got: %s want %s for %s\n", tok, l.tokenType, tok.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
if l.literal != tok.String() {
|
if l.literal != tok.Text {
|
||||||
t.Errorf("got: %s want %s\n", tok, l.literal)
|
t.Errorf("got: %s want %s\n", tok, l.literal)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -408,7 +408,7 @@ func testTokenList(t *testing.T, tokenList []tokenPair) {
|
|||||||
t.Errorf("tok = %q want %q for %q\n", tok, ident.tok, ident.text)
|
t.Errorf("tok = %q want %q for %q\n", tok, ident.tok, ident.text)
|
||||||
}
|
}
|
||||||
|
|
||||||
if tok.String() != ident.text {
|
if tok.Text != ident.text {
|
||||||
t.Errorf("text = %q want %q", tok.String(), ident.text)
|
t.Errorf("text = %q want %q", tok.String(), ident.text)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user