diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-03-08 16:07:01 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-03-08 16:07:01 -0300 |
| commit | 4db04555f06b90f3b2cda96c2f4aaeb78266ea86 (patch) | |
| tree | d8aa27830bffa5e2565b7a9a2c9bb4f41ab04502 | |
| parent | c16a35d6696ac995ef6c7b60f251f6821811f50f (diff) | |
| download | lua-4db04555f06b90f3b2cda96c2f4aaeb78266ea86.tar.gz lua-4db04555f06b90f3b2cda96c2f4aaeb78266ea86.tar.bz2 lua-4db04555f06b90f3b2cda96c2f4aaeb78266ea86.zip | |
implementation of long comments
| -rw-r--r-- | llex.c | 42 |
1 files changed, 25 insertions, 17 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: llex.c,v 1.96 2002/02/08 22:40:27 roberto Exp roberto $ | 2 | ** $Id: llex.c,v 1.97 2002/03/04 15:27:14 roberto Exp roberto $ |
| 3 | ** Lexical Analyzer | 3 | ** Lexical Analyzer |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -135,20 +135,20 @@ void luaX_setinput (lua_State *L, LexState *LS, ZIO *z, TString *source) { | |||
| 135 | /* use Mbuffer to store names, literal strings and numbers */ | 135 | /* use Mbuffer to store names, literal strings and numbers */ |
| 136 | 136 | ||
| 137 | #define EXTRABUFF 128 | 137 | #define EXTRABUFF 128 |
| 138 | #define checkbuffer(L, n, len) \ | 138 | #define checkbuffer(L, len) \ |
| 139 | if (((len)+(n))*sizeof(char) > G(L)->Mbuffsize) \ | 139 | if (((len)+10)*sizeof(char) > G(L)->Mbuffsize) \ |
| 140 | luaO_openspace(L, (len)+(n)+EXTRABUFF, char) | 140 | luaO_openspace(L, (len)+EXTRABUFF, char) |
| 141 | 141 | ||
| 142 | #define save(L, c, l) (cast(char *, G(L)->Mbuffer)[l++] = (char)c) | 142 | #define save(L, c, l) (cast(char *, G(L)->Mbuffer)[l++] = cast(char, c)) |
| 143 | #define save_and_next(L, LS, l) (save(L, LS->current, l), next(LS)) | 143 | #define save_and_next(L, LS, l) (save(L, LS->current, l), next(LS)) |
| 144 | 144 | ||
| 145 | 145 | ||
| 146 | static size_t readname (LexState *LS) { | 146 | static size_t readname (LexState *LS) { |
| 147 | lua_State *L = LS->L; | 147 | lua_State *L = LS->L; |
| 148 | size_t l = 0; | 148 | size_t l = 0; |
| 149 | checkbuffer(L, 10, l); | 149 | checkbuffer(L, l); |
| 150 | do { | 150 | do { |
| 151 | checkbuffer(L, 10, l); | 151 | checkbuffer(L, l); |
| 152 | save_and_next(L, LS, l); | 152 | save_and_next(L, LS, l); |
| 153 | } while (isalnum(LS->current) || LS->current == '_'); | 153 | } while (isalnum(LS->current) || LS->current == '_'); |
| 154 | save(L, '\0', l); | 154 | save(L, '\0', l); |
| @@ -160,10 +160,10 @@ static size_t readname (LexState *LS) { | |||
| 160 | static void read_number (LexState *LS, int comma, SemInfo *seminfo) { | 160 | static void read_number (LexState *LS, int comma, SemInfo *seminfo) { |
| 161 | lua_State *L = LS->L; | 161 | lua_State *L = LS->L; |
| 162 | size_t l = 0; | 162 | size_t l = 0; |
| 163 | checkbuffer(L, 10, l); | 163 | checkbuffer(L, l); |
| 164 | if (comma) save(L, '.', l); | 164 | if (comma) save(L, '.', l); |
| 165 | while (isdigit(LS->current)) { | 165 | while (isdigit(LS->current)) { |
| 166 | checkbuffer(L, 10, l); | 166 | checkbuffer(L, l); |
| 167 | save_and_next(L, LS, l); | 167 | save_and_next(L, LS, l); |
| 168 | } | 168 | } |
| 169 | if (LS->current == '.') { | 169 | if (LS->current == '.') { |
| @@ -177,7 +177,7 @@ static void read_number (LexState *LS, int comma, SemInfo *seminfo) { | |||
| 177 | } | 177 | } |
| 178 | } | 178 | } |
| 179 | while (isdigit(LS->current)) { | 179 | while (isdigit(LS->current)) { |
| 180 | checkbuffer(L, 10, l); | 180 | checkbuffer(L, l); |
| 181 | save_and_next(L, LS, l); | 181 | save_and_next(L, LS, l); |
| 182 | } | 182 | } |
| 183 | if (LS->current == 'e' || LS->current == 'E') { | 183 | if (LS->current == 'e' || LS->current == 'E') { |
| @@ -185,7 +185,7 @@ static void read_number (LexState *LS, int comma, SemInfo *seminfo) { | |||
| 185 | if (LS->current == '+' || LS->current == '-') | 185 | if (LS->current == '+' || LS->current == '-') |
| 186 | save_and_next(L, LS, l); /* optional exponent sign */ | 186 | save_and_next(L, LS, l); /* optional exponent sign */ |
| 187 | while (isdigit(LS->current)) { | 187 | while (isdigit(LS->current)) { |
| 188 | checkbuffer(L, 10, l); | 188 | checkbuffer(L, l); |
| 189 | save_and_next(L, LS, l); | 189 | save_and_next(L, LS, l); |
| 190 | } | 190 | } |
| 191 | } | 191 | } |
| @@ -199,13 +199,13 @@ static void read_long_string (LexState *LS, SemInfo *seminfo) { | |||
| 199 | lua_State *L = LS->L; | 199 | lua_State *L = LS->L; |
| 200 | int cont = 0; | 200 | int cont = 0; |
| 201 | size_t l = 0; | 201 | size_t l = 0; |
| 202 | checkbuffer(L, 10, l); | 202 | checkbuffer(L, l); |
| 203 | save(L, '[', l); /* save first `[' */ | 203 | save(L, '[', l); /* save first `[' */ |
| 204 | save_and_next(L, LS, l); /* pass the second `[' */ | 204 | save_and_next(L, LS, l); /* pass the second `[' */ |
| 205 | if (LS->current == '\n') /* string starts with a newline? */ | 205 | if (LS->current == '\n') /* string starts with a newline? */ |
| 206 | inclinenumber(LS); /* skip it */ | 206 | inclinenumber(LS); /* skip it */ |
| 207 | for (;;) { | 207 | for (;;) { |
| 208 | checkbuffer(L, 10, l); | 208 | checkbuffer(L, l); |
| 209 | switch (LS->current) { | 209 | switch (LS->current) { |
| 210 | case EOZ: | 210 | case EOZ: |
| 211 | save(L, '\0', l); | 211 | save(L, '\0', l); |
| @@ -229,6 +229,7 @@ static void read_long_string (LexState *LS, SemInfo *seminfo) { | |||
| 229 | case '\n': | 229 | case '\n': |
| 230 | save(L, '\n', l); | 230 | save(L, '\n', l); |
| 231 | inclinenumber(LS); | 231 | inclinenumber(LS); |
| 232 | if (!seminfo) l = 0; /* reset buffer to avoid wasting space */ | ||
| 232 | continue; | 233 | continue; |
| 233 | default: | 234 | default: |
| 234 | save_and_next(L, LS, l); | 235 | save_and_next(L, LS, l); |
| @@ -236,17 +237,18 @@ static void read_long_string (LexState *LS, SemInfo *seminfo) { | |||
| 236 | } endloop: | 237 | } endloop: |
| 237 | save_and_next(L, LS, l); /* skip the second `]' */ | 238 | save_and_next(L, LS, l); /* skip the second `]' */ |
| 238 | save(L, '\0', l); | 239 | save(L, '\0', l); |
| 239 | seminfo->ts = luaS_newlstr(L, cast(char *, G(L)->Mbuffer)+2, l-5); | 240 | if (seminfo) |
| 241 | seminfo->ts = luaS_newlstr(L, cast(char *, G(L)->Mbuffer)+2, l-5); | ||
| 240 | } | 242 | } |
| 241 | 243 | ||
| 242 | 244 | ||
| 243 | static void read_string (LexState *LS, int del, SemInfo *seminfo) { | 245 | static void read_string (LexState *LS, int del, SemInfo *seminfo) { |
| 244 | lua_State *L = LS->L; | 246 | lua_State *L = LS->L; |
| 245 | size_t l = 0; | 247 | size_t l = 0; |
| 246 | checkbuffer(L, 10, l); | 248 | checkbuffer(L, l); |
| 247 | save_and_next(L, LS, l); | 249 | save_and_next(L, LS, l); |
| 248 | while (LS->current != del) { | 250 | while (LS->current != del) { |
| 249 | checkbuffer(L, 10, l); | 251 | checkbuffer(L, l); |
| 250 | switch (LS->current) { | 252 | switch (LS->current) { |
| 251 | case EOZ: case '\n': | 253 | case EOZ: case '\n': |
| 252 | save(L, '\0', l); | 254 | save(L, '\0', l); |
| @@ -308,7 +310,13 @@ int luaX_lex (LexState *LS, SemInfo *seminfo) { | |||
| 308 | case '-': | 310 | case '-': |
| 309 | next(LS); | 311 | next(LS); |
| 310 | if (LS->current != '-') return '-'; | 312 | if (LS->current != '-') return '-'; |
| 311 | do { next(LS); } while (LS->current != '\n' && LS->current != EOZ); | 313 | /* else is a comment */ |
| 314 | next(LS); | ||
| 315 | if (LS->current == '[' && (next(LS), LS->current == '[')) | ||
| 316 | read_long_string(LS, NULL); /* long comment */ | ||
| 317 | else /* short comment */ | ||
| 318 | while (LS->current != '\n' && LS->current != EOZ) | ||
| 319 | next(LS); | ||
| 312 | continue; | 320 | continue; |
| 313 | 321 | ||
| 314 | case '[': | 322 | case '[': |
