Skip to content
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

[osh/cmd_parse] Produce a more useful error message if a dangling do is detected #1570

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
19 changes: 18 additions & 1 deletion osh/cmd_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from _devbuild.gen.id_kind_asdl import Id, Id_t, Kind
from _devbuild.gen.types_asdl import lex_mode_e
from _devbuild.gen.syntax_asdl import (
loc,
loc, loc_t,
condition, condition_t,
for_iter,
command, command_t,
Expand Down Expand Up @@ -1241,11 +1241,13 @@ def _ParseForEachLoop(self, for_spid):

in_spid = runtime.NO_SPID
semi_spid = runtime.NO_SPID
dangling_do = None # type: Optional[loc_t]

self._Peek()
if self.c_id == Id.KW_In:
in_spid = word_.LeftMostSpanForWord(self.cur_word) # for translation only


self._Next() # skip in
if self.w_parser.LookPastSpace() == Id.Op_LParen:
enode, last_token = self.parse_ctx.ParseOilExpr(self.lexer, grammar_nt.oil_expr)
Expand All @@ -1272,6 +1274,16 @@ def _ParseForEachLoop(self, for_spid):
if num_iter_names > 2:
p_die('Expected at most 2 loop variables', loc.Span(for_spid))

# check for dangling "do" word (might be indicative of a syntax error)
if len(iter_words) > 0:
last_word = iter_words[-1]
if len(last_word.parts) == 1:
UP_last_word_part = last_word.parts[0]
if UP_last_word_part.tag_() == word_part_e.Literal:
last_word_part = cast(Token, UP_last_word_part)
if last_word_part.id == Id.KW_Do:
dangling_do = loc.Word(last_word_part)

elif self.c_id == Id.KW_Do:
node.iterable = for_iter.Args() # implicitly loop over "$@"
# do not advance
Expand All @@ -1287,6 +1299,11 @@ def _ParseForEachLoop(self, for_spid):
if self.c_id == Id.Lit_LBrace: # parse_opts.parse_brace() must be on
node.body = self.ParseBraceGroup()
else:
if self.c_id != Id.KW_Do and dangling_do:
# calling self.ParseDoGroup will fail, we can produce a better error
# message if we know the last item in the iterable list is "do"
p_die('Expected a semi-colon or newline before the "do".', dangling_do)

node.body = self.ParseDoGroup()

node.spids.append(in_spid)
Expand Down