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