bitwise
lex.re
1#include "util.h"
2#include "kav.h"
3
4static void settok(struct lexer *l, struct token *t, enum tokenkind k) {
5 t->kind = k;
6 t->str = l->tok;
7 /*we moved past the matched text already */
8 t->length = l->cur - l->tok;
9 t->loc.line = l->line;
10 t->loc.col = l->tok - l->linestart + 1;
11 t->loc.file = l->file;
12 if(k != TNONE && k != TDOC)
13 l->afteruse = k == TUSE;
14}
15
16void lex(struct lexer *l, struct token *t) {
17 loop:
18 l->tok = l->cur;
19 if(l->afteruse && *l->cur == '<')
20 goto usepath;
21 /*!re2c
22 re2c:define:YYCTYPE = char;
23 re2c:define:YYCURSOR = "l->cur";
24 re2c:define:YYMARKER = "l->mar";
25 re2c:yyfill:enable = 0;
26
27 [ \t\r]+ { goto loop; }
28 "\n" { l->line++; l->linestart = l->cur; goto loop; }
29 "\x00" { settok(l, t, TEOF); return; }
30 "//" [^\n\x00]* { goto loop; }
31 "/*" { settok(l, t, TNONE); goto comment; }
32 "/%" { settok(l, t, TDOC); goto doc; }
33
34 //I LOVE MACROHACKS
35 !include "rules.re";
36
37 [a-zA-Z_] [a-zA-Z_0-9]* { settok(l, t, TIDENT); return; }
38 [0-9]+ { settok(l, t, TNUMBER); return; }
39 '"' ( [^"\n\x00\\] | "\\" [^\n\x00] )* '"' { settok(l, t, TSTRING); return; }
40 '"' ( [^"\n\x00\\] | "\\" [^\n\x00] )* { settok(l, t, TSTRING); errorat(&t->loc, "unterminated string"); }
41 "'" ( [^'\n\x00\\] | "\\" [ntr0\\'] ) "'" { settok(l, t, TCHAR); return; }
42 "'" { settok(l, t, TNONE); errorat(&t->loc, "bad character literal"); }
43
44 * { settok(l, t, TNONE); errorat(&t->loc, "unexpected character"); }
45 */
46 comment:
47 /*!re2c
48 "*/" { goto loop; }
49 "\n" { l->line++; l->linestart = l->cur; goto comment; }
50 "\x00" { errorat(&t->loc, "unterminated comment"); }
51 * { goto comment; }
52 */
53 doc:
54 /*!re2c
55 "%/" { t->str = l->tok + 2; t->length = l->cur - t->str - 2; return; }
56 "\n" { l->line++; l->linestart = l->cur; goto doc; }
57 "\x00" { errorat(&t->loc, "unterminated doc comment"); }
58 * { goto doc; }
59 */
60 usepath:
61 /*!re2c
62 "<" [^>\n\x00]* ">" { settok(l, t, TUSEPATH); if(t->length == 2) errorat(&t->loc, "empty module path"); return; }
63 "<" [^>\n\x00]* { settok(l, t, TUSEPATH); errorat(&t->loc, "unterminated module path"); }
64 * { settok(l, t, TNONE); errorat(&t->loc, "expected system module path"); }
65 */
66}
67
68static const char *const tokname[] = {
69#define TOKEN(t, s) #t,
70#include "tokens.h"
71#undef TOKEN
72};
73
74static const char *const tokspell[] = {
75#define TOKEN(t, s) "" s,
76#include "tokens.h"
77#undef TOKEN
78};
79
80
81const char *tokstr(enum tokenkind k){
82 return tokname[k];
83
84}
85
86const char *tokwhat(enum tokenkind k){
87 return tokspell[k];
88
89}