justinmk / tree-sitter-ini

tree-sitter grammar/parser for INI files
Apache License 2.0
20 stars 10 forks source link

inline comments, after the setting value #13

Open gregnis opened 4 weeks ago

gregnis commented 4 weeks ago

In the following example

[section name]
some_key = some_value # comment
another-key = another value ; comment

[another section]
# a comment
;some_key = some_value
an#other-key = another value

comments on lines 2 and 3 are not detected whereas all other are:

document [0, 0] - [8, 0]
  section [0, 0] - [3, 0]
    section_name [0, 0] - [1, 0]
      text [0, 1] - [0, 13]
    setting [1, 0] - [2, 0]
      setting_name [1, 0] - [1, 8]
      setting_value [1, 10] - [1, 31]
    setting [2, 0] - [3, 0]
      setting_name [2, 0] - [2, 11]
      setting_value [2, 13] - [2, 37]
  section [4, 0] - [5, 0]
    section_name [4, 0] - [5, 0]
      text [4, 1] - [4, 16]
  comment [5, 0] - [6, 0]
    text [5, 1] - [5, 11]
  comment [6, 0] - [7, 0]
    text [6, 1] - [6, 22]
  ERROR [7, 0] - [7, 2]
    setting_name [7, 0] - [7, 2]
  comment [7, 2] - [8, 0]
    text [7, 3] - [7, 28]

``

justinmk commented 4 weeks ago

Can't find a reference about this. https://stackoverflow.com/a/19550081/152142 indicates that there is no spec for this.

If this was supported, then "#" could not be used in setting values (unless it was escaped; is there a standard escape char?).

Also, this directly conflicts with https://github.com/justinmk/tree-sitter-ini/issues/12 .

Need a clear spec one way or the other. Else we're just fumbling around and will do unexpected things depending on the INI "flavor". A spec for each "flavor" would be a starting point.

gregnis commented 4 weeks ago

I was going off https://en.wikipedia.org/wiki/INI_file, the Comments section. I do agree with you about following GetPrivateProfileString as a spec, and if you do that, the current behavior is almost correct. I say "almost" since, if there is a comment symbol in the setting name, like in

an#other-key = another value

it is treated as a comment after and including the '#', which makes the behavior inconsistent.

justinmk commented 4 weeks ago

https://en.wikipedia.org/wiki/INI_file#Comments

... Under the WinAPI's GetPrivateProfileString's dialect, comments must occur on lines by themselves.

This seems the most reasonable. Especially given that escape chars don't seem standard.

OTOH, gitconfig appears to support inline comments, except when quoted in a string?

gregnis commented 4 weeks ago

OTOH, gitconfig appears to support inline comments, except when quoted in a string?

I think this is the most reasonable thing to do, if possible,