summaryrefslogtreecommitdiffhomepage
path: root/lexer.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2020-11-30 17:38:50 +0100
committerJo-Philipp Wich <jo@mein.io>2020-11-30 17:38:50 +0100
commitac5cb8736f89f8da0c1bbe407d7d4ae9df530a5c (patch)
tree8e1f0a1e5e1bcde78f275839fb109e3eaf9bdb7b /lexer.c
parentf7b079ce3a41a0f92adb623b0de10419fc9f5df9 (diff)
syntax: fix string and regex literal parsing quirks
- Do not interprete escape sequences in regexp literals - Do not improperly substitute control escape sequences such as `\n` or `\a` after a backslash Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'lexer.c')
-rw-r--r--lexer.c24
1 files changed, 13 insertions, 11 deletions
diff --git a/lexer.c b/lexer.c
index 2867e76..21a3b3a 100644
--- a/lexer.c
+++ b/lexer.c
@@ -394,6 +394,16 @@ parse_string(struct uc_state *s)
/* continuation of escape sequence */
if (s->lex.is_escape) {
if (s->lex.esclen == 0) {
+ /* regex mode => do not interprete escapes */
+ if (q == '/') {
+ s->lex.is_escape = false;
+ lookbehind_append(s, "\\", 1);
+ lookbehind_append(s, ptr, 1);
+ buf_consume(s, (ptr + 1) - s->lex.bufstart);
+
+ continue;
+ }
+
/* non-unicode escape following a lead surrogate, emit replacement... */
if (s->lex.lead_surrogate && *ptr != 'u') {
append_utf8(s, 0xFFFD);
@@ -416,16 +426,8 @@ parse_string(struct uc_state *s)
case '7':
case '8':
case '9':
- /* regex mode => backref, retain literally */
- if (q == '/') {
- s->lex.is_escape = false;
- lookbehind_append(s, "\\", 1);
- lookbehind_append(s, ptr, 1);
- buf_consume(s, (ptr + 1) - s->lex.bufstart);
- }
-
- /* string mode => likely octal */
- else if (*ptr < '8') {
+ /* likely octal */
+ if (*ptr < '8') {
s->lex.esc[s->lex.esclen++] = 'o';
s->lex.esc[s->lex.esclen++] = *ptr;
}
@@ -442,7 +444,7 @@ parse_string(struct uc_state *s)
default:
s->lex.is_escape = false;
c = strchr("a\ab\be\ef\fn\nr\rt\tv\v", *ptr);
- lookbehind_append(s, c ? c + 1 : ptr, 1);
+ lookbehind_append(s, (c && *c >= 'a') ? c + 1 : ptr, 1);
buf_consume(s, (ptr + 1) - s->lex.bufstart);
break;
}