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)
112 struct Token *tok, *last = p->tree.lastChild;
114 /* Adjacent string literals get concatenated */
115 if(type == TOK_LITERAL && str) {
116 if(last && last->type == TOK_LITERAL) {
117 int i, len1 = last->strlen;
118 char* str2 = naParseAlloc(p, len1 + slen);
119 for(i=0; i<len1; i++) str2[i] = last->str[i];
120 for(i=0; i<slen; i++) str2[i+len1] = str[i];
122 last->strlen += slen;
127 tok = naParseAlloc(p, sizeof(struct Token));
129 tok->line = getLine(p, pos);
133 tok->parent = &p->tree;
139 // Context sensitivity hack: a "-" following a binary operator of
140 // equal or higher precedence must be a unary negation. Needed to
141 // get precedence right in the parser for expressiong like "a * -2"
142 if(type == TOK_MINUS && tok->prev) {
143 int pt = tok->prev->type;
144 if(pt==TOK_PLUS||pt==TOK_MINUS||pt==TOK_CAT||pt==TOK_MUL||pt==TOK_DIV)
145 tok->type = type = TOK_NEG;
148 if(!p->tree.children) p->tree.children = tok;
149 if(p->tree.lastChild) p->tree.lastChild->next = tok;
150 p->tree.lastChild = tok;
153 static int hex(char c)
155 if(c >= '0' && c <= '9') return c - '0';
156 if(c >= 'A' && c <= 'F') return c - 'A' + 10;
157 if(c >= 'a' && c <= 'f') return c - 'a' + 10;
161 static int hexc(char c, struct Parser* p, int index)
164 if(n < 0) error(p, "bad hex constant", index);
168 // Escape and returns a single backslashed expression in a single
169 // quoted string. Trivial, just escape \' and leave everything else
171 static void sqEscape(char* buf, int len, int index, struct Parser* p,
172 char* cOut, int* eatenOut)
174 if(len < 2) error(p, "unterminated string", index);
184 // Ditto, but more complicated for double quotes.
185 static void dqEscape(char* buf, int len, int index, struct Parser* p,
186 char* cOut, int* eatenOut)
188 if(len < 2) error(p, "unterminated string", index);
191 case '"': *cOut = '"'; break;
192 case 'r': *cOut = '\r'; break;
193 case 'n': *cOut = '\n'; break;
194 case 't': *cOut = '\t'; break;
195 case '\\': *cOut = '\\'; break;
196 case '`': *cOut = '`'; break;
198 if(len < 4) error(p, "unterminated string", index);
199 *cOut = (char)((hexc(buf[2], p, index)<<4) | hexc(buf[3], p, index));
203 // Unhandled, put the backslash back
209 static void charLiteral(struct Parser* p, int index, char* s, int len)
212 c = naLexUtf8C(s, len, &n);
213 if(c < 0 || n != len) error(p, "invalid utf8 character constant", index);
214 newToken(p, index, TOK_LITERAL, 0, 0, c);
217 // Read in a string literal
218 static int lexStringLiteral(struct Parser* p, int index, char q)
220 int i, j, len, iteration;
224 for(iteration = 0; iteration<2; iteration++) {
232 if(q == '\'') sqEscape(buf+i, p->len-i, i, p, &c, &eaten);
233 else dqEscape(buf+i, p->len-i, i, p, &c, &eaten);
235 if(iteration == 1) out[j++] = c;
239 // Finished stage one -- allocate the buffer for stage two
240 if(iteration == 0) out = naParseAlloc(p, len);
242 if(q == '`') charLiteral(p, index, out, len);
243 else newToken(p, index, TOK_LITERAL, out, len, 0);
247 static int lexHexLiteral(struct Parser* p, int index)
251 while(i < p->len && (nib = hex(p->buf[i])) >= 0) {
255 newToken(p, index, TOK_LITERAL, 0, 0, d);
259 static int lexNumLiteral(struct Parser* p, int index)
261 int len = p->len, i = index;
262 unsigned char* buf = (unsigned char*)p->buf;
265 if(i+1<len && buf[i+1] == 'x') return lexHexLiteral(p, index+2);
267 while(i<len && buf[i] >= '0' && buf[i] <= '9') i++;
268 if(i<len && buf[i] == '.') {
270 while(i<len && buf[i] >= '0' && buf[i] <= '9') i++;
272 if(i<len && (buf[i] == 'e' || buf[i] == 'E')) {
275 && (buf[i] == '-' || buf[i] == '+')
276 && (i+1<len && buf[i+1] >= '0' && buf[i+1] <= '9')) i++;
277 while(i<len && buf[i] >= '0' && buf[i] <= '9') i++;
279 naStr_parsenum(p->buf + index, i - index, &d);
280 newToken(p, index, TOK_LITERAL, 0, 0, d);
284 static int trySymbol(struct Parser* p, int start)
287 while((i < p->len) &&
288 ((p->buf[i] == '_') ||
289 (p->buf[i] >= 'A' && p->buf[i] <= 'Z') ||
290 (p->buf[i] >= 'a' && p->buf[i] <= 'z') ||
291 (p->buf[i] >= '0' && p->buf[i] <= '9')))
296 // Returns the length of lexeme l if the buffer prefix matches, or
298 static int matchLexeme(char* buf, int len, char* l)
301 for(i=0; i<len; i++) {
302 if(l[i] == 0) return i;
303 if(l[i] != buf[i]) return 0;
305 // Ran out of buffer. This is still OK if we're also at the end
307 if(l[i] == 0) return i;
311 // This is dumb and algorithmically slow. It would be much more
312 // elegant to sort and binary search the lexeme list, but that's a lot
313 // more code and this really isn't very slow in practice; it checks
314 // every byte of every lexeme for each input byte. There are less
315 // than 100 bytes of lexemes in the grammar. Returns the number of
316 // bytes in the lexeme read (or zero if none was recognized)
317 static int tryLexemes(struct Parser* p, int index, int* lexemeOut)
319 int i, n, best, bestIndex=-1;
320 char* start = p->buf + index;
321 int len = p->len - index;
323 n = sizeof(LEXEMES) / sizeof(struct Lexeme);
326 int l = matchLexeme(start, len, LEXEMES[i].str);
332 if(best > 0) *lexemeOut = bestIndex;
336 #define ISNUM(c) ((c) >= '0' && (c) <= '9')
337 void naLex(struct Parser* p)
344 // Whitespace, comments and string literals have obvious
345 // markers and can be handled by a switch:
348 case ' ': case '\t': case '\n': case '\r': case '\f': case '\v':
352 i = lineEnd(p, getLine(p, i));
354 case '\'': case '"': case '`':
355 i = lexStringLiteral(p, i, c);
358 if(ISNUM(c) || (c == '.' && (i+1)<p->len && ISNUM(p->buf[i+1])))
359 i = lexNumLiteral(p, i);
363 // Lexemes and symbols are a little more complicated. Pick
364 // the longest one that matches. Since some lexemes look like
365 // symbols (e.g. "or") they need a higher precedence, but we
366 // don't want a lexeme match to clobber the beginning of a
367 // symbol (e.g. "orchid"). If neither match, we have a bad
368 // character in the mix.
370 int symlen=0, lexlen=0, lexeme=-1;
371 lexlen = tryLexemes(p, i, &lexeme);
372 if((c>='A' && c<='Z') || (c>='a' && c<='z') || (c=='_'))
373 symlen = trySymbol(p, i);
374 if(lexlen && lexlen >= symlen) {
375 newToken(p, i, LEXEMES[lexeme].tok, 0, 0, 0);
378 newToken(p, i, TOK_SYMBOL, p->buf+i, symlen, 0);
381 error(p, "illegal character", i);