]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - liblua/llex.c
2 ** $Id: llex.c,v 2.63 2013/03/16 21:10:18 roberto Exp $
4 ** See Copyright Notice in lua.h
28 #define next(ls) (ls->current = zgetc(ls->z))
32 #define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r')
36 static const char *const luaX_tokens
[] = {
37 "and", "break", "do", "else", "elseif",
38 "end", "false", "for", "function", "goto", "if",
39 "in", "local", "nil", "not", "or", "repeat",
40 "return", "then", "true", "until", "while",
41 "..", "...", "==", ">=", "<=", "~=", "::", "<eof>",
42 "<number>", "<name>", "<string>"
46 #define save_and_next(ls) (save(ls, ls->current), next(ls))
49 static l_noret
lexerror (LexState
*ls
, const char *msg
, int token
);
52 static void save (LexState
*ls
, int c
) {
53 Mbuffer
*b
= ls
->buff
;
54 if (luaZ_bufflen(b
) + 1 > luaZ_sizebuffer(b
)) {
56 if (luaZ_sizebuffer(b
) >= MAX_SIZET
/2)
57 lexerror(ls
, "lexical element too long", 0);
58 newsize
= luaZ_sizebuffer(b
) * 2;
59 luaZ_resizebuffer(ls
->L
, b
, newsize
);
61 b
->buffer
[luaZ_bufflen(b
)++] = cast(char, c
);
65 void luaX_init (lua_State
*L
) {
67 for (i
=0; i
<NUM_RESERVED
; i
++) {
68 TString
*ts
= luaS_new(L
, luaX_tokens
[i
]);
69 luaS_fix(ts
); /* reserved words are never collected */
70 ts
->tsv
.extra
= cast_byte(i
+1); /* reserved word */
75 const char *luaX_token2str (LexState
*ls
, int token
) {
76 if (token
< FIRST_RESERVED
) { /* single-byte symbols? */
77 lua_assert(token
== cast(unsigned char, token
));
78 return (lisprint(token
)) ? luaO_pushfstring(ls
->L
, LUA_QL("%c"), token
) :
79 luaO_pushfstring(ls
->L
, "char(%d)", token
);
82 const char *s
= luaX_tokens
[token
- FIRST_RESERVED
];
83 if (token
< TK_EOS
) /* fixed format (symbols and reserved words)? */
84 return luaO_pushfstring(ls
->L
, LUA_QS
, s
);
85 else /* names, strings, and numerals */
91 static const char *txtToken (LexState
*ls
, int token
) {
97 return luaO_pushfstring(ls
->L
, LUA_QS
, luaZ_buffer(ls
->buff
));
99 return luaX_token2str(ls
, token
);
104 static l_noret
lexerror (LexState
*ls
, const char *msg
, int token
) {
105 char buff
[LUA_IDSIZE
];
106 luaO_chunkid(buff
, getstr(ls
->source
), LUA_IDSIZE
);
107 msg
= luaO_pushfstring(ls
->L
, "%s:%d: %s", buff
, ls
->linenumber
, msg
);
109 luaO_pushfstring(ls
->L
, "%s near %s", msg
, txtToken(ls
, token
));
110 luaD_throw(ls
->L
, LUA_ERRSYNTAX
);
114 l_noret
luaX_syntaxerror (LexState
*ls
, const char *msg
) {
115 lexerror(ls
, msg
, ls
->t
.token
);
120 ** creates a new string and anchors it in function's table so that
121 ** it will not be collected until the end of the function's compilation
122 ** (by that time it should be anchored in function's prototype)
124 TString
*luaX_newstring (LexState
*ls
, const char *str
, size_t l
) {
125 lua_State
*L
= ls
->L
;
126 TValue
*o
; /* entry for `str' */
127 TString
*ts
= luaS_newlstr(L
, str
, l
); /* create new string */
128 setsvalue2s(L
, L
->top
++, ts
); /* temporarily anchor it in stack */
129 o
= luaH_set(L
, ls
->fs
->h
, L
->top
- 1);
130 if (ttisnil(o
)) { /* not in use yet? (see 'addK') */
131 /* boolean value does not need GC barrier;
132 table has no metatable, so it does not need to invalidate cache */
133 setbvalue(o
, 1); /* t[string] = true */
136 L
->top
--; /* remove string from stack */
142 ** increment line number and skips newline sequence (any of
143 ** \n, \r, \n\r, or \r\n)
145 static void inclinenumber (LexState
*ls
) {
146 int old
= ls
->current
;
147 lua_assert(currIsNewline(ls
));
148 next(ls
); /* skip `\n' or `\r' */
149 if (currIsNewline(ls
) && ls
->current
!= old
)
150 next(ls
); /* skip `\n\r' or `\r\n' */
151 if (++ls
->linenumber
>= MAX_INT
)
152 luaX_syntaxerror(ls
, "chunk has too many lines");
156 void luaX_setinput (lua_State
*L
, LexState
*ls
, ZIO
*z
, TString
*source
,
160 ls
->current
= firstchar
;
161 ls
->lookahead
.token
= TK_EOS
; /* no look-ahead token */
167 ls
->envn
= luaS_new(L
, LUA_ENV
); /* create env name */
168 luaS_fix(ls
->envn
); /* never collect this name */
169 luaZ_resizebuffer(ls
->L
, ls
->buff
, LUA_MINBUFFER
); /* initialize buffer */
175 ** =======================================================
177 ** =======================================================
182 static int check_next (LexState
*ls
, const char *set
) {
183 if (ls
->current
== '\0' || !strchr(set
, ls
->current
))
191 ** change all characters 'from' in buffer to 'to'
193 static void buffreplace (LexState
*ls
, char from
, char to
) {
194 size_t n
= luaZ_bufflen(ls
->buff
);
195 char *p
= luaZ_buffer(ls
->buff
);
197 if (p
[n
] == from
) p
[n
] = to
;
202 #define getlocaldecpoint() '.'
203 #elif !defined(getlocaledecpoint)
204 #define getlocaledecpoint() (localeconv()->decimal_point[0])
208 #define buff2d(b,e) luaO_str2d(luaZ_buffer(b), luaZ_bufflen(b) - 1, e)
211 ** in case of format error, try to change decimal point separator to
212 ** the one defined in the current locale and check again
214 static void trydecpoint (LexState
*ls
, SemInfo
*seminfo
) {
215 char old
= ls
->decpoint
;
216 ls
->decpoint
= getlocaledecpoint();
217 buffreplace(ls
, old
, ls
->decpoint
); /* try new decimal separator */
218 if (!buff2d(ls
->buff
, &seminfo
->r
)) {
219 /* format error with correct decimal point: no more options */
220 buffreplace(ls
, ls
->decpoint
, '.'); /* undo change (for error message) */
221 lexerror(ls
, "malformed number", TK_NUMBER
);
228 ** this function is quite liberal in what it accepts, as 'luaO_str2d'
229 ** will reject ill-formed numerals.
231 static void read_numeral (LexState
*ls
, SemInfo
*seminfo
) {
232 const char *expo
= "Ee";
233 int first
= ls
->current
;
234 lua_assert(lisdigit(ls
->current
));
236 if (first
== '0' && check_next(ls
, "Xx")) /* hexadecimal? */
239 if (check_next(ls
, expo
)) /* exponent part? */
240 check_next(ls
, "+-"); /* optional exponent sign */
241 if (lisxdigit(ls
->current
) || ls
->current
== '.')
246 buffreplace(ls
, '.', ls
->decpoint
); /* follow locale for decimal point */
247 if (!buff2d(ls
->buff
, &seminfo
->r
)) /* format error? */
248 trydecpoint(ls
, seminfo
); /* try to update decimal point separator */
253 ** skip a sequence '[=*[' or ']=*]' and return its number of '='s or
254 ** -1 if sequence is malformed
256 static int skip_sep (LexState
*ls
) {
259 lua_assert(s
== '[' || s
== ']');
261 while (ls
->current
== '=') {
265 return (ls
->current
== s
) ? count
: (-count
) - 1;
269 static void read_long_string (LexState
*ls
, SemInfo
*seminfo
, int sep
) {
270 save_and_next(ls
); /* skip 2nd `[' */
271 if (currIsNewline(ls
)) /* string starts with a newline? */
272 inclinenumber(ls
); /* skip it */
274 switch (ls
->current
) {
276 lexerror(ls
, (seminfo
) ? "unfinished long string" :
277 "unfinished long comment", TK_EOS
);
278 break; /* to avoid warnings */
280 if (skip_sep(ls
) == sep
) {
281 save_and_next(ls
); /* skip 2nd `]' */
286 case '\n': case '\r': {
289 if (!seminfo
) luaZ_resetbuffer(ls
->buff
); /* avoid wasting space */
293 if (seminfo
) save_and_next(ls
);
299 seminfo
->ts
= luaX_newstring(ls
, luaZ_buffer(ls
->buff
) + (2 + sep
),
300 luaZ_bufflen(ls
->buff
) - 2*(2 + sep
));
304 static void escerror (LexState
*ls
, int *c
, int n
, const char *msg
) {
306 luaZ_resetbuffer(ls
->buff
); /* prepare error message */
308 for (i
= 0; i
< n
&& c
[i
] != EOZ
; i
++)
310 lexerror(ls
, msg
, TK_STRING
);
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 */
320 if (!lisxdigit(c
[i
]))
321 escerror(ls
, c
, i
+ 1, "hexadecimal digit expected");
322 r
= (r
<< 4) + luaO_hexavalue(c
[i
]);
328 static int readdecesc (LexState
*ls
) {
330 int r
= 0; /* result accumulator */
331 for (i
= 0; i
< 3 && lisdigit(ls
->current
); i
++) { /* read up to 3 digits */
333 r
= 10*r
+ c
[i
] - '0';
337 escerror(ls
, c
, i
, "decimal escape too large");
342 static void read_string (LexState
*ls
, int del
, SemInfo
*seminfo
) {
343 save_and_next(ls
); /* keep delimiter (for error messages) */
344 while (ls
->current
!= del
) {
345 switch (ls
->current
) {
347 lexerror(ls
, "unfinished string", TK_EOS
);
348 break; /* to avoid warnings */
351 lexerror(ls
, "unfinished string", TK_STRING
);
352 break; /* to avoid warnings */
353 case '\\': { /* escape sequences */
354 int c
; /* final character to be saved */
355 next(ls
); /* do not save the `\' */
356 switch (ls
->current
) {
357 case 'a': c
= '\a'; goto read_save
;
358 case 'b': c
= '\b'; goto read_save
;
359 case 'f': c
= '\f'; goto read_save
;
360 case 'n': c
= '\n'; goto read_save
;
361 case 'r': c
= '\r'; goto read_save
;
362 case 't': c
= '\t'; goto read_save
;
363 case 'v': c
= '\v'; goto read_save
;
364 case 'x': c
= readhexaesc(ls
); goto read_save
;
365 case '\n': case '\r':
366 inclinenumber(ls
); c
= '\n'; goto only_save
;
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
);
379 if (!lisdigit(ls
->current
))
380 escerror(ls
, &ls
->current
, 1, "invalid escape sequence");
381 /* digital escape \ddd */
386 read_save
: next(ls
); /* read next character */
387 only_save
: save(ls
, c
); /* save 'c' */
394 save_and_next(ls
); /* skip delimiter */
395 seminfo
->ts
= luaX_newstring(ls
, luaZ_buffer(ls
->buff
) + 1,
396 luaZ_bufflen(ls
->buff
) - 2);
400 static int llex (LexState
*ls
, SemInfo
*seminfo
) {
401 luaZ_resetbuffer(ls
->buff
);
403 switch (ls
->current
) {
404 case '\n': case '\r': { /* line breaks */
408 case ' ': case '\f': case '\t': case '\v': { /* spaces */
412 case '-': { /* '-' or '--' (comment) */
414 if (ls
->current
!= '-') return '-';
415 /* else is a comment */
417 if (ls
->current
== '[') { /* long comment? */
418 int sep
= skip_sep(ls
);
419 luaZ_resetbuffer(ls
->buff
); /* `skip_sep' may dirty the buffer */
421 read_long_string(ls
, NULL
, sep
); /* skip long comment */
422 luaZ_resetbuffer(ls
->buff
); /* previous call may dirty the buff. */
426 /* else short comment */
427 while (!currIsNewline(ls
) && ls
->current
!= EOZ
)
428 next(ls
); /* skip until end of line (or end of file) */
431 case '[': { /* long string or simply '[' */
432 int sep
= skip_sep(ls
);
434 read_long_string(ls
, seminfo
, sep
);
437 else if (sep
== -1) return '[';
438 else lexerror(ls
, "invalid long string delimiter", TK_STRING
);
442 if (ls
->current
!= '=') return '=';
443 else { next(ls
); return TK_EQ
; }
447 if (ls
->current
!= '=') return '<';
448 else { next(ls
); return TK_LE
; }
452 if (ls
->current
!= '=') return '>';
453 else { next(ls
); return TK_GE
; }
457 if (ls
->current
!= '=') return '~';
458 else { next(ls
); return TK_NE
; }
462 if (ls
->current
!= ':') return ':';
463 else { next(ls
); return TK_DBCOLON
; }
465 case '"': case '\'': { /* short literal strings */
466 read_string(ls
, ls
->current
, seminfo
);
469 case '.': { /* '.', '..', '...', or number */
471 if (check_next(ls
, ".")) {
472 if (check_next(ls
, "."))
473 return TK_DOTS
; /* '...' */
474 else return TK_CONCAT
; /* '..' */
476 else if (!lisdigit(ls
->current
)) return '.';
477 /* else go through */
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
);
488 if (lislalpha(ls
->current
)) { /* identifier or reserved word? */
492 } while (lislalnum(ls
->current
));
493 ts
= luaX_newstring(ls
, luaZ_buffer(ls
->buff
),
494 luaZ_bufflen(ls
->buff
));
496 if (isreserved(ts
)) /* reserved word? */
497 return ts
->tsv
.extra
- 1 + FIRST_RESERVED
;
502 else { /* single-char tokens (+ - / ...) */
513 void luaX_next (LexState
*ls
) {
514 ls
->lastline
= ls
->linenumber
;
515 if (ls
->lookahead
.token
!= TK_EOS
) { /* is there a look-ahead token? */
516 ls
->t
= ls
->lookahead
; /* use this one */
517 ls
->lookahead
.token
= TK_EOS
; /* and discharge it */
520 ls
->t
.token
= llex(ls
, &ls
->t
.seminfo
); /* read next token */
524 int luaX_lookahead (LexState
*ls
) {
525 lua_assert(ls
->lookahead
.token
== TK_EOS
);
526 ls
->lookahead
.token
= llex(ls
, &ls
->lookahead
.seminfo
);
527 return ls
->lookahead
.token
;