Skip to content

Commit

Permalink
[ysh] Remove $'''
Browse files Browse the repository at this point in the history
We have u''' and b''' so it's no longer necessary.
  • Loading branch information
Andy C committed Jan 3, 2024
1 parent 9c5bc76 commit 76c1e2e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
1 change: 0 additions & 1 deletion frontend/id_kind_def.py
Expand Up @@ -445,7 +445,6 @@ def AddKinds(spec):
'RTSingleQuote', # r''' '''
'UTSingleQuote', # u''' '''
'BTSingleQuote', # b''' '''
'DollarTSingleQuote', # $''' '''
'Backtick', # `
'DollarParen', # $(
'DollarBrace', # ${
Expand Down
3 changes: 1 addition & 2 deletions frontend/lexer_def.py
Expand Up @@ -808,14 +808,13 @@ def R(pat, tok_type):
C("r'", Id.Left_RSingleQuote),
C("u'", Id.Left_USingleQuote),
C("b'", Id.Left_BSingleQuote),
C("$'", Id.Left_DollarSingleQuote), # TODO: remove
C("$'", Id.Left_DollarSingleQuote),
C('"""', Id.Left_TDoubleQuote),
# In expression mode, we add the r'' and c'' prefixes for '' and $''.
C("'''", Id.Left_TSingleQuote),
C("r'''", Id.Left_RTSingleQuote),
C("u'''", Id.Left_UTSingleQuote),
C("b'''", Id.Left_BTSingleQuote),
C("$'''", Id.Left_DollarTSingleQuote), # TODO: remove from YSH expressions
C('@(', Id.Left_AtParen), # Split Command Sub
C('^(', Id.Left_CaretParen), # Block literals in expression mode
C('^[', Id.Left_CaretBracket), # Expr literals
Expand Down
1 change: 0 additions & 1 deletion osh/word_compile.py
Expand Up @@ -138,7 +138,6 @@ def EvalSingleQuoted(part):
s = ''.join(tmp)

elif part.left.id in (Id.Left_DollarSingleQuote,
Id.Left_DollarTSingleQuote,
Id.Left_USingleQuote, Id.Left_BSingleQuote,
Id.Left_UTSingleQuote, Id.Left_BTSingleQuote):
# NOTE: This could be done at compile time
Expand Down
31 changes: 16 additions & 15 deletions osh/word_parse.py
Expand Up @@ -627,9 +627,8 @@ def ReadSingleQuoted(self, lex_mode, left_token, tokens, is_ysh_expr):
no_backslashes = is_ysh_expr and left_token.id == Id.Left_SingleQuote

expected_end_tokens = 3 if left_token.id in (
Id.Left_TSingleQuote, Id.Left_DollarTSingleQuote,
Id.Left_RTSingleQuote, Id.Left_UTSingleQuote,
Id.Left_BTSingleQuote) else 1
Id.Left_TSingleQuote, Id.Left_RTSingleQuote,
Id.Left_UTSingleQuote, Id.Left_BTSingleQuote) else 1
num_end_tokens = 0

while num_end_tokens < expected_end_tokens:
Expand Down Expand Up @@ -693,9 +692,8 @@ def ReadSingleQuoted(self, lex_mode, left_token, tokens, is_ysh_expr):
tokens.pop()

# Remove space from ''' r''' $''' in both expression mode and command mode
if left_token.id in (Id.Left_TSingleQuote, Id.Left_DollarTSingleQuote,
Id.Left_RTSingleQuote, Id.Left_UTSingleQuote,
Id.Left_BTSingleQuote):
if left_token.id in (Id.Left_TSingleQuote, Id.Left_RTSingleQuote,
Id.Left_UTSingleQuote, Id.Left_BTSingleQuote):
word_compile.RemoveLeadingSpaceSQ(tokens)

return self.cur_token
Expand Down Expand Up @@ -796,13 +794,16 @@ def _ReadUnquotedLeftParts(self, triple_out):

if self.token_type in (Id.Left_SingleQuote, Id.Left_RSingleQuote,
Id.Left_DollarSingleQuote):
if self.token_type == Id.Left_DollarSingleQuote:
lexer_mode = lex_mode_e.SQ_C
new_id = Id.Left_DollarTSingleQuote
else:
if self.token_type == Id.Left_SingleQuote:
lexer_mode = lex_mode_e.SQ_Raw
# Note we should also use Id.Left_RTSingleQuote
new_id = Id.Left_TSingleQuote
triple_left_id = Id.Left_TSingleQuote
elif self.token_type == Id.Left_RSingleQuote:
lexer_mode = lex_mode_e.SQ_Raw
triple_left_id = Id.Left_RTSingleQuote
else:
lexer_mode = lex_mode_e.SQ_C
# this can't be used because triple_out won't be set
triple_left_id = Id.Undefined_Tok

sq_part = self._ReadSingleQuoted(self.cur_token, lexer_mode)
# Got empty '' or r'' or $'' and there's a ' after
Expand All @@ -812,12 +813,12 @@ def _ReadUnquotedLeftParts(self, triple_out):
self._SetNext(lex_mode_e.ShCommand)
self._GetToken()

# TODO: multi-line strings must he Id.Left_SingleQuote

# HACK: magically transform the third ' in ''' to
# Id.Left_TSingleQuote, so that ''' is the terminator
left_sq_token = self.cur_token
left_sq_token.id = new_id
assert triple_left_id != Id.Undefined_Tok
left_sq_token.id = triple_left_id

triple_out.b = True # let caller know we got it
return self._ReadSingleQuoted(left_sq_token, lexer_mode)

Expand Down
6 changes: 5 additions & 1 deletion ysh/expr_parse.py
Expand Up @@ -286,7 +286,11 @@ def _PushOilTokens(parse_ctx, gr, p, lex, tea_keywords):

continue

# 'x' r'x' $'x' and '''x''' r'''x''' $'''x'''
# 'x' '''x'''
# r'x' r'''x'''
# $'x'
# u'x' u'''x'''
# b'x' b'''x'''
if tok.id in (Id.Left_SingleQuote, Id.Left_TSingleQuote,
Id.Left_RSingleQuote, Id.Left_RTSingleQuote,
Id.Left_DollarSingleQuote,
Expand Down

0 comments on commit 76c1e2e

Please sign in to comment.