Skip to content

Commit

Permalink
number name support and tests
Browse files Browse the repository at this point in the history
Removed unecessary test function.
  • Loading branch information
Parzival-3141 committed Jan 26, 2024
1 parent 3c9f0a5 commit 603f4ae
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 27 deletions.
16 changes: 15 additions & 1 deletion lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,10 @@ func lexStartExpansion(l *lexer) stateFn {
l.ignore()
l.depth++
return lexBracketName
case isAlphaNum(c):
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
31 changes: 5 additions & 26 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 Down Expand Up @@ -206,28 +210,3 @@ func TestExpand_assign(t *testing.T) {
equals(t, "word", x)
equals(t, map[string]string{"unset": "word"}, mapping)
}

func TestExpand_shellPositionalArg(t *testing.T) {
mapping := map[string]string{
"1": "foo",
"2": "bar",
}

params := []struct {
in string
out string
}{
{"$1$2", "foobar"},
{"/home/$1/$2", "/home/foo/bar"},
}

for _, tt := range params {
x, err := Expand(tt.in, Map(mapping))
if err != nil {
t.Errorf("pattern %#v should not have produced an error, but got: %s", tt.in, err)
}
if x != tt.out {
t.Errorf("pattern %#v should expand to %#v, but got %#v", tt.in, tt.out, x)
}
}
}

0 comments on commit 603f4ae

Please sign in to comment.