Skip to content

Commit

Permalink
comments and name improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ionous committed Dec 15, 2023
1 parent fcff1f5 commit e6f7dca
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 14 deletions.
7 changes: 4 additions & 3 deletions decode/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ func (d *Decoder) SetMapper(maps collect.MapFactory) {
d.collector.maps = maps
}

// record comment blocks
// pass a valid target for document level comments
// a nil disables comment collection
// ( comments are disabled by default )
func (d *Decoder) UseNotes(b *note.Book) {
d.docBlock = b
d.collector.keepComments = b != nil
Expand Down Expand Up @@ -218,7 +220,7 @@ func (d *Decoder) newKey(at token.Pos, key string) (err error) {
} else if e := d.out.setKey(at.Y, key); e != nil {
err = e
} else {
d.out.NextKey()
d.out.NextTerm()
d.state = d.waitForValue // same as current state.
}
return
Expand Down Expand Up @@ -248,7 +250,6 @@ func (d *Decoder) newComment(defaultType note.Type, at token.Pos, str string) (e
d.out.Comment(noteType, str)
}
}

}
return
}
6 changes: 4 additions & 2 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ func (d *Decoder) SetSequencer(seq collect.SequenceFactory) {
d.inner.SetSequencer(seq)
}

// control the creation of comment blocks for the upcoming Decode.
// the default is to discard comments.
// pass a valid target for collecting document level comments
// during an upcoming call to Decode.
// the default behavior is to discard comments.
// ( passing nil will also discard them. )
func (d *Decoder) UseNotes(b *note.Book) {
d.inner.UseNotes(b)
}
Expand Down
7 changes: 4 additions & 3 deletions note/book.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package note

import "strings"

// record comment block
// collects comments to generate a comment block
// ( during document decoding )
type Book struct {
book content
}
Expand All @@ -17,9 +18,9 @@ func (p *Book) EndCollection() {
p.book.EndCollection()
}
}
func (p *Book) NextKey() {
func (p *Book) NextTerm() {
if p.book.buf != nil {
p.book.NextKey()
p.book.NextTerm()
}
}
func (p *Book) Comment(kind Type, str string) {
Expand Down
7 changes: 5 additions & 2 deletions note/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ type bookState struct {

func (b *content) BeginCollection(buf *strings.Builder) {
b.buf = buf
// if there is a (footer or prefix) comment pending
// steal it from the shared buffer, and use it as
// the header of the first element.
if buf.Len() > 0 {
appendLine(&b.out, buf.String())
buf.Reset()
Expand All @@ -57,7 +60,7 @@ func (b *content) EndCollection() {
}

// new key in this block
func (b *content) NextKey() {
func (b *content) NextTerm() {
// note: if there's a sub-collection
// its begin() will have stolen our buffer away
b.nextKeys++
Expand Down Expand Up @@ -110,7 +113,7 @@ func (b *content) flushTerm() {
//
// FirstKey: # inline prefix
// # header for next key
// NextKey:
// NextTerm:
//
if b.buf.Len() > 0 {
b.writeKeys()
Expand Down
20 changes: 17 additions & 3 deletions note/note.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,26 @@ package note

import "strings"

// a comment block
// a comment block generator
// see Nothing ( which discards comments )
// and Book ( which compiles comments into a comment block. )
type Taker interface {
// start recording comments for a new sequence, mapping, or document.
// every collection in a document must share the same string builder;
// but each should probably have its own unique taker.
// passing nil will disable comment collection.
BeginCollection(*strings.Builder)
// record a comment
Comment(Type, string)
NextKey()
// separates comments for each term within a collection
// ( terms in a sequence are indicated by a dash
// terms in a mapping are indicated by a signature style key )
NextTerm()
// stop recording comments for this collection
// probably best to not reuse the taker after this call.
EndCollection()
// return true
// return the unified comment block for a collection.
// initially true, if BeginCollection had been passed a valid string buffer.
// subsequent calls may return false.
Resolve() (ret string, okay bool)
}
4 changes: 4 additions & 0 deletions note/noteType.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package note

// differentiates different comment types
// as described in the package README.
type Type int

const (
Expand All @@ -16,6 +18,7 @@ const (
Footer
)

// is this one of the two prefix types?
func (n Type) Prefix() (okay bool) {
switch n {
case Prefix, PrefixInline:
Expand All @@ -24,6 +27,7 @@ func (n Type) Prefix() (okay bool) {
return
}

// is this one of the two suffix types?
func (n Type) Suffix() (okay bool) {
switch n {
case Suffix, SuffixInline:
Expand Down
2 changes: 1 addition & 1 deletion note/nothing.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ type Nothing struct{}

func (Nothing) BeginCollection(buf *strings.Builder) {}
func (Nothing) EndCollection() {}
func (Nothing) NextKey() {}
func (Nothing) NextTerm() {}
func (Nothing) Comment(Type, string) {}
func (Nothing) Resolve() (_ string, _ bool) { return }

0 comments on commit e6f7dca

Please sign in to comment.