8 #include "parser_proc.h"
10 void *PSI_ParserProcAlloc(void*(unsigned long));
11 void PSI_ParserProcFree(void*, void(*)(void*));
12 void PSI_ParserProc(void *, token_t, PSI_Token *, PSI_Parser *);
13 void PSI_ParserProcTrace(FILE *, const char*);
15 PSI_Parser *PSI_ParserInit(PSI_Parser *P, const char *filename, psi_error_cb error, unsigned flags)
19 fp = fopen(filename, "r");
22 if (!(flags & PSI_PARSER_SILENT)) {
23 error(NULL, NULL, PSI_WARNING, "Could not open '%s' for reading: %s",
24 filename, strerror(errno));
30 P = malloc(sizeof(*P));
32 memset(P, 0, sizeof(*P));
34 P->psi.file.fn = strdup(filename);
41 P->proc = PSI_ParserProcAlloc(malloc);
42 if (flags & PSI_PARSER_DEBUG) {
43 PSI_ParserProcTrace(stderr, "PSI> ");
51 size_t PSI_ParserFill(PSI_Parser *P, size_t n)
53 if (P->flags & PSI_PARSER_DEBUG) {
54 fprintf(stderr, "PSI> Fill: n=%zu\n", n);
57 P->cur = P->tok = P->lim = P->mrk = P->buf;
62 size_t consumed = P->tok - P->buf;
63 size_t reserved = P->lim - P->tok;
64 size_t available = BSIZE - reserved;
68 memmove(P->buf, P->tok, reserved);
75 didread = fread(P->lim, 1, available, P->fp);
77 if (didread < available) {
81 if (P->flags & PSI_PARSER_DEBUG) {
82 fprintf(stderr, "PSI> Fill: consumed=%zu reserved=%zu available=%zu didread=%zu\n",
83 consumed, reserved, available, didread);
86 if (P->flags & PSI_PARSER_DEBUG) {
87 fprintf(stderr, "PSI> Fill: avail=%zu\n", P->lim - P->cur);
89 return P->lim - P->cur;
92 void PSI_ParserParse(PSI_Parser *P, PSI_Token *T)
95 PSI_ParserProc(P->proc, T->type, T, P);
97 PSI_ParserProc(P->proc, 0, NULL, P);
101 void PSI_ParserDtor(PSI_Parser *P)
103 PSI_ParserProcFree(P->proc, free);
109 PSI_DataDtor((PSI_Data *) P);
111 memset(P, 0, sizeof(*P));
114 void PSI_ParserFree(PSI_Parser **P)
126 #if BSIZE < YYMAXFILL
127 # error BSIZE must be greater than YYMAXFILL
130 #define RETURN(t) do { \
132 if (P->flags & PSI_PARSER_DEBUG) { \
133 fprintf(stderr, "PSI> TOKEN: %d %.*s (EOF=%d %s:%u:%u)\n", \
134 P->num, (int) (P->cur-P->tok), P->tok, P->num == PSI_T_EOF, \
135 P->psi.file.fn, P->line, P->col); \
141 P->col += P->cur - P->tok
143 #define NEWLINE(label) \
148 token_t PSI_ParserScan(PSI_Parser *P)
156 re2c:define:YYCTYPE = "unsigned char";
157 re2c:define:YYCURSOR = P->cur;
158 re2c:define:YYLIMIT = P->lim;
159 re2c:define:YYMARKER = P->mrk;
160 re2c:define:YYFILL = "{ if (!PSI_ParserFill(P,@@)) RETURN(PSI_T_EOF); }";
161 re2c:yyfill:parameter = 0;
166 NSNAME = (NAME)? ("\\" NAME)+;
167 DOLLAR_NAME = '$' NAME;
168 QUOTED_STRING = "\"" ([^\"])+ "\"";
169 NUMBER = [+-]? [0-9]* "."? [0-9]+ ([eE] [+-]? [0-9]+)?;
171 "/*" { goto comment; }
172 ("#"|"//") .* "\n" { NEWLINE(nextline); }
173 "(" {RETURN(PSI_T_LPAREN);}
174 ")" {RETURN(PSI_T_RPAREN);}
175 ";" {RETURN(PSI_T_EOS);}
176 "," {RETURN(PSI_T_COMMA);}
177 ":" {RETURN(PSI_T_COLON);}
178 "{" {RETURN(PSI_T_LBRACE);}
179 "}" {RETURN(PSI_T_RBRACE);}
180 "[" {RETURN(PSI_T_LBRACKET);}
181 "]" {RETURN(PSI_T_RBRACKET);}
182 "=" {RETURN(PSI_T_EQUALS);}
183 "*" {RETURN(PSI_T_ASTERISK);}
184 "&" {RETURN(PSI_T_AMPERSAND);}
185 "+" {RETURN(PSI_T_PLUS);}
186 "-" {RETURN(PSI_T_MINUS);}
187 "/" {RETURN(PSI_T_SLASH);}
188 "..." {RETURN(PSI_T_ELLIPSIS);}
189 [\r\n] { NEWLINE(nextline); }
191 'TRUE' {RETURN(PSI_T_TRUE);}
192 'FALSE' {RETURN(PSI_T_FALSE);}
193 'NULL' {RETURN(PSI_T_NULL);}
194 'MIXED' {RETURN(PSI_T_MIXED);}
195 'CALLABLE' {RETURN(PSI_T_CALLABLE);}
196 'VOID' {RETURN(PSI_T_VOID);}
197 'BOOL' {RETURN(PSI_T_BOOL);}
198 'CHAR' {RETURN(PSI_T_CHAR);}
199 'SHORT' {RETURN(PSI_T_SHORT);}
200 'INT' {RETURN(PSI_T_INT);}
201 'LONG' {RETURN(PSI_T_LONG);}
202 'FLOAT' {RETURN(PSI_T_FLOAT);}
203 'DOUBLE' {RETURN(PSI_T_DOUBLE);}
204 'INT8_T' {RETURN(PSI_T_INT8);}
205 'UINT8_T' {RETURN(PSI_T_UINT8);}
206 'INT16_T' {RETURN(PSI_T_INT16);}
207 'UINT16_T' {RETURN(PSI_T_UINT16);}
208 'INT32_T' {RETURN(PSI_T_INT32);}
209 'UINT32_T' {RETURN(PSI_T_UINT32);}
210 'INT64_T' {RETURN(PSI_T_INT64);}
211 'UINT64_T' {RETURN(PSI_T_UINT64);}
212 'UNSIGNED' {RETURN(PSI_T_UNSIGNED);}
213 'SIGNED' {RETURN(PSI_T_SIGNED);}
214 'STRING' {RETURN(PSI_T_STRING);}
215 'ARRAY' {RETURN(PSI_T_ARRAY);}
216 'OBJECT' {RETURN(PSI_T_OBJECT);}
217 'CALLBACK' {RETURN(PSI_T_CALLBACK);}
218 'FUNCTION' {RETURN(PSI_T_FUNCTION);}
219 'TYPEDEF' {RETURN(PSI_T_TYPEDEF);}
220 'STRUCT' {RETURN(PSI_T_STRUCT);}
221 'UNION' {RETURN(PSI_T_UNION);}
222 'ENUM' {RETURN(PSI_T_ENUM);}
223 'CONST' {RETURN(PSI_T_CONST);}
224 'LIB' {RETURN(PSI_T_LIB);}
225 'LET' {RETURN(PSI_T_LET);}
226 'SET' {RETURN(PSI_T_SET);}
227 'RETURN' {RETURN(PSI_T_RETURN);}
228 'FREE' {RETURN(PSI_T_FREE);}
229 'TEMP' {RETURN(PSI_T_TEMP);}
230 'STRLEN' {RETURN(PSI_T_STRLEN);}
231 'STRVAL' {RETURN(PSI_T_STRVAL);}
232 'PATHVAL' {RETURN(PSI_T_PATHVAL);}
233 'INTVAL' {RETURN(PSI_T_INTVAL);}
234 'FLOATVAL' {RETURN(PSI_T_FLOATVAL);}
235 'BOOLVAL' {RETURN(PSI_T_BOOLVAL);}
236 'ARRVAL' {RETURN(PSI_T_ARRVAL);}
237 'OBJVAL' {RETURN(PSI_T_OBJVAL);}
238 'ZVAL' {RETURN(PSI_T_ZVAL);}
239 'CALLOC' {RETURN(PSI_T_CALLOC);}
240 'TO_OBJECT' {RETURN(PSI_T_TO_OBJECT);}
241 'TO_ARRAY' {RETURN(PSI_T_TO_ARRAY);}
242 'TO_STRING' {RETURN(PSI_T_TO_STRING);}
243 'TO_INT' {RETURN(PSI_T_TO_INT);}
244 'TO_FLOAT' {RETURN(PSI_T_TO_FLOAT);}
245 'TO_BOOL' {RETURN(PSI_T_TO_BOOL);}
246 NUMBER {RETURN(PSI_T_NUMBER);}
247 NAME {RETURN(PSI_T_NAME);}
248 NSNAME {RETURN(PSI_T_NSNAME);}
249 DOLLAR_NAME {RETURN(PSI_T_DOLLAR_NAME);}
250 QUOTED_STRING {RETURN(PSI_T_QUOTED_STRING);}
257 "\n" { NEWLINE(comment); }
258 "*" "/" { continue; }
259 [^] { goto comment; }