Skip to content

Commit

Permalink
[oil-language] Implement parse_raw_string
Browse files Browse the repository at this point in the history
The r prefix is a no-op, so this now prints a backslash:

    echo r'\'

Addresses issue #940
  • Loading branch information
Andy C committed May 15, 2021
1 parent 1caa085 commit e4bfb65
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion doc/oil-help-topics.md
Expand Up @@ -135,9 +135,9 @@ X [Testing] check
parse_at echo @array @arrayfunc(x, y)
parse_brace if true { ... }; cd ~/src { ... }
parse_paren if (x > 0) ...
parse_raw_string echo r'\' (command mode)
X parse_triple_quoted ''' """
X parse_triple_dots Multiline pipelines
X parse_raw_string r'\' in command mode
command_sub_errexit Synchronous errexit check
process_sub_fail Analogous to pipefail for process subs
sigpipe_status_ok status 141 -> 0 in pipelines
Expand Down
25 changes: 25 additions & 0 deletions doc/oil-help.md
Expand Up @@ -525,10 +525,35 @@ When this option is disabled, that statement is a syntax error.

### Oil Basic

#### parse_at

TODO

#### parse_brace

TODO

#### parse_paren

TODO

#### parse_raw_string

Allow the r prefix for raw strings in command mode:

echo r'\' # a single backslash

Since shell strings are already raw, this means that Oil just ignores the r
prefix.

#### command_sub_errexit

TODO

#### process_sub_fail

TODO

#### sigpipe_status_ok

If a process that's part of a pipeline exits with status 141 when this is
Expand Down
1 change: 1 addition & 0 deletions frontend/option_def.py
Expand Up @@ -150,6 +150,7 @@ def DoneWithImplementedOptions(self):
'parse_at', # @foo, @array(a, b)
'parse_brace', # cd /bin { ... }
'parse_paren', # if (x > 0) ...
'parse_raw_string', # echo r'\'
'parse_triple_quoted', # for ''' and """
'parse_triple_dots', # ...

Expand Down
7 changes: 7 additions & 0 deletions osh/word_parse.py
Expand Up @@ -1594,6 +1594,13 @@ def _ReadWord(self, lex_mode):
return no_word, True # tell Read() to try again after comment

else:
# parse_raw_string: Is there an r'' at the beginning of a word?
if (self.parse_opts.parse_raw_string() and
self.token_type == Id.Lit_Chars and
self.cur_token.val == 'r'):
if self.lexer.LookAhead(lex_mode_e.ShCommand) == Id.Left_SingleQuote:
self._Next(lex_mode_e.ShCommand)

w = self._ReadCompoundWord(lex_mode)
return cast(word_t, w), False

Expand Down
24 changes: 24 additions & 0 deletions spec/oil-string.test.sh
Expand Up @@ -45,3 +45,27 @@ bar
equal
## END


#### shopt parse_raw_string

# Ignored prefix
echo r'\'

# These use shell rules!
echo ra'\'
echo raw'\'

echo r"\\"

# Now it's a regular r
shopt --unset parse_raw_string
write unset r'\'

## STDOUT:
\
ra\
raw\
r\
unset
r\
## END

0 comments on commit e4bfb65

Please sign in to comment.