Skip to content

[llvm-lit] Support curly brace syntax in lit internal shell #102829

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

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 3 additions & 2 deletions llvm/utils/lit/lit/ShCommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,15 @@ def toShell(self, file, pipefail=False):


class Seq:
def __init__(self, lhs, op, rhs):
def __init__(self, lhs, op, rhs, seq_type):
assert op in (";", "&", "||", "&&")
self.op = op
self.lhs = lhs
self.rhs = rhs
self.type = seq_type

def __repr__(self):
return "Seq(%r, %r, %r)" % (self.lhs, self.op, self.rhs)
return "Seq(%r, %r, %r, %r)" % (self.lhs, self.op, self.rhs, self.type)

def __eq__(self, other):
if not isinstance(other, Seq):
Expand Down
36 changes: 33 additions & 3 deletions llvm/utils/lit/lit/ShUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@ def lex_one_token(self):
lex_one_token - Lex a single 'sh' token."""

c = self.eat()
if c == "{" or c == "}":
return (c,)
if c == ";":
if self.maybe_eat("}") or (self.maybe_eat(" ") and self.maybe_eat("}")):
return("}",)
return (c,)
if c == "|":
if self.maybe_eat("|"):
Expand Down Expand Up @@ -200,6 +204,8 @@ def __init__(self, data, win32Escapes=False, pipefail=False):
self.data = data
self.pipefail = pipefail
self.tokens = ShLexer(data, win32Escapes=win32Escapes).lex()
self.brace_stack = []
self.brace_dict = {'{': '}'}

def lex(self):
for item in self.tokens:
Expand Down Expand Up @@ -255,18 +261,42 @@ def parse_pipeline(self):
self.lex()
commands.append(self.parse_command())
return Pipeline(commands, negate, self.pipefail)


# {echo foo; echo bar;} && echo hello
# echo hello && {echo foo; echo bar}

def parse(self, seq_type):
lhs = None
if isinstance(self.look(), tuple):
brace = self.lex()
self.brace_stack.append(brace)
if brace[0] == '{':
lhs = self.parse(('{', '}'))
else:
raise ValueError("syntax error near unexpected token %r" % brace[0])

def parse(self):
lhs = self.parse_pipeline()
else:
lhs = self.parse_pipeline()

while self.look():
operator = self.lex()
assert isinstance(operator, tuple) and len(operator) == 1

if operator == self.brace_dict[self.brace_stack.peek()]:
break

if not self.look():
raise ValueError("missing argument to operator %r" % operator[0])

# FIXME: Operator precedence!!
lhs = Seq(lhs, operator[0], self.parse_pipeline())
if isinstance(self.look(), tuple):
lhs = self.parse(('{', '}'))
else:
lhs = Seq(lhs, operator[0], self.parse_pipeline(), seq_type)
seq_type = None

if not stack.empty():
raise ValueError("missing token to %r" % stack.peek())

return lhs
2 changes: 1 addition & 1 deletion llvm/utils/lit/lit/TestRunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,7 @@ def executeScriptInternal(
ln = command
try:
cmds.append(
ShUtil.ShParser(ln, litConfig.isWindows, test.config.pipefail).parse()
ShUtil.ShParser(ln, litConfig.isWindows, test.config.pipefail).parse(None)
)
except:
raise ScriptFatal(
Expand Down
Loading