diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-04-07 11:48:53 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-04-07 11:48:53 -0300 |
commit | c9e3d321828dd095d46e295873bc991f0c9ed3d7 (patch) | |
tree | 1efce0077f163b1bc0b733327e582e73fe8ac8a3 /lex.c | |
parent | 00050b8a6b11c3ac2032ca147d236743b90f5168 (diff) | |
download | lua-c9e3d321828dd095d46e295873bc991f0c9ed3d7.tar.gz lua-c9e3d321828dd095d46e295873bc991f0c9ed3d7.tar.bz2 lua-c9e3d321828dd095d46e295873bc991f0c9ed3d7.zip |
first implementation of "$if";
new function "findstring" (useful in good places)
Diffstat (limited to 'lex.c')
-rw-r--r-- | lex.c | 87 |
1 files changed, 66 insertions, 21 deletions
@@ -1,4 +1,4 @@ | |||
1 | char *rcs_lex = "$Id: lex.c,v 2.44 1997/03/31 14:17:09 roberto Exp roberto $"; | 1 | char *rcs_lex = "$Id: lex.c,v 2.45 1997/04/01 21:23:20 roberto Exp roberto $"; |
2 | 2 | ||
3 | 3 | ||
4 | #include <ctype.h> | 4 | #include <ctype.h> |
@@ -22,27 +22,31 @@ char *rcs_lex = "$Id: lex.c,v 2.44 1997/03/31 14:17:09 roberto Exp roberto $"; | |||
22 | 22 | ||
23 | static int current; /* look ahead character */ | 23 | static int current; /* look ahead character */ |
24 | static Input input; /* input function */ | 24 | static Input input; /* input function */ |
25 | static int iflevel; /* level of nested $if's */ | ||
25 | 26 | ||
26 | 27 | ||
27 | void lua_setinput (Input fn) | 28 | void lua_setinput (Input fn) |
28 | { | 29 | { |
29 | current = '\n'; | 30 | current = '\n'; |
30 | lua_linenumber = 0; | 31 | lua_linenumber = 0; |
32 | iflevel = 0; | ||
31 | input = fn; | 33 | input = fn; |
32 | } | 34 | } |
33 | 35 | ||
34 | static void luaI_auxsyntaxerror (char *s, char *token) | 36 | static void luaI_auxsyntaxerror (char *s, char *token) |
35 | { | 37 | { |
38 | if (token == NULL) | ||
39 | luaL_verror("%s;\n> at line %d in file %s", | ||
40 | s, lua_linenumber, lua_parsedfile); | ||
41 | if (token[0] == 0) | ||
42 | token = "<eof>"; | ||
36 | luaL_verror("%s;\n> last token read: \"%s\" at line %d in file %s", | 43 | luaL_verror("%s;\n> last token read: \"%s\" at line %d in file %s", |
37 | s, token, lua_linenumber, lua_parsedfile); | 44 | s, token, lua_linenumber, lua_parsedfile); |
38 | } | 45 | } |
39 | 46 | ||
40 | void luaI_syntaxerror (char *s) | 47 | void luaI_syntaxerror (char *s) |
41 | { | 48 | { |
42 | char *token = luaI_buffer(1); | 49 | luaI_auxsyntaxerror(s, luaI_buffer(1)); |
43 | if (token[0] == 0) | ||
44 | token = "<eof>"; | ||
45 | luaI_auxsyntaxerror(s, token); | ||
46 | } | 50 | } |
47 | 51 | ||
48 | 52 | ||
@@ -82,24 +86,63 @@ void luaI_addReserved (void) | |||
82 | } | 86 | } |
83 | } | 87 | } |
84 | 88 | ||
85 | static int inclinenumber (int pragma_allowed) | 89 | |
90 | static void readname (char *buff) | ||
86 | { | 91 | { |
92 | int i = 0; | ||
93 | while (current == ' ') next(); | ||
94 | while (isalnum((unsigned char)current)) { | ||
95 | if (i >= MINBUFF) luaI_syntaxerror("pragma too long"); | ||
96 | buff[i++] = current; | ||
97 | next(); | ||
98 | } | ||
99 | buff[i] = 0; | ||
100 | if (!isalpha(buff[0]) || (current != 0 && !isspace(current))) | ||
101 | luaI_auxsyntaxerror("invalid pragma format", NULL); | ||
102 | } | ||
103 | |||
104 | static int inclinenumber (void) | ||
105 | { | ||
106 | static char *pragmas [] = {"debug", "nodebug", "endif", "ifnil", "if", NULL}; | ||
107 | int ifnil = 0; | ||
87 | ++lua_linenumber; | 108 | ++lua_linenumber; |
88 | if (pragma_allowed && current == '$') { /* is a pragma? */ | 109 | if (current == '$') { /* is a pragma? */ |
89 | char buff[MINBUFF+1]; | 110 | char buff[MINBUFF+1]; |
90 | int i = 0; | ||
91 | next(); /* skip $ */ | 111 | next(); /* skip $ */ |
92 | while (isalnum((unsigned char)current)) { | 112 | readname(buff); |
93 | if (i >= MINBUFF) luaI_syntaxerror("pragma too long"); | 113 | switch (luaI_findstring(buff, pragmas)) { |
94 | buff[i++] = current; | 114 | case 0: /* debug */ |
95 | next(); | 115 | lua_debug = 1; |
116 | break; | ||
117 | case 1: /* nodebug */ | ||
118 | lua_debug = 0; | ||
119 | break; | ||
120 | case 2: /* endif */ | ||
121 | if (--iflevel < 0) | ||
122 | luaI_auxsyntaxerror("too many $endif's", NULL); | ||
123 | break; | ||
124 | case 3: /* ifnil */ | ||
125 | ifnil = 1; | ||
126 | /* go through */ | ||
127 | case 4: { /* if */ | ||
128 | int thisiflevel = iflevel++; | ||
129 | readname(buff); | ||
130 | if ((ifnil && luaI_globaldefined(buff)) || | ||
131 | (!ifnil && !luaI_globaldefined(buff))) { /* skip the $if? */ | ||
132 | do { | ||
133 | if (current == '\n') { | ||
134 | next(); | ||
135 | inclinenumber(); | ||
136 | } | ||
137 | else if (current == 0) | ||
138 | luaI_auxsyntaxerror("input ends inside a $if", NULL); | ||
139 | else next(); | ||
140 | } while (iflevel > thisiflevel); | ||
141 | } | ||
142 | break; | ||
143 | } | ||
144 | default: luaI_auxsyntaxerror("invalid pragma", buff); | ||
96 | } | 145 | } |
97 | buff[i] = 0; | ||
98 | if (strcmp(buff, "debug") == 0) | ||
99 | lua_debug = 1; | ||
100 | else if (strcmp(buff, "nodebug") == 0) | ||
101 | lua_debug = 0; | ||
102 | else luaI_auxsyntaxerror("invalid pragma", buff); | ||
103 | } | 146 | } |
104 | return lua_linenumber; | 147 | return lua_linenumber; |
105 | } | 148 | } |
@@ -136,7 +179,7 @@ static int read_long_string (char *yytext, int buffsize) | |||
136 | continue; | 179 | continue; |
137 | case '\n': | 180 | case '\n': |
138 | save_and_next(); | 181 | save_and_next(); |
139 | inclinenumber(0); | 182 | inclinenumber(); |
140 | continue; | 183 | continue; |
141 | default: | 184 | default: |
142 | save_and_next(); | 185 | save_and_next(); |
@@ -167,7 +210,7 @@ int luaY_lex (void) | |||
167 | { | 210 | { |
168 | case '\n': | 211 | case '\n': |
169 | next(); | 212 | next(); |
170 | linelasttoken = inclinenumber(1); | 213 | linelasttoken = inclinenumber(); |
171 | continue; | 214 | continue; |
172 | 215 | ||
173 | case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */ | 216 | case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */ |
@@ -231,7 +274,7 @@ int luaY_lex (void) | |||
231 | case 'n': save('\n'); next(); break; | 274 | case 'n': save('\n'); next(); break; |
232 | case 't': save('\t'); next(); break; | 275 | case 't': save('\t'); next(); break; |
233 | case 'r': save('\r'); next(); break; | 276 | case 'r': save('\r'); next(); break; |
234 | case '\n': save_and_next(); inclinenumber(0); break; | 277 | case '\n': save_and_next(); inclinenumber(); break; |
235 | default : save_and_next(); break; | 278 | default : save_and_next(); break; |
236 | } | 279 | } |
237 | break; | 280 | break; |
@@ -339,6 +382,8 @@ int luaY_lex (void) | |||
339 | 382 | ||
340 | case 0: | 383 | case 0: |
341 | save(0); | 384 | save(0); |
385 | if (iflevel > 0) | ||
386 | luaI_syntaxerror("missing $endif"); | ||
342 | return 0; | 387 | return 0; |
343 | 388 | ||
344 | default: | 389 | default: |