~andyc/oil

7fabe88ece9092ac98a926a69aaae1a22c613c8f — Andy Chu a month ago b071056
[oil-language] Fix bug parsing elif.

Missing self._Peek().

Thanks to Danilo Spinella for the bug report!

Add a couple other test cases.
3 files changed, 35 insertions(+), 13 deletions(-)

M osh/cmd_parse.py
M spec/oil-user-feedback.test.sh
A spec/testdata/global-lib.sh
M osh/cmd_parse.py => osh/cmd_parse.py +4 -2
@@ 1358,7 1358,7 @@ class CommandParser(object):
    if test -f foo {
      echo foo
    } elif test -f bar; test -f spam {
  # ^ we parsed up to here
    ^ we parsed up to here
      echo bar
    } else {
      echo none


@@ 1369,7 1369,8 @@ class CommandParser(object):
    while self.c_id == Id.KW_Elif:
      elif_spid = word_.LeftMostSpanForWord(self.cur_word)
      self._Next()  # skip elif
      if self.parse_opts.parse_paren() and self.w_parser.LookAhead() == Id.Op_LParen:
      if (self.parse_opts.parse_paren() and
          self.w_parser.LookAhead() == Id.Op_LParen):
        enode, _ = self.parse_ctx.ParseOilExpr(self.lexer, grammar_nt.oil_expr)
        # NOTE: OilCondition could have spids of ( and ) ?
        cond = condition.Oil(enode)  # type: condition_t


@@ 1380,6 1381,7 @@ class CommandParser(object):
        cond = condition.Shell(commands.children)

      body = self.ParseBraceGroup()
      self._Peek()

      arm = syntax_asdl.if_arm(cond, body.children, [elif_spid])
      arms.append(arm)

M spec/oil-user-feedback.test.sh => spec/oil-user-feedback.test.sh +22 -11
@@ 35,23 35,32 @@ not equal
## END


#### Regex Literal
#### elif bug
if (true) {
  echo A
} elif (true) {
  echo B
} elif (true) {
  echo C
} else {
  echo else
}
## STDOUT:
A
## END

# This seems to work as expected?
#### global vars
builtin set -u

proc get_opt(arg, :out) {
  setref out = $(write -- $arg | cut -d'=' -f2)
main() {
  source $REPO_ROOT/spec/testdata/global-lib.sh
}

var variant = ''
var arg = '--variant=foo'
if ( arg ~ / '--variant=' <word> / ) {
  get_opt $arg :variant
  echo variant=$variant
}
main
test_func

## status: 1
## STDOUT:
variant=foo
## END

#### Julia port


@@ 121,3 130,5 @@ done
## STDOUT:
x
## END



A spec/testdata/global-lib.sh => spec/testdata/global-lib.sh +9 -0
@@ 0,0 1,9 @@
# for spec/oil-user-feedback

#var a = 'here'

declare a='here'

test_func() {
  echo $a
}