Skip to content

Commit

Permalink
[frontend refactor] Function to get Token for CompoundWord
Browse files Browse the repository at this point in the history
This is a special case of word_t
  • Loading branch information
Andy C committed May 21, 2023
1 parent e78e0eb commit ebdce70
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
15 changes: 10 additions & 5 deletions frontend/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,15 @@ def _RightTokenForWordPart(part):
raise AssertionError(part.tag())


def LeftTokenForCompoundWord(w):
# type: (CompoundWord) -> Optional[Token]
if len(w.parts):
return LeftTokenForWordPart(w.parts[0])
else:
# This is possible for empty brace sub alternative {a,b,}
return None


def LeftTokenForWord(w):
# type: (word_t) -> Optional[Token]
if w is None:
Expand All @@ -334,11 +343,7 @@ def LeftTokenForWord(w):
with tagswitch(w) as case:
if case(word_e.Compound):
w = cast(CompoundWord, UP_w)
if len(w.parts):
return LeftTokenForWordPart(w.parts[0])
else:
# This is possible for empty brace sub alternative {a,b,}
return None
return LeftTokenForCompoundWord(w)

elif case(word_e.Token):
tok = cast(Token, UP_w)
Expand Down
14 changes: 6 additions & 8 deletions osh/cmd_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -987,8 +987,7 @@ def ParseSimpleCommand(self):
for preparsed in preparsed_list:
pairs.append(_MakeAssignPair(self.parse_ctx, preparsed, self.arena))

# TODO: get token directly
left_tok = location.LeftTokenForWord(words[0])
left_tok = location.LeftTokenForCompoundWord(words[0])
return command.ShAssignment(left_tok, pairs, redirects)

kind, kw_token = word_.IsControlFlow(suffix_words[0])
Expand Down Expand Up @@ -1289,7 +1288,6 @@ def ParseWhileUntil(self, keyword):

if self.parse_opts.parse_paren() and self.w_parser.LookPastSpace() == 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
else:
self.allow_block = False
Expand Down Expand Up @@ -1317,7 +1315,8 @@ def ParseCaseArm(self):
"""
self.lexer.PushHint(Id.Op_RParen, Id.Right_CasePat)

left_tok = location.LeftTokenForWord(self.cur_word)
left_tok = location.LeftTokenForWord(self.cur_word) # ( or pat

if self.c_id == Id.Op_LParen: # Optional (
self._Next()

Expand Down Expand Up @@ -1391,7 +1390,8 @@ def ParseOilCaseArm(self):
pat_expr : '(' oil_expr ')'
pat_eggex : '/' oil_eggex '/'
"""
left_tok = location.LeftTokenForWord(self.cur_word)
left_tok = location.LeftTokenForWord(self.cur_word) # pat

pat_words = [] # type: List[word_t]
while True:
self._Peek()
Expand Down Expand Up @@ -1526,7 +1526,6 @@ def _ParseOilElifElse(self, if_node):
if (self.parse_opts.parse_paren() and
self.w_parser.LookPastSpace() == 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
else:
self.allow_block = False
Expand Down Expand Up @@ -1628,7 +1627,6 @@ def ParseIf(self):
# Remove ambiguity with if cd / {
if self.parse_opts.parse_paren() and self.w_parser.LookPastSpace() == 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
else:
self.allow_block = False
Expand Down Expand Up @@ -1784,7 +1782,7 @@ def ParseFunctionDef(self):
with ctx_VarChecker(self.var_checker, blame_tok):
func.body = self.ParseCompoundCommand()

func.name_tok = location.LeftTokenForWord(word0)
func.name_tok = location.LeftTokenForCompoundWord(word0)
return func
else:
p_die('Expected ) in function definition', loc.Word(self.cur_word))
Expand Down

0 comments on commit ebdce70

Please sign in to comment.