diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-09-25 18:52:00 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-09-25 18:52:00 -0300 |
commit | 0af581f0bf5a7d0d67803a1a25959a53d5bb6cda (patch) | |
tree | 308955cc0ef9a22e3447b44fcf0d244d3965a50e | |
parent | 2a506ea9d2ed9531352c91a4930bf80549e2e495 (diff) | |
download | lua-0af581f0bf5a7d0d67803a1a25959a53d5bb6cda.tar.gz lua-0af581f0bf5a7d0d67803a1a25959a53d5bb6cda.tar.bz2 lua-0af581f0bf5a7d0d67803a1a25959a53d5bb6cda.zip |
new way to handle pragmas (at the lexical level, instead of parsing).
-rw-r--r-- | lex.c | 58 | ||||
-rw-r--r-- | lua.stx | 12 |
2 files changed, 35 insertions, 35 deletions
@@ -1,4 +1,4 @@ | |||
1 | char *rcs_lex = "$Id: lex.c,v 2.34 1996/05/30 14:04:07 roberto Exp roberto $"; | 1 | char *rcs_lex = "$Id: lex.c,v 2.35 1996/09/09 14:11:11 roberto Exp roberto $"; |
2 | 2 | ||
3 | 3 | ||
4 | #include <ctype.h> | 4 | #include <ctype.h> |
@@ -25,7 +25,8 @@ static Input input; /* input function */ | |||
25 | 25 | ||
26 | void lua_setinput (Input fn) | 26 | void lua_setinput (Input fn) |
27 | { | 27 | { |
28 | current = ' '; | 28 | current = '\n'; |
29 | lua_linenumber = 0; | ||
29 | input = fn; | 30 | input = fn; |
30 | } | 31 | } |
31 | 32 | ||
@@ -71,6 +72,26 @@ void luaI_addReserved (void) | |||
71 | } | 72 | } |
72 | } | 73 | } |
73 | 74 | ||
75 | static int inclinenumber (void) | ||
76 | { | ||
77 | if (current == '$') { /* is a pragma? */ | ||
78 | char buff[MINBUFF]; | ||
79 | int i = 0; | ||
80 | next(); /* skip $ */ | ||
81 | while (isalnum(current)) { | ||
82 | if (i >= MINBUFF) lua_error("pragma too long"); | ||
83 | buff[i++] = current; | ||
84 | next(); | ||
85 | } | ||
86 | buff[i] = 0; | ||
87 | if (strcmp(buff, "debug") == 0) | ||
88 | lua_debug = 1; | ||
89 | else if (strcmp(buff, "nodebug") == 0) | ||
90 | lua_debug = 0; | ||
91 | else lua_error("invalid pragma"); | ||
92 | } | ||
93 | return ++lua_linenumber; | ||
94 | } | ||
74 | 95 | ||
75 | static int read_long_string (char *yytext, int buffsize) | 96 | static int read_long_string (char *yytext, int buffsize) |
76 | { | 97 | { |
@@ -103,7 +124,9 @@ static int read_long_string (char *yytext, int buffsize) | |||
103 | } | 124 | } |
104 | continue; | 125 | continue; |
105 | case '\n': | 126 | case '\n': |
106 | lua_linenumber++; /* goes through */ | 127 | save_and_next(); |
128 | inclinenumber(); | ||
129 | continue; | ||
107 | default: | 130 | default: |
108 | save_and_next(); | 131 | save_and_next(); |
109 | } | 132 | } |
@@ -131,29 +154,14 @@ int luaY_lex (void) | |||
131 | int tokensize = 0; | 154 | int tokensize = 0; |
132 | switch (current) | 155 | switch (current) |
133 | { | 156 | { |
134 | case '\n': linelasttoken = ++lua_linenumber; | 157 | case '\n': |
135 | case ' ': | ||
136 | case '\r': /* CR: to avoid problems with DOS/Windows */ | ||
137 | case '\t': | ||
138 | next(); | 158 | next(); |
159 | linelasttoken = inclinenumber(); | ||
139 | continue; | 160 | continue; |
140 | 161 | ||
141 | case '$': | 162 | case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */ |
142 | save_and_next(); | 163 | next(); |
143 | while (isalnum(current) || current == '_') | 164 | continue; |
144 | save_and_next(); | ||
145 | save(0); | ||
146 | if (strcmp(yytext+1, "debug") == 0) | ||
147 | { | ||
148 | luaY_lval.vInt = 1; | ||
149 | return DEBUG; | ||
150 | } | ||
151 | else if (strcmp(yytext+1, "nodebug") == 0) | ||
152 | { | ||
153 | luaY_lval.vInt = 0; | ||
154 | return DEBUG; | ||
155 | } | ||
156 | return WRONGTOKEN; | ||
157 | 165 | ||
158 | case '-': | 166 | case '-': |
159 | save_and_next(); | 167 | save_and_next(); |
@@ -212,8 +220,8 @@ int luaY_lex (void) | |||
212 | case 'n': save('\n'); next(); break; | 220 | case 'n': save('\n'); next(); break; |
213 | case 't': save('\t'); next(); break; | 221 | case 't': save('\t'); next(); break; |
214 | case 'r': save('\r'); next(); break; | 222 | case 'r': save('\r'); next(); break; |
215 | case '\n': lua_linenumber++; /* goes through */ | 223 | case '\n': save_and_next(); inclinenumber(); break; |
216 | default : save(current); next(); break; | 224 | default : save_and_next(); break; |
217 | } | 225 | } |
218 | break; | 226 | break; |
219 | default: | 227 | default: |
@@ -1,6 +1,6 @@ | |||
1 | %{ | 1 | %{ |
2 | 2 | ||
3 | char *rcs_luastx = "$Id: lua.stx,v 3.38 1996/07/24 14:38:12 roberto Exp roberto $"; | 3 | char *rcs_luastx = "$Id: lua.stx,v 3.39 1996/09/24 17:29:50 roberto Exp roberto $"; |
4 | 4 | ||
5 | #include <stdio.h> | 5 | #include <stdio.h> |
6 | #include <stdlib.h> | 6 | #include <stdlib.h> |
@@ -440,7 +440,6 @@ void lua_parse (TFunc *tf) | |||
440 | %token <vFloat> NUMBER | 440 | %token <vFloat> NUMBER |
441 | %token <vWord> STRING | 441 | %token <vWord> STRING |
442 | %token <pTStr> NAME | 442 | %token <pTStr> NAME |
443 | %token <vInt> DEBUG | ||
444 | 443 | ||
445 | %type <vLong> PrepJump | 444 | %type <vLong> PrepJump |
446 | %type <vLong> exprlist, exprlist1 /* if > 0, points to function return | 445 | %type <vLong> exprlist, exprlist1 /* if > 0, points to function return |
@@ -469,14 +468,10 @@ void lua_parse (TFunc *tf) | |||
469 | chunk : chunklist ret | 468 | chunk : chunklist ret |
470 | 469 | ||
471 | chunklist : /* empty */ | 470 | chunklist : /* empty */ |
472 | | chunklist globalstat | 471 | | chunklist stat sc |
473 | | chunklist function | 472 | | chunklist function |
474 | ; | 473 | ; |
475 | 474 | ||
476 | globalstat : stat sc | ||
477 | | setdebug | ||
478 | ; | ||
479 | |||
480 | function : FUNCTION funcname body | 475 | function : FUNCTION funcname body |
481 | { | 476 | { |
482 | code_byte(PUSHFUNCTION); | 477 | code_byte(PUSHFUNCTION); |
@@ -804,7 +799,4 @@ decinit : /* empty */ { $$ = 0; } | |||
804 | | '=' exprlist1 { $$ = $2; } | 799 | | '=' exprlist1 { $$ = $2; } |
805 | ; | 800 | ; |
806 | 801 | ||
807 | setdebug : DEBUG { lua_debug = $1; } | ||
808 | ; | ||
809 | |||
810 | %% | 802 | %% |