1 char *rcs_lex
= "$Id: lex.c,v 3.5 1997/06/16 16:50:22 roberto Exp $";
18 static int current
; /* look ahead character */
22 #define next() (current = zgetc(lex_z))
23 #define save(x) (yytext[tokensize++] = (x))
24 #define save_and_next() (save(current), next())
29 /* "ifstate" keeps the state of each nested $if the lexical is dealing with. */
32 int elsepart
; /* true if its in the $else part */
33 int condition
; /* true if $if condition is true */
34 int skip
; /* true if part must be skiped */
37 static int iflevel
; /* level of nested $if's */
40 void lua_setinput (ZIO
*z
)
46 ifstate
[0].elsepart
= 1; /* to avoid a free $else */
51 static void luaI_auxsyntaxerror (char *s
)
53 luaL_verror("%s;\n> at line %d in file %s",
54 s
, lua_linenumber
, lua_parsedfile
);
57 static void luaI_auxsynterrbf (char *s
, char *token
)
61 luaL_verror("%s;\n> last token read: \"%s\" at line %d in file %s",
62 s
, token
, lua_linenumber
, lua_parsedfile
);
65 void luaI_syntaxerror (char *s
)
67 luaI_auxsynterrbf(s
, luaI_buffer(1));
81 {"function", FUNCTION
},
94 #define RESERVEDSIZE (sizeof(reserved)/sizeof(reserved[0]))
97 void luaI_addReserved (void)
100 for (i
=0; i
<RESERVEDSIZE
; i
++)
102 TaggedString
*ts
= lua_createstring(reserved
[i
].name
);
103 ts
->marked
= reserved
[i
].token
; /* reserved word (always > 255) */
112 #define PRAGMASIZE 20
114 static void skipspace (void)
116 while (current
== ' ' || current
== '\t') next();
120 static int checkcond (char *buff
)
122 static char *opts
[] = {"nil", "1"};
123 int i
= luaI_findstring(buff
, opts
);
124 if (i
>= 0) return i
;
125 else if (isalpha((unsigned char)buff
[0]) || buff
[0] == '_')
126 return luaI_globaldefined(buff
);
128 luaI_auxsynterrbf("invalid $if condition", buff
);
129 return 0; /* to avoid warnings */
134 static void readname (char *buff
)
138 while (isalnum((unsigned char)current
) || current
== '_') {
139 if (i
>= PRAGMASIZE
) {
140 buff
[PRAGMASIZE
] = 0;
141 luaI_auxsynterrbf("pragma too long", buff
);
150 static void inclinenumber (void);
153 static void ifskip (void)
155 while (ifstate
[iflevel
].skip
) {
158 else if (current
== EOZ
)
159 luaI_auxsyntaxerror("input ends inside a $if");
165 static void inclinenumber (void)
167 static char *pragmas
[] =
168 {"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL
};
169 next(); /* skip '\n' */
171 if (current
== '$') { /* is a pragma? */
172 char buff
[PRAGMASIZE
+1];
174 int skip
= ifstate
[iflevel
].skip
;
177 switch (luaI_findstring(buff
, pragmas
)) {
179 if (!skip
) lua_debug
= 1;
181 case 1: /* nodebug */
182 if (!skip
) lua_debug
= 0;
184 case 2: /* endinput */
187 iflevel
= 0; /* to allow $endinput inside a $if */
192 luaI_auxsyntaxerror("unmatched $endif");
198 if (iflevel
== MAX_IFS
-1)
199 luaI_auxsyntaxerror("too many nested `$ifs'");
202 ifstate
[iflevel
].elsepart
= 0;
203 ifstate
[iflevel
].condition
= checkcond(buff
) ? !ifnot
: ifnot
;
204 ifstate
[iflevel
].skip
= skip
|| !ifstate
[iflevel
].condition
;
207 if (ifstate
[iflevel
].elsepart
)
208 luaI_auxsyntaxerror("unmatched $else");
209 ifstate
[iflevel
].elsepart
= 1;
210 ifstate
[iflevel
].skip
=
211 ifstate
[iflevel
-1].skip
|| ifstate
[iflevel
].condition
;
214 luaI_auxsynterrbf("invalid pragma", buff
);
217 if (current
== '\n') /* pragma must end with a '\n' ... */
219 else if (current
!= EOZ
) /* or eof */
220 luaI_auxsyntaxerror("invalid pragma format");
225 static int read_long_string (char *yytext
, int buffsize
)
228 int tokensize
= 2; /* '[[' already stored */
231 if (buffsize
-tokensize
<= 2) /* may read more than 1 char in one cicle */
232 yytext
= luaI_buffer(buffsize
*= 2);
250 if (cont
== 0) goto endloop
;
263 save_and_next(); /* pass the second ']' */
264 yytext
[tokensize
-2] = 0; /* erases ']]' */
265 luaY_lval
.vWord
= luaI_findconstantbyname(yytext
+2);
266 yytext
[tokensize
-2] = ']'; /* restores ']]' */
273 static int linelasttoken
= 0;
275 int buffsize
= MINBUFF
;
276 char *yytext
= luaI_buffer(buffsize
);
277 yytext
[1] = yytext
[2] = yytext
[3] = 0;
279 luaI_codedebugline(linelasttoken
);
280 linelasttoken
= lua_linenumber
;
288 linelasttoken
= lua_linenumber
;
291 case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */
297 if (current
!= '-') return '-';
298 do { next(); } while (current
!= '\n' && current
!= EOZ
);
303 if (current
!= '[') return '[';
306 save_and_next(); /* pass the second '[' */
307 return read_long_string(yytext
, buffsize
);
312 if (current
!= '=') return '=';
313 else { save_and_next(); return EQ
; }
317 if (current
!= '=') return '<';
318 else { save_and_next(); return LE
; }
322 if (current
!= '=') return '>';
323 else { save_and_next(); return GE
; }
327 if (current
!= '=') return '~';
328 else { save_and_next(); return NE
; }
335 while (current
!= del
)
337 if (buffsize
-tokensize
<= 2) /* may read more than 1 char in one cicle */
338 yytext
= luaI_buffer(buffsize
*= 2);
346 next(); /* do not save the '\' */
349 case 'n': save('\n'); next(); break;
350 case 't': save('\t'); next(); break;
351 case 'r': save('\r'); next(); break;
352 case '\n': save('\n'); inclinenumber(); break;
353 default : save_and_next(); break;
360 next(); /* skip delimiter */
362 luaY_lval
.vWord
= luaI_findconstantbyname(yytext
+1);
364 save(del
); save(0); /* restore delimiter */
368 case 'a': case 'b': case 'c': case 'd': case 'e':
369 case 'f': case 'g': case 'h': case 'i': case 'j':
370 case 'k': case 'l': case 'm': case 'n': case 'o':
371 case 'p': case 'q': case 'r': case 's': case 't':
372 case 'u': case 'v': case 'w': case 'x': case 'y':
374 case 'A': case 'B': case 'C': case 'D': case 'E':
375 case 'F': case 'G': case 'H': case 'I': case 'J':
376 case 'K': case 'L': case 'M': case 'N': case 'O':
377 case 'P': case 'Q': case 'R': case 'S': case 'T':
378 case 'U': case 'V': case 'W': case 'X': case 'Y':
385 } while (isalnum((unsigned char)current
) || current
== '_');
387 ts
= lua_createstring(yytext
);
389 return ts
->marked
; /* reserved word */
390 luaY_lval
.pTStr
= ts
;
391 ts
->marked
= 2; /* avoid GC */
403 return DOTS
; /* ... */
405 else return CONC
; /* .. */
407 else if (!isdigit((unsigned char)current
)) return '.';
408 /* current is a digit: goes through to number */
412 case '0': case '1': case '2': case '3': case '4':
413 case '5': case '6': case '7': case '8': case '9':
416 a
=10.0*a
+(current
-'0');
418 } while (isdigit((unsigned char)current
));
419 if (current
== '.') {
423 "ambiguous syntax (decimal point x string concatenation)");
427 while (isdigit((unsigned char)current
))
433 if (current
== 'e' || current
== 'E')
440 if (current
== '+' || current
== '-') save_and_next();
441 if (!isdigit((unsigned char)current
)) {
442 save(0); return WRONGTOKEN
; }
444 e
=10.0*e
+(current
-'0');
446 } while (isdigit((unsigned char)current
));
447 for (ea
=neg
?0.1:10.0; e
>0; e
>>=1)
453 luaY_lval
.vFloat
= a
;
461 luaI_syntaxerror("missing $endif");