1 #include "php_psi_stdinc.h"
7 void *psi_parser_proc_init(void);
8 void psi_parser_proc_free(void **parser_proc);
9 void psi_parser_proc_parse(void *parser_proc, token_t r, struct psi_token *token, struct psi_parser *parser);
10 void psi_parser_proc_trace(FILE *out, char *prefix);
12 struct psi_parser *psi_parser_init(struct psi_parser *P, psi_error_cb error, unsigned flags)
15 P = malloc(sizeof(*P));
17 memset(P, 0, sizeof(*P));
19 psi_data_ctor_with_dtors(PSI_DATA(P), error, flags);
23 P->proc = psi_parser_proc_init();
25 if (flags & PSI_DEBUG) {
26 psi_parser_proc_trace(stderr, "PSI> ");
32 bool psi_parser_open_file(struct psi_parser *P, const char *filename)
34 FILE *fp = fopen(filename, "r");
37 P->error(PSI_DATA(P), NULL, PSI_WARNING,
38 "Could not open '%s' for reading: %s",
39 filename, strerror(errno));
43 P->input.type = PSI_PARSE_FILE;
44 P->input.data.file.handle = fp;
51 P->error(PSI_DATA(P), NULL, PSI_WARNING,
52 "Could not stat '%s': %s",
53 filename, strerror(errno));
57 P->input.data.file.buffer = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
58 if (MAP_FAILED == P->input.data.file.buffer) {
59 P->error(PSI_DATA(P), NULL, PSI_WARNING,
60 "Could not map '%s' for reading: %s",
61 filename, strerror(errno));
64 P->input.data.file.length = sb.st_size;
66 P->input.data.file.buffer = malloc(BSIZE);
69 P->file.fn = strdup(filename);
74 bool psi_parser_open_string(struct psi_parser *P, const char *string, size_t length)
76 P->input.type = PSI_PARSE_STRING;
77 P->input.data.string.length = length;
78 if (!(P->input.data.string.buffer = strndup(string, length))) {
82 P->file.fn = strdup("<input>");
87 static ssize_t psi_parser_fill(struct psi_parser *P, size_t n)
89 PSI_DEBUG_PRINT(P, "PSI< Fill: n=%zu (input.type=%d)\n", n, P->input.type);
93 switch (P->input.type) {
95 P->cur = P->tok = P->mrk = P->input.data.file.buffer;
97 P->eof = P->input.data.file.buffer + P->input.data.file.length;
101 P->lim = P->input.data.file.buffer;
105 case PSI_PARSE_STRING:
106 P->cur = P->tok = P->mrk = P->input.data.string.buffer;
107 P->eof = P->input.data.string.buffer + P->input.data.string.length;
112 PSI_DEBUG_PRINT(P, "PSI< Fill: cur=%p lim=%p eof=%p\n", P->cur, P->lim, P->eof);
115 switch (P->input.type) {
116 case PSI_PARSE_STRING:
122 size_t consumed = P->tok - P->buf;
123 size_t reserved = P->lim - P->tok;
124 size_t available = BSIZE - reserved;
128 memmove(P->buf, P->tok, reserved);
135 didread = fread(P->lim, 1, available, P->fp);
137 if (didread < available) {
140 PSI_DEBUG_PRINT(P, "PSI< Fill: consumed=%zu reserved=%zu available=%zu didread=%zu\n",
141 consumed, reserved, available, didread);
147 PSI_DEBUG_PRINT(P, "PSI< Fill: avail=%td\n", P->lim - P->cur);
149 return P->lim - P->cur;
152 void psi_parser_parse(struct psi_parser *P, struct psi_token *T)
155 psi_parser_proc_parse(P->proc, T->type, T, P);
157 psi_parser_proc_parse(P->proc, 0, NULL, P);
161 void psi_parser_dtor(struct psi_parser *P)
163 psi_parser_proc_free(&P->proc);
165 switch (P->input.type) {
167 if (P->input.data.file.buffer) {
169 munmap(P->input.data.file.buffer, P->input.data.file.length);
171 free(P->input.data.file.buffer);
174 if (P->input.data.file.handle) {
175 fclose(P->input.data.file.handle);
179 case PSI_PARSE_STRING:
180 if (P->input.data.string.buffer) {
181 free(P->input.data.string.buffer);
186 psi_data_dtor(PSI_DATA(P));
188 memset(P, 0, sizeof(*P));
191 void psi_parser_free(struct psi_parser **P)
201 #if BSIZE < YYMAXFILL
202 # error BSIZE must be greater than YYMAXFILL
205 #define RETURN(t) do { \
207 PSI_DEBUG_PRINT(P, "PSI< TOKEN: %d %.*s (EOF=%d %s:%u:%u)\n", \
208 P->num, (int) (P->cur-P->tok), P->tok, P->num == PSI_T_EOF, \
209 P->file.fn, P->line, P->col); \
214 P->col += P->cur - P->tok
216 #define NEWLINE(label) \
221 token_t psi_parser_scan(struct psi_parser *P)
224 psi_parser_fill(P, 0);
232 re2c:define:YYCTYPE = "unsigned char";
233 re2c:define:YYCURSOR = P->cur;
234 re2c:define:YYLIMIT = P->lim;
235 re2c:define:YYMARKER = P->mrk;
236 re2c:define:YYFILL = "{ if (!psi_parser_fill(P,@@)) RETURN(PSI_T_EOF); }";
237 re2c:yyfill:parameter = 0;
242 NSNAME = (NAME)? ("\\" NAME)+;
243 DOLLAR_NAME = '$' W+;
244 QUOTED_STRING = "\"" ([^\"])+ "\"";
245 NUMBER = [+-]? [0-9]* "."? [0-9]+ ([eE] [+-]? [0-9]+)?;
247 "/*" { goto comment; }
248 ("#"|"//") .* "\n" { NEWLINE(nextline); }
249 "(" {RETURN(PSI_T_LPAREN);}
250 ")" {RETURN(PSI_T_RPAREN);}
251 ";" {RETURN(PSI_T_EOS);}
252 "," {RETURN(PSI_T_COMMA);}
253 ":" {RETURN(PSI_T_COLON);}
254 "{" {RETURN(PSI_T_LBRACE);}
255 "}" {RETURN(PSI_T_RBRACE);}
256 "[" {RETURN(PSI_T_LBRACKET);}
257 "]" {RETURN(PSI_T_RBRACKET);}
258 "=" {RETURN(PSI_T_EQUALS);}
259 "*" {RETURN(PSI_T_ASTERISK);}
260 "~" {RETURN(PSI_T_TILDE);}
261 "!" {RETURN(PSI_T_NOT);}
262 "%" {RETURN(PSI_T_MODULO);}
263 "&" {RETURN(PSI_T_AMPERSAND);}
264 "+" {RETURN(PSI_T_PLUS);}
265 "-" {RETURN(PSI_T_MINUS);}
266 "/" {RETURN(PSI_T_SLASH);}
267 "|" {RETURN(PSI_T_PIPE);}
268 "^" {RETURN(PSI_T_CARET);}
269 "<<" {RETURN(PSI_T_LSHIFT);}
270 ">>" {RETURN(PSI_T_RSHIFT);}
271 "..." {RETURN(PSI_T_ELLIPSIS);}
272 [\r\n] { NEWLINE(nextline); }
274 'TRUE' {RETURN(PSI_T_TRUE);}
275 'FALSE' {RETURN(PSI_T_FALSE);}
276 'NULL' {RETURN(PSI_T_NULL);}
277 'MIXED' {RETURN(PSI_T_MIXED);}
278 'CALLABLE' {RETURN(PSI_T_CALLABLE);}
279 'VOID' {RETURN(PSI_T_VOID);}
280 'BOOL' {RETURN(PSI_T_BOOL);}
281 'CHAR' {RETURN(PSI_T_CHAR);}
282 'SHORT' {RETURN(PSI_T_SHORT);}
283 'INT' {RETURN(PSI_T_INT);}
284 'LONG' {RETURN(PSI_T_LONG);}
285 'FLOAT' {RETURN(PSI_T_FLOAT);}
286 'DOUBLE' {RETURN(PSI_T_DOUBLE);}
287 'INT8_T' {RETURN(PSI_T_INT8);}
288 'UINT8_T' {RETURN(PSI_T_UINT8);}
289 'INT16_T' {RETURN(PSI_T_INT16);}
290 'UINT16_T' {RETURN(PSI_T_UINT16);}
291 'INT32_T' {RETURN(PSI_T_INT32);}
292 'UINT32_T' {RETURN(PSI_T_UINT32);}
293 'INT64_T' {RETURN(PSI_T_INT64);}
294 'UINT64_T' {RETURN(PSI_T_UINT64);}
295 'UNSIGNED' {RETURN(PSI_T_UNSIGNED);}
296 'SIGNED' {RETURN(PSI_T_SIGNED);}
297 'STRING' {RETURN(PSI_T_STRING);}
298 'ARRAY' {RETURN(PSI_T_ARRAY);}
299 'OBJECT' {RETURN(PSI_T_OBJECT);}
300 'CALLBACK' {RETURN(PSI_T_CALLBACK);}
301 'STATIC' {RETURN(PSI_T_STATIC);}
302 'FUNCTION' {RETURN(PSI_T_FUNCTION);}
303 'TYPEDEF' {RETURN(PSI_T_TYPEDEF);}
304 'STRUCT' {RETURN(PSI_T_STRUCT);}
305 'UNION' {RETURN(PSI_T_UNION);}
306 'ENUM' {RETURN(PSI_T_ENUM);}
307 'CONST' {RETURN(PSI_T_CONST);}
308 'LIB' {RETURN(PSI_T_LIB);}
309 'LET' {RETURN(PSI_T_LET);}
310 'SET' {RETURN(PSI_T_SET);}
311 'RETURN' {RETURN(PSI_T_RETURN);}
312 'FREE' {RETURN(PSI_T_FREE);}
313 'TEMP' {RETURN(PSI_T_TEMP);}
314 'STRLEN' {RETURN(PSI_T_STRLEN);}
315 'STRVAL' {RETURN(PSI_T_STRVAL);}
316 'PATHVAL' {RETURN(PSI_T_PATHVAL);}
317 'INTVAL' {RETURN(PSI_T_INTVAL);}
318 'FLOATVAL' {RETURN(PSI_T_FLOATVAL);}
319 'BOOLVAL' {RETURN(PSI_T_BOOLVAL);}
320 'ARRVAL' {RETURN(PSI_T_ARRVAL);}
321 'OBJVAL' {RETURN(PSI_T_OBJVAL);}
322 'ZVAL' {RETURN(PSI_T_ZVAL);}
323 'COUNT' {RETURN(PSI_T_COUNT);}
324 'CALLOC' {RETURN(PSI_T_CALLOC);}
325 'TO_OBJECT' {RETURN(PSI_T_TO_OBJECT);}
326 'TO_ARRAY' {RETURN(PSI_T_TO_ARRAY);}
327 'TO_STRING' {RETURN(PSI_T_TO_STRING);}
328 'TO_INT' {RETURN(PSI_T_TO_INT);}
329 'TO_FLOAT' {RETURN(PSI_T_TO_FLOAT);}
330 'TO_BOOL' {RETURN(PSI_T_TO_BOOL);}
331 NUMBER {RETURN(PSI_T_NUMBER);}
332 NAME {RETURN(PSI_T_NAME);}
333 NSNAME {RETURN(PSI_T_NSNAME);}
334 DOLLAR_NAME {RETURN(PSI_T_DOLLAR_NAME);}
335 QUOTED_STRING {RETURN(PSI_T_QUOTED_STRING);}
342 "\n" { NEWLINE(comment); }
343 "*" "/" { continue; }
344 [^] { goto comment; }