Skip to content

Commit

Permalink
handle comments inside literal of option (emicklei#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
emicklei authored Nov 2, 2023
1 parent 6f007c9 commit 4ced960
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
15 changes: 15 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ type Literal struct {
Source string
IsString bool

// It not nil then the entry is actually a comment with line(s)
// modelled this way because Literal is not an elementContainer
Comment *Comment

// The rune use to delimit the string value (only valid iff IsString)
QuoteRune rune

Expand Down Expand Up @@ -191,6 +195,17 @@ func (l Literal) SourceRepresentation() string {
// parse expects to read a literal constant after =.
func (l *Literal) parse(p *Parser) error {
pos, tok, lit := p.next()
// handle special element inside literal, a comment line
if isComment(lit) {
nc := newComment(pos, lit)
if l.Comment == nil {
l.Comment = nc
} else {
l.Comment.Merge(nc)
}
// continue with remaining entries
return l.parse(p)
}
if tok == tLEFTSQUARE {
// collect array elements
array := []*Literal{}
Expand Down
27 changes: 27 additions & 0 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,3 +741,30 @@ func TestStringValuesParsedAsNumbers(t *testing.T) {
t.Errorf("got [%v:%T] want [%v:%T]", got, got, want, want)
}
}

func TestCommentInsideArray(t *testing.T) {
src := `option test = {
scope_rules : [
// A comment
// Another comment
{has : [
// comment on test
"test"
]}
]
};
`
opt, err := newParserOn(src).Parse()
if err != nil {
t.Fatal(err)
}
opt2 := opt.Elements[0].(*Option)
scp, _ := opt2.Constant.OrderedMap.Get("scope_rules")
elem0 := scp.Array[0]
t.Log("comment:", elem0.Comment.Lines)
has, _ := elem0.OrderedMap.Get("has")
if got, want := has.Array[0].Source, "test"; got != want {
t.Errorf("got [%v:%T] want [%v:%T]", got, got, want, want)
}
t.Log("comment:", has.Array[0].Comment.Lines)
}

0 comments on commit 4ced960

Please sign in to comment.