diff options
author | Mike Pall <mike> | 2010-11-19 17:39:33 +0100 |
---|---|---|
committer | Mike Pall <mike> | 2010-11-19 18:15:50 +0100 |
commit | 29b8959df143994219e1192e099e5e70cf9181aa (patch) | |
tree | db3f94b53e774ad521dd642e9e3550c7e8106c2b /src | |
parent | 57cd5026ebe6be2c7f1c2851557b9b7e2261a3d3 (diff) | |
download | luajit-29b8959df143994219e1192e099e5e70cf9181aa.tar.gz luajit-29b8959df143994219e1192e099e5e70cf9181aa.tar.bz2 luajit-29b8959df143994219e1192e099e5e70cf9181aa.zip |
Parse hexadecimal escapes in strings (from Lua 5.2).
Diffstat (limited to 'src')
-rw-r--r-- | src/lj_errmsg.h | 2 | ||||
-rw-r--r-- | src/lj_lex.c | 40 |
2 files changed, 29 insertions, 13 deletions
diff --git a/src/lj_errmsg.h b/src/lj_errmsg.h index b06a7798..d9fc615b 100644 --- a/src/lj_errmsg.h +++ b/src/lj_errmsg.h | |||
@@ -117,7 +117,7 @@ ERRDEF(XNUMBER, "malformed number") | |||
117 | ERRDEF(XLSTR, "unfinished long string") | 117 | ERRDEF(XLSTR, "unfinished long string") |
118 | ERRDEF(XLCOM, "unfinished long comment") | 118 | ERRDEF(XLCOM, "unfinished long comment") |
119 | ERRDEF(XSTR, "unfinished string") | 119 | ERRDEF(XSTR, "unfinished string") |
120 | ERRDEF(XESC, "escape sequence too large") | 120 | ERRDEF(XESC, "invalid escape sequence") |
121 | ERRDEF(XLDELIM, "invalid long string delimiter") | 121 | ERRDEF(XLDELIM, "invalid long string delimiter") |
122 | ERRDEF(XBCLOAD, "cannot load Lua bytecode") | 122 | ERRDEF(XBCLOAD, "cannot load Lua bytecode") |
123 | ERRDEF(XTOKEN, LUA_QS " expected") | 123 | ERRDEF(XTOKEN, LUA_QS " expected") |
diff --git a/src/lj_lex.c b/src/lj_lex.c index d3544e8e..fde7d9ca 100644 --- a/src/lj_lex.c +++ b/src/lj_lex.c | |||
@@ -154,7 +154,7 @@ static void read_string(LexState *ls, int delim, TValue *tv) | |||
154 | continue; | 154 | continue; |
155 | case '\\': { | 155 | case '\\': { |
156 | int c; | 156 | int c; |
157 | next(ls); /* do not save the `\' */ | 157 | next(ls); /* Skip the '\\'. */ |
158 | switch (ls->current) { | 158 | switch (ls->current) { |
159 | case 'a': c = '\a'; break; | 159 | case 'a': c = '\a'; break; |
160 | case 'b': c = '\b'; break; | 160 | case 'b': c = '\b'; break; |
@@ -163,20 +163,36 @@ static void read_string(LexState *ls, int delim, TValue *tv) | |||
163 | case 'r': c = '\r'; break; | 163 | case 'r': c = '\r'; break; |
164 | case 't': c = '\t'; break; | 164 | case 't': c = '\t'; break; |
165 | case 'v': c = '\v'; break; | 165 | case 'v': c = '\v'; break; |
166 | case 'x': /* Hexadecimal escape '\xXX'. */ | ||
167 | c = (next(ls) & 15u) << 4; | ||
168 | if (!lj_char_isdigit(ls->current)) { | ||
169 | if (!lj_char_isxdigit(ls->current)) goto err_xesc; | ||
170 | c += 9 << 4; | ||
171 | } | ||
172 | c += (next(ls) & 15u); | ||
173 | if (!lj_char_isdigit(ls->current)) { | ||
174 | if (!lj_char_isxdigit(ls->current)) goto err_xesc; | ||
175 | c += 9; | ||
176 | } | ||
177 | break; | ||
166 | case '\n': case '\r': save(ls, '\n'); inclinenumber(ls); continue; | 178 | case '\n': case '\r': save(ls, '\n'); inclinenumber(ls); continue; |
167 | case END_OF_STREAM: continue; /* will raise an error next loop */ | 179 | case END_OF_STREAM: continue; |
168 | default: | 180 | default: |
169 | if (!lj_char_isdigit(ls->current)) { | 181 | if (!lj_char_isdigit(ls->current)) { |
170 | save_and_next(ls); /* handles \\, \", \', and \? */ | 182 | save_and_next(ls); /* Handles '\\', '\"' and "\'". */ |
171 | } else { /* \xxx */ | 183 | } else { /* Decimal escape '\ddd'. */ |
172 | int i = 0; | 184 | c = (ls->current - '0'); |
173 | c = 0; | 185 | if (lj_char_isdigit(next(ls))) { |
174 | do { | 186 | c = c*10 + (ls->current - '0'); |
175 | c = 10*c + (ls->current-'0'); | 187 | if (lj_char_isdigit(next(ls))) { |
176 | next(ls); | 188 | c = c*10 + (ls->current - '0'); |
177 | } while (++i<3 && lj_char_isdigit(ls->current)); | 189 | if (c > 255) { |
178 | if (c > 255) | 190 | err_xesc: |
179 | lj_lex_error(ls, TK_string, LJ_ERR_XESC); | 191 | lj_lex_error(ls, TK_string, LJ_ERR_XESC); |
192 | } | ||
193 | next(ls); | ||
194 | } | ||
195 | } | ||
180 | save(ls, c); | 196 | save(ls, c); |
181 | } | 197 | } |
182 | continue; | 198 | continue; |