parser: working assignstatement is finished

This commit is contained in:
Fatih Arslan 2015-10-12 02:49:07 +03:00
parent 00c13fa7e1
commit 0e668f87b2
3 changed files with 23 additions and 22 deletions

View File

@ -48,7 +48,7 @@ func (s *Source) String() string {
func (s *Source) Pos() scanner.Pos { func (s *Source) Pos() scanner.Pos {
// always returns the uninitiliazed position // always returns the uninitiliazed position
return scanner.Pos{} return s.nodes[0].Pos()
} }
// IdentStatement represents an identifier. // IdentStatement represents an identifier.

View File

@ -29,14 +29,14 @@ func (p *Parser) Parse() Node {
node := &Source{} node := &Source{}
for { for {
if n := p.parseStatement(); n != nil {
node.add(n)
}
// break if we hit the end // break if we hit the end
if p.tok.Type == scanner.EOF { if p.tok.Type == scanner.EOF {
break break
} }
if n := p.parseStatement(); n != nil {
node.add(n)
}
} }
return node return node
@ -48,20 +48,16 @@ func (p *Parser) parseStatement() Node {
tok := p.scan() tok := p.scan()
if tok.Type.IsLiteral() { if tok.Type.IsLiteral() {
// found an object
if p.prevTok.Type.IsLiteral() { if p.prevTok.Type.IsLiteral() {
return p.parseObject() return p.parseObject()
} }
return p.parseIdent()
}
switch tok.Type { if tok := p.scan(); tok.Type == scanner.ASSIGN {
case scanner.LBRACE: return p.parseAssignment()
return p.parseObject() }
case scanner.LBRACK:
return p.parseList() p.unscan()
case scanner.ASSIGN: return p.parseIdent()
return p.parseAssignment()
} }
return nil return nil
@ -113,7 +109,10 @@ func (p *Parser) scan() scanner.Token {
} }
// unscan pushes the previously read token back onto the buffer. // unscan pushes the previously read token back onto the buffer.
func (p *Parser) unscan() { p.n = 1 } func (p *Parser) unscan() {
p.n = 1
p.tok = p.prevTok
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Parsing support // Parsing support

View File

@ -1,17 +1,19 @@
package parser package parser
import ( import "testing"
"fmt"
"testing"
)
func TestAssignStatment(t *testing.T) { func TestAssignStatment(t *testing.T) {
src := `ami = "${var.foo}"` src := `ami = "${var.foo}"`
p := New([]byte(src)) p := New([]byte(src))
p.enableTrace = true p.enableTrace = true
n := p.Parse() n := p.Parse()
fmt.Println(n) if n.String() != src {
t.Errorf("AssignStatement is not parsed correctly\n\twant: '%s'\n\tgot : '%s'", src, n.String())
}
if n.Pos().Line != 1 {
t.Errorf("AssignStatement position is wrong\n\twant: '%d'\n\tgot : '%d'", 1, n.Pos().Line)
}
} }