Skip to content

Commit

Permalink
[ysh] Remove more remnants of $'''
Browse files Browse the repository at this point in the history
Also update the ysh-tour for J8 strings.

TODO: have to update doc/ref.
  • Loading branch information
Andy C committed Jan 3, 2024
1 parent 76c1e2e commit 02ef294
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
28 changes: 14 additions & 14 deletions doc/ysh-tour.md
Expand Up @@ -153,7 +153,7 @@ concept in shell.
You can choose the quoting style that's most convenient to write a given
string.

#### Single-Quoted, Double-Quoted, and C-Style
#### Double-Quoted, Single-Quoted, and J8 strings (like JSON)

Double-quoted strings allow **interpolation with `$`**:

Expand All @@ -169,15 +169,19 @@ can't be expressed):

echo 'c:\Program Files\' # => c:\Program Files\

C-style strings look like `$'foo'` and respect backslash **character escapes**:
If you want C-style backslash **character escapes**, use a J8 string, which is
like JSON, but with single quotes::

echo $' A is \x41 \n line two, with backslash \\'
echo u' A is \u{41} \n line two, with backslash \\'
# =>
# A is A
# line two, with backslash \

(The `$` before the quote doesn't mean "interpolation". It's an unfortunate
syntax collision that will be changed to JSON-style `j""` strings.)
The `u''` strings are guaranteed to be valid Unicode (unlike JSON), but you can
also use `b''` strings:

echo b'byte \yff' # byte that's not valid unicode, like \xff in other languages
# do not confuse with \u{ff}

#### Multi-line Strings

Expand All @@ -202,15 +206,14 @@ three varieties, and leading whitespace is stripped in a convenient way.
# $1.99
# $2.00

sort <<< $'''
sort <<< u'''
C\tD
A\tB
'''
''' # b''' strings also supported
# =>
# A B
# C D


(Use multiline strings instead of shell's [here docs]($xref:here-doc).)

### Three Kinds of Substitution
Expand Down Expand Up @@ -848,16 +851,13 @@ Floats are written like you'd expect:
#### Str

See the section above called *Three Kinds of String Literals*. It described
`'single quoted'`, `"double ${quoted}"`, and `$'c-style\n'` strings; as well as
their multiline variants.
`'single quoted'`, `"double ${quoted}"`, and `u'J8-style\n'` strings; as well
as their multiline variants.

Strings are UTF-8 encoded in memory, like strings in the [Go
language](https://golang.org). There isn't a separate string and unicode type,
as in Python.

(In the future, JSON-like *J8 Notation* will distinguish between strings and
bytes on the wire.)

Strings are **immutable**, as in Python and JavaScript. This means they only
have **transforming** methods:

Expand Down Expand Up @@ -1147,7 +1147,7 @@ including those with newlines and terminal escape sequences.
Example:

# A line with a tab char in the middle
var mystr = $'pea\t' ++ $'42\n'
var mystr = u'pea\t' ++ u'42\n'

# Print it to stdout
write --qsn $mystr # => 'pea\t42\n'
Expand Down
9 changes: 5 additions & 4 deletions osh/word_parse.py
Expand Up @@ -806,17 +806,18 @@ def _ReadUnquotedLeftParts(self, triple_out):
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
if (triple_out and len(sq_part.tokens) == 0 and
self.lexer.ByteLookAhead() == "'"):

# Got empty '' or r'' and there's a ' after
# u'' and b'' are handled in _ReadYshSingleQuoted
if (triple_out and triple_left_id != Id.Undefined_Tok and
len(sq_part.tokens) == 0 and self.lexer.ByteLookAhead() == "'"):

self._SetNext(lex_mode_e.ShCommand)
self._GetToken()

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

triple_out.b = True # let caller know we got it
Expand Down
14 changes: 14 additions & 0 deletions spec/ysh-multiline.test.sh
Expand Up @@ -177,3 +177,17 @@ THREE
TWO
ONE
## END

#### $''' isn't a a multiline string (removed)

shopt -s ysh:upgrade

echo $'''
foo
'''

## STDOUT:

foo

## END

0 comments on commit 02ef294

Please sign in to comment.