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

Allow simple expansion variables to start with a number #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ func lexStartExpansion(l *lexer) stateFn {
return lexBracketName
case isAlpha(c):
return lexSimpleName
case isNum(c):
return lexNumberName
}
return nil // FIXME
}
Expand All @@ -310,6 +312,18 @@ func lexSimpleName(l *lexer) stateFn {
}
}

func lexNumberName(l *lexer) stateFn {
for {
if !isNum(l.next()) {
l.backup()
name := l.token()
l.emit(itemReadParam(name))
l.ignore()
return lexText
}
}
}

func lexBracketName(l *lexer) stateFn {
if l.next() == '#' {
l.ignore()
Expand Down
10 changes: 7 additions & 3 deletions posix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ var paramtests = []struct {
{"${null}", "", ""},
{"${unset}", "", ""},
{"${1}X${2}", "oneXtwo", ""},
{"${11}X${22}", "elevenXtwenty-two", ""},

// Names, no brackets
{"$set", "yes", ""},
{"$set$set2", "yesyes-two", ""},
// {"$1X$2", "oneXtwo", ""}, // TODO(#3) parse numeric simple names
{"$1X$2", "oneXtwo", ""},
{"$11X$22", "elevenXtwenty-two", ""},

// Default
{"${set:-word}", "yes", ""},
Expand Down Expand Up @@ -154,6 +156,8 @@ func TestExpand_simple(t *testing.T) {
"null": "",
"1": "one",
"2": "two",
"11": "eleven",
"22": "twenty-two",
}

for _, tt := range paramtests {
Expand All @@ -171,7 +175,7 @@ func TestExpand_simple(t *testing.T) {
}
}

func TestExand_assignReadOnlyFunc(t *testing.T) {
func TestExpand_assignReadOnlyFunc(t *testing.T) {
_, err := Expand("${unset:=word}", Func(func(s string) string {
return ""
}))
Expand All @@ -180,7 +184,7 @@ func TestExand_assignReadOnlyFunc(t *testing.T) {
}
}

func TestExand_assignReadOnlyMap(t *testing.T) {
func TestExpand_assignReadOnlyMap(t *testing.T) {
_, err := Expand("${unset:=word}", Map(nil))
if err == nil {
t.Fatal("assignment on read-only map should return an error")
Expand Down