parser: add Walk scanner: fix tests

This commit is contained in:
Fatih Arslan 2015-10-16 22:57:56 +03:00
parent 378bec0cf4
commit 38490ad4dc
4 changed files with 40 additions and 20 deletions

View File

@ -9,8 +9,8 @@ type Node interface {
}
func (ObjectList) node() {}
func (ObjectItem) node() {}
func (ObjectKey) node() {}
func (ObjectItem) node() {}
func (ObjectType) node() {}
func (LiteralType) node() {}

View File

@ -28,7 +28,7 @@ var errEofToken = errors.New("EOF token found")
// Parse returns the fully parsed source and returns the abstract syntax tree.
func (p *Parser) Parse() (Node, error) {
defer un(trace(p, "ParseSource"))
defer un(trace(p, "ParseObjectList"))
node := &ObjectList{}
for {
@ -80,39 +80,37 @@ func (p *Parser) parseObjectItem() (*ObjectItem, error) {
fmt.Println("object")
}
switch len(keys) {
case 1:
// assignment or object
default:
// nested object
return nil, fmt.Errorf("not yet implemented: %s", p.tok.Type)
}
// 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()
fmt.Println(tok) // debug
switch tok.Type {
case scanner.LBRACK:
// return p.parseListType()
case scanner.NUMBER, scanner.FLOAT, scanner.BOOL, scanner.STRING:
return p.parseLiteralType()
case scanner.LBRACE:
// return p.parseObjectTpe()
return p.parseObjectType()
case scanner.LBRACK:
return p.parseListType()
case scanner.COMMENT:
// implement comment
case scanner.EOF:
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")
}
// parseObjectKey parses an object key and returns a ObjectKey AST
func (p *Parser) parseObjectKey() ([]*ObjectKey, error) {
tok := p.scan()
if tok.Type == scanner.EOF {
return nil, errEofToken
}
keys := make([]*ObjectKey, 0)
@ -165,11 +163,15 @@ func (p *Parser) parseLiteralType() (*LiteralType, error) {
// parseObjectType parses an object type and returns a ObjectType AST
func (p *Parser) parseObjectType() (*ObjectType, error) {
defer un(trace(p, "ParseObjectYpe"))
return nil, errors.New("ObjectType is not implemented yet")
}
// parseListType parses a list type and returns a ListType AST
func (p *Parser) parseListType() (*ListType, error) {
defer un(trace(p, "ParseListType"))
return nil, errors.New("ListType is not implemented yet")
}

View File

@ -10,6 +10,24 @@ import (
"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) {
keys := []struct {
exp []scanner.TokenType

View File

@ -336,7 +336,7 @@ func TestRealExample(t *testing.T) {
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)
}
}
@ -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)
}
if tok.String() != ident.text {
if tok.Text != ident.text {
t.Errorf("text = %q want %q", tok.String(), ident.text)
}