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