diff options
Diffstat (limited to 'src/lj_lex.c')
-rw-r--r-- | src/lj_lex.c | 375 |
1 files changed, 202 insertions, 173 deletions
diff --git a/src/lj_lex.c b/src/lj_lex.c index ca942583..05a2efc3 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" |
@@ -24,6 +25,7 @@ | |||
24 | #include "lj_parse.h" | 25 | #include "lj_parse.h" |
25 | #include "lj_char.h" | 26 | #include "lj_char.h" |
26 | #include "lj_strscan.h" | 27 | #include "lj_strscan.h" |
28 | #include "lj_strfmt.h" | ||
27 | 29 | ||
28 | /* Lua lexer token names. */ | 30 | /* Lua lexer token names. */ |
29 | static const char *const tokennames[] = { | 31 | static const char *const tokennames[] = { |
@@ -37,54 +39,54 @@ TKDEF(TKSTR1, TKSTR2) | |||
37 | 39 | ||
38 | /* -- Buffer handling ----------------------------------------------------- */ | 40 | /* -- Buffer handling ----------------------------------------------------- */ |
39 | 41 | ||
40 | #define char2int(c) ((int)(uint8_t)(c)) | 42 | #define LEX_EOF (-1) |
41 | #define next(ls) \ | 43 | #define lex_iseol(ls) (ls->c == '\n' || ls->c == '\r') |
42 | (ls->current = (ls->n--) > 0 ? char2int(*ls->p++) : fillbuf(ls)) | ||
43 | #define save_and_next(ls) (save(ls, ls->current), next(ls)) | ||
44 | #define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r') | ||
45 | #define END_OF_STREAM (-1) | ||
46 | 44 | ||
47 | static int fillbuf(LexState *ls) | 45 | /* Get more input from reader. */ |
46 | static LJ_NOINLINE LexChar lex_more(LexState *ls) | ||
48 | { | 47 | { |
49 | size_t sz; | 48 | size_t sz; |
50 | const char *buf = ls->rfunc(ls->L, ls->rdata, &sz); | 49 | const char *p = ls->rfunc(ls->L, ls->rdata, &sz); |
51 | if (buf == NULL || sz == 0) return END_OF_STREAM; | 50 | if (p == NULL || sz == 0) return LEX_EOF; |
52 | if (sz >= LJ_MAX_MEM) { | 51 | if (sz >= LJ_MAX_BUF) { |
53 | if (sz != ~(size_t)0) lj_err_mem(ls->L); | 52 | if (sz != ~(size_t)0) lj_err_mem(ls->L); |
53 | sz = ~(uintptr_t)0 - (uintptr_t)p; | ||
54 | if (sz >= LJ_MAX_BUF) sz = LJ_MAX_BUF-1; | ||
54 | ls->endmark = 1; | 55 | ls->endmark = 1; |
55 | } | 56 | } |
56 | ls->n = (MSize)sz - 1; | 57 | ls->pe = p + sz; |
57 | ls->p = buf; | 58 | ls->p = p + 1; |
58 | return char2int(*(ls->p++)); | 59 | return (LexChar)(uint8_t)p[0]; |
59 | } | 60 | } |
60 | 61 | ||
61 | static LJ_NOINLINE void save_grow(LexState *ls, int c) | 62 | /* Get next character. */ |
63 | static LJ_AINLINE LexChar lex_next(LexState *ls) | ||
62 | { | 64 | { |
63 | MSize newsize; | 65 | return (ls->c = ls->p < ls->pe ? (LexChar)(uint8_t)*ls->p++ : lex_more(ls)); |
64 | if (ls->sb.sz >= LJ_MAX_STR/2) | ||
65 | lj_lex_error(ls, 0, LJ_ERR_XELEM); | ||
66 | newsize = ls->sb.sz * 2; | ||
67 | lj_str_resizebuf(ls->L, &ls->sb, newsize); | ||
68 | ls->sb.buf[ls->sb.n++] = (char)c; | ||
69 | } | 66 | } |
70 | 67 | ||
71 | static LJ_AINLINE void save(LexState *ls, int c) | 68 | /* Save character. */ |
69 | static LJ_AINLINE void lex_save(LexState *ls, LexChar c) | ||
72 | { | 70 | { |
73 | if (LJ_UNLIKELY(ls->sb.n + 1 > ls->sb.sz)) | 71 | lj_buf_putb(&ls->sb, c); |
74 | save_grow(ls, c); | ||
75 | else | ||
76 | ls->sb.buf[ls->sb.n++] = (char)c; | ||
77 | } | 72 | } |
78 | 73 | ||
79 | static void inclinenumber(LexState *ls) | 74 | /* Save previous character and get next character. */ |
75 | static LJ_AINLINE LexChar lex_savenext(LexState *ls) | ||
80 | { | 76 | { |
81 | int old = ls->current; | 77 | lex_save(ls, ls->c); |
82 | lua_assert(currIsNewline(ls)); | 78 | return lex_next(ls); |
83 | next(ls); /* skip `\n' or `\r' */ | 79 | } |
84 | if (currIsNewline(ls) && ls->current != old) | 80 | |
85 | next(ls); /* skip `\n\r' or `\r\n' */ | 81 | /* Skip line break. Handles "\n", "\r", "\r\n" or "\n\r". */ |
82 | static void lex_newline(LexState *ls) | ||
83 | { | ||
84 | LexChar old = ls->c; | ||
85 | lua_assert(lex_iseol(ls)); | ||
86 | lex_next(ls); /* Skip "\n" or "\r". */ | ||
87 | if (lex_iseol(ls) && ls->c != old) lex_next(ls); /* Skip "\n\r" or "\r\n". */ | ||
86 | if (++ls->linenumber >= LJ_MAX_LINE) | 88 | if (++ls->linenumber >= LJ_MAX_LINE) |
87 | lj_lex_error(ls, ls->token, LJ_ERR_XLINES); | 89 | lj_lex_error(ls, ls->tok, LJ_ERR_XLINES); |
88 | } | 90 | } |
89 | 91 | ||
90 | /* -- Scanner for terminals ----------------------------------------------- */ | 92 | /* -- Scanner for terminals ----------------------------------------------- */ |
@@ -93,19 +95,17 @@ static void inclinenumber(LexState *ls) | |||
93 | static void lex_number(LexState *ls, TValue *tv) | 95 | static void lex_number(LexState *ls, TValue *tv) |
94 | { | 96 | { |
95 | StrScanFmt fmt; | 97 | StrScanFmt fmt; |
96 | int c, xp = 'e'; | 98 | LexChar c, xp = 'e'; |
97 | lua_assert(lj_char_isdigit(ls->current)); | 99 | lua_assert(lj_char_isdigit(ls->c)); |
98 | if ((c = ls->current) == '0') { | 100 | if ((c = ls->c) == '0' && (lex_savenext(ls) | 0x20) == 'x') |
99 | save_and_next(ls); | 101 | xp = 'p'; |
100 | if ((ls->current | 0x20) == 'x') xp = 'p'; | 102 | while (lj_char_isident(ls->c) || ls->c == '.' || |
101 | } | 103 | ((ls->c == '-' || ls->c == '+') && (c | 0x20) == xp)) { |
102 | while (lj_char_isident(ls->current) || ls->current == '.' || | 104 | c = ls->c; |
103 | ((ls->current == '-' || ls->current == '+') && (c | 0x20) == xp)) { | 105 | lex_savenext(ls); |
104 | c = ls->current; | ||
105 | save_and_next(ls); | ||
106 | } | 106 | } |
107 | save(ls, '\0'); | 107 | lex_save(ls, '\0'); |
108 | fmt = lj_strscan_scan((const uint8_t *)ls->sb.buf, tv, | 108 | fmt = lj_strscan_scan((const uint8_t *)sbufB(&ls->sb), tv, |
109 | (LJ_DUALNUM ? STRSCAN_OPT_TOINT : STRSCAN_OPT_TONUM) | | 109 | (LJ_DUALNUM ? STRSCAN_OPT_TOINT : STRSCAN_OPT_TONUM) | |
110 | (LJ_HASFFI ? (STRSCAN_OPT_LL|STRSCAN_OPT_IMAG) : 0)); | 110 | (LJ_HASFFI ? (STRSCAN_OPT_LL|STRSCAN_OPT_IMAG) : 0)); |
111 | if (LJ_DUALNUM && fmt == STRSCAN_INT) { | 111 | if (LJ_DUALNUM && fmt == STRSCAN_INT) { |
@@ -138,60 +138,60 @@ static void lex_number(LexState *ls, TValue *tv) | |||
138 | } | 138 | } |
139 | } | 139 | } |
140 | 140 | ||
141 | static int skip_sep(LexState *ls) | 141 | /* Skip equal signs for "[=...=[" and "]=...=]" and return their count. */ |
142 | static int lex_skipeq(LexState *ls) | ||
142 | { | 143 | { |
143 | int count = 0; | 144 | int count = 0; |
144 | int s = ls->current; | 145 | LexChar s = ls->c; |
145 | lua_assert(s == '[' || s == ']'); | 146 | lua_assert(s == '[' || s == ']'); |
146 | save_and_next(ls); | 147 | while (lex_savenext(ls) == '=' && count < 0x20000000) |
147 | while (ls->current == '=' && count < 0x20000000) { | ||
148 | save_and_next(ls); | ||
149 | count++; | 148 | count++; |
150 | } | 149 | return (ls->c == s) ? count : (-count) - 1; |
151 | return (ls->current == s) ? count : (-count) - 1; | ||
152 | } | 150 | } |
153 | 151 | ||
154 | static void read_long_string(LexState *ls, TValue *tv, int sep) | 152 | /* Parse a long string or long comment (tv set to NULL). */ |
153 | static void lex_longstring(LexState *ls, TValue *tv, int sep) | ||
155 | { | 154 | { |
156 | save_and_next(ls); /* skip 2nd `[' */ | 155 | lex_savenext(ls); /* Skip second '['. */ |
157 | if (currIsNewline(ls)) /* string starts with a newline? */ | 156 | if (lex_iseol(ls)) /* Skip initial newline. */ |
158 | inclinenumber(ls); /* skip it */ | 157 | lex_newline(ls); |
159 | for (;;) { | 158 | for (;;) { |
160 | switch (ls->current) { | 159 | switch (ls->c) { |
161 | case END_OF_STREAM: | 160 | case LEX_EOF: |
162 | lj_lex_error(ls, TK_eof, tv ? LJ_ERR_XLSTR : LJ_ERR_XLCOM); | 161 | lj_lex_error(ls, TK_eof, tv ? LJ_ERR_XLSTR : LJ_ERR_XLCOM); |
163 | break; | 162 | break; |
164 | case ']': | 163 | case ']': |
165 | if (skip_sep(ls) == sep) { | 164 | if (lex_skipeq(ls) == sep) { |
166 | save_and_next(ls); /* skip 2nd `]' */ | 165 | lex_savenext(ls); /* Skip second ']'. */ |
167 | goto endloop; | 166 | goto endloop; |
168 | } | 167 | } |
169 | break; | 168 | break; |
170 | case '\n': | 169 | case '\n': |
171 | case '\r': | 170 | case '\r': |
172 | save(ls, '\n'); | 171 | lex_save(ls, '\n'); |
173 | inclinenumber(ls); | 172 | lex_newline(ls); |
174 | if (!tv) lj_str_resetbuf(&ls->sb); /* avoid wasting space */ | 173 | if (!tv) lj_buf_reset(&ls->sb); /* Don't waste space for comments. */ |
175 | break; | 174 | break; |
176 | default: | 175 | default: |
177 | if (tv) save_and_next(ls); | 176 | lex_savenext(ls); |
178 | else next(ls); | ||
179 | break; | 177 | break; |
180 | } | 178 | } |
181 | } endloop: | 179 | } endloop: |
182 | if (tv) { | 180 | if (tv) { |
183 | GCstr *str = lj_parse_keepstr(ls, ls->sb.buf + (2 + (MSize)sep), | 181 | GCstr *str = lj_parse_keepstr(ls, sbufB(&ls->sb) + (2 + (MSize)sep), |
184 | ls->sb.n - 2*(2 + (MSize)sep)); | 182 | sbuflen(&ls->sb) - 2*(2 + (MSize)sep)); |
185 | setstrV(ls->L, tv, str); | 183 | setstrV(ls->L, tv, str); |
186 | } | 184 | } |
187 | } | 185 | } |
188 | 186 | ||
189 | static void read_string(LexState *ls, int delim, TValue *tv) | 187 | /* Parse a string. */ |
188 | static void lex_string(LexState *ls, TValue *tv) | ||
190 | { | 189 | { |
191 | save_and_next(ls); | 190 | LexChar delim = ls->c; /* Delimiter is '\'' or '"'. */ |
192 | while (ls->current != delim) { | 191 | lex_savenext(ls); |
193 | switch (ls->current) { | 192 | while (ls->c != delim) { |
194 | case END_OF_STREAM: | 193 | switch (ls->c) { |
194 | case LEX_EOF: | ||
195 | lj_lex_error(ls, TK_eof, LJ_ERR_XSTR); | 195 | lj_lex_error(ls, TK_eof, LJ_ERR_XSTR); |
196 | continue; | 196 | continue; |
197 | case '\n': | 197 | case '\n': |
@@ -199,7 +199,7 @@ static void read_string(LexState *ls, int delim, TValue *tv) | |||
199 | lj_lex_error(ls, TK_string, LJ_ERR_XSTR); | 199 | lj_lex_error(ls, TK_string, LJ_ERR_XSTR); |
200 | continue; | 200 | continue; |
201 | case '\\': { | 201 | case '\\': { |
202 | int c = next(ls); /* Skip the '\\'. */ | 202 | LexChar c = lex_next(ls); /* Skip the '\\'. */ |
203 | switch (c) { | 203 | switch (c) { |
204 | case 'a': c = '\a'; break; | 204 | case 'a': c = '\a'; break; |
205 | case 'b': c = '\b'; break; | 205 | case 'b': c = '\b'; break; |
@@ -209,111 +209,139 @@ static void read_string(LexState *ls, int delim, TValue *tv) | |||
209 | case 't': c = '\t'; break; | 209 | case 't': c = '\t'; break; |
210 | case 'v': c = '\v'; break; | 210 | case 'v': c = '\v'; break; |
211 | case 'x': /* Hexadecimal escape '\xXX'. */ | 211 | case 'x': /* Hexadecimal escape '\xXX'. */ |
212 | c = (next(ls) & 15u) << 4; | 212 | c = (lex_next(ls) & 15u) << 4; |
213 | if (!lj_char_isdigit(ls->current)) { | 213 | if (!lj_char_isdigit(ls->c)) { |
214 | if (!lj_char_isxdigit(ls->current)) goto err_xesc; | 214 | if (!lj_char_isxdigit(ls->c)) goto err_xesc; |
215 | c += 9 << 4; | 215 | c += 9 << 4; |
216 | } | 216 | } |
217 | c += (next(ls) & 15u); | 217 | c += (lex_next(ls) & 15u); |
218 | if (!lj_char_isdigit(ls->current)) { | 218 | if (!lj_char_isdigit(ls->c)) { |
219 | if (!lj_char_isxdigit(ls->current)) goto err_xesc; | 219 | if (!lj_char_isxdigit(ls->c)) goto err_xesc; |
220 | c += 9; | 220 | c += 9; |
221 | } | 221 | } |
222 | break; | 222 | break; |
223 | case 'u': /* Unicode escape '\u{XX...}'. */ | ||
224 | if (lex_next(ls) != '{') goto err_xesc; | ||
225 | lex_next(ls); | ||
226 | c = 0; | ||
227 | do { | ||
228 | c = (c << 4) | (ls->c & 15u); | ||
229 | if (!lj_char_isdigit(ls->c)) { | ||
230 | if (!lj_char_isxdigit(ls->c)) goto err_xesc; | ||
231 | c += 9; | ||
232 | } | ||
233 | if (c >= 0x110000) goto err_xesc; /* Out of Unicode range. */ | ||
234 | } while (lex_next(ls) != '}'); | ||
235 | if (c < 0x800) { | ||
236 | if (c < 0x80) break; | ||
237 | lex_save(ls, 0xc0 | (c >> 6)); | ||
238 | } else { | ||
239 | if (c >= 0x10000) { | ||
240 | lex_save(ls, 0xf0 | (c >> 18)); | ||
241 | lex_save(ls, 0x80 | ((c >> 12) & 0x3f)); | ||
242 | } else { | ||
243 | if (c >= 0xd800 && c < 0xe000) goto err_xesc; /* No surrogates. */ | ||
244 | lex_save(ls, 0xe0 | (c >> 12)); | ||
245 | } | ||
246 | lex_save(ls, 0x80 | ((c >> 6) & 0x3f)); | ||
247 | } | ||
248 | c = 0x80 | (c & 0x3f); | ||
249 | break; | ||
223 | case 'z': /* Skip whitespace. */ | 250 | case 'z': /* Skip whitespace. */ |
224 | next(ls); | 251 | lex_next(ls); |
225 | while (lj_char_isspace(ls->current)) | 252 | while (lj_char_isspace(ls->c)) |
226 | if (currIsNewline(ls)) inclinenumber(ls); else next(ls); | 253 | if (lex_iseol(ls)) lex_newline(ls); else lex_next(ls); |
227 | continue; | 254 | continue; |
228 | case '\n': case '\r': save(ls, '\n'); inclinenumber(ls); continue; | 255 | case '\n': case '\r': lex_save(ls, '\n'); lex_newline(ls); continue; |
229 | case '\\': case '\"': case '\'': break; | 256 | case '\\': case '\"': case '\'': break; |
230 | case END_OF_STREAM: continue; | 257 | case LEX_EOF: continue; |
231 | default: | 258 | default: |
232 | if (!lj_char_isdigit(c)) | 259 | if (!lj_char_isdigit(c)) |
233 | goto err_xesc; | 260 | goto err_xesc; |
234 | c -= '0'; /* Decimal escape '\ddd'. */ | 261 | c -= '0'; /* Decimal escape '\ddd'. */ |
235 | if (lj_char_isdigit(next(ls))) { | 262 | if (lj_char_isdigit(lex_next(ls))) { |
236 | c = c*10 + (ls->current - '0'); | 263 | c = c*10 + (ls->c - '0'); |
237 | if (lj_char_isdigit(next(ls))) { | 264 | if (lj_char_isdigit(lex_next(ls))) { |
238 | c = c*10 + (ls->current - '0'); | 265 | c = c*10 + (ls->c - '0'); |
239 | if (c > 255) { | 266 | if (c > 255) { |
240 | err_xesc: | 267 | err_xesc: |
241 | lj_lex_error(ls, TK_string, LJ_ERR_XESC); | 268 | lj_lex_error(ls, TK_string, LJ_ERR_XESC); |
242 | } | 269 | } |
243 | next(ls); | 270 | lex_next(ls); |
244 | } | 271 | } |
245 | } | 272 | } |
246 | save(ls, c); | 273 | lex_save(ls, c); |
247 | continue; | 274 | continue; |
248 | } | 275 | } |
249 | save(ls, c); | 276 | lex_save(ls, c); |
250 | next(ls); | 277 | lex_next(ls); |
251 | continue; | 278 | continue; |
252 | } | 279 | } |
253 | default: | 280 | default: |
254 | save_and_next(ls); | 281 | lex_savenext(ls); |
255 | break; | 282 | break; |
256 | } | 283 | } |
257 | } | 284 | } |
258 | save_and_next(ls); /* skip delimiter */ | 285 | lex_savenext(ls); /* Skip trailing delimiter. */ |
259 | setstrV(ls->L, tv, lj_parse_keepstr(ls, ls->sb.buf + 1, ls->sb.n - 2)); | 286 | setstrV(ls->L, tv, |
287 | lj_parse_keepstr(ls, sbufB(&ls->sb)+1, sbuflen(&ls->sb)-2)); | ||
260 | } | 288 | } |
261 | 289 | ||
262 | /* -- Main lexical scanner ------------------------------------------------ */ | 290 | /* -- Main lexical scanner ------------------------------------------------ */ |
263 | 291 | ||
264 | static int llex(LexState *ls, TValue *tv) | 292 | /* Get next lexical token. */ |
293 | static LexToken lex_scan(LexState *ls, TValue *tv) | ||
265 | { | 294 | { |
266 | lj_str_resetbuf(&ls->sb); | 295 | lj_buf_reset(&ls->sb); |
267 | for (;;) { | 296 | for (;;) { |
268 | if (lj_char_isident(ls->current)) { | 297 | if (lj_char_isident(ls->c)) { |
269 | GCstr *s; | 298 | GCstr *s; |
270 | if (lj_char_isdigit(ls->current)) { /* Numeric literal. */ | 299 | if (lj_char_isdigit(ls->c)) { /* Numeric literal. */ |
271 | lex_number(ls, tv); | 300 | lex_number(ls, tv); |
272 | return TK_number; | 301 | return TK_number; |
273 | } | 302 | } |
274 | /* Identifier or reserved word. */ | 303 | /* Identifier or reserved word. */ |
275 | do { | 304 | do { |
276 | save_and_next(ls); | 305 | lex_savenext(ls); |
277 | } while (lj_char_isident(ls->current)); | 306 | } while (lj_char_isident(ls->c)); |
278 | s = lj_parse_keepstr(ls, ls->sb.buf, ls->sb.n); | 307 | s = lj_parse_keepstr(ls, sbufB(&ls->sb), sbuflen(&ls->sb)); |
279 | setstrV(ls->L, tv, s); | 308 | setstrV(ls->L, tv, s); |
280 | if (s->reserved > 0) /* Reserved word? */ | 309 | if (s->reserved > 0) /* Reserved word? */ |
281 | return TK_OFS + s->reserved; | 310 | return TK_OFS + s->reserved; |
282 | return TK_name; | 311 | return TK_name; |
283 | } | 312 | } |
284 | switch (ls->current) { | 313 | switch (ls->c) { |
285 | case '\n': | 314 | case '\n': |
286 | case '\r': | 315 | case '\r': |
287 | inclinenumber(ls); | 316 | lex_newline(ls); |
288 | continue; | 317 | continue; |
289 | case ' ': | 318 | case ' ': |
290 | case '\t': | 319 | case '\t': |
291 | case '\v': | 320 | case '\v': |
292 | case '\f': | 321 | case '\f': |
293 | next(ls); | 322 | lex_next(ls); |
294 | continue; | 323 | continue; |
295 | case '-': | 324 | case '-': |
296 | next(ls); | 325 | lex_next(ls); |
297 | if (ls->current != '-') return '-'; | 326 | if (ls->c != '-') return '-'; |
298 | /* else is a comment */ | 327 | lex_next(ls); |
299 | next(ls); | 328 | if (ls->c == '[') { /* Long comment "--[=*[...]=*]". */ |
300 | if (ls->current == '[') { | 329 | int sep = lex_skipeq(ls); |
301 | int sep = skip_sep(ls); | 330 | lj_buf_reset(&ls->sb); /* `lex_skipeq' may dirty the buffer */ |
302 | lj_str_resetbuf(&ls->sb); /* `skip_sep' may dirty the buffer */ | ||
303 | if (sep >= 0) { | 331 | if (sep >= 0) { |
304 | read_long_string(ls, NULL, sep); /* long comment */ | 332 | lex_longstring(ls, NULL, sep); |
305 | lj_str_resetbuf(&ls->sb); | 333 | lj_buf_reset(&ls->sb); |
306 | continue; | 334 | continue; |
307 | } | 335 | } |
308 | } | 336 | } |
309 | /* else short comment */ | 337 | /* Short comment "--.*\n". */ |
310 | while (!currIsNewline(ls) && ls->current != END_OF_STREAM) | 338 | while (!lex_iseol(ls) && ls->c != LEX_EOF) |
311 | next(ls); | 339 | lex_next(ls); |
312 | continue; | 340 | continue; |
313 | case '[': { | 341 | case '[': { |
314 | int sep = skip_sep(ls); | 342 | int sep = lex_skipeq(ls); |
315 | if (sep >= 0) { | 343 | if (sep >= 0) { |
316 | read_long_string(ls, tv, sep); | 344 | lex_longstring(ls, tv, sep); |
317 | return TK_string; | 345 | return TK_string; |
318 | } else if (sep == -1) { | 346 | } else if (sep == -1) { |
319 | return '['; | 347 | return '['; |
@@ -323,44 +351,43 @@ static int llex(LexState *ls, TValue *tv) | |||
323 | } | 351 | } |
324 | } | 352 | } |
325 | case '=': | 353 | case '=': |
326 | next(ls); | 354 | lex_next(ls); |
327 | if (ls->current != '=') return '='; else { next(ls); return TK_eq; } | 355 | if (ls->c != '=') return '='; else { lex_next(ls); return TK_eq; } |
328 | case '<': | 356 | case '<': |
329 | next(ls); | 357 | lex_next(ls); |
330 | if (ls->current != '=') return '<'; else { next(ls); return TK_le; } | 358 | if (ls->c != '=') return '<'; else { lex_next(ls); return TK_le; } |
331 | case '>': | 359 | case '>': |
332 | next(ls); | 360 | lex_next(ls); |
333 | if (ls->current != '=') return '>'; else { next(ls); return TK_ge; } | 361 | if (ls->c != '=') return '>'; else { lex_next(ls); return TK_ge; } |
334 | case '~': | 362 | case '~': |
335 | next(ls); | 363 | lex_next(ls); |
336 | if (ls->current != '=') return '~'; else { next(ls); return TK_ne; } | 364 | if (ls->c != '=') return '~'; else { lex_next(ls); return TK_ne; } |
337 | case ':': | 365 | case ':': |
338 | next(ls); | 366 | lex_next(ls); |
339 | if (ls->current != ':') return ':'; else { next(ls); return TK_label; } | 367 | if (ls->c != ':') return ':'; else { lex_next(ls); return TK_label; } |
340 | case '"': | 368 | case '"': |
341 | case '\'': | 369 | case '\'': |
342 | read_string(ls, ls->current, tv); | 370 | lex_string(ls, tv); |
343 | return TK_string; | 371 | return TK_string; |
344 | case '.': | 372 | case '.': |
345 | save_and_next(ls); | 373 | if (lex_savenext(ls) == '.') { |
346 | if (ls->current == '.') { | 374 | lex_next(ls); |
347 | next(ls); | 375 | if (ls->c == '.') { |
348 | if (ls->current == '.') { | 376 | lex_next(ls); |
349 | next(ls); | ||
350 | return TK_dots; /* ... */ | 377 | return TK_dots; /* ... */ |
351 | } | 378 | } |
352 | return TK_concat; /* .. */ | 379 | return TK_concat; /* .. */ |
353 | } else if (!lj_char_isdigit(ls->current)) { | 380 | } else if (!lj_char_isdigit(ls->c)) { |
354 | return '.'; | 381 | return '.'; |
355 | } else { | 382 | } else { |
356 | lex_number(ls, tv); | 383 | lex_number(ls, tv); |
357 | return TK_number; | 384 | return TK_number; |
358 | } | 385 | } |
359 | case END_OF_STREAM: | 386 | case LEX_EOF: |
360 | return TK_eof; | 387 | return TK_eof; |
361 | default: { | 388 | default: { |
362 | int c = ls->current; | 389 | LexChar c = ls->c; |
363 | next(ls); | 390 | lex_next(ls); |
364 | return c; /* Single-char tokens (+ - / ...). */ | 391 | return c; /* Single-char tokens (+ - / ...). */ |
365 | } | 392 | } |
366 | } | 393 | } |
@@ -375,36 +402,33 @@ int lj_lex_setup(lua_State *L, LexState *ls) | |||
375 | int header = 0; | 402 | int header = 0; |
376 | ls->L = L; | 403 | ls->L = L; |
377 | ls->fs = NULL; | 404 | ls->fs = NULL; |
378 | ls->n = 0; | 405 | ls->pe = ls->p = NULL; |
379 | ls->p = NULL; | ||
380 | ls->vstack = NULL; | 406 | ls->vstack = NULL; |
381 | ls->sizevstack = 0; | 407 | ls->sizevstack = 0; |
382 | ls->vtop = 0; | 408 | ls->vtop = 0; |
383 | ls->bcstack = NULL; | 409 | ls->bcstack = NULL; |
384 | ls->sizebcstack = 0; | 410 | ls->sizebcstack = 0; |
385 | ls->token = 0; | 411 | ls->tok = 0; |
386 | ls->lookahead = TK_eof; /* No look-ahead token. */ | 412 | ls->lookahead = TK_eof; /* No look-ahead token. */ |
387 | ls->linenumber = 1; | 413 | ls->linenumber = 1; |
388 | ls->lastline = 1; | 414 | ls->lastline = 1; |
389 | ls->endmark = 0; | 415 | ls->endmark = 0; |
390 | lj_str_resizebuf(ls->L, &ls->sb, LJ_MIN_SBUF); | 416 | lex_next(ls); /* Read-ahead first char. */ |
391 | next(ls); /* Read-ahead first char. */ | 417 | if (ls->c == 0xef && ls->p + 2 <= ls->pe && (uint8_t)ls->p[0] == 0xbb && |
392 | if (ls->current == 0xef && ls->n >= 2 && char2int(ls->p[0]) == 0xbb && | 418 | (uint8_t)ls->p[1] == 0xbf) { /* Skip UTF-8 BOM (if buffered). */ |
393 | char2int(ls->p[1]) == 0xbf) { /* Skip UTF-8 BOM (if buffered). */ | ||
394 | ls->n -= 2; | ||
395 | ls->p += 2; | 419 | ls->p += 2; |
396 | next(ls); | 420 | lex_next(ls); |
397 | header = 1; | 421 | header = 1; |
398 | } | 422 | } |
399 | if (ls->current == '#') { /* Skip POSIX #! header line. */ | 423 | if (ls->c == '#') { /* Skip POSIX #! header line. */ |
400 | do { | 424 | do { |
401 | next(ls); | 425 | lex_next(ls); |
402 | if (ls->current == END_OF_STREAM) return 0; | 426 | if (ls->c == LEX_EOF) return 0; |
403 | } while (!currIsNewline(ls)); | 427 | } while (!lex_iseol(ls)); |
404 | inclinenumber(ls); | 428 | lex_newline(ls); |
405 | header = 1; | 429 | header = 1; |
406 | } | 430 | } |
407 | if (ls->current == LUA_SIGNATURE[0]) { /* Bytecode dump. */ | 431 | if (ls->c == LUA_SIGNATURE[0]) { /* Bytecode dump. */ |
408 | if (header) { | 432 | if (header) { |
409 | /* | 433 | /* |
410 | ** Loading bytecode with an extra header is disabled for security | 434 | ** Loading bytecode with an extra header is disabled for security |
@@ -426,55 +450,60 @@ void lj_lex_cleanup(lua_State *L, LexState *ls) | |||
426 | global_State *g = G(L); | 450 | global_State *g = G(L); |
427 | lj_mem_freevec(g, ls->bcstack, ls->sizebcstack, BCInsLine); | 451 | lj_mem_freevec(g, ls->bcstack, ls->sizebcstack, BCInsLine); |
428 | lj_mem_freevec(g, ls->vstack, ls->sizevstack, VarInfo); | 452 | lj_mem_freevec(g, ls->vstack, ls->sizevstack, VarInfo); |
429 | lj_str_freebuf(g, &ls->sb); | 453 | lj_buf_free(g, &ls->sb); |
430 | } | 454 | } |
431 | 455 | ||
456 | /* Return next lexical token. */ | ||
432 | void lj_lex_next(LexState *ls) | 457 | void lj_lex_next(LexState *ls) |
433 | { | 458 | { |
434 | ls->lastline = ls->linenumber; | 459 | ls->lastline = ls->linenumber; |
435 | if (LJ_LIKELY(ls->lookahead == TK_eof)) { /* No lookahead token? */ | 460 | if (LJ_LIKELY(ls->lookahead == TK_eof)) { /* No lookahead token? */ |
436 | ls->token = llex(ls, &ls->tokenval); /* Get next token. */ | 461 | ls->tok = lex_scan(ls, &ls->tokval); /* Get next token. */ |
437 | } else { /* Otherwise return lookahead token. */ | 462 | } else { /* Otherwise return lookahead token. */ |
438 | ls->token = ls->lookahead; | 463 | ls->tok = ls->lookahead; |
439 | ls->lookahead = TK_eof; | 464 | ls->lookahead = TK_eof; |
440 | ls->tokenval = ls->lookaheadval; | 465 | ls->tokval = ls->lookaheadval; |
441 | } | 466 | } |
442 | } | 467 | } |
443 | 468 | ||
469 | /* Look ahead for the next token. */ | ||
444 | LexToken lj_lex_lookahead(LexState *ls) | 470 | LexToken lj_lex_lookahead(LexState *ls) |
445 | { | 471 | { |
446 | lua_assert(ls->lookahead == TK_eof); | 472 | lua_assert(ls->lookahead == TK_eof); |
447 | ls->lookahead = llex(ls, &ls->lookaheadval); | 473 | ls->lookahead = lex_scan(ls, &ls->lookaheadval); |
448 | return ls->lookahead; | 474 | return ls->lookahead; |
449 | } | 475 | } |
450 | 476 | ||
451 | const char *lj_lex_token2str(LexState *ls, LexToken token) | 477 | /* Convert token to string. */ |
478 | const char *lj_lex_token2str(LexState *ls, LexToken tok) | ||
452 | { | 479 | { |
453 | if (token > TK_OFS) | 480 | if (tok > TK_OFS) |
454 | return tokennames[token-TK_OFS-1]; | 481 | return tokennames[tok-TK_OFS-1]; |
455 | else if (!lj_char_iscntrl(token)) | 482 | else if (!lj_char_iscntrl(tok)) |
456 | return lj_str_pushf(ls->L, "%c", token); | 483 | return lj_strfmt_pushf(ls->L, "%c", tok); |
457 | else | 484 | else |
458 | return lj_str_pushf(ls->L, "char(%d)", token); | 485 | return lj_strfmt_pushf(ls->L, "char(%d)", tok); |
459 | } | 486 | } |
460 | 487 | ||
461 | void lj_lex_error(LexState *ls, LexToken token, ErrMsg em, ...) | 488 | /* Lexer error. */ |
489 | void lj_lex_error(LexState *ls, LexToken tok, ErrMsg em, ...) | ||
462 | { | 490 | { |
463 | const char *tok; | 491 | const char *tokstr; |
464 | va_list argp; | 492 | va_list argp; |
465 | if (token == 0) { | 493 | if (tok == 0) { |
466 | tok = NULL; | 494 | tokstr = NULL; |
467 | } else if (token == TK_name || token == TK_string || token == TK_number) { | 495 | } else if (tok == TK_name || tok == TK_string || tok == TK_number) { |
468 | save(ls, '\0'); | 496 | lex_save(ls, '\0'); |
469 | tok = ls->sb.buf; | 497 | tokstr = sbufB(&ls->sb); |
470 | } else { | 498 | } else { |
471 | tok = lj_lex_token2str(ls, token); | 499 | tokstr = lj_lex_token2str(ls, tok); |
472 | } | 500 | } |
473 | va_start(argp, em); | 501 | va_start(argp, em); |
474 | lj_err_lex(ls->L, ls->chunkname, tok, ls->linenumber, em, argp); | 502 | lj_err_lex(ls->L, ls->chunkname, tokstr, ls->linenumber, em, argp); |
475 | va_end(argp); | 503 | va_end(argp); |
476 | } | 504 | } |
477 | 505 | ||
506 | /* Initialize strings for reserved words. */ | ||
478 | void lj_lex_init(lua_State *L) | 507 | void lj_lex_init(lua_State *L) |
479 | { | 508 | { |
480 | uint32_t i; | 509 | uint32_t i; |