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 // Parse a hex nibble
140 static int hexc(char c, struct Parser* p, int index)
142 if(c >= '0' && c <= '9') return c - '0';
143 if(c >= 'A' && c <= 'F') return c - 'A' + 10;
144 if(c >= 'a' && c <= 'f') return c - 'a' + 10;
145 error(p, "bad hex constant", index);
149 // Escape and returns a single backslashed expression in a single
150 // quoted string. Trivial, just escape \' and leave everything else
152 static void sqEscape(char* buf, int len, int index, struct Parser* p,
153 char* cOut, int* eatenOut)
155 if(len < 2) error(p, "unterminated string", index);
165 // Ditto, but more complicated for double quotes.
166 static void dqEscape(char* buf, int len, int index, struct Parser* p,
167 char* cOut, int* eatenOut)
169 if(len < 2) error(p, "unterminated string", index);
172 case '"': *cOut = '"'; break;
173 case 'r': *cOut = '\r'; break;
174 case 'n': *cOut = '\n'; break;
175 case 't': *cOut = '\t'; break;
176 case '\\': *cOut = '\\'; break;
178 if(len < 4) error(p, "unterminated string", index);
179 *cOut = (char)((hexc(buf[2], p, index)<<4) | hexc(buf[3], p, index));
183 // Unhandled, put the backslash back
189 // Read in a string literal
190 static int lexStringLiteral(struct Parser* p, int index, int singleQuote)
192 int i, j, len, iteration;
195 char endMark = singleQuote ? '\'' : '"';
197 for(iteration = 0; iteration<2; iteration++) {
206 if(singleQuote) sqEscape(buf+i, p->len-i, i, p, &c, &eaten);
207 else dqEscape(buf+i, p->len-i, i, p, &c, &eaten);
209 if(iteration == 1) out[j++] = c;
213 // Finished stage one -- allocate the buffer for stage two
214 if(iteration == 0) out = naParseAlloc(p, len);
216 newToken(p, index, TOK_LITERAL, out, len, 0);
220 static int lexNumLiteral(struct Parser* p, int index)
222 int len = p->len, i = index;
223 unsigned char* buf = p->buf;
226 while(i<len && buf[i] >= '0' && buf[i] <= '9') i++;
227 if(i<len && buf[i] == '.') {
229 while(i<len && buf[i] >= '0' && buf[i] <= '9') i++;
231 if(i<len && (buf[i] == 'e' || buf[i] == 'E')) {
234 && (buf[i] == '-' || buf[i] == '+')
235 && (i+1<len && buf[i+1] >= '0' && buf[i+1] <= '9')) i++;
236 while(i<len && buf[i] >= '0' && buf[i] <= '9') i++;
238 naStr_parsenum(p->buf + index, i - index, &d);
239 newToken(p, index, TOK_LITERAL, 0, 0, d);
243 static int trySymbol(struct Parser* p, int start)
246 while((i < p->len) &&
247 ((p->buf[i] == '_') ||
248 (p->buf[i] >= 'A' && p->buf[i] <= 'Z') ||
249 (p->buf[i] >= 'a' && p->buf[i] <= 'z') ||
250 (p->buf[i] >= '0' && p->buf[i] <= '9')))
255 // Returns the length of lexeme l if the buffer prefix matches, or
257 static int matchLexeme(char* buf, int len, char* l)
260 for(i=0; i<len; i++) {
261 if(l[i] == 0) return i;
262 if(l[i] != buf[i]) return 0;
264 // Ran out of buffer. This is still OK if we're also at the end
266 if(l[i] == 0) return i;
270 // This is dumb and algorithmically slow. It would be much more
271 // elegant to sort and binary search the lexeme list, but that's a lot
272 // more code and this really isn't very slow in practice; it checks
273 // every byte of every lexeme for each input byte. There are less
274 // than 100 bytes of lexemes in the grammar. Returns the number of
275 // bytes in the lexeme read (or zero if none was recognized)
276 static int tryLexemes(struct Parser* p, int index, int* lexemeOut)
278 int i, n, best, bestIndex=-1;
279 char* start = p->buf + index;
280 int len = p->len - index;
282 n = sizeof(LEXEMES) / sizeof(struct Lexeme);
285 int l = matchLexeme(start, len, LEXEMES[i].str);
291 if(best > 0) *lexemeOut = bestIndex;
295 void naLex(struct Parser* p)
302 // Whitespace, comments and string literals have obvious
303 // markers and can be handled by a switch:
306 case ' ': case '\t': case '\n': case '\r': case '\f': case '\v':
310 i = lineEnd(p, getLine(p, i));
313 i = lexStringLiteral(p, i, (c=='"' ? 0 : 1));
316 if(c >= '0' && c <= '9') i = lexNumLiteral(p, i);
320 // Lexemes and symbols are a little more complicated. Pick
321 // the longest one that matches. Since some lexemes look like
322 // symbols (e.g. "or") they need a higher precedence, but we
323 // don't want a lexeme match to clobber the beginning of a
324 // symbol (e.g. "orchid"). If neither match, we have a bad
325 // character in the mix.
327 int symlen=0, lexlen=0, lexeme;
328 lexlen = tryLexemes(p, i, &lexeme);
329 if((c>='A' && c<='Z') || (c>='a' && c<='z') || (c=='_'))
330 symlen = trySymbol(p, i);
331 if(lexlen && lexlen >= symlen) {
332 newToken(p, i, LEXEMES[lexeme].tok, 0, 0, 0);
335 newToken(p, i, TOK_SYMBOL, p->buf+i, symlen, 0);
338 error(p, "illegal character", i);