diff options
author | Marcin Bukat <marcin.bukat@gmail.com> | 2014-04-02 20:46:06 +0200 |
---|---|---|
committer | Marcin Bukat <marcin.bukat@gmail.com> | 2014-04-02 20:46:06 +0200 |
commit | bfd0179042b0b02fb88748d54e56e7e208bb117f (patch) | |
tree | 42d5fd51574054caaf673420fca1ec962d62d2f2 /apps/plugins/lua/llex.c | |
parent | 36378988ad4059982742f05f5eb50580b456840a (diff) | |
download | rockbox-bfd0179042b0b02fb88748d54e56e7e208bb117f.tar.gz rockbox-bfd0179042b0b02fb88748d54e56e7e208bb117f.zip |
Revert "Update lua plugin to 5.2.3"
FILE typedef to *void needs more work to not break sim and
application builds. I checked only a few random native builds
unfortunately. Sorry for inconvenience.
Diffstat (limited to 'apps/plugins/lua/llex.c')
-rw-r--r-- | apps/plugins/lua/llex.c | 368 |
1 files changed, 150 insertions, 218 deletions
diff --git a/apps/plugins/lua/llex.c b/apps/plugins/lua/llex.c index 8d410f620d..85ac516be3 100644 --- a/apps/plugins/lua/llex.c +++ b/apps/plugins/lua/llex.c | |||
@@ -1,10 +1,12 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: llex.c,v 2.63.1.2 2013/08/30 15:49:41 roberto Exp $ | 2 | ** $Id: llex.c,v 2.20.1.1 2007/12/27 13:02:25 roberto Exp $ |
3 | ** Lexical Analyzer | 3 | ** Lexical Analyzer |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
6 | 6 | ||
7 | 7 | ||
8 | #include <ctype.h> | ||
9 | /* #include <locale.h> */ | ||
8 | #include <string.h> | 10 | #include <string.h> |
9 | 11 | ||
10 | #define llex_c | 12 | #define llex_c |
@@ -12,7 +14,6 @@ | |||
12 | 14 | ||
13 | #include "lua.h" | 15 | #include "lua.h" |
14 | 16 | ||
15 | #include "lctype.h" | ||
16 | #include "ldo.h" | 17 | #include "ldo.h" |
17 | #include "llex.h" | 18 | #include "llex.h" |
18 | #include "lobject.h" | 19 | #include "lobject.h" |
@@ -28,36 +29,35 @@ | |||
28 | 29 | ||
29 | 30 | ||
30 | 31 | ||
32 | |||
31 | #define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r') | 33 | #define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r') |
32 | 34 | ||
33 | 35 | ||
34 | /* ORDER RESERVED */ | 36 | /* ORDER RESERVED */ |
35 | static const char *const luaX_tokens [] = { | 37 | const char *const luaX_tokens [] = { |
36 | "and", "break", "do", "else", "elseif", | 38 | "and", "break", "do", "else", "elseif", |
37 | "end", "false", "for", "function", "goto", "if", | 39 | "end", "false", "for", "function", "if", |
38 | "in", "local", "nil", "not", "or", "repeat", | 40 | "in", "local", "nil", "not", "or", "repeat", |
39 | "return", "then", "true", "until", "while", | 41 | "return", "then", "true", "until", "while", |
40 | "..", "...", "==", ">=", "<=", "~=", "::", "<eof>", | 42 | "..", "...", "==", ">=", "<=", "~=", |
41 | "<number>", "<name>", "<string>" | 43 | "<number>", "<name>", "<string>", "<eof>", |
44 | NULL | ||
42 | }; | 45 | }; |
43 | 46 | ||
44 | 47 | ||
45 | #define save_and_next(ls) (save(ls, ls->current), next(ls)) | 48 | #define save_and_next(ls) (save(ls, ls->current), next(ls)) |
46 | 49 | ||
47 | 50 | ||
48 | static l_noret lexerror (LexState *ls, const char *msg, int token); | ||
49 | |||
50 | |||
51 | static void save (LexState *ls, int c) { | 51 | static void save (LexState *ls, int c) { |
52 | Mbuffer *b = ls->buff; | 52 | Mbuffer *b = ls->buff; |
53 | if (luaZ_bufflen(b) + 1 > luaZ_sizebuffer(b)) { | 53 | if (b->n + 1 > b->buffsize) { |
54 | size_t newsize; | 54 | size_t newsize; |
55 | if (luaZ_sizebuffer(b) >= MAX_SIZET/2) | 55 | if (b->buffsize >= MAX_SIZET/2) |
56 | lexerror(ls, "lexical element too long", 0); | 56 | luaX_lexerror(ls, "lexical element too long", 0); |
57 | newsize = luaZ_sizebuffer(b) * 2; | 57 | newsize = b->buffsize * 2; |
58 | luaZ_resizebuffer(ls->L, b, newsize); | 58 | luaZ_resizebuffer(ls->L, b, newsize); |
59 | } | 59 | } |
60 | b->buffer[luaZ_bufflen(b)++] = cast(char, c); | 60 | b->buffer[b->n++] = cast(char, c); |
61 | } | 61 | } |
62 | 62 | ||
63 | 63 | ||
@@ -66,24 +66,23 @@ void luaX_init (lua_State *L) { | |||
66 | for (i=0; i<NUM_RESERVED; i++) { | 66 | for (i=0; i<NUM_RESERVED; i++) { |
67 | TString *ts = luaS_new(L, luaX_tokens[i]); | 67 | TString *ts = luaS_new(L, luaX_tokens[i]); |
68 | luaS_fix(ts); /* reserved words are never collected */ | 68 | luaS_fix(ts); /* reserved words are never collected */ |
69 | ts->tsv.extra = cast_byte(i+1); /* reserved word */ | 69 | lua_assert(strlen(luaX_tokens[i])+1 <= TOKEN_LEN); |
70 | ts->tsv.reserved = cast_byte(i+1); /* reserved word */ | ||
70 | } | 71 | } |
71 | } | 72 | } |
72 | 73 | ||
73 | 74 | ||
75 | #define MAXSRC 80 | ||
76 | |||
77 | |||
74 | const char *luaX_token2str (LexState *ls, int token) { | 78 | const char *luaX_token2str (LexState *ls, int token) { |
75 | if (token < FIRST_RESERVED) { /* single-byte symbols? */ | 79 | if (token < FIRST_RESERVED) { |
76 | lua_assert(token == cast(unsigned char, token)); | 80 | lua_assert(token == cast(unsigned char, token)); |
77 | return (lisprint(token)) ? luaO_pushfstring(ls->L, LUA_QL("%c"), token) : | 81 | return (iscntrl(token)) ? luaO_pushfstring(ls->L, "char(%d)", token) : |
78 | luaO_pushfstring(ls->L, "char(%d)", token); | 82 | luaO_pushfstring(ls->L, "%c", token); |
79 | } | ||
80 | else { | ||
81 | const char *s = luaX_tokens[token - FIRST_RESERVED]; | ||
82 | if (token < TK_EOS) /* fixed format (symbols and reserved words)? */ | ||
83 | return luaO_pushfstring(ls->L, LUA_QS, s); | ||
84 | else /* names, strings, and numerals */ | ||
85 | return s; | ||
86 | } | 83 | } |
84 | else | ||
85 | return luaX_tokens[token-FIRST_RESERVED]; | ||
87 | } | 86 | } |
88 | 87 | ||
89 | 88 | ||
@@ -93,57 +92,38 @@ static const char *txtToken (LexState *ls, int token) { | |||
93 | case TK_STRING: | 92 | case TK_STRING: |
94 | case TK_NUMBER: | 93 | case TK_NUMBER: |
95 | save(ls, '\0'); | 94 | save(ls, '\0'); |
96 | return luaO_pushfstring(ls->L, LUA_QS, luaZ_buffer(ls->buff)); | 95 | return luaZ_buffer(ls->buff); |
97 | default: | 96 | default: |
98 | return luaX_token2str(ls, token); | 97 | return luaX_token2str(ls, token); |
99 | } | 98 | } |
100 | } | 99 | } |
101 | 100 | ||
102 | 101 | ||
103 | static l_noret lexerror (LexState *ls, const char *msg, int token) { | 102 | void luaX_lexerror (LexState *ls, const char *msg, int token) { |
104 | char buff[LUA_IDSIZE]; | 103 | char buff[MAXSRC]; |
105 | luaO_chunkid(buff, getstr(ls->source), LUA_IDSIZE); | 104 | luaO_chunkid(buff, getstr(ls->source), MAXSRC); |
106 | msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg); | 105 | msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg); |
107 | if (token) | 106 | if (token) |
108 | luaO_pushfstring(ls->L, "%s near %s", msg, txtToken(ls, token)); | 107 | luaO_pushfstring(ls->L, "%s near " LUA_QS, msg, txtToken(ls, token)); |
109 | luaD_throw(ls->L, LUA_ERRSYNTAX); | 108 | luaD_throw(ls->L, LUA_ERRSYNTAX); |
110 | } | 109 | } |
111 | 110 | ||
112 | 111 | ||
113 | l_noret luaX_syntaxerror (LexState *ls, const char *msg) { | 112 | void luaX_syntaxerror (LexState *ls, const char *msg) { |
114 | lexerror(ls, msg, ls->t.token); | 113 | luaX_lexerror(ls, msg, ls->t.token); |
115 | } | 114 | } |
116 | 115 | ||
117 | 116 | ||
118 | /* | ||
119 | ** creates a new string and anchors it in function's table so that | ||
120 | ** it will not be collected until the end of the function's compilation | ||
121 | ** (by that time it should be anchored in function's prototype) | ||
122 | */ | ||
123 | TString *luaX_newstring (LexState *ls, const char *str, size_t l) { | 117 | TString *luaX_newstring (LexState *ls, const char *str, size_t l) { |
124 | lua_State *L = ls->L; | 118 | lua_State *L = ls->L; |
125 | TValue *o; /* entry for `str' */ | 119 | TString *ts = luaS_newlstr(L, str, l); |
126 | TString *ts = luaS_newlstr(L, str, l); /* create new string */ | 120 | TValue *o = luaH_setstr(L, ls->fs->h, ts); /* entry for `str' */ |
127 | setsvalue2s(L, L->top++, ts); /* temporarily anchor it in stack */ | 121 | if (ttisnil(o)) |
128 | o = luaH_set(L, ls->fs->h, L->top - 1); | 122 | setbvalue(o, 1); /* make sure `str' will not be collected */ |
129 | if (ttisnil(o)) { /* not in use yet? (see 'addK') */ | ||
130 | /* boolean value does not need GC barrier; | ||
131 | table has no metatable, so it does not need to invalidate cache */ | ||
132 | setbvalue(o, 1); /* t[string] = true */ | ||
133 | luaC_checkGC(L); | ||
134 | } | ||
135 | else { /* string already present */ | ||
136 | ts = rawtsvalue(keyfromval(o)); /* re-use value previously stored */ | ||
137 | } | ||
138 | L->top--; /* remove string from stack */ | ||
139 | return ts; | 123 | return ts; |
140 | } | 124 | } |
141 | 125 | ||
142 | 126 | ||
143 | /* | ||
144 | ** increment line number and skips newline sequence (any of | ||
145 | ** \n, \r, \n\r, or \r\n) | ||
146 | */ | ||
147 | static void inclinenumber (LexState *ls) { | 127 | static void inclinenumber (LexState *ls) { |
148 | int old = ls->current; | 128 | int old = ls->current; |
149 | lua_assert(currIsNewline(ls)); | 129 | lua_assert(currIsNewline(ls)); |
@@ -155,20 +135,17 @@ static void inclinenumber (LexState *ls) { | |||
155 | } | 135 | } |
156 | 136 | ||
157 | 137 | ||
158 | void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source, | 138 | void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source) { |
159 | int firstchar) { | ||
160 | ls->decpoint = '.'; | 139 | ls->decpoint = '.'; |
161 | ls->L = L; | 140 | ls->L = L; |
162 | ls->current = firstchar; | ||
163 | ls->lookahead.token = TK_EOS; /* no look-ahead token */ | 141 | ls->lookahead.token = TK_EOS; /* no look-ahead token */ |
164 | ls->z = z; | 142 | ls->z = z; |
165 | ls->fs = NULL; | 143 | ls->fs = NULL; |
166 | ls->linenumber = 1; | 144 | ls->linenumber = 1; |
167 | ls->lastline = 1; | 145 | ls->lastline = 1; |
168 | ls->source = source; | 146 | ls->source = source; |
169 | ls->envn = luaS_new(L, LUA_ENV); /* create env name */ | ||
170 | luaS_fix(ls->envn); /* never collect this name */ | ||
171 | luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */ | 147 | luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */ |
148 | next(ls); /* read first char */ | ||
172 | } | 149 | } |
173 | 150 | ||
174 | 151 | ||
@@ -182,16 +159,13 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source, | |||
182 | 159 | ||
183 | 160 | ||
184 | static int check_next (LexState *ls, const char *set) { | 161 | static int check_next (LexState *ls, const char *set) { |
185 | if (ls->current == '\0' || !strchr(set, ls->current)) | 162 | if (!strchr(set, ls->current)) |
186 | return 0; | 163 | return 0; |
187 | save_and_next(ls); | 164 | save_and_next(ls); |
188 | return 1; | 165 | return 1; |
189 | } | 166 | } |
190 | 167 | ||
191 | 168 | ||
192 | /* | ||
193 | ** change all characters 'from' in buffer to 'to' | ||
194 | */ | ||
195 | static void buffreplace (LexState *ls, char from, char to) { | 169 | static void buffreplace (LexState *ls, char from, char to) { |
196 | size_t n = luaZ_bufflen(ls->buff); | 170 | size_t n = luaZ_bufflen(ls->buff); |
197 | char *p = luaZ_buffer(ls->buff); | 171 | char *p = luaZ_buffer(ls->buff); |
@@ -200,59 +174,37 @@ static void buffreplace (LexState *ls, char from, char to) { | |||
200 | } | 174 | } |
201 | 175 | ||
202 | 176 | ||
203 | #if !defined(getlocaledecpoint) | ||
204 | #define getlocaledecpoint() (localeconv()->decimal_point[0]) | ||
205 | #endif | ||
206 | |||
207 | |||
208 | #define buff2d(b,e) luaO_str2d(luaZ_buffer(b), luaZ_bufflen(b) - 1, e) | ||
209 | |||
210 | /* | ||
211 | ** in case of format error, try to change decimal point separator to | ||
212 | ** the one defined in the current locale and check again | ||
213 | */ | ||
214 | static void trydecpoint (LexState *ls, SemInfo *seminfo) { | 177 | static void trydecpoint (LexState *ls, SemInfo *seminfo) { |
178 | /* format error: try to update decimal point separator */ | ||
179 | /* struct lconv *cv = localeconv(); */ | ||
215 | char old = ls->decpoint; | 180 | char old = ls->decpoint; |
216 | ls->decpoint = getlocaledecpoint(); | 181 | ls->decpoint = '.'; /* (cv ? cv->decimal_point[0] : '.'); */ |
217 | buffreplace(ls, old, ls->decpoint); /* try new decimal separator */ | 182 | buffreplace(ls, old, ls->decpoint); /* try updated decimal separator */ |
218 | if (!buff2d(ls->buff, &seminfo->r)) { | 183 | if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) { |
219 | /* format error with correct decimal point: no more options */ | 184 | /* format error with correct decimal point: no more options */ |
220 | buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */ | 185 | buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */ |
221 | lexerror(ls, "malformed number", TK_NUMBER); | 186 | luaX_lexerror(ls, "malformed number", TK_NUMBER); |
222 | } | 187 | } |
223 | } | 188 | } |
224 | 189 | ||
225 | 190 | ||
226 | /* LUA_NUMBER */ | 191 | /* LUA_NUMBER */ |
227 | /* | ||
228 | ** this function is quite liberal in what it accepts, as 'luaO_str2d' | ||
229 | ** will reject ill-formed numerals. | ||
230 | */ | ||
231 | static void read_numeral (LexState *ls, SemInfo *seminfo) { | 192 | static void read_numeral (LexState *ls, SemInfo *seminfo) { |
232 | const char *expo = "Ee"; | 193 | lua_assert(isdigit(ls->current)); |
233 | int first = ls->current; | 194 | do { |
234 | lua_assert(lisdigit(ls->current)); | 195 | save_and_next(ls); |
235 | save_and_next(ls); | 196 | } while (isdigit(ls->current) || ls->current == '.'); |
236 | if (first == '0' && check_next(ls, "Xx")) /* hexadecimal? */ | 197 | if (check_next(ls, "Ee")) /* `E'? */ |
237 | expo = "Pp"; | 198 | check_next(ls, "+-"); /* optional exponent sign */ |
238 | for (;;) { | 199 | while (isalnum(ls->current) || ls->current == '_') |
239 | if (check_next(ls, expo)) /* exponent part? */ | 200 | save_and_next(ls); |
240 | check_next(ls, "+-"); /* optional exponent sign */ | ||
241 | if (lisxdigit(ls->current) || ls->current == '.') | ||
242 | save_and_next(ls); | ||
243 | else break; | ||
244 | } | ||
245 | save(ls, '\0'); | 201 | save(ls, '\0'); |
246 | buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */ | 202 | buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */ |
247 | if (!buff2d(ls->buff, &seminfo->r)) /* format error? */ | 203 | if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) /* format error? */ |
248 | trydecpoint(ls, seminfo); /* try to update decimal point separator */ | 204 | trydecpoint(ls, seminfo); /* try to update decimal point separator */ |
249 | } | 205 | } |
250 | 206 | ||
251 | 207 | ||
252 | /* | ||
253 | ** skip a sequence '[=*[' or ']=*]' and return its number of '='s or | ||
254 | ** -1 if sequence is malformed | ||
255 | */ | ||
256 | static int skip_sep (LexState *ls) { | 208 | static int skip_sep (LexState *ls) { |
257 | int count = 0; | 209 | int count = 0; |
258 | int s = ls->current; | 210 | int s = ls->current; |
@@ -267,23 +219,43 @@ static int skip_sep (LexState *ls) { | |||
267 | 219 | ||
268 | 220 | ||
269 | static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) { | 221 | static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) { |
222 | int cont = 0; | ||
223 | (void)(cont); /* avoid warnings when `cont' is not used */ | ||
270 | save_and_next(ls); /* skip 2nd `[' */ | 224 | save_and_next(ls); /* skip 2nd `[' */ |
271 | if (currIsNewline(ls)) /* string starts with a newline? */ | 225 | if (currIsNewline(ls)) /* string starts with a newline? */ |
272 | inclinenumber(ls); /* skip it */ | 226 | inclinenumber(ls); /* skip it */ |
273 | for (;;) { | 227 | for (;;) { |
274 | switch (ls->current) { | 228 | switch (ls->current) { |
275 | case EOZ: | 229 | case EOZ: |
276 | lexerror(ls, (seminfo) ? "unfinished long string" : | 230 | luaX_lexerror(ls, (seminfo) ? "unfinished long string" : |
277 | "unfinished long comment", TK_EOS); | 231 | "unfinished long comment", TK_EOS); |
278 | break; /* to avoid warnings */ | 232 | break; /* to avoid warnings */ |
233 | #if defined(LUA_COMPAT_LSTR) | ||
234 | case '[': { | ||
235 | if (skip_sep(ls) == sep) { | ||
236 | save_and_next(ls); /* skip 2nd `[' */ | ||
237 | cont++; | ||
238 | #if LUA_COMPAT_LSTR == 1 | ||
239 | if (sep == 0) | ||
240 | luaX_lexerror(ls, "nesting of [[...]] is deprecated", '['); | ||
241 | #endif | ||
242 | } | ||
243 | break; | ||
244 | } | ||
245 | #endif | ||
279 | case ']': { | 246 | case ']': { |
280 | if (skip_sep(ls) == sep) { | 247 | if (skip_sep(ls) == sep) { |
281 | save_and_next(ls); /* skip 2nd `]' */ | 248 | save_and_next(ls); /* skip 2nd `]' */ |
249 | #if defined(LUA_COMPAT_LSTR) && LUA_COMPAT_LSTR == 2 | ||
250 | cont--; | ||
251 | if (sep == 0 && cont >= 0) break; | ||
252 | #endif | ||
282 | goto endloop; | 253 | goto endloop; |
283 | } | 254 | } |
284 | break; | 255 | break; |
285 | } | 256 | } |
286 | case '\n': case '\r': { | 257 | case '\n': |
258 | case '\r': { | ||
287 | save(ls, '\n'); | 259 | save(ls, '\n'); |
288 | inclinenumber(ls); | 260 | inclinenumber(ls); |
289 | if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */ | 261 | if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */ |
@@ -301,91 +273,51 @@ static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) { | |||
301 | } | 273 | } |
302 | 274 | ||
303 | 275 | ||
304 | static void escerror (LexState *ls, int *c, int n, const char *msg) { | ||
305 | int i; | ||
306 | luaZ_resetbuffer(ls->buff); /* prepare error message */ | ||
307 | save(ls, '\\'); | ||
308 | for (i = 0; i < n && c[i] != EOZ; i++) | ||
309 | save(ls, c[i]); | ||
310 | lexerror(ls, msg, TK_STRING); | ||
311 | } | ||
312 | |||
313 | |||
314 | static int readhexaesc (LexState *ls) { | ||
315 | int c[3], i; /* keep input for error message */ | ||
316 | int r = 0; /* result accumulator */ | ||
317 | c[0] = 'x'; /* for error message */ | ||
318 | for (i = 1; i < 3; i++) { /* read two hexadecimal digits */ | ||
319 | c[i] = next(ls); | ||
320 | if (!lisxdigit(c[i])) | ||
321 | escerror(ls, c, i + 1, "hexadecimal digit expected"); | ||
322 | r = (r << 4) + luaO_hexavalue(c[i]); | ||
323 | } | ||
324 | return r; | ||
325 | } | ||
326 | |||
327 | |||
328 | static int readdecesc (LexState *ls) { | ||
329 | int c[3], i; | ||
330 | int r = 0; /* result accumulator */ | ||
331 | for (i = 0; i < 3 && lisdigit(ls->current); i++) { /* read up to 3 digits */ | ||
332 | c[i] = ls->current; | ||
333 | r = 10*r + c[i] - '0'; | ||
334 | next(ls); | ||
335 | } | ||
336 | if (r > UCHAR_MAX) | ||
337 | escerror(ls, c, i, "decimal escape too large"); | ||
338 | return r; | ||
339 | } | ||
340 | |||
341 | |||
342 | static void read_string (LexState *ls, int del, SemInfo *seminfo) { | 276 | static void read_string (LexState *ls, int del, SemInfo *seminfo) { |
343 | save_and_next(ls); /* keep delimiter (for error messages) */ | 277 | save_and_next(ls); |
344 | while (ls->current != del) { | 278 | while (ls->current != del) { |
345 | switch (ls->current) { | 279 | switch (ls->current) { |
346 | case EOZ: | 280 | case EOZ: |
347 | lexerror(ls, "unfinished string", TK_EOS); | 281 | luaX_lexerror(ls, "unfinished string", TK_EOS); |
348 | break; /* to avoid warnings */ | 282 | continue; /* to avoid warnings */ |
349 | case '\n': | 283 | case '\n': |
350 | case '\r': | 284 | case '\r': |
351 | lexerror(ls, "unfinished string", TK_STRING); | 285 | luaX_lexerror(ls, "unfinished string", TK_STRING); |
352 | break; /* to avoid warnings */ | 286 | continue; /* to avoid warnings */ |
353 | case '\\': { /* escape sequences */ | 287 | case '\\': { |
354 | int c; /* final character to be saved */ | 288 | int c; |
355 | next(ls); /* do not save the `\' */ | 289 | next(ls); /* do not save the `\' */ |
356 | switch (ls->current) { | 290 | switch (ls->current) { |
357 | case 'a': c = '\a'; goto read_save; | 291 | case 'a': c = '\a'; break; |
358 | case 'b': c = '\b'; goto read_save; | 292 | case 'b': c = '\b'; break; |
359 | case 'f': c = '\f'; goto read_save; | 293 | case 'f': c = '\f'; break; |
360 | case 'n': c = '\n'; goto read_save; | 294 | case 'n': c = '\n'; break; |
361 | case 'r': c = '\r'; goto read_save; | 295 | case 'r': c = '\r'; break; |
362 | case 't': c = '\t'; goto read_save; | 296 | case 't': c = '\t'; break; |
363 | case 'v': c = '\v'; goto read_save; | 297 | case 'v': c = '\v'; break; |
364 | case 'x': c = readhexaesc(ls); goto read_save; | 298 | case '\n': /* go through */ |
365 | case '\n': case '\r': | 299 | case '\r': save(ls, '\n'); inclinenumber(ls); continue; |
366 | inclinenumber(ls); c = '\n'; goto only_save; | 300 | case EOZ: continue; /* will raise an error next loop */ |
367 | case '\\': case '\"': case '\'': | ||
368 | c = ls->current; goto read_save; | ||
369 | case EOZ: goto no_save; /* will raise an error next loop */ | ||
370 | case 'z': { /* zap following span of spaces */ | ||
371 | next(ls); /* skip the 'z' */ | ||
372 | while (lisspace(ls->current)) { | ||
373 | if (currIsNewline(ls)) inclinenumber(ls); | ||
374 | else next(ls); | ||
375 | } | ||
376 | goto no_save; | ||
377 | } | ||
378 | default: { | 301 | default: { |
379 | if (!lisdigit(ls->current)) | 302 | if (!isdigit(ls->current)) |
380 | escerror(ls, &ls->current, 1, "invalid escape sequence"); | 303 | save_and_next(ls); /* handles \\, \", \', and \? */ |
381 | /* digital escape \ddd */ | 304 | else { /* \xxx */ |
382 | c = readdecesc(ls); | 305 | int i = 0; |
383 | goto only_save; | 306 | c = 0; |
307 | do { | ||
308 | c = 10*c + (ls->current-'0'); | ||
309 | next(ls); | ||
310 | } while (++i<3 && isdigit(ls->current)); | ||
311 | if (c > UCHAR_MAX) | ||
312 | luaX_lexerror(ls, "escape sequence too large", TK_STRING); | ||
313 | save(ls, c); | ||
314 | } | ||
315 | continue; | ||
384 | } | 316 | } |
385 | } | 317 | } |
386 | read_save: next(ls); /* read next character */ | 318 | save(ls, c); |
387 | only_save: save(ls, c); /* save 'c' */ | 319 | next(ls); |
388 | no_save: break; | 320 | continue; |
389 | } | 321 | } |
390 | default: | 322 | default: |
391 | save_and_next(ls); | 323 | save_and_next(ls); |
@@ -401,41 +333,38 @@ static int llex (LexState *ls, SemInfo *seminfo) { | |||
401 | luaZ_resetbuffer(ls->buff); | 333 | luaZ_resetbuffer(ls->buff); |
402 | for (;;) { | 334 | for (;;) { |
403 | switch (ls->current) { | 335 | switch (ls->current) { |
404 | case '\n': case '\r': { /* line breaks */ | 336 | case '\n': |
337 | case '\r': { | ||
405 | inclinenumber(ls); | 338 | inclinenumber(ls); |
406 | break; | 339 | continue; |
407 | } | 340 | } |
408 | case ' ': case '\f': case '\t': case '\v': { /* spaces */ | 341 | case '-': { |
409 | next(ls); | ||
410 | break; | ||
411 | } | ||
412 | case '-': { /* '-' or '--' (comment) */ | ||
413 | next(ls); | 342 | next(ls); |
414 | if (ls->current != '-') return '-'; | 343 | if (ls->current != '-') return '-'; |
415 | /* else is a comment */ | 344 | /* else is a comment */ |
416 | next(ls); | 345 | next(ls); |
417 | if (ls->current == '[') { /* long comment? */ | 346 | if (ls->current == '[') { |
418 | int sep = skip_sep(ls); | 347 | int sep = skip_sep(ls); |
419 | luaZ_resetbuffer(ls->buff); /* `skip_sep' may dirty the buffer */ | 348 | luaZ_resetbuffer(ls->buff); /* `skip_sep' may dirty the buffer */ |
420 | if (sep >= 0) { | 349 | if (sep >= 0) { |
421 | read_long_string(ls, NULL, sep); /* skip long comment */ | 350 | read_long_string(ls, NULL, sep); /* long comment */ |
422 | luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */ | 351 | luaZ_resetbuffer(ls->buff); |
423 | break; | 352 | continue; |
424 | } | 353 | } |
425 | } | 354 | } |
426 | /* else short comment */ | 355 | /* else short comment */ |
427 | while (!currIsNewline(ls) && ls->current != EOZ) | 356 | while (!currIsNewline(ls) && ls->current != EOZ) |
428 | next(ls); /* skip until end of line (or end of file) */ | 357 | next(ls); |
429 | break; | 358 | continue; |
430 | } | 359 | } |
431 | case '[': { /* long string or simply '[' */ | 360 | case '[': { |
432 | int sep = skip_sep(ls); | 361 | int sep = skip_sep(ls); |
433 | if (sep >= 0) { | 362 | if (sep >= 0) { |
434 | read_long_string(ls, seminfo, sep); | 363 | read_long_string(ls, seminfo, sep); |
435 | return TK_STRING; | 364 | return TK_STRING; |
436 | } | 365 | } |
437 | else if (sep == -1) return '['; | 366 | else if (sep == -1) return '['; |
438 | else lexerror(ls, "invalid long string delimiter", TK_STRING); | 367 | else luaX_lexerror(ls, "invalid long string delimiter", TK_STRING); |
439 | } | 368 | } |
440 | case '=': { | 369 | case '=': { |
441 | next(ls); | 370 | next(ls); |
@@ -457,52 +386,56 @@ static int llex (LexState *ls, SemInfo *seminfo) { | |||
457 | if (ls->current != '=') return '~'; | 386 | if (ls->current != '=') return '~'; |
458 | else { next(ls); return TK_NE; } | 387 | else { next(ls); return TK_NE; } |
459 | } | 388 | } |
460 | case ':': { | 389 | case '"': |
461 | next(ls); | 390 | case '\'': { |
462 | if (ls->current != ':') return ':'; | ||
463 | else { next(ls); return TK_DBCOLON; } | ||
464 | } | ||
465 | case '"': case '\'': { /* short literal strings */ | ||
466 | read_string(ls, ls->current, seminfo); | 391 | read_string(ls, ls->current, seminfo); |
467 | return TK_STRING; | 392 | return TK_STRING; |
468 | } | 393 | } |
469 | case '.': { /* '.', '..', '...', or number */ | 394 | case '.': { |
470 | save_and_next(ls); | 395 | save_and_next(ls); |
471 | if (check_next(ls, ".")) { | 396 | if (check_next(ls, ".")) { |
472 | if (check_next(ls, ".")) | 397 | if (check_next(ls, ".")) |
473 | return TK_DOTS; /* '...' */ | 398 | return TK_DOTS; /* ... */ |
474 | else return TK_CONCAT; /* '..' */ | 399 | else return TK_CONCAT; /* .. */ |
400 | } | ||
401 | else if (!isdigit(ls->current)) return '.'; | ||
402 | else { | ||
403 | read_numeral(ls, seminfo); | ||
404 | return TK_NUMBER; | ||
475 | } | 405 | } |
476 | else if (!lisdigit(ls->current)) return '.'; | ||
477 | /* else go through */ | ||
478 | } | ||
479 | case '0': case '1': case '2': case '3': case '4': | ||
480 | case '5': case '6': case '7': case '8': case '9': { | ||
481 | read_numeral(ls, seminfo); | ||
482 | return TK_NUMBER; | ||
483 | } | 406 | } |
484 | case EOZ: { | 407 | case EOZ: { |
485 | return TK_EOS; | 408 | return TK_EOS; |
486 | } | 409 | } |
487 | default: { | 410 | default: { |
488 | if (lislalpha(ls->current)) { /* identifier or reserved word? */ | 411 | if (isspace(ls->current)) { |
412 | lua_assert(!currIsNewline(ls)); | ||
413 | next(ls); | ||
414 | continue; | ||
415 | } | ||
416 | else if (isdigit(ls->current)) { | ||
417 | read_numeral(ls, seminfo); | ||
418 | return TK_NUMBER; | ||
419 | } | ||
420 | else if (isalpha(ls->current) || ls->current == '_') { | ||
421 | /* identifier or reserved word */ | ||
489 | TString *ts; | 422 | TString *ts; |
490 | do { | 423 | do { |
491 | save_and_next(ls); | 424 | save_and_next(ls); |
492 | } while (lislalnum(ls->current)); | 425 | } while (isalnum(ls->current) || ls->current == '_'); |
493 | ts = luaX_newstring(ls, luaZ_buffer(ls->buff), | 426 | ts = luaX_newstring(ls, luaZ_buffer(ls->buff), |
494 | luaZ_bufflen(ls->buff)); | 427 | luaZ_bufflen(ls->buff)); |
495 | seminfo->ts = ts; | 428 | if (ts->tsv.reserved > 0) /* reserved word? */ |
496 | if (isreserved(ts)) /* reserved word? */ | 429 | return ts->tsv.reserved - 1 + FIRST_RESERVED; |
497 | return ts->tsv.extra - 1 + FIRST_RESERVED; | ||
498 | else { | 430 | else { |
431 | seminfo->ts = ts; | ||
499 | return TK_NAME; | 432 | return TK_NAME; |
500 | } | 433 | } |
501 | } | 434 | } |
502 | else { /* single-char tokens (+ - / ...) */ | 435 | else { |
503 | int c = ls->current; | 436 | int c = ls->current; |
504 | next(ls); | 437 | next(ls); |
505 | return c; | 438 | return c; /* single-char tokens (+ - / ...) */ |
506 | } | 439 | } |
507 | } | 440 | } |
508 | } | 441 | } |
@@ -521,9 +454,8 @@ void luaX_next (LexState *ls) { | |||
521 | } | 454 | } |
522 | 455 | ||
523 | 456 | ||
524 | int luaX_lookahead (LexState *ls) { | 457 | void luaX_lookahead (LexState *ls) { |
525 | lua_assert(ls->lookahead.token == TK_EOS); | 458 | lua_assert(ls->lookahead.token == TK_EOS); |
526 | ls->lookahead.token = llex(ls, &ls->lookahead.seminfo); | 459 | ls->lookahead.token = llex(ls, &ls->lookahead.seminfo); |
527 | return ls->lookahead.token; | ||
528 | } | 460 | } |
529 | 461 | ||