diff --git a/parser/ast.go b/parser/ast.go index 98b640f..1ff158c 100644 --- a/parser/ast.go +++ b/parser/ast.go @@ -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() {} diff --git a/parser/parser.go b/parser/parser.go index 670fa83..8aafa1e 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -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") } diff --git a/parser/parser_test.go b/parser/parser_test.go index 9e847f5..efbb8d2 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -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 diff --git a/scanner/scanner_test.go b/scanner/scanner_test.go index 0556766..62590d6 100644 --- a/scanner/scanner_test.go +++ b/scanner/scanner_test.go @@ -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) }