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

lexer: allow extglob wildcards as function names #771

Merged
merged 1 commit into from
Dec 11, 2021
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
14 changes: 14 additions & 0 deletions interp/interp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3117,6 +3117,20 @@ hello, world
hello, world
`,
},
{
// globbing wildcard as function name
`@() { echo "$@"; }; @ lala; function +() { echo "$@"; }; + foo`,
"lala\nfoo\n",
},
{
` @() { echo "$@"; }; @ lala;`,
"lala\n",
},
{
// globbing wildcard as function name but with space after the name
`+ () { echo "$@"; }; + foo; @ () { echo "$@"; }; @ lala; ? () { echo "$@"; }; ? bar`,
"foo\nlala\nbar\n",
},
}

var runTestsWindows = []runTest{
Expand Down
26 changes: 24 additions & 2 deletions syntax/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ skipSpace:
p.advanceLitNone(r)
}
case '?', '*', '+', '@', '!':
if p.peekByte('(') {
if p.tokenizeGlob() {
switch r {
case '?':
p.tok = globQuest
Expand Down Expand Up @@ -346,6 +346,28 @@ skipSpace:
}
}

// tokenizeGlob determines whether the expression should be tokenized as a glob literal
func (p *Parser) tokenizeGlob() bool {
if p.val == "function" {
return false
}
// NOTE: empty pattern list is a valid globbing syntax, eg @()
// but we'll operate on the "likelihood" that it is a function;
// only tokenize if its a non-empty pattern list
if p.peekBytes("()") {
return false
}
return p.peekByte('(')
}

func (p *Parser) peekBytes(s string) bool {
for p.bsp+(len(p.bs)-1) >= len(p.bs) {
p.fill()
}
bw := p.bsp + len(s)
return bw < len(p.bs) && bytes.HasPrefix(p.bs[p.bsp:bw], []byte(s))
}

func (p *Parser) peekByte(b byte) bool {
if p.bsp == len(p.bs) {
p.fill()
Expand Down Expand Up @@ -882,7 +904,7 @@ loop:
tok = _Lit
break loop
case '?', '*', '+', '@', '!':
if p.peekByte('(') {
if p.tokenizeGlob() {
tok = _Lit
break loop
}
Expand Down