3 // Static table of recognized lexemes in the language
38 {"foreach", TOK_FOREACH},
40 {"return", TOK_RETURN},
42 {"continue", TOK_CONTINUE},
44 {"...", TOK_ELLIPSIS},
52 {"forindex", TOK_FORINDEX},
55 // Build a table of where each line ending is
56 static int* findLines(struct Parser* p)
59 int sz = p->len/10 + 16;
60 int* lines = naParseAlloc(p, (sizeof(int) * sz));
63 for(i=0; i<p->len; i++) {
64 // Not a line ending at all
65 if(buf[i] != '\n' && buf[i] != '\r')
68 // Skip over the \r of a \r\n pair.
69 if(buf[i] == '\r' && (i+1)<p->len && buf[i+1] == '\n') {
72 // Reallocate if necessary
76 nl = naParseAlloc(p, sizeof(int) * sz);
77 for(j=0; j<n; j++) nl[j] = lines[j];
87 // What line number is the index on?
88 static int getLine(struct Parser* p, int index)
91 for(i=0; i<p->nLines; i++)
92 if(p->lines[i] > index)
93 return (p->firstLine-1) + i+1;
94 return (p->firstLine-1) + p->nLines+1;
97 static void error(struct Parser* p, char* msg, int index)
99 naParseError(p, msg, getLine(p, index));
102 // End index (the newline character) of the given line
103 static int lineEnd(struct Parser* p, int line)
105 if(line > p->nLines) return p->len;
106 return p->lines[line-1];
109 static void newToken(struct Parser* p, int pos, int type,
110 char* str, int slen, double num)
114 tok = naParseAlloc(p, sizeof(struct Token));
116 tok->line = getLine(p, pos);
120 tok->parent = &p->tree;
122 tok->prev = p->tree.lastChild;
126 // Context sensitivity hack: a "-" following a binary operator of
127 // higher precedence (MUL and DIV, basically) must be a unary
128 // negation. Needed to get precedence right in the parser for
129 // expressiong like "a * -2"
130 if(type == TOK_MINUS && tok->prev)
131 if(tok->prev->type == TOK_MUL || tok->prev->type == TOK_DIV)
132 tok->type = type = TOK_NEG;
134 if(!p->tree.children) p->tree.children = tok;
135 if(p->tree.lastChild) p->tree.lastChild->next = tok;
136 p->tree.lastChild = tok;
139 static int hex(char c)
141 if(c >= '0' && c <= '9') return c - '0';
142 if(c >= 'A' && c <= 'F') return c - 'A' + 10;
143 if(c >= 'a' && c <= 'f') return c - 'a' + 10;
147 static int hexc(char c, struct Parser* p, int index)
150 if(n < 0) error(p, "bad hex constant", index);
154 // Escape and returns a single backslashed expression in a single
155 // quoted string. Trivial, just escape \' and leave everything else
157 static void sqEscape(char* buf, int len, int index, struct Parser* p,
158 char* cOut, int* eatenOut)
160 if(len < 2) error(p, "unterminated string", index);
170 // Ditto, but more complicated for double quotes.
171 static void dqEscape(char* buf, int len, int index, struct Parser* p,
172 char* cOut, int* eatenOut)
174 if(len < 2) error(p, "unterminated string", index);
177 case '"': *cOut = '"'; break;
178 case 'r': *cOut = '\r'; break;
179 case 'n': *cOut = '\n'; break;
180 case 't': *cOut = '\t'; break;
181 case '\\': *cOut = '\\'; break;
183 if(len < 4) error(p, "unterminated string", index);
184 *cOut = (char)((hexc(buf[2], p, index)<<4) | hexc(buf[3], p, index));
188 // Unhandled, put the backslash back
194 // FIXME: should handle UTF8 too
195 static void charLiteral(struct Parser* p, int index, char* s, int len)
197 if(len != 1) error(p, "character constant not single character", index);
198 newToken(p, index, TOK_LITERAL, 0, 0, *s);
201 // Read in a string literal
202 static int lexStringLiteral(struct Parser* p, int index, char q)
204 int i, j, len, iteration;
208 for(iteration = 0; iteration<2; iteration++) {
216 if(q == '\'') sqEscape(buf+i, p->len-i, i, p, &c, &eaten);
217 else dqEscape(buf+i, p->len-i, i, p, &c, &eaten);
219 if(iteration == 1) out[j++] = c;
223 // Finished stage one -- allocate the buffer for stage two
224 if(iteration == 0) out = naParseAlloc(p, len);
226 if(q == '`') charLiteral(p, index, out, len);
227 else newToken(p, index, TOK_LITERAL, out, len, 0);
231 static int lexHexLiteral(struct Parser* p, int index)
235 while(i < p->len && (nib = hex(p->buf[i])) >= 0) {
239 newToken(p, index, TOK_LITERAL, 0, 0, d);
243 static int lexNumLiteral(struct Parser* p, int index)
245 int len = p->len, i = index;
246 unsigned char* buf = (unsigned char*)p->buf;
249 if(i+1<len && buf[i+1] == 'x') return lexHexLiteral(p, index+2);
251 while(i<len && buf[i] >= '0' && buf[i] <= '9') i++;
252 if(i<len && buf[i] == '.') {
254 while(i<len && buf[i] >= '0' && buf[i] <= '9') i++;
256 if(i<len && (buf[i] == 'e' || buf[i] == 'E')) {
259 && (buf[i] == '-' || buf[i] == '+')
260 && (i+1<len && buf[i+1] >= '0' && buf[i+1] <= '9')) i++;
261 while(i<len && buf[i] >= '0' && buf[i] <= '9') i++;
263 naStr_parsenum(p->buf + index, i - index, &d);
264 newToken(p, index, TOK_LITERAL, 0, 0, d);
268 static int trySymbol(struct Parser* p, int start)
271 while((i < p->len) &&
272 ((p->buf[i] == '_') ||
273 (p->buf[i] >= 'A' && p->buf[i] <= 'Z') ||
274 (p->buf[i] >= 'a' && p->buf[i] <= 'z') ||
275 (p->buf[i] >= '0' && p->buf[i] <= '9')))
280 // Returns the length of lexeme l if the buffer prefix matches, or
282 static int matchLexeme(char* buf, int len, char* l)
285 for(i=0; i<len; i++) {
286 if(l[i] == 0) return i;
287 if(l[i] != buf[i]) return 0;
289 // Ran out of buffer. This is still OK if we're also at the end
291 if(l[i] == 0) return i;
295 // This is dumb and algorithmically slow. It would be much more
296 // elegant to sort and binary search the lexeme list, but that's a lot
297 // more code and this really isn't very slow in practice; it checks
298 // every byte of every lexeme for each input byte. There are less
299 // than 100 bytes of lexemes in the grammar. Returns the number of
300 // bytes in the lexeme read (or zero if none was recognized)
301 static int tryLexemes(struct Parser* p, int index, int* lexemeOut)
303 int i, n, best, bestIndex=-1;
304 char* start = p->buf + index;
305 int len = p->len - index;
307 n = sizeof(LEXEMES) / sizeof(struct Lexeme);
310 int l = matchLexeme(start, len, LEXEMES[i].str);
316 if(best > 0) *lexemeOut = bestIndex;
320 void naLex(struct Parser* p)
327 // Whitespace, comments and string literals have obvious
328 // markers and can be handled by a switch:
331 case ' ': case '\t': case '\n': case '\r': case '\f': case '\v':
335 i = lineEnd(p, getLine(p, i));
337 case '\'': case '"': case '`':
338 i = lexStringLiteral(p, i, c);
341 if(c >= '0' && c <= '9') i = lexNumLiteral(p, i);
345 // Lexemes and symbols are a little more complicated. Pick
346 // the longest one that matches. Since some lexemes look like
347 // symbols (e.g. "or") they need a higher precedence, but we
348 // don't want a lexeme match to clobber the beginning of a
349 // symbol (e.g. "orchid"). If neither match, we have a bad
350 // character in the mix.
352 int symlen=0, lexlen=0, lexeme=-1;
353 lexlen = tryLexemes(p, i, &lexeme);
354 if((c>='A' && c<='Z') || (c>='a' && c<='z') || (c=='_'))
355 symlen = trySymbol(p, i);
356 if(lexlen && lexlen >= symlen) {
357 newToken(p, i, LEXEMES[lexeme].tok, 0, 0, 0);
360 newToken(p, i, TOK_SYMBOL, p->buf+i, symlen, 0);
363 error(p, "illegal character", i);