summaryrefslogtreecommitdiff
path: root/src/lj_lex.c
diff options
context:
space:
mode:
authorMike Pall <mike>2013-02-27 21:17:27 +0100
committerMike Pall <mike>2013-02-27 21:28:28 +0100
commit116cdd7e9a578efffa5a9ca38167d059d12296d7 (patch)
treecc78e44c4b7a2175f2b16bc5f8898597a72bb228 /src/lj_lex.c
parent28cfcf77445e144335961a020e3e08d84cf0091f (diff)
downloadluajit-116cdd7e9a578efffa5a9ca38167d059d12296d7.tar.gz
luajit-116cdd7e9a578efffa5a9ca38167d059d12296d7.tar.bz2
luajit-116cdd7e9a578efffa5a9ca38167d059d12296d7.zip
String buffer refactoring, part 2.
Switch to pointers for start/pos/end of buffer. Abstract out some buffer writers.
Diffstat (limited to 'src/lj_lex.c')
-rw-r--r--src/lj_lex.c27
1 files changed, 9 insertions, 18 deletions
diff --git a/src/lj_lex.c b/src/lj_lex.c
index 3227cadd..c4d52da2 100644
--- a/src/lj_lex.c
+++ b/src/lj_lex.c
@@ -12,6 +12,7 @@
12#include "lj_obj.h" 12#include "lj_obj.h"
13#include "lj_gc.h" 13#include "lj_gc.h"
14#include "lj_err.h" 14#include "lj_err.h"
15#include "lj_buf.h"
15#include "lj_str.h" 16#include "lj_str.h"
16#if LJ_HASFFI 17#if LJ_HASFFI
17#include "lj_tab.h" 18#include "lj_tab.h"
@@ -54,20 +55,9 @@ static int fillbuf(LexState *ls)
54 return char2int(*(ls->p++)); 55 return char2int(*(ls->p++));
55} 56}
56 57
57static LJ_NOINLINE void save_grow(LexState *ls, int c)
58{
59 if (ls->sb.sz >= LJ_MAX_STR/2)
60 lj_lex_error(ls, 0, LJ_ERR_XELEM);
61 lj_buf_grow(ls->L, &ls->sb, 0);
62 ls->sb.buf[ls->sb.n++] = (char)c;
63}
64
65static LJ_AINLINE void save(LexState *ls, int c) 58static LJ_AINLINE void save(LexState *ls, int c)
66{ 59{
67 if (LJ_UNLIKELY(ls->sb.n + 1 > ls->sb.sz)) 60 lj_buf_putb(ls->L, &ls->sb, c);
68 save_grow(ls, c);
69 else
70 ls->sb.buf[ls->sb.n++] = (char)c;
71} 61}
72 62
73static void inclinenumber(LexState *ls) 63static void inclinenumber(LexState *ls)
@@ -99,7 +89,7 @@ static void lex_number(LexState *ls, TValue *tv)
99 save_and_next(ls); 89 save_and_next(ls);
100 } 90 }
101 save(ls, '\0'); 91 save(ls, '\0');
102 fmt = lj_strscan_scan((const uint8_t *)ls->sb.buf, tv, 92 fmt = lj_strscan_scan((const uint8_t *)sbufB(&ls->sb), tv,
103 (LJ_DUALNUM ? STRSCAN_OPT_TOINT : STRSCAN_OPT_TONUM) | 93 (LJ_DUALNUM ? STRSCAN_OPT_TOINT : STRSCAN_OPT_TONUM) |
104 (LJ_HASFFI ? (STRSCAN_OPT_LL|STRSCAN_OPT_IMAG) : 0)); 94 (LJ_HASFFI ? (STRSCAN_OPT_LL|STRSCAN_OPT_IMAG) : 0));
105 if (LJ_DUALNUM && fmt == STRSCAN_INT) { 95 if (LJ_DUALNUM && fmt == STRSCAN_INT) {
@@ -174,8 +164,8 @@ static void read_long_string(LexState *ls, TValue *tv, int sep)
174 } 164 }
175 } endloop: 165 } endloop:
176 if (tv) { 166 if (tv) {
177 GCstr *str = lj_parse_keepstr(ls, ls->sb.buf + (2 + (MSize)sep), 167 GCstr *str = lj_parse_keepstr(ls, sbufB(&ls->sb) + (2 + (MSize)sep),
178 ls->sb.n - 2*(2 + (MSize)sep)); 168 sbuflen(&ls->sb) - 2*(2 + (MSize)sep));
179 setstrV(ls->L, tv, str); 169 setstrV(ls->L, tv, str);
180 } 170 }
181} 171}
@@ -250,7 +240,8 @@ static void read_string(LexState *ls, int delim, TValue *tv)
250 } 240 }
251 } 241 }
252 save_and_next(ls); /* skip delimiter */ 242 save_and_next(ls); /* skip delimiter */
253 setstrV(ls->L, tv, lj_parse_keepstr(ls, ls->sb.buf + 1, ls->sb.n - 2)); 243 setstrV(ls->L, tv,
244 lj_parse_keepstr(ls, sbufB(&ls->sb)+1, sbuflen(&ls->sb)-2));
254} 245}
255 246
256/* -- Main lexical scanner ------------------------------------------------ */ 247/* -- Main lexical scanner ------------------------------------------------ */
@@ -269,7 +260,7 @@ static int llex(LexState *ls, TValue *tv)
269 do { 260 do {
270 save_and_next(ls); 261 save_and_next(ls);
271 } while (lj_char_isident(ls->current)); 262 } while (lj_char_isident(ls->current));
272 s = lj_parse_keepstr(ls, ls->sb.buf, ls->sb.n); 263 s = lj_parse_keepstr(ls, sbufB(&ls->sb), sbuflen(&ls->sb));
273 setstrV(ls->L, tv, s); 264 setstrV(ls->L, tv, s);
274 if (s->reserved > 0) /* Reserved word? */ 265 if (s->reserved > 0) /* Reserved word? */
275 return TK_OFS + s->reserved; 266 return TK_OFS + s->reserved;
@@ -457,7 +448,7 @@ void lj_lex_error(LexState *ls, LexToken token, ErrMsg em, ...)
457 tok = NULL; 448 tok = NULL;
458 } else if (token == TK_name || token == TK_string || token == TK_number) { 449 } else if (token == TK_name || token == TK_string || token == TK_number) {
459 save(ls, '\0'); 450 save(ls, '\0');
460 tok = ls->sb.buf; 451 tok = sbufB(&ls->sb);
461 } else { 452 } else {
462 tok = lj_lex_token2str(ls, token); 453 tok = lj_lex_token2str(ls, token);
463 } 454 }