diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2003-08-21 11:16:43 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2003-08-21 11:16:43 -0300 |
| commit | 97af24ea3246dca0258ba7089cf2df7ac2080560 (patch) | |
| tree | e2e58e9ecdb4e209ff0592084f328e80a1baf54a | |
| parent | 433cb1d13a767023e03111fc09c46903f40366ff (diff) | |
| download | lua-97af24ea3246dca0258ba7089cf2df7ac2080560.tar.gz lua-97af24ea3246dca0258ba7089cf2df7ac2080560.tar.bz2 lua-97af24ea3246dca0258ba7089cf2df7ac2080560.zip | |
newlines can be `\n', `\r', `\r\n', or `\n\r'
| -rw-r--r-- | llex.c | 23 |
1 files changed, 17 insertions, 6 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: llex.c,v 1.119 2003/03/24 12:39:34 roberto Exp roberto $ | 2 | ** $Id: llex.c,v 1.120 2003/05/15 12:20:24 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 | */ |
| @@ -25,6 +25,8 @@ | |||
| 25 | #define next(LS) (LS->current = zgetc(LS->z)) | 25 | #define next(LS) (LS->current = zgetc(LS->z)) |
| 26 | 26 | ||
| 27 | 27 | ||
| 28 | #define nextIsNewline(LS) (LS->current == '\n' || LS->current == '\r') | ||
| 29 | |||
| 28 | 30 | ||
| 29 | /* ORDER RESERVED */ | 31 | /* ORDER RESERVED */ |
| 30 | static const char *const token2string [] = { | 32 | static const char *const token2string [] = { |
| @@ -110,7 +112,11 @@ static void luaX_lexerror (LexState *ls, const char *s, int token) { | |||
| 110 | 112 | ||
| 111 | 113 | ||
| 112 | static void inclinenumber (LexState *LS) { | 114 | static void inclinenumber (LexState *LS) { |
| 113 | next(LS); /* skip `\n' */ | 115 | int old = LS->current; |
| 116 | lua_assert(nextIsNewline(LS)); | ||
| 117 | next(LS); /* skip `\n' or `\r' */ | ||
| 118 | if (nextIsNewline(LS) && LS->current != old) | ||
| 119 | next(LS); /* skip `\n\r' or `\r\n' */ | ||
| 114 | ++LS->linenumber; | 120 | ++LS->linenumber; |
| 115 | luaX_checklimit(LS, LS->linenumber, MAX_INT, "lines in a chunk"); | 121 | luaX_checklimit(LS, LS->linenumber, MAX_INT, "lines in a chunk"); |
| 116 | } | 122 | } |
| @@ -209,7 +215,7 @@ static void read_long_string (LexState *LS, SemInfo *seminfo) { | |||
| 209 | checkbuffer(LS, l); | 215 | checkbuffer(LS, l); |
| 210 | save(LS, '[', l); /* save first `[' */ | 216 | save(LS, '[', l); /* save first `[' */ |
| 211 | save_and_next(LS, l); /* pass the second `[' */ | 217 | save_and_next(LS, l); /* pass the second `[' */ |
| 212 | if (LS->current == '\n') /* string starts with a newline? */ | 218 | if (nextIsNewline(LS)) /* string starts with a newline? */ |
| 213 | inclinenumber(LS); /* skip it */ | 219 | inclinenumber(LS); /* skip it */ |
| 214 | for (;;) { | 220 | for (;;) { |
| 215 | checkbuffer(LS, l); | 221 | checkbuffer(LS, l); |
| @@ -235,6 +241,7 @@ static void read_long_string (LexState *LS, SemInfo *seminfo) { | |||
| 235 | } | 241 | } |
| 236 | continue; | 242 | continue; |
| 237 | case '\n': | 243 | case '\n': |
| 244 | case '\r': | ||
| 238 | save(LS, '\n', l); | 245 | save(LS, '\n', l); |
| 239 | inclinenumber(LS); | 246 | inclinenumber(LS); |
| 240 | if (!seminfo) l = 0; /* reset buffer to avoid wasting space */ | 247 | if (!seminfo) l = 0; /* reset buffer to avoid wasting space */ |
| @@ -262,6 +269,7 @@ static void read_string (LexState *LS, int del, SemInfo *seminfo) { | |||
| 262 | luaX_lexerror(LS, "unfinished string", TK_EOS); | 269 | luaX_lexerror(LS, "unfinished string", TK_EOS); |
| 263 | break; /* to avoid warnings */ | 270 | break; /* to avoid warnings */ |
| 264 | case '\n': | 271 | case '\n': |
| 272 | case '\r': | ||
| 265 | save(LS, '\0', l); | 273 | save(LS, '\0', l); |
| 266 | luaX_lexerror(LS, "unfinished string", TK_STRING); | 274 | luaX_lexerror(LS, "unfinished string", TK_STRING); |
| 267 | break; /* to avoid warnings */ | 275 | break; /* to avoid warnings */ |
| @@ -275,7 +283,8 @@ static void read_string (LexState *LS, int del, SemInfo *seminfo) { | |||
| 275 | case 'r': save(LS, '\r', l); next(LS); break; | 283 | case 'r': save(LS, '\r', l); next(LS); break; |
| 276 | case 't': save(LS, '\t', l); next(LS); break; | 284 | case 't': save(LS, '\t', l); next(LS); break; |
| 277 | case 'v': save(LS, '\v', l); next(LS); break; | 285 | case 'v': save(LS, '\v', l); next(LS); break; |
| 278 | case '\n': save(LS, '\n', l); inclinenumber(LS); break; | 286 | case '\n': /* go through */ |
| 287 | case '\r': save(LS, '\n', l); inclinenumber(LS); break; | ||
| 279 | case EOZ: break; /* will raise an error next loop */ | 288 | case EOZ: break; /* will raise an error next loop */ |
| 280 | default: { | 289 | default: { |
| 281 | if (!isdigit(LS->current)) | 290 | if (!isdigit(LS->current)) |
| @@ -310,7 +319,8 @@ int luaX_lex (LexState *LS, SemInfo *seminfo) { | |||
| 310 | for (;;) { | 319 | for (;;) { |
| 311 | switch (LS->current) { | 320 | switch (LS->current) { |
| 312 | 321 | ||
| 313 | case '\n': { | 322 | case '\n': |
| 323 | case '\r': { | ||
| 314 | inclinenumber(LS); | 324 | inclinenumber(LS); |
| 315 | continue; | 325 | continue; |
| 316 | } | 326 | } |
| @@ -322,7 +332,7 @@ int luaX_lex (LexState *LS, SemInfo *seminfo) { | |||
| 322 | if (LS->current == '[' && (next(LS), LS->current == '[')) | 332 | if (LS->current == '[' && (next(LS), LS->current == '[')) |
| 323 | read_long_string(LS, NULL); /* long comment */ | 333 | read_long_string(LS, NULL); /* long comment */ |
| 324 | else /* short comment */ | 334 | else /* short comment */ |
| 325 | while (LS->current != '\n' && LS->current != EOZ) | 335 | while (!nextIsNewline(LS) && LS->current != EOZ) |
| 326 | next(LS); | 336 | next(LS); |
| 327 | continue; | 337 | continue; |
| 328 | } | 338 | } |
| @@ -380,6 +390,7 @@ int luaX_lex (LexState *LS, SemInfo *seminfo) { | |||
| 380 | } | 390 | } |
| 381 | default: { | 391 | default: { |
| 382 | if (isspace(LS->current)) { | 392 | if (isspace(LS->current)) { |
| 393 | lua_assert(!nextIsNewline(LS)); | ||
| 383 | next(LS); | 394 | next(LS); |
| 384 | continue; | 395 | continue; |
| 385 | } | 396 | } |
