~emersion/public-inbox

3 2

[go-scfg] attach comments to Directives

Sebastien Binet
Details
Message ID
<CDF182CXDAKT.13CQA2B3QUGC7@clrinfopc42>
DKIM signature
missing
Download raw message
hi,

I am considering using go-scfg for an application.
an essential component of that application would be (among other things)
to reformat "the one true way" scfg files (like gofmt does).
for this, it would be needed to have a way to retain comments and the
Directive they are attached to.

is this something that has been considered but rejected?

cheers,
-s
Details
Message ID
<smOm-hE3k4xW0vTfFR4scvdSVp8qBRUNjganRf7rUnmyJwJAQsY7VJduWhMg1EWOa6r-U-HaSBrH6Ir3aqju9Ltl_THQxeciq2eQrqJRX-A=@emersion.fr>
In-Reply-To
<CDF182CXDAKT.13CQA2B3QUGC7@clrinfopc42> (view parent)
DKIM signature
pass
Download raw message
On Monday, August 9th, 2021 at 15:55, Sebastien Binet <binet@cern.ch> wrote:

> an essential component of that application would be (among other things)
> to reformat "the one true way" scfg files (like gofmt does).
> for this, it would be needed to have a way to retain comments and the
> Directive they are attached to.
>
> is this something that has been considered but rejected?

I'd welcome such an improvement. I wonder how it could look like as far
as the API design goes though. How does the Go stdlib represent this?

I think we're also missing some line/col/index information in our AST,
maybe that'd be useful for you as well?
Sebastien Binet
Details
Message ID
<CDF3QFHEKTNP.ZHK1TWR0I1GR@clrinfopc42>
In-Reply-To
<smOm-hE3k4xW0vTfFR4scvdSVp8qBRUNjganRf7rUnmyJwJAQsY7VJduWhMg1EWOa6r-U-HaSBrH6Ir3aqju9Ltl_THQxeciq2eQrqJRX-A=@emersion.fr> (view parent)
DKIM signature
missing
Download raw message
On Mon Aug 9, 2021 at 16:20 CET, Simon Ser wrote:
> On Monday, August 9th, 2021 at 15:55, Sebastien Binet <binet@cern.ch>
> wrote:
>
> > an essential component of that application would be (among other things)
> > to reformat "the one true way" scfg files (like gofmt does).
> > for this, it would be needed to have a way to retain comments and the
> > Directive they are attached to.
> >
> > is this something that has been considered but rejected?
>
> I'd welcome such an improvement. I wonder how it could look like as far
> as the API design goes though. How does the Go stdlib represent this?

the Go stdlib has a go/ast.Comment type:

$> go doc go/ast Comment
package ast // import "go/ast"

type Comment struct {
	Slash token.Pos // position of "/" starting the comment
	Text  string    // comment text (excluding '\n' for //-style comments)
}
    A Comment node represents a single //-style or /*-style comment.

    The Text field contains the comment text without carriage returns (\r) that
    may have been present in the source. Because a comment's end position is
    computed using len(Text), the position reported by End() does not match the
    true source end position for comments containing carriage returns.

func (c *Comment) End() token.Pos
func (c *Comment) Pos() token.Pos

I remember (when working on go-python/gopy) that it was a pain to
reassociate a Comment text with a given go/types node.

> I think we're also missing some line/col/index information in our AST,
> maybe that'd be useful for you as well?

wouldn't hurt for nicer diagnostics when there's a "fat fingers" error
:)

-s
Details
Message ID
<-9sCj5kemTmdPt1rpVWeNDA7fJKXC_d-09R3qxVcT7osKBNqu9DNLE6ixOe0juUerDIQGdGDQp-0dNBKGtLQUSFHyzNz5fCzzN5Gq3HDwDM=@emersion.fr>
In-Reply-To
<CDF3QFHEKTNP.ZHK1TWR0I1GR@clrinfopc42> (view parent)
DKIM signature
pass
Download raw message
OK, so a list of comments in Directive (and possibly Block to
accommodate for the empty file?) should do the trick? Or just a string
containing the comment lines, I suppose.

We probably don't want to allow comments at end of lines, because of
the reasons stated in [1].

Parsing the comments will likely require migrating away from shlex,
which is something I've wanted to do for ages. shlex just eats the
comments and doesn't allow us to access them. Let me know if you don't
have time to prepare a patch for this, I'll try to get time to do it.

[1]: https://lists.sr.ht/~emersion/public-inbox/%3CC6WOAAJ4R55M.8AKQ3HAU9971%40nitro%3E
Reply to thread Export thread (mbox)