Skip to content

Commit 7842a6b

Browse files
author
Andy C
committed
[osh] Start parsing case terminators ;& and ;;&
1 parent efd5f56 commit 7842a6b

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

frontend/id_kind_def.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ def AddKinds(spec):
300300
'DPipe', # ||
301301
'Semi', # ;
302302
'DSemi', # ;; for case
303+
'SemiAmp', # ;& for case
304+
'DSemiAmp', # ;;& for case
303305
'LParen', # For subshell. Not Kind.Left because it's NOT a WordPart.
304306
'RParen', # Default, will be translated to Id.Right_*
305307
'DLeftParen',

frontend/lexer_def.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,10 @@ def R(pat, tok_type):
160160
C('&&', Id.Op_DAmp),
161161
C('||', Id.Op_DPipe),
162162
C(';', Id.Op_Semi),
163+
# Case terminators
163164
C(';;', Id.Op_DSemi),
165+
C(';&', Id.Op_SemiAmp),
166+
C(';;&', Id.Op_DSemiAmp),
164167
C('(', Id.Op_LParen),
165168
C(')', Id.Op_RParen),
166169
R(r'[^\0]', Id.Lit_Other), # any other single char is a literal

osh/cmd_parse.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from __future__ import print_function
1111

1212
from _devbuild.gen import grammar_nt
13-
from _devbuild.gen.id_kind_asdl import Id, Id_t, Kind, Kind_str
13+
from _devbuild.gen.id_kind_asdl import Id, Id_t, Id_str, Kind, Kind_str
1414
from _devbuild.gen.types_asdl import lex_mode_e, cmd_mode_e, cmd_mode_t
1515
from _devbuild.gen.syntax_asdl import (
1616
loc,
@@ -1995,8 +1995,9 @@ def ParseCompoundCommand(self):
19951995
return self.ParseTime()
19961996

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

20022003
def ParseFunctionDef(self):
@@ -2547,8 +2548,9 @@ def _ParseCommandLine(self):
25472548

25482549
else:
25492550
# e.g. echo a(b)
2550-
p_die('Invalid word while parsing command line',
2551-
loc.Word(self.cur_word))
2551+
p_die(
2552+
'Invalid word while parsing command line (%s)' %
2553+
Id_str(self.c_id), loc.Word(self.cur_word))
25522554

25532555
children.append(child)
25542556

test/parse-errors.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,42 @@ cases-in-files() {
630630
done
631631
}
632632

633+
test-case() {
634+
readonly -a YES=(
635+
'case $x in foo) echo ;; esac'
636+
#'case $x in foo) echo ;& esac'
637+
#'case $x in foo) echo ;;& esac'
638+
)
639+
640+
readonly -a NO=(
641+
';&'
642+
'echo ;&'
643+
'echo ;;&'
644+
)
645+
646+
for c in "${YES[@]}"; do
647+
echo "--- test-case YES $c"
648+
649+
_osh-should-parse "$c"
650+
echo
651+
652+
bash -n -c "$c"
653+
echo bash=$?
654+
done
655+
656+
for c in "${NO[@]}"; do
657+
echo "--- test-case NO $c"
658+
659+
_osh-parse-error "$c"
660+
661+
set +o errexit
662+
bash -n -c "$c"
663+
echo bash=$?
664+
set -o errexit
665+
done
666+
667+
}
668+
633669
all() {
634670
section-banner 'Cases in Files'
635671

0 commit comments

Comments
 (0)