flush
[m6w6/ext-psi] / src / parser.re
1 #include <stdio.h>
2 #include <assert.h>
3
4 #include "parser.h"
5 #include "parser_proc.h"
6
7 void *PSI_ParserProcAlloc(void*(unsigned long));
8 void PSI_ParserProcFree(void*, void(*)(void*));
9 void PSI_ParserProc(void *, token_t, PSI_Token *, PSI_Parser *);
10 void PSI_ParserProcTrace(FILE *, const char*);
11
12 PSI_Parser *PSI_ParserInit(PSI_Parser *P, const char *filename, psi_error_cb error, unsigned flags)
13 {
14 FILE *fp;
15
16 if (!P) {
17 P = malloc(sizeof(*P));
18 }
19 memset(P, 0, sizeof(*P));
20
21 fp = fopen(filename, "r");
22
23 if (!fp) {
24 perror(filename);
25 return NULL;
26 }
27
28 if (!P) {
29 P = malloc(sizeof(*P));
30 }
31 memset(P, 0, sizeof(*P));
32
33 P->fp = fp;
34 P->fn = strdup(filename);
35 P->line = 1;
36 P->error = error;
37 P->flags = flags;
38
39 P->proc = PSI_ParserProcAlloc(malloc);
40 if (flags & PSI_PARSER_DEBUG) {
41 PSI_ParserProcTrace(stderr, "PSI> ");
42 }
43
44 PSI_ParserFill(P, 0);
45
46 return P;
47 }
48
49 void PSI_ParserSyntaxError(PSI_Parser *P, const char *fn, size_t ln, const char *msg, ...) {
50 char buf[0x1000] = {0};
51 va_list argv;
52
53 va_start(argv, msg);
54 vsnprintf(buf, 0x1000-1, msg, argv);
55 va_end(argv);
56
57 P->error(PSI_WARNING, "PSI syntax error on line %zu in '%s'%s%s",
58 ln, fn, msg ? ": ": "", buf);
59
60 ++P->errors;
61 }
62
63 size_t PSI_ParserFill(PSI_Parser *P, size_t n)
64 {
65 if (P->flags & PSI_PARSER_DEBUG) {
66 fprintf(stderr, "PSI> Fill: n=%zu\n", n);
67 }
68 if (!n) {
69 P->cur = P->tok = P->lim = P->mrk = P->buf;
70 P->eof = NULL;
71 }
72
73 if (!P->eof) {
74 size_t consumed = P->tok - P->buf;
75 size_t reserved = P->lim - P->tok;
76 size_t available = BSIZE - reserved;
77 size_t didread;
78
79 if (consumed) {
80 memmove(P->buf, P->tok, reserved);
81 P->tok -= consumed;
82 P->cur -= consumed;
83 P->lim -= consumed;
84 P->mrk -= consumed;
85 }
86
87 didread = fread(P->lim, 1, available, P->fp);
88 P->lim += didread;
89 if (didread < available) {
90 P->eof = P->lim;
91 }
92
93 if (P->flags & PSI_PARSER_DEBUG) {
94 fprintf(stderr, "PSI> Fill: consumed=%zu reserved=%zu available=%zu didread=%zu\n",
95 consumed, reserved, available, didread);
96 }
97 }
98 if (P->flags & PSI_PARSER_DEBUG) {
99 fprintf(stderr, "PSI> Fill: avail=%zu\n", P->lim - P->cur);
100 }
101 return P->lim - P->cur;
102 }
103
104 void PSI_ParserParse(PSI_Parser *P, PSI_Token *T)
105 {
106 if (T) {
107 PSI_ParserProc(P->proc, T->type, T, P);
108 } else {
109 PSI_ParserProc(P->proc, 0, NULL, P);
110 }
111 }
112
113 void PSI_ParserDtor(PSI_Parser *P)
114 {
115 PSI_ParserProcFree(P->proc, free);
116
117 if (P->fp) {
118 fclose(P->fp);
119 }
120
121 PSI_DataDtor((PSI_Data *) P);
122
123 memset(P, 0, sizeof(*P));
124 }
125
126 void PSI_ParserFree(PSI_Parser **P)
127 {
128 if (*P) {
129 PSI_ParserDtor(*P);
130 free(*P);
131 *P = NULL;
132 }
133 }
134
135 /*!max:re2c*/
136 #define BSIZE 256
137
138 #if BSIZE < YYMAXFILL
139 # error BSIZE must be greater than YYMAXFILL
140 #endif
141
142 #define RETURN(t) do { \
143 P->num = t; \
144 return t; \
145 } while(1)
146
147 /* DIGIT = [0-9]
148 DIGITS = DIGIT+
149 DECIMALS = (+|-)? DIGIT* "."
150 digits ::= digits DIGIT.
151 decimals ::= digits DOT digits.
152 decimals ::= DOT digits.
153 decimals ::= digits DOT.
154 number ::= digits.
155 number ::= PLUS digits.
156 number ::= MINUS digits.
157 number ::= decimals.
158 number ::= MINUS decimals.
159 number ::= PLUS decimals.
160
161 */
162 token_t PSI_ParserScan(PSI_Parser *P)
163 {
164 for (;;) {
165 P->tok = P->cur;
166 /*!re2c
167 re2c:indent:top = 2;
168 re2c:define:YYCTYPE = "unsigned char";
169 re2c:define:YYCURSOR = P->cur;
170 re2c:define:YYLIMIT = P->lim;
171 re2c:define:YYMARKER = P->mrk;
172 re2c:define:YYFILL = "{ if (!PSI_ParserFill(P,@@)) RETURN(-1); }";
173 re2c:yyfill:parameter = 0;
174
175 B = [^a-zA-Z0-9_];
176 W = [a-zA-Z0-9_];
177 NAME = [a-zA-Z_]W*;
178 NSNAME = (NAME)? ("\\" NAME)+;
179 QUOTED_STRING = "\"" ([^\"])+ "\"";
180 NUMBER = [+-]? [0-9]* "."? [0-9]+ ([eE] [+-]? [0-9]+)?;
181
182 "#" .* "\n" { ++P->line; RETURN(PSI_T_COMMENT);}
183 "(" {RETURN(PSI_T_LPAREN);}
184 ")" {RETURN(PSI_T_RPAREN);}
185 ";" {RETURN(PSI_T_EOS);}
186 "," {RETURN(PSI_T_COMMA);}
187 ":" {RETURN(PSI_T_COLON);}
188 "{" {RETURN(PSI_T_LBRACE);}
189 "}" {RETURN(PSI_T_RBRACE);}
190 "[" {RETURN(PSI_T_LBRACKET);}
191 "]" {RETURN(PSI_T_RBRACKET);}
192 "=" {RETURN(PSI_T_EQUALS);}
193 "$" {RETURN(PSI_T_DOLLAR);}
194 "*" {RETURN(PSI_T_POINTER);}
195 "&" {RETURN(PSI_T_REFERENCE);}
196 [\r\n] { ++P->line; continue; }
197 [\t ]+ { continue; }
198 'TRUE' {RETURN(PSI_T_TRUE);}
199 'FALSE' {RETURN(PSI_T_FALSE);}
200 'NULL' {RETURN(PSI_T_NULL);}
201 'MIXED' {RETURN(PSI_T_MIXED);}
202 'VOID' {RETURN(PSI_T_VOID);}
203 'BOOL' {RETURN(PSI_T_BOOL);}
204 'INT' {RETURN(PSI_T_INT);}
205 'FLOAT' {RETURN(PSI_T_FLOAT);}
206 'DOUBLE' {RETURN(PSI_T_DOUBLE);}
207 'INT8_T' {RETURN(PSI_T_SINT8);}
208 'UINT8_T' {RETURN(PSI_T_UINT8);}
209 'INT16_T' {RETURN(PSI_T_SINT16);}
210 'UINT16_T' {RETURN(PSI_T_UINT16);}
211 'INT32_T' {RETURN(PSI_T_SINT32);}
212 'UINT32_T' {RETURN(PSI_T_UINT32);}
213 'INT64_T' {RETURN(PSI_T_SINT64);}
214 'UINT64_T' {RETURN(PSI_T_UINT64);}
215 'STRING' {RETURN(PSI_T_STRING);}
216 'ARRAY' {RETURN(PSI_T_ARRAY);}
217 'FUNCTION' {RETURN(PSI_T_FUNCTION);}
218 'TYPEDEF' {RETURN(PSI_T_TYPEDEF);}
219 'STRUCT' {RETURN(PSI_T_STRUCT);}
220 'CONST' {RETURN(PSI_T_CONST);}
221 'LIB' {RETURN(PSI_T_LIB);}
222 'LET' {RETURN(PSI_T_LET);}
223 'SET' {RETURN(PSI_T_SET);}
224 'RETURN' {RETURN(PSI_T_RETURN);}
225 'FREE' {RETURN(PSI_T_FREE);}
226 'STRLEN' {RETURN(PSI_T_STRLEN);}
227 'STRVAL' {RETURN(PSI_T_STRVAL);}
228 'INTVAL' {RETURN(PSI_T_INTVAL);}
229 'FLOATVAL' {RETURN(PSI_T_FLOATVAL);}
230 'BOOLVAL' {RETURN(PSI_T_BOOLVAL);}
231 'ARRVAL' {RETURN(PSI_T_ARRVAL);}
232 'CALLOC' {RETURN(PSI_T_CALLOC);}
233 'TO_ARRAY' {RETURN(PSI_T_TO_ARRAY);}
234 'TO_STRING' {RETURN(PSI_T_TO_STRING);}
235 'TO_INT' {RETURN(PSI_T_TO_INT);}
236 'TO_FLOAT' {RETURN(PSI_T_TO_FLOAT);}
237 'TO_BOOL' {RETURN(PSI_T_TO_BOOL);}
238 NUMBER {RETURN(PSI_T_NUMBER);}
239 NAME {RETURN(PSI_T_NAME);}
240 NSNAME {RETURN(PSI_T_NSNAME);}
241 QUOTED_STRING {RETURN(PSI_T_QUOTED_STRING);}
242 [^] {break;}
243 */
244 }
245 return -1;
246 }