Skip to content

Commit

Permalink
[osh-language] Fix "${undef-\z}"
Browse files Browse the repository at this point in the history
VSub_ArgDQ should have the same backslash behavior as DQ.

Reported by Crestwave on issue #695.
  • Loading branch information
Andy Chu committed Apr 7, 2020
1 parent 14548dc commit 84c2f4f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
19 changes: 12 additions & 7 deletions frontend/lexer_def.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ def R(pat, tok_type):
C('\\\n', Id.Ignored_LineCont),
]

# Only 4 characters are backslash escaped inside "".
# https://www.gnu.org/software/bash/manual/bash.html#Double-Quotes
_DQ_BACKSLASH = [
R(r'\\[$`"\\]', Id.Lit_EscapedChar)
]

VAR_NAME_RE = r'[a-zA-Z_][a-zA-Z0-9_]*'

# All Kind.VSub
Expand Down Expand Up @@ -398,10 +404,7 @@ def IsKeyword(name):
R(r'[^\0]', Id.Lit_Other), # everything else is literal
]

LEXER_DEF[lex_mode_e.DQ] = [
# Only 4 characters are backslash escaped inside "".
# https://www.gnu.org/software/bash/manual/bash.html#Double-Quotes
R(r'\\[$`"\\]', Id.Lit_EscapedChar),
LEXER_DEF[lex_mode_e.DQ] = _DQ_BACKSLASH + [
C('\\\n', Id.Ignored_LineCont),
] + _LEFT_SUBS + _VARS + [
R(r'[^$`"\0\\]+', Id.Lit_Chars), # matches a line at most
Expand All @@ -410,7 +413,7 @@ def IsKeyword(name):
R(r'[^\0]', Id.Lit_Other), # e.g. "$"
]

_VS_ARG_COMMON = _BACKSLASH + [
_VS_ARG_COMMON = [
C('/', Id.Lit_Slash), # for patsub (not Id.VOp2_Slash)
C('#', Id.Lit_Pound), # for patsub prefix (not Id.VOp1_Pound)
C('%', Id.Lit_Percent), # for patsdub suffix (not Id.VOp1_Percent)
Expand All @@ -419,14 +422,16 @@ def IsKeyword(name):

# Kind.{LIT,IGNORED,VS,LEFT,RIGHT,Eof}
LEXER_DEF[lex_mode_e.VSub_ArgUnquoted] = \
_VS_ARG_COMMON + _LEFT_SUBS + _LEFT_UNQUOTED + _VARS + [
_BACKSLASH + _VS_ARG_COMMON + _LEFT_SUBS + _LEFT_UNQUOTED + _VARS + [
# NOTE: added < and > so it doesn't eat <()
R(r'[^$`/}"\'\0\\#%<>]+', Id.Lit_Chars),
R(r'[^\0]', Id.Lit_Other), # e.g. "$", must be last
]

# Kind.{LIT,IGNORED,VS,LEFT,RIGHT,Eof}
LEXER_DEF[lex_mode_e.VSub_ArgDQ] = _VS_ARG_COMMON + _LEFT_SUBS + _VARS + [
LEXER_DEF[lex_mode_e.VSub_ArgDQ] = \
_DQ_BACKSLASH + _VS_ARG_COMMON + _LEFT_SUBS + _VARS + [

R(r'[^$`/}"\0\\#%]+', Id.Lit_Chars), # matches a line at most

# Weird wart: even in double quoted state, double quotes are allowed
Expand Down
33 changes: 33 additions & 0 deletions spec/var-op-test.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,36 @@ echo ${#arr[@]}
## N-I dash stdout-json: ""
## N-I mksh status: 1
## N-I mksh stdout-json: ""

#### "\z" as arg
echo "${undef-\$}"
echo "${undef-\(}"
echo "${undef-\z}"
echo "${undef-\"}"
echo "${undef-\`}"
echo "${undef-\\}"
## STDOUT:
$
\(
\z
"
`
\
## END
## BUG yash STDOUT:
$
(
z
"
`
\
## END
#### "\e" as arg
echo "${undef-\e}"
## STDOUT:
\e
## END
## BUG zsh/mksh stdout-repr: '\x1b\n'
## BUG yash stdout: e

0 comments on commit 84c2f4f

Please sign in to comment.