Skip to content

Commit

Permalink
[osh] Start parsing case terminators ;& and ;;&
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy C committed Mar 29, 2024
1 parent efd5f56 commit 7842a6b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
2 changes: 2 additions & 0 deletions frontend/id_kind_def.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ def AddKinds(spec):
'DPipe', # ||
'Semi', # ;
'DSemi', # ;; for case
'SemiAmp', # ;& for case
'DSemiAmp', # ;;& for case
'LParen', # For subshell. Not Kind.Left because it's NOT a WordPart.
'RParen', # Default, will be translated to Id.Right_*
'DLeftParen',
Expand Down
3 changes: 3 additions & 0 deletions frontend/lexer_def.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,10 @@ def R(pat, tok_type):
C('&&', Id.Op_DAmp),
C('||', Id.Op_DPipe),
C(';', Id.Op_Semi),
# Case terminators
C(';;', Id.Op_DSemi),
C(';&', Id.Op_SemiAmp),
C(';;&', Id.Op_DSemiAmp),
C('(', Id.Op_LParen),
C(')', Id.Op_RParen),
R(r'[^\0]', Id.Lit_Other), # any other single char is a literal
Expand Down
12 changes: 7 additions & 5 deletions osh/cmd_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from __future__ import print_function

from _devbuild.gen import grammar_nt
from _devbuild.gen.id_kind_asdl import Id, Id_t, Kind, Kind_str
from _devbuild.gen.id_kind_asdl import Id, Id_t, Id_str, Kind, Kind_str
from _devbuild.gen.types_asdl import lex_mode_e, cmd_mode_e, cmd_mode_t
from _devbuild.gen.syntax_asdl import (
loc,
Expand Down Expand Up @@ -1995,8 +1995,9 @@ def ParseCompoundCommand(self):
return self.ParseTime()

# Happens in function body, e.g. myfunc() oops
p_die('Unexpected word while parsing compound command',
loc.Word(self.cur_word))
p_die(
'Unexpected word while parsing compound command (%s)' %
Id_str(self.c_id), loc.Word(self.cur_word))
assert False # for MyPy

def ParseFunctionDef(self):
Expand Down Expand Up @@ -2547,8 +2548,9 @@ def _ParseCommandLine(self):

else:
# e.g. echo a(b)
p_die('Invalid word while parsing command line',
loc.Word(self.cur_word))
p_die(
'Invalid word while parsing command line (%s)' %
Id_str(self.c_id), loc.Word(self.cur_word))

children.append(child)

Expand Down
36 changes: 36 additions & 0 deletions test/parse-errors.sh
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,42 @@ cases-in-files() {
done
}

test-case() {
readonly -a YES=(
'case $x in foo) echo ;; esac'
#'case $x in foo) echo ;& esac'
#'case $x in foo) echo ;;& esac'
)

readonly -a NO=(
';&'
'echo ;&'
'echo ;;&'
)

for c in "${YES[@]}"; do
echo "--- test-case YES $c"

_osh-should-parse "$c"
echo

bash -n -c "$c"
echo bash=$?
done

for c in "${NO[@]}"; do
echo "--- test-case NO $c"

_osh-parse-error "$c"

set +o errexit
bash -n -c "$c"
echo bash=$?
set -o errexit
done

}

all() {
section-banner 'Cases in Files'

Expand Down

0 comments on commit 7842a6b

Please sign in to comment.