Skip to content

Commit

Permalink
parse: proper single char variable detection (#45)
Browse files Browse the repository at this point in the history
Fixes #43, regression from #42
  • Loading branch information
neiser authored Feb 8, 2023
1 parent 4932428 commit 97345a5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions parse/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ func lexSubstitutionOperator(l *lexer) stateFn {
return lexText
case r == eof || isEndOfLine(r):
return l.errorf("closing brace expected")
case isAlphaNumeric(r) && strings.HasPrefix(l.input[l.lastPos:], "${"):
return lexVariable
case r == '+':
l.emit(itemPlus)
case r == '-':
Expand Down
6 changes: 6 additions & 0 deletions parse/lex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ var lexTests = []lexTest{
{itemVariable, 0, "$hello"},
tEOF,
}},
{"single char var", "${A}", []item{
tLeft,
{itemVariable, 0, "A"},
tRight,
tEOF,
}},
{"2 vars", "$hello $world", []item{
{itemVariable, 0, "$hello"},
{itemText, 0, " "},
Expand Down
4 changes: 4 additions & 0 deletions parse/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var FakeEnv = []string{
"FOO=foo",
"EMPTY=",
"ALSO_EMPTY=",
"A=AAA",
}

type mode int
Expand Down Expand Up @@ -69,6 +70,9 @@ var parseTests = []parseTest{
{"gh-issue-41-3", "${NOTSET=-1}", "-1", errNone},
{"gh-issue-41-4", "${NOTSET:==1}", "=1", errNone},

// single letter
{"gh-issue-43-1", "${A}", "AAA", errNone},

// bad substitution
{"closing brace expected", "hello ${", "", errAll},

Expand Down

0 comments on commit 97345a5

Please sign in to comment.