annotate lib/lua/lua-5.2.2/llex.c @ 1866:41cc4dd3c122

Lua 5.2.2 added.
author Nomad
date Wed, 16 Oct 2013 13:34:26 +0200
parents
children
rev   line source
1866
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
2 ** $Id: llex.c,v 2.63 2013/03/16 21:10:18 roberto Exp $
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
3 ** Lexical Analyzer
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
4 ** See Copyright Notice in lua.h
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
5 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
6
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
7
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
8 #include <locale.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
9 #include <string.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
10
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
11 #define llex_c
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
12 #define LUA_CORE
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
13
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
14 #include "lua.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
15
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
16 #include "lctype.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
17 #include "ldo.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
18 #include "llex.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
19 #include "lobject.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
20 #include "lparser.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
21 #include "lstate.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
22 #include "lstring.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
23 #include "ltable.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
24 #include "lzio.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
25
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
26
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
27
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
28 #define next(ls) (ls->current = zgetc(ls->z))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
29
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
30
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
31
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
32 #define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r')
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
33
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
34
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
35 /* ORDER RESERVED */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
36 static const char *const luaX_tokens [] = {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
37 "and", "break", "do", "else", "elseif",
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
38 "end", "false", "for", "function", "goto", "if",
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
39 "in", "local", "nil", "not", "or", "repeat",
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
40 "return", "then", "true", "until", "while",
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
41 "..", "...", "==", ">=", "<=", "~=", "::", "<eof>",
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
42 "<number>", "<name>", "<string>"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
43 };
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
44
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
45
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
46 #define save_and_next(ls) (save(ls, ls->current), next(ls))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
47
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
48
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
49 static l_noret lexerror (LexState *ls, const char *msg, int token);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
50
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
51
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
52 static void save (LexState *ls, int c) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
53 Mbuffer *b = ls->buff;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
54 if (luaZ_bufflen(b) + 1 > luaZ_sizebuffer(b)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
55 size_t newsize;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
56 if (luaZ_sizebuffer(b) >= MAX_SIZET/2)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
57 lexerror(ls, "lexical element too long", 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
58 newsize = luaZ_sizebuffer(b) * 2;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
59 luaZ_resizebuffer(ls->L, b, newsize);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
60 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
61 b->buffer[luaZ_bufflen(b)++] = cast(char, c);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
62 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
63
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
64
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
65 void luaX_init (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
66 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
67 for (i=0; i<NUM_RESERVED; i++) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
68 TString *ts = luaS_new(L, luaX_tokens[i]);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
69 luaS_fix(ts); /* reserved words are never collected */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
70 ts->tsv.extra = cast_byte(i+1); /* reserved word */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
71 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
72 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
73
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
74
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
75 const char *luaX_token2str (LexState *ls, int token) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
76 if (token < FIRST_RESERVED) { /* single-byte symbols? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
77 lua_assert(token == cast(unsigned char, token));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
78 return (lisprint(token)) ? luaO_pushfstring(ls->L, LUA_QL("%c"), token) :
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
79 luaO_pushfstring(ls->L, "char(%d)", token);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
80 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
81 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
82 const char *s = luaX_tokens[token - FIRST_RESERVED];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
83 if (token < TK_EOS) /* fixed format (symbols and reserved words)? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
84 return luaO_pushfstring(ls->L, LUA_QS, s);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
85 else /* names, strings, and numerals */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
86 return s;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
87 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
88 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
89
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
90
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
91 static const char *txtToken (LexState *ls, int token) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
92 switch (token) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
93 case TK_NAME:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
94 case TK_STRING:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
95 case TK_NUMBER:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
96 save(ls, '\0');
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
97 return luaO_pushfstring(ls->L, LUA_QS, luaZ_buffer(ls->buff));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
98 default:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
99 return luaX_token2str(ls, token);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
100 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
101 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
102
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
103
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
104 static l_noret lexerror (LexState *ls, const char *msg, int token) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
105 char buff[LUA_IDSIZE];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
106 luaO_chunkid(buff, getstr(ls->source), LUA_IDSIZE);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
107 msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
108 if (token)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
109 luaO_pushfstring(ls->L, "%s near %s", msg, txtToken(ls, token));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
110 luaD_throw(ls->L, LUA_ERRSYNTAX);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
111 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
112
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
113
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
114 l_noret luaX_syntaxerror (LexState *ls, const char *msg) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
115 lexerror(ls, msg, ls->t.token);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
116 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
117
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
118
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
119 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
120 ** creates a new string and anchors it in function's table so that
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
121 ** it will not be collected until the end of the function's compilation
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
122 ** (by that time it should be anchored in function's prototype)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
123 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
124 TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
125 lua_State *L = ls->L;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
126 TValue *o; /* entry for `str' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
127 TString *ts = luaS_newlstr(L, str, l); /* create new string */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
128 setsvalue2s(L, L->top++, ts); /* temporarily anchor it in stack */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
129 o = luaH_set(L, ls->fs->h, L->top - 1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
130 if (ttisnil(o)) { /* not in use yet? (see 'addK') */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
131 /* boolean value does not need GC barrier;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
132 table has no metatable, so it does not need to invalidate cache */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
133 setbvalue(o, 1); /* t[string] = true */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
134 luaC_checkGC(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
135 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
136 L->top--; /* remove string from stack */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
137 return ts;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
138 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
139
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
140
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
141 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
142 ** increment line number and skips newline sequence (any of
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
143 ** \n, \r, \n\r, or \r\n)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
144 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
145 static void inclinenumber (LexState *ls) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
146 int old = ls->current;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
147 lua_assert(currIsNewline(ls));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
148 next(ls); /* skip `\n' or `\r' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
149 if (currIsNewline(ls) && ls->current != old)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
150 next(ls); /* skip `\n\r' or `\r\n' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
151 if (++ls->linenumber >= MAX_INT)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
152 luaX_syntaxerror(ls, "chunk has too many lines");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
153 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
154
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
155
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
156 void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
157 int firstchar) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
158 ls->decpoint = '.';
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
159 ls->L = L;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
160 ls->current = firstchar;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
161 ls->lookahead.token = TK_EOS; /* no look-ahead token */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
162 ls->z = z;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
163 ls->fs = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
164 ls->linenumber = 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
165 ls->lastline = 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
166 ls->source = source;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
167 ls->envn = luaS_new(L, LUA_ENV); /* create env name */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
168 luaS_fix(ls->envn); /* never collect this name */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
169 luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
170 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
171
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
172
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
173
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
174 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
175 ** =======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
176 ** LEXICAL ANALYZER
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
177 ** =======================================================
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
178 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
179
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
180
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
181
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
182 static int check_next (LexState *ls, const char *set) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
183 if (ls->current == '\0' || !strchr(set, ls->current))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
184 return 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
185 save_and_next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
186 return 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
187 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
188
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
189
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
190 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
191 ** change all characters 'from' in buffer to 'to'
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
192 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
193 static void buffreplace (LexState *ls, char from, char to) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
194 size_t n = luaZ_bufflen(ls->buff);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
195 char *p = luaZ_buffer(ls->buff);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
196 while (n--)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
197 if (p[n] == from) p[n] = to;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
198 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
199
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
200
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
201 #if !defined(getlocaledecpoint)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
202 #define getlocaledecpoint() (localeconv()->decimal_point[0])
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
203 #endif
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
204
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
205
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
206 #define buff2d(b,e) luaO_str2d(luaZ_buffer(b), luaZ_bufflen(b) - 1, e)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
207
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
208 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
209 ** in case of format error, try to change decimal point separator to
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
210 ** the one defined in the current locale and check again
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
211 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
212 static void trydecpoint (LexState *ls, SemInfo *seminfo) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
213 char old = ls->decpoint;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
214 ls->decpoint = getlocaledecpoint();
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
215 buffreplace(ls, old, ls->decpoint); /* try new decimal separator */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
216 if (!buff2d(ls->buff, &seminfo->r)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
217 /* format error with correct decimal point: no more options */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
218 buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
219 lexerror(ls, "malformed number", TK_NUMBER);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
220 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
221 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
222
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
223
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
224 /* LUA_NUMBER */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
225 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
226 ** this function is quite liberal in what it accepts, as 'luaO_str2d'
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
227 ** will reject ill-formed numerals.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
228 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
229 static void read_numeral (LexState *ls, SemInfo *seminfo) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
230 const char *expo = "Ee";
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
231 int first = ls->current;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
232 lua_assert(lisdigit(ls->current));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
233 save_and_next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
234 if (first == '0' && check_next(ls, "Xx")) /* hexadecimal? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
235 expo = "Pp";
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
236 for (;;) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
237 if (check_next(ls, expo)) /* exponent part? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
238 check_next(ls, "+-"); /* optional exponent sign */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
239 if (lisxdigit(ls->current) || ls->current == '.')
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
240 save_and_next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
241 else break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
242 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
243 save(ls, '\0');
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
244 buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
245 if (!buff2d(ls->buff, &seminfo->r)) /* format error? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
246 trydecpoint(ls, seminfo); /* try to update decimal point separator */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
247 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
248
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
249
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
250 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
251 ** skip a sequence '[=*[' or ']=*]' and return its number of '='s or
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
252 ** -1 if sequence is malformed
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
253 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
254 static int skip_sep (LexState *ls) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
255 int count = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
256 int s = ls->current;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
257 lua_assert(s == '[' || s == ']');
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
258 save_and_next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
259 while (ls->current == '=') {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
260 save_and_next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
261 count++;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
262 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
263 return (ls->current == s) ? count : (-count) - 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
264 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
265
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
266
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
267 static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
268 save_and_next(ls); /* skip 2nd `[' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
269 if (currIsNewline(ls)) /* string starts with a newline? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
270 inclinenumber(ls); /* skip it */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
271 for (;;) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
272 switch (ls->current) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
273 case EOZ:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
274 lexerror(ls, (seminfo) ? "unfinished long string" :
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
275 "unfinished long comment", TK_EOS);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
276 break; /* to avoid warnings */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
277 case ']': {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
278 if (skip_sep(ls) == sep) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
279 save_and_next(ls); /* skip 2nd `]' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
280 goto endloop;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
281 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
282 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
283 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
284 case '\n': case '\r': {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
285 save(ls, '\n');
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
286 inclinenumber(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
287 if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
288 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
289 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
290 default: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
291 if (seminfo) save_and_next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
292 else next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
293 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
294 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
295 } endloop:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
296 if (seminfo)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
297 seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + (2 + sep),
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
298 luaZ_bufflen(ls->buff) - 2*(2 + sep));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
299 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
300
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
301
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
302 static void escerror (LexState *ls, int *c, int n, const char *msg) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
303 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
304 luaZ_resetbuffer(ls->buff); /* prepare error message */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
305 save(ls, '\\');
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
306 for (i = 0; i < n && c[i] != EOZ; i++)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
307 save(ls, c[i]);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
308 lexerror(ls, msg, TK_STRING);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
309 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
310
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
311
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
312 static int readhexaesc (LexState *ls) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
313 int c[3], i; /* keep input for error message */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
314 int r = 0; /* result accumulator */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
315 c[0] = 'x'; /* for error message */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
316 for (i = 1; i < 3; i++) { /* read two hexadecimal digits */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
317 c[i] = next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
318 if (!lisxdigit(c[i]))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
319 escerror(ls, c, i + 1, "hexadecimal digit expected");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
320 r = (r << 4) + luaO_hexavalue(c[i]);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
321 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
322 return r;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
323 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
324
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
325
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
326 static int readdecesc (LexState *ls) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
327 int c[3], i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
328 int r = 0; /* result accumulator */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
329 for (i = 0; i < 3 && lisdigit(ls->current); i++) { /* read up to 3 digits */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
330 c[i] = ls->current;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
331 r = 10*r + c[i] - '0';
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
332 next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
333 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
334 if (r > UCHAR_MAX)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
335 escerror(ls, c, i, "decimal escape too large");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
336 return r;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
337 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
338
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
339
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
340 static void read_string (LexState *ls, int del, SemInfo *seminfo) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
341 save_and_next(ls); /* keep delimiter (for error messages) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
342 while (ls->current != del) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
343 switch (ls->current) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
344 case EOZ:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
345 lexerror(ls, "unfinished string", TK_EOS);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
346 break; /* to avoid warnings */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
347 case '\n':
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
348 case '\r':
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
349 lexerror(ls, "unfinished string", TK_STRING);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
350 break; /* to avoid warnings */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
351 case '\\': { /* escape sequences */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
352 int c; /* final character to be saved */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
353 next(ls); /* do not save the `\' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
354 switch (ls->current) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
355 case 'a': c = '\a'; goto read_save;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
356 case 'b': c = '\b'; goto read_save;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
357 case 'f': c = '\f'; goto read_save;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
358 case 'n': c = '\n'; goto read_save;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
359 case 'r': c = '\r'; goto read_save;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
360 case 't': c = '\t'; goto read_save;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
361 case 'v': c = '\v'; goto read_save;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
362 case 'x': c = readhexaesc(ls); goto read_save;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
363 case '\n': case '\r':
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
364 inclinenumber(ls); c = '\n'; goto only_save;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
365 case '\\': case '\"': case '\'':
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
366 c = ls->current; goto read_save;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
367 case EOZ: goto no_save; /* will raise an error next loop */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
368 case 'z': { /* zap following span of spaces */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
369 next(ls); /* skip the 'z' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
370 while (lisspace(ls->current)) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
371 if (currIsNewline(ls)) inclinenumber(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
372 else next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
373 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
374 goto no_save;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
375 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
376 default: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
377 if (!lisdigit(ls->current))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
378 escerror(ls, &ls->current, 1, "invalid escape sequence");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
379 /* digital escape \ddd */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
380 c = readdecesc(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
381 goto only_save;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
382 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
383 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
384 read_save: next(ls); /* read next character */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
385 only_save: save(ls, c); /* save 'c' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
386 no_save: break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
387 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
388 default:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
389 save_and_next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
390 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
391 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
392 save_and_next(ls); /* skip delimiter */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
393 seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
394 luaZ_bufflen(ls->buff) - 2);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
395 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
396
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
397
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
398 static int llex (LexState *ls, SemInfo *seminfo) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
399 luaZ_resetbuffer(ls->buff);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
400 for (;;) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
401 switch (ls->current) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
402 case '\n': case '\r': { /* line breaks */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
403 inclinenumber(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
404 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
405 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
406 case ' ': case '\f': case '\t': case '\v': { /* spaces */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
407 next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
408 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
409 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
410 case '-': { /* '-' or '--' (comment) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
411 next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
412 if (ls->current != '-') return '-';
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
413 /* else is a comment */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
414 next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
415 if (ls->current == '[') { /* long comment? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
416 int sep = skip_sep(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
417 luaZ_resetbuffer(ls->buff); /* `skip_sep' may dirty the buffer */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
418 if (sep >= 0) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
419 read_long_string(ls, NULL, sep); /* skip long comment */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
420 luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
421 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
422 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
423 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
424 /* else short comment */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
425 while (!currIsNewline(ls) && ls->current != EOZ)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
426 next(ls); /* skip until end of line (or end of file) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
427 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
428 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
429 case '[': { /* long string or simply '[' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
430 int sep = skip_sep(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
431 if (sep >= 0) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
432 read_long_string(ls, seminfo, sep);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
433 return TK_STRING;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
434 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
435 else if (sep == -1) return '[';
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
436 else lexerror(ls, "invalid long string delimiter", TK_STRING);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
437 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
438 case '=': {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
439 next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
440 if (ls->current != '=') return '=';
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
441 else { next(ls); return TK_EQ; }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
442 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
443 case '<': {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
444 next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
445 if (ls->current != '=') return '<';
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
446 else { next(ls); return TK_LE; }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
447 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
448 case '>': {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
449 next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
450 if (ls->current != '=') return '>';
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
451 else { next(ls); return TK_GE; }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
452 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
453 case '~': {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
454 next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
455 if (ls->current != '=') return '~';
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
456 else { next(ls); return TK_NE; }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
457 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
458 case ':': {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
459 next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
460 if (ls->current != ':') return ':';
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
461 else { next(ls); return TK_DBCOLON; }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
462 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
463 case '"': case '\'': { /* short literal strings */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
464 read_string(ls, ls->current, seminfo);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
465 return TK_STRING;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
466 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
467 case '.': { /* '.', '..', '...', or number */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
468 save_and_next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
469 if (check_next(ls, ".")) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
470 if (check_next(ls, "."))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
471 return TK_DOTS; /* '...' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
472 else return TK_CONCAT; /* '..' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
473 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
474 else if (!lisdigit(ls->current)) return '.';
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
475 /* else go through */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
476 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
477 case '0': case '1': case '2': case '3': case '4':
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
478 case '5': case '6': case '7': case '8': case '9': {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
479 read_numeral(ls, seminfo);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
480 return TK_NUMBER;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
481 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
482 case EOZ: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
483 return TK_EOS;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
484 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
485 default: {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
486 if (lislalpha(ls->current)) { /* identifier or reserved word? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
487 TString *ts;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
488 do {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
489 save_and_next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
490 } while (lislalnum(ls->current));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
491 ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
492 luaZ_bufflen(ls->buff));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
493 seminfo->ts = ts;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
494 if (isreserved(ts)) /* reserved word? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
495 return ts->tsv.extra - 1 + FIRST_RESERVED;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
496 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
497 return TK_NAME;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
498 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
499 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
500 else { /* single-char tokens (+ - / ...) */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
501 int c = ls->current;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
502 next(ls);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
503 return c;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
504 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
505 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
506 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
507 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
508 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
509
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
510
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
511 void luaX_next (LexState *ls) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
512 ls->lastline = ls->linenumber;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
513 if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
514 ls->t = ls->lookahead; /* use this one */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
515 ls->lookahead.token = TK_EOS; /* and discharge it */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
516 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
517 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
518 ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
519 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
520
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
521
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
522 int luaX_lookahead (LexState *ls) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
523 lua_assert(ls->lookahead.token == TK_EOS);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
524 ls->lookahead.token = llex(ls, &ls->lookahead.seminfo);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
525 return ls->lookahead.token;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
526 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
527