3 // Static table of recognized lexemes in the language
38 {"foreach", TOK_FOREACH},
40 {"return", TOK_RETURN},
42 {"continue", TOK_CONTINUE},
46 // Build a table of where each line ending is
47 static int* findLines(struct Parser* p)
50 int sz = p->len/10 + 16;
51 int* lines = naParseAlloc(p, (sizeof(int) * sz));
54 for(i=0; i<p->len; i++) {
55 // Not a line ending at all
56 if(buf[i] != '\n' && buf[i] != '\r')
59 // Skip over the \r of a \r\n pair.
60 if(buf[i] == '\r' && (i+1)<p->len && buf[i+1] == '\n') {
64 // Reallocate if necessary
68 nl = naParseAlloc(p, sizeof(int) * sz);
69 for(j=0; j<n; j++) nl[j] = lines[j];
79 // What line number is the index on?
80 static int getLine(struct Parser* p, int index)
83 for(i=0; i<p->nLines; i++)
84 if(p->lines[i] > index)
85 return (p->firstLine-1) + i+1;
86 return (p->firstLine-1) + p->nLines+1;
89 static void error(struct Parser* p, char* msg, int index)
91 naParseError(p, msg, getLine(p, index));
94 // End index (the newline character) of the given line
95 static int lineEnd(struct Parser* p, int line)
97 if(line > p->nLines) return p->len;
98 return p->lines[line-1];
101 static void newToken(struct Parser* p, int pos, int type,
102 char* str, int slen, double num)
106 tok = naParseAlloc(p, sizeof(struct Token));
108 tok->line = getLine(p, pos);
112 tok->parent = &p->tree;
114 tok->prev = p->tree.lastChild;
118 // Context sensitivity hack: a "-" following a binary operator of
119 // higher precedence (MUL and DIV, basically) must be a unary
120 // negation. Needed to get precedence right in the parser for
121 // expressiong like "a * -2"
122 if(type == TOK_MINUS && tok->prev)
123 if(tok->prev->type == TOK_MUL || tok->prev->type == TOK_DIV)
124 tok->type = type = TOK_NEG;
126 if(!p->tree.children) p->tree.children = tok;
127 if(p->tree.lastChild) p->tree.lastChild->next = tok;
128 p->tree.lastChild = tok;
131 // Parse a hex nibble
132 static int hexc(char c, struct Parser* p, int index)
134 if(c >= '0' && c <= '9') return c - '0';
135 if(c >= 'A' && c <= 'F') return c - 'a' + 10;
136 if(c >= 'a' && c <= 'f') return c - 'a' + 10;
137 error(p, "bad hex constant", index);
141 // Escape and returns a single backslashed expression in a single
142 // quoted string. Trivial, just escape \' and leave everything else
144 static void sqEscape(char* buf, int len, int index, struct Parser* p,
145 char* cOut, int* eatenOut)
147 if(len < 2) error(p, "unterminated string", index);
157 // Ditto, but more complicated for double quotes.
158 static void dqEscape(char* buf, int len, int index, struct Parser* p,
159 char* cOut, int* eatenOut)
161 if(len < 2) error(p, "unterminated string", index);
164 case '"': *cOut = '"'; break;
165 case 'r': *cOut = '\r'; break;
166 case 'n': *cOut = '\n'; break;
167 case 't': *cOut = '\t'; break;
168 case '\\': *cOut = '\\'; break;
170 if(len < 4) error(p, "unterminated string", index);
171 *cOut = (char)((hexc(buf[2], p, index)<<4) | hexc(buf[3], p, index));
174 // Unhandled, put the backslash back
180 // Read in a string literal
181 static int lexStringLiteral(struct Parser* p, int index, int singleQuote)
183 int i, j, len, iteration;
186 char endMark = singleQuote ? '\'' : '"';
188 for(iteration = 0; iteration<2; iteration++) {
197 if(singleQuote) sqEscape(buf+i, p->len-i, i, p, &c, &eaten);
198 else dqEscape(buf+i, p->len-i, i, p, &c, &eaten);
200 if(iteration == 1) out[j++] = c;
204 // Finished stage one -- allocate the buffer for stage two
205 if(iteration == 0) out = naParseAlloc(p, len);
207 newToken(p, index, TOK_LITERAL, out, len, 0);
211 static int lexNumLiteral(struct Parser* p, int index)
213 int len = p->len, i = index;
214 unsigned char* buf = p->buf;
217 while(i<len && buf[i] >= '0' && buf[i] <= '9') i++;
218 if(i<len && buf[i] == '.') {
220 while(i<len && buf[i] >= '0' && buf[i] <= '9') i++;
222 if(i<len && (buf[i] == 'e' || buf[i] == 'E')) {
225 && (buf[i] == '-' || buf[i] == '+')
226 && (i+1<len && buf[i+1] >= '0' && buf[i+1] <= '9')) i++;
227 while(i<len && buf[i] >= '0' && buf[i] <= '9') i++;
229 naStr_parsenum(p->buf + index, i - index, &d);
230 newToken(p, index, TOK_LITERAL, 0, 0, d);
234 static int trySymbol(struct Parser* p, int start)
237 while((i < p->len) &&
238 ((p->buf[i] >= 'A' && p->buf[i] <= 'Z') ||
239 (p->buf[i] >= 'a' && p->buf[i] <= 'z') ||
240 (p->buf[i] >= '0' && p->buf[i] <= '9')))
245 // Returns the length of lexeme l if the buffer prefix matches, or
247 static int matchLexeme(char* buf, int len, char* l)
250 for(i=0; i<len; i++) {
251 if(l[i] == 0) return i;
252 if(l[i] != buf[i]) return 0;
254 // Ran out of buffer. This is still OK if we're also at the end
256 if(l[i] == 0) return i;
260 // This is dumb and algorithmically slow. It would be much more
261 // elegant to sort and binary search the lexeme list, but that's a lot
262 // more code and this really isn't very slow in practice; it checks
263 // every byte of every lexeme for each input byte. There are less
264 // than 100 bytes of lexemes in the grammar. Returns the number of
265 // bytes in the lexeme read (or zero if none was recognized)
266 static int tryLexemes(struct Parser* p, int index, int* lexemeOut)
268 int i, n, best, bestIndex=-1;
269 char* start = p->buf + index;
270 int len = p->len - index;
272 n = sizeof(LEXEMES) / sizeof(struct Lexeme);
275 int l = matchLexeme(start, len, LEXEMES[i].str);
281 if(best > 0) *lexemeOut = bestIndex;
285 void naLex(struct Parser* p)
292 // Whitespace, comments and string literals have obvious
293 // markers and can be handled by a switch:
296 case ' ': case '\t': case '\n': case '\r': case '\f': case '\v':
300 i = lineEnd(p, getLine(p, i));
303 i = lexStringLiteral(p, i, (c=='"' ? 0 : 1));
306 if(c >= '0' && c <= '9') i = lexNumLiteral(p, i);
310 // Lexemes and symbols are a little more complicated. Pick
311 // the longest one that matches. Since some lexemes look like
312 // symbols (e.g. "or") they need a higher precedence, but we
313 // don't want a lexeme match to clobber the beginning of a
314 // symbol (e.g. "orchid"). If neither match, we have a bad
315 // character in the mix.
317 int symlen=0, lexlen=0, lexeme;
318 lexlen = tryLexemes(p, i, &lexeme);
319 if((c>='A' && c<='Z') || (c>='a' && c<='z'))
320 symlen = trySymbol(p, i);
321 if(lexlen && lexlen >= symlen) {
322 newToken(p, i, LEXEMES[lexeme].tok, 0, 0, 0);
325 newToken(p, i, TOK_SYMBOL, p->buf+i, symlen, 0);
328 error(p, "illegal character", i);