2 ** $Id: llex.c,v 2.20.1.1 2007/12/27 13:02:25 roberto Exp $
4 ** See Copyright Notice in lua.h
28 #define _HEX2INT_(v) ( (v) <= '9' ? (v)-'0' : tolower(v)-'a'+10 )
31 #define next(ls) (ls->current = zgetc(ls->z))
36 #define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r')
40 const char *const luaX_tokens
[] = {
41 "and", "break", "continue", "do", "else", "elseif",
42 "end", "false", "for", "function", "if",
43 "in", "local", "nil", "not", "or", "repeat",
44 "return", "then", "true", "until", "while",
45 "..", "...", "==", ">=", "<=", "~=",
46 "<number>", "<name>", "<string>", "<eof>",
52 #define save_and_next(ls) (save(ls, ls->current), next(ls))
55 static void save (LexState
*ls
, int c
) {
56 Mbuffer
*b
= ls
->buff
;
57 if (b
->n
+ 1 > b
->buffsize
) {
59 if (b
->buffsize
>= MAX_SIZET
/2)
60 luaX_lexerror(ls
, "lexical element too long", 0);
61 newsize
= b
->buffsize
* 2;
62 luaZ_resizebuffer(ls
->L
, b
, newsize
);
64 b
->buffer
[b
->n
++] = cast(char, c
);
68 void luaX_init (lua_State
*L
) {
70 for (i
=0; i
<NUM_RESERVED
; i
++) {
71 TString
*ts
= luaS_new(L
, luaX_tokens
[i
]);
72 luaS_fix(ts
); /* reserved words are never collected */
73 lua_assert(strlen(luaX_tokens
[i
])+1 <= TOKEN_LEN
);
74 ts
->tsv
.reserved
= cast_byte(i
+1); /* reserved word */
82 const char *luaX_token2str (LexState
*ls
, int token
) {
83 if (token
< FIRST_RESERVED
) {
84 lua_assert(token
== cast(unsigned char, token
));
85 return (iscntrl(token
)) ? luaO_pushfstring(ls
->L
, "char(%d)", token
) :
86 luaO_pushfstring(ls
->L
, "%c", token
);
89 return luaX_tokens
[token
-FIRST_RESERVED
];
93 static const char *txtToken (LexState
*ls
, int token
) {
99 return luaZ_buffer(ls
->buff
);
101 return luaX_token2str(ls
, token
);
106 void luaX_lexerror (LexState
*ls
, const char *msg
, int token
) {
108 luaO_chunkid(buff
, getstr(ls
->source
), MAXSRC
);
109 msg
= luaO_pushfstring(ls
->L
, "%s:%d: %s", buff
, ls
->linenumber
, msg
);
111 luaO_pushfstring(ls
->L
, "%s near " LUA_QS
, msg
, txtToken(ls
, token
));
112 luaD_throw(ls
->L
, LUA_ERRSYNTAX
);
116 void luaX_syntaxerror (LexState
*ls
, const char *msg
) {
117 luaX_lexerror(ls
, msg
, ls
->t
.token
);
121 TString
*luaX_newstring (LexState
*ls
, const char *str
, size_t l
) {
122 lua_State
*L
= ls
->L
;
123 TString
*ts
= luaS_newlstr(L
, str
, l
);
124 TValue
*o
= luaH_setstr(L
, ls
->fs
->h
, ts
); /* entry for `str' */
126 setbvalue(o
, 1); /* make sure `str' will not be collected */
131 static void inclinenumber (LexState
*ls
) {
132 int old
= ls
->current
;
133 lua_assert(currIsNewline(ls
));
134 next(ls
); /* skip `\n' or `\r' */
135 if (currIsNewline(ls
) && ls
->current
!= old
)
136 next(ls
); /* skip `\n\r' or `\r\n' */
137 if (++ls
->linenumber
>= MAX_INT
)
138 luaX_syntaxerror(ls
, "chunk has too many lines");
142 void luaX_setinput(lua_State
*L
, LexState
*ls
, ZIO
*z
, TString
*source
)
146 ls
->lookahead
.token
= TK_EOS
; /* no look-ahead token */
153 luaZ_resizebuffer(ls
->L
, ls
->buff
, LUA_MINBUFFER
); /* initialize buffer */
154 next(ls
); /* read first char */
160 ** =======================================================
162 ** =======================================================
167 static int check_next (LexState
*ls
, const char *set
) {
168 if (!strchr(set
, ls
->current
))
175 static void buffreplace (LexState
*ls
, char from
, char to
) {
176 size_t n
= luaZ_bufflen(ls
->buff
);
177 char *p
= luaZ_buffer(ls
->buff
);
179 if (p
[n
] == from
) p
[n
] = to
;
183 static void trydecpoint (LexState
*ls
, SemInfo
*seminfo
) {
184 /* format error: try to update decimal point separator */
185 struct lconv
*cv
= localeconv();
186 char old
= ls
->decpoint
;
187 ls
->decpoint
= (cv
? cv
->decimal_point
[0] : '.');
188 buffreplace(ls
, old
, ls
->decpoint
); /* try updated decimal separator */
189 if (!luaO_str2d(luaZ_buffer(ls
->buff
), &seminfo
->r
)) {
190 /* format error with correct decimal point: no more options */
191 buffreplace(ls
, ls
->decpoint
, '.'); /* undo change (for error message) */
192 luaX_lexerror(ls
, "malformed number", TK_NUMBER
);
198 static void read_numeral (LexState
*ls
, SemInfo
*seminfo
) {
199 lua_assert(isdigit(ls
->current
));
202 } while (isdigit(ls
->current
) || ls
->current
== '.');
203 if (check_next(ls
, "Ee")) /* `E'? */
204 check_next(ls
, "+-"); /* optional exponent sign */
205 while (isalnum(ls
->current
) || ls
->current
== '_')
208 buffreplace(ls
, '.', ls
->decpoint
); /* follow locale for decimal point */
209 if (!luaO_str2d(luaZ_buffer(ls
->buff
), &seminfo
->r
)) /* format error? */
210 trydecpoint(ls
, seminfo
); /* try to update decimal point separator */
214 static int skip_sep (LexState
*ls
) {
217 lua_assert(s
== '[' || s
== ']');
219 while (ls
->current
== '=') {
223 return (ls
->current
== s
) ? count
: (-count
) - 1;
227 static void read_long_string (LexState
*ls
, SemInfo
*seminfo
, int sep
) {
229 (void)(cont
); /* avoid warnings when `cont' is not used */
230 save_and_next(ls
); /* skip 2nd `[' */
231 if (currIsNewline(ls
)) /* string starts with a newline? */
232 inclinenumber(ls
); /* skip it */
234 switch (ls
->current
) {
236 luaX_lexerror(ls
, (seminfo
) ? "unfinished long string" :
237 "unfinished long comment", TK_EOS
);
238 break; /* to avoid warnings */
239 #if defined(LUA_COMPAT_LSTR)
241 if (skip_sep(ls
) == sep
) {
242 save_and_next(ls
); /* skip 2nd `[' */
244 #if LUA_COMPAT_LSTR == 1
246 luaX_lexerror(ls
, "nesting of [[...]] is deprecated", '[');
253 if (skip_sep(ls
) == sep
) {
254 save_and_next(ls
); /* skip 2nd `]' */
255 #if defined(LUA_COMPAT_LSTR) && LUA_COMPAT_LSTR == 2
257 if (sep
== 0 && cont
>= 0) break;
267 if (!seminfo
) luaZ_resetbuffer(ls
->buff
); /* avoid wasting space */
271 if (seminfo
) save_and_next(ls
);
277 seminfo
->ts
= luaX_newstring(ls
, luaZ_buffer(ls
->buff
) + (2 + sep
),
278 luaZ_bufflen(ls
->buff
) - 2*(2 + sep
));
282 static void read_string (LexState
*ls
, int del
, SemInfo
*seminfo
) {
284 while (ls
->current
!= del
) {
285 switch (ls
->current
) {
287 luaX_lexerror(ls
, "unfinished string", TK_EOS
);
288 continue; /* to avoid warnings */
291 luaX_lexerror(ls
, "unfinished string", TK_STRING
);
292 continue; /* to avoid warnings */
295 next(ls
); /* do not save the `\' */
296 switch (ls
->current
) {
297 case 'a': c
= '\a'; break;
298 case 'b': c
= '\b'; break;
299 case 'f': c
= '\f'; break;
300 case 'n': c
= '\n'; break;
301 case 'r': c
= '\r'; break;
302 case 't': c
= '\t'; break;
303 case 'v': c
= '\v'; break;
304 case '\n': /* go through */
305 case '\r': save(ls
, '\n'); inclinenumber(ls
); continue;
306 case EOZ
: continue; /* will raise an error next loop */
309 save_and_next(ls
); /* skip $ */
310 seminfo
->ts
= luaX_newstring(ls
, luaZ_buffer(ls
->buff
) + 1,
311 luaZ_bufflen(ls
->buff
) - 2);
312 ls
->refstr
++; /* expect '\' anytime soon */
313 lua_assert(ls
->lookahead
.token
== TK_EOS
);
314 ls
->lookahead
.token
= TK_CONCAT
;
318 if (!isdigit(ls
->current
))
319 save_and_next(ls
); /* handles \\, \", \', and \? */
324 c
= 10*c
+ (ls
->current
-'0');
326 } while (++i
<3 && isdigit(ls
->current
));
328 luaX_lexerror(ls
, "escape sequence too large", TK_STRING
);
333 // "\x00".."\xFFFF": raw hex literals
338 next(ls
); // skip 'x'
340 for( i
=0; i
<5 && isxdigit(ls
->current
); i
++ ) {
341 c
= (c
<<4) | _HEX2INT_(ls
->current
);
346 case 4: save( ls
, (c
>>8) & 0xff ); // pass-through..
347 case 2: save( ls
, c
&0xff );
351 luaX_lexerror(ls
, "escape sequence wrong size (2 or 4 digits)", TK_STRING
);
356 // "\u0000".."\x10FFFF": UTF-8 encoded Unicode
358 // Note that although codes are entered like this (must have min. four digit,
359 // just to tease you!) the actual outcome in the string will vary between
360 // 1..4 bytes, depending on the value range.
365 next(ls
); // skip 'x'
367 for ( i
=0; i
<7 && isxdigit(ls
->current
); i
++ ) {
368 c
= (c
<<4) | _HEX2INT_(ls
->current
);
372 if ((i
!=4) && (i
!=6)) {
373 luaX_lexerror(ls
, "escape sequence wrong size (4 or 6 digits)", TK_STRING
);
376 /* http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
377 U-00000000 - U-0000007F: 0xxxxxxx
378 U-00000080 - U-000007FF: 110xxxxx 10xxxxxx
379 U-00000800 - U-0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx
380 U-00010000 - U-001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
381 (U-00200000 - U-03FFFFFF: 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx)
382 (U-04000000 - U-7FFFFFFF: 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx)
384 if ( c
<= 0x007f ) { // 1-byte
386 } else if ( c
<= 0x000007ff ) { // 2-byte
387 save( ls
, 0xc0 | ((c
>>6)&0x1f) );
388 save( ls
, 0x80 | (c
&0x3f) );
389 } else if ( c
<= 0x0000ffff ) { // 3-byte
390 save( ls
, 0xe0 | ((c
>>12)&0x0f) );
391 save( ls
, 0x80 | ((c
>>6)&0x3f) );
392 save( ls
, 0x80 | (c
&0x3f) );
393 } else if ( c
<= 0x001fffff ) { // 4-byte
394 save( ls
, 0xf0 | ((c
>>18)&0x07) );
395 save( ls
, 0x80 | ((c
>>12)&0x3f) );
396 save( ls
, 0x80 | ((c
>>6)&0x3f) );
397 save( ls
, 0x80 | (c
&0x3f) );
399 luaX_lexerror(ls
, "escape sequence too large", TK_STRING
);
412 save_and_next(ls
); /* skip delimiter */
413 seminfo
->ts
= luaX_newstring(ls
, luaZ_buffer(ls
->buff
) + 1,
414 luaZ_bufflen(ls
->buff
) - 2);
417 static void pre_lua(LexState
*ls
)
420 luaZ_resetbuffer(ls
->buff
);
421 sprintf(mstr
, "= macro at %d", ls
->linenumber
);
423 //printf("a%d\n", ls->current);
425 //printf("b%d\n", ls->current);
426 /* save line till newline */
427 while (ls
->current
!= '\r' && ls
->current
!= '\n' && ls
->current
!= EOZ
) {
430 if (ls
->current
!= EOZ
) {
435 //printf("%d\n", ls->current);
436 if (ls
->current
== EOZ
|| ls
->current
!= '#') break;
437 //printf("ja%d\n", ls->current);
439 //printf("jb%d\n", ls->current);
440 switch (ls
->current
) {
444 while (!currIsNewline(ls
) && ls
->current
!= EOZ
)
447 if (ls
->current
== '#')
449 //printf(" x%d\n", ls->current);
456 luaX_lexerror(ls
, "invalid pragma", ls
->current
);
460 // printf("compiling %s %d\n",luaZ_buffer(ls->buff),luaZ_bufflen(ls->buff));
461 luaL_loadbuffer(ls
->L
, luaZ_buffer(ls
->buff
), luaZ_bufflen(ls
->buff
), mstr
);
462 luaZ_resetbuffer(ls
->buff
);
463 /* call the buffer, expect one return value */
464 lua_call(ls
->L
, 0, 1);
465 if (!lua_isnil(ls
->L
, -1)) {
467 const char *s
= luaL_checklstring(ls
->L
, -1, &l
);
468 //printf("prepending %s %d %d\n", s, strlen(s), l);
469 luaZ_prepend(ls
->z
, s
, l
);
475 static int llex (LexState
*ls
, SemInfo
*seminfo
) {
476 luaZ_resetbuffer(ls
->buff
);
478 switch (ls
->current
) {
482 /* "# " is comment, ## is lua preprocessing */
483 if (ls
->current
== '#') {
485 switch (ls
->current
) {
491 while (!currIsNewline(ls
) && ls
->current
!= EOZ
)
495 luaX_lexerror(ls
, "invalid pragma", '#');
502 if (ls
->current
!= '-') return '-';
503 /* else is a comment */
505 if (ls
->current
== '[') {
506 int sep
= skip_sep(ls
);
507 luaZ_resetbuffer(ls
->buff
); /* `skip_sep' may dirty the buffer */
509 read_long_string(ls
, NULL
, sep
); /* long comment */
510 luaZ_resetbuffer(ls
->buff
);
514 /* else short comment */
515 while (!currIsNewline(ls
) && ls
->current
!= EOZ
)
520 int sep
= skip_sep(ls
);
522 read_long_string(ls
, seminfo
, sep
);
525 else if (sep
== -1) return '[';
526 else luaX_lexerror(ls
, "invalid long string delimiter", TK_STRING
);
530 if (ls
->current
!= '=') return '=';
531 else { next(ls
); return TK_EQ
; }
535 if (ls
->current
== '<') { next(ls
); return TK_SHL
; }
536 if (ls
->current
!= '=') return '<';
537 else { next(ls
); return TK_LE
; }
541 if (ls
->current
== '>') { next(ls
); return TK_SHR
; }
542 if (ls
->current
!= '=') return '>';
543 else { next(ls
); return TK_GE
; }
547 if (ls
->current
== '^') { next(ls
); return TK_XOR
; }
552 if (ls
->current
!= '=') return '~';
553 else { next(ls
); return TK_NE
; }
557 if (ls
->current
!= '=') return '~';
558 else { next(ls
); return TK_NE
; }
562 read_string(ls
, ls
->current
, seminfo
);
567 if (check_next(ls
, ".")) {
568 if (check_next(ls
, "."))
569 return TK_DOTS
; /* ... */
570 else return TK_CONCAT
; /* .. */
572 else if (!isdigit(ls
->current
)) return '.';
574 read_numeral(ls
, seminfo
);
581 while (isdigit(ls
->current
)) {
582 i
= 10*i
+ (ls
->current
-'0');
591 case '\\': if (ls
->refstr
) {
593 ls
->current
= '"'; /* whacky! */
597 if (isspace(ls
->current
)) {
598 lua_assert(!currIsNewline(ls
));
602 else if (isdigit(ls
->current
)) {
603 read_numeral(ls
, seminfo
);
606 else if (isalpha(ls
->current
) || ls
->current
== '_') {
607 /* identifier or reserved word */
611 } while (isalnum(ls
->current
) || ls
->current
== '_');
612 ts
= luaX_newstring(ls
, luaZ_buffer(ls
->buff
),
613 luaZ_bufflen(ls
->buff
));
614 if (ts
->tsv
.reserved
> 0) /* reserved word? */
615 return ts
->tsv
.reserved
- 1 + FIRST_RESERVED
;
624 return c
; /* single-char tokens (+ - / ...) */
633 void luaX_next (LexState
*ls
) {
634 ls
->lastline
= ls
->linenumber
;
635 if (ls
->lookahead
.token
!= TK_EOS
) { /* is there a look-ahead token? */
636 ls
->t
= ls
->lookahead
; /* use this one */
637 ls
->lookahead
.token
= TK_EOS
; /* and discharge it */
640 ls
->t
.token
= llex(ls
, &ls
->t
.seminfo
); /* read next token */
644 void luaX_lookahead (LexState
*ls
) {
645 lua_assert(ls
->lookahead
.token
== TK_EOS
);
646 ls
->lookahead
.token
= llex(ls
, &ls
->lookahead
.seminfo
);