Skip to content

Commit

Permalink
don't recursively parse section tokens
Browse files Browse the repository at this point in the history
it caused a bug where the slices constructed by the parser were in reverse order
  • Loading branch information
subpop committed Sep 21, 2019
1 parent 56fb705 commit f928f19
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
25 changes: 15 additions & 10 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ func (e *errParse) Error() string {
}

type parser struct {
ast ast
l *lexer
tok token
ast ast
l *lexer
tok token
prev *token
}

func newParser(data []byte) *parser {
Expand All @@ -29,7 +30,16 @@ func newParser(data []byte) *parser {
}

func (p *parser) nextToken() {
p.tok = p.l.nextToken()
if p.prev != nil {
p.tok = *p.prev
p.prev = nil
} else {
p.tok = p.l.nextToken()
}
}

func (p *parser) backup() {
p.prev = &p.tok
}

func (p *parser) parse() error {
Expand All @@ -49,6 +59,7 @@ func (p *parser) parse() error {
return err
}
p.ast.addSection(sec)
p.backup()
case tokenKey:
prop := newProperty(p.tok.val)
if err := p.parseProperty(&prop); err != nil {
Expand Down Expand Up @@ -82,12 +93,6 @@ func (p *parser) parseSection(out *section) error {
return err
}
out.addProperty(prop)
case tokenSection:
sec := newSection(p.tok.val)
if err := p.parseSection(&sec); err != nil {
return err
}
p.ast.addSection(sec)
default:
return nil
}
Expand Down
15 changes: 14 additions & 1 deletion parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestParse(t *testing.T) {
want ast
}{
{
input: "version=1.2.3\n\n[user]\nname=root\nshell=/bin/bash",
input: "version=1.2.3\n\n[user]\nname=root\nshell=/bin/bash\n\n[user]\nname=admin\nshell=/bin/bash",
want: ast{
"": []section{
section{
Expand All @@ -39,6 +39,19 @@ func TestParse(t *testing.T) {
},
},
},
section{
name: "user",
props: map[string]property{
"name": property{
key: "name",
val: []string{"admin"},
},
"shell": property{
key: "shell",
val: []string{"/bin/bash"},
},
},
},
},
},
},
Expand Down

0 comments on commit f928f19

Please sign in to comment.