Skip to content

Commit

Permalink
[oil-language] Allow Eof_Real after 'var' and 'setvar'.
Browse files Browse the repository at this point in the history
I figured out what is wrong with 'var' inside command subs.  We're
sharing the same expression parser!
  • Loading branch information
Andy Chu committed Aug 5, 2019
1 parent 257f590 commit d45aeea
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
4 changes: 2 additions & 2 deletions oil_lang/expr_parse.py
Expand Up @@ -227,7 +227,7 @@ def _PushOilTokens(parse_ctx, gr, p, lex):
if tok.id == Id.Left_AtParen:
# NOTE: Not using Right_ArrayLiteral like command mode one. That is only
# for command sub I believe.
lex.PushHint(Id.Op_RParen, Id.Op_RParen)
lex.PushHint(Id.Op_RParen, Id.Right_ArrayLiteral)

# Blame the opening token
line_reader = reader.DisallowedLineReader(parse_ctx.arena, tok)
Expand All @@ -240,7 +240,7 @@ def _PushOilTokens(parse_ctx, gr, p, lex):

if isinstance(w, word__TokenWord):
word_id = word.CommandId(w)
if word_id == Id.Op_RParen:
if word_id == Id.Right_ArrayLiteral:
break
elif word_id == Id.Op_Newline: # internal newlines allowed
continue
Expand Down
16 changes: 11 additions & 5 deletions oil_lang/expr_parse_test.py
Expand Up @@ -98,16 +98,22 @@ def testShellCommandSub(self):
hi)
""")

node = self._ParseOsh('var x = $(echo $((1+2)));')
#node = self._ParseOsh('var x = $(var x = @(a b));')

# Here docs use the Reader, so aren't allowed
return
node = self._ParseOsh("""var x = $(cat <<EOF
self.assertRaises(util.ParseError, self._ParseOsh, """var x = $(cat <<EOF
hi
EOF)
""")

node = self._ParseOsh('var x = $(echo $((1+2)));')
node = self._ParseOsh('var x = $(for i in 1 2 3; do echo $i; done);')

node = self._ParseOsh('var x = @(a b)')

# TODO: Recursive 'var' shouldn't be allowed!
return
node = self._ParseOsh('var x = $(var x = @(a b););')
node = self._ParseOsh('var x = $(var x = @(a b));')

def testOtherExpr(self):
"""Some examples copied from pgen2/pgen2-test.sh mode-test."""

Expand Down
11 changes: 6 additions & 5 deletions oil_lang/grammar.pgen2
Expand Up @@ -171,7 +171,7 @@ array_literal: (
'@[' [WS_Space] [word] (WS_Space word)* [WS_Space] Op_RBracket
)

sh_array_literal: '@(' Expr_WordsDummy Op_RParen
sh_array_literal: '@(' Expr_WordsDummy Right_ArrayLiteral
sh_command_sub: '$(' Expr_CommandDummy Eof_RParen

char_class_part: Lit_Chars # TODO: letters, escaped, and ranges
Expand Down Expand Up @@ -216,10 +216,11 @@ lvalue_list: lvalue (',' lvalue)*

type_expr: NAME [ '[' type_expr (',' type_expr)* ']' ]

# TODO: Eof should be allowed too.

oil_var: lvalue_list [type_expr] '=' testlist (Op_Semi | Op_Newline)
oil_setvar: lvalue_list (augassign | '=') testlist (Op_Semi | Op_Newline)
# NOTE: Eof_RParen and Eof_Backtick aren't allowed because we don't want 'var'
# in command subs.
end_assign: Op_Semi | Op_Newline | Eof_Real
oil_var: lvalue_list [type_expr] '=' testlist end_assign
oil_setvar: lvalue_list (augassign | '=') testlist end_assign

# Problem: we can't end it on a newline because say dict literals will have
# multiple lines. But EOF isn't right either.
Expand Down

0 comments on commit d45aeea

Please sign in to comment.