From 603f4aea1c3d7015b6bec5df0cba1f885693407d Mon Sep 17 00:00:00 2001 From: Parzival-3141 <29632054+Parzival-3141@users.noreply.github.com> Date: Fri, 26 Jan 2024 18:37:50 -0500 Subject: [PATCH] number name support and tests Removed unecessary test function. --- lexer.go | 16 +++++++++++++++- posix_test.go | 31 +++++-------------------------- 2 files changed, 20 insertions(+), 27 deletions(-) diff --git a/lexer.go b/lexer.go index 514a02a..f300959 100644 --- a/lexer.go +++ b/lexer.go @@ -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 } @@ -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() diff --git a/posix_test.go b/posix_test.go index d63f029..ffa3b86 100644 --- a/posix_test.go +++ b/posix_test.go @@ -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", ""}, @@ -154,6 +156,8 @@ func TestExpand_simple(t *testing.T) { "null": "", "1": "one", "2": "two", + "11": "eleven", + "22": "twenty-two", } for _, tt := range paramtests { @@ -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) - } - } -}