Skip to content

Commit

Permalink
fix comment parsing between include statements
Browse files Browse the repository at this point in the history
  • Loading branch information
josh-wolfe-okcupid committed Aug 29, 2024
1 parent fd4b8e7 commit 7fec36e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 4 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,12 @@ func (p *parser) extractObject(isSubObject ...bool) (Object, error) {
lastRow := 0

for tok := p.scanner.Peek(); tok != scanner.EOF; tok = p.scanner.Peek() {
for p.scanner.TokenText() == commentToken {
if p.scanner.TokenText() == commentToken {
p.consumeComment()
continue
}

for p.scanner.TokenText() == includeToken {
if p.scanner.TokenText() == includeToken {
p.advance()

includedObject, err := p.parseIncludedResource()
Expand All @@ -267,6 +268,7 @@ func (p *parser) extractObject(isSubObject ...bool) (Object, error) {

mergeObjects(object, includedObject)
p.advance()
continue
}

if !parenthesisBalanced && p.scanner.TokenText() == objectEndToken {
Expand Down
13 changes: 13 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,19 @@ func TestExtractObject(t *testing.T) {
assertDeepEqual(t, got, expected)
})

t.Run("parse a comment between two includes", func(t *testing.T) {
parser := newParser(strings.NewReader(
`include "testdata/a.conf"
# comment
include "testdata/b.conf"
`))
parser.advance()
expected := Object{"a": Int(1), "b": Int(2)}
got, err := parser.extractObject()
assertNoError(t, err)
assertDeepEqual(t, got, expected)
})

t.Run("parse correctly if the last line is a comment", func(t *testing.T) {
config := `{
a: 1
Expand Down

0 comments on commit 7fec36e

Please sign in to comment.