Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse: proper single char variable detection #45

Merged
merged 1 commit into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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