Permalink
Browse files

Handle backslash escapes at parse time rather than at runtime.

Addresses issue #49.
  • Loading branch information...
Andy Chu
Andy Chu committed Nov 15, 2017
1 parent e96d835 commit 6ece46427722de0ec525feff12c6616312adba96
Showing with 7 additions and 12 deletions.
  1. +1 −11 core/word_eval.py
  2. +6 −1 osh/lex.py
View
@@ -851,17 +851,7 @@ def _EvalWordPart(self, part, quoted=False):
val = part.token.val
assert len(val) == 2, val # e.g. \*
assert val[0] == '\\'
c = val[1]
# TODO: This can be done at compile time instead! Change _BACKSLASH
# definition in DQ state.
if quoted:
# https://www.gnu.org/software/bash/manual/bash.html#Double-Quotes
if c in ('$', '`', '"', '\\'):
s = c
else:
s = val
else:
s = c
s = val[1]
return [runtime.StringPartValue(s, False, False)]
elif part.tag == word_part_e.SingleQuotedPart:
View
@@ -282,7 +282,12 @@ def IsKeyword(name):
C('|', Id.Lit_Chars),
] + _UNQUOTED
LEXER_DEF[LexMode.DQ] = _BACKSLASH + _LEFT_SUBS + _VARS + [
LEXER_DEF[LexMode.DQ] = [
# Only 4 characters are backslash escaped inside "".
# https://www.gnu.org/software/bash/manual/bash.html#Double-Quotes
R(r'\\[$`"\\]', Id.Lit_EscapedChar),
C('\\\n', Id.Ignored_LineCont),
] + _LEFT_SUBS + _VARS + [
R(r'[^$`"\0\\]+', Id.Lit_Chars), # matches a line at most
# NOTE: When parsing here doc line, this token doesn't end it.
C('"', Id.Right_DoubleQuote),

0 comments on commit 6ece464

Please sign in to comment.