Skip to content

Commit 086bcae

Browse files
committed
Add comments and inline comments support
This is a workaround. The ideal would be implement in the grammar Signed-off-by: Ulysses Souza <[email protected]>
1 parent 02cbe99 commit 086bcae

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

envlang_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,19 +249,18 @@ A = "aaa ${B} ccc "
249249
func TestFull(t *testing.T) {
250250
log.SetLevel(log.DebugLevel)
251251
is := `
252+
# my comment
252253
253-
254-
A=aaa
255-
B="bbb"
254+
A=aaa # my inline comment on A
255+
B="bbb" # my inline comment on B
256256
C =ccc
257257
D= ddd
258-
E='eee'
258+
E='eee' # my inline comment on E
259259
F=
260260
G
261261
262262
H="my_value"
263263
264-
265264
I = bar baz
266265
267266
export EXPORTED_VAR = exported_value

handlers/filehandler.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ func (l *EnvLangFileListener) ExitEntry(c *fileparser.EntryContext) {
3737
// TODO: Implement the logic to handle the export keyword in the grammar file
3838
id, _ = strings.CutPrefix(id, "export ")
3939
id = strings.TrimSpace(id)
40+
if strings.HasPrefix(id, "#") {
41+
return
42+
}
4043

4144
hasAssign := true
4245
if c.ASSIGN() == nil || c.ASSIGN().GetText() == "" {
@@ -47,6 +50,7 @@ func (l *EnvLangFileListener) ExitEntry(c *fileparser.EntryContext) {
4750
valuePtr = &v
4851
}
4952

53+
toTrim := true
5054
if c.Value() != nil {
5155
v := strings.TrimSpace(c.Value().GetText())
5256
switch c.Value().GetStr().GetTokenType() {
@@ -55,8 +59,15 @@ func (l *EnvLangFileListener) ExitEntry(c *fileparser.EntryContext) {
5559
valuePtr = &v
5660
case fileparser.EnvLangFileParserDQSTRING:
5761
v = v[1 : len(v)-1] // Removing quotes
62+
toTrim = false
5863
fallthrough
5964
case fileparser.EnvLangFileParserTEXT:
65+
if strings.Index(v, "#") != 0 {
66+
v = strings.SplitN(v, "#", pair)[0]
67+
if toTrim {
68+
v = strings.TrimSpace(v)
69+
}
70+
}
6071
v = GetValue(l.d, v)
6172
valuePtr = &v
6273
default:

0 commit comments

Comments
 (0)