2 * PaperJam -- Command parser
4 * (c) 2018 Martin Mares <mj@ucw.cz>
33 static token_type this_token = TOK_NONE;
34 static token_type buffered_token = TOK_NONE;
36 static double token_num;
38 static void NONRET parse_error(const char *msg, ...);
39 static void parse_commands(list<cmd *> &cmds);
41 static void parse_error(const char *msg, ...)
45 fprintf(stderr, "Parse error: ");
46 vfprintf(stderr, msg, args);
47 fprintf(stderr, "\n");
52 static token_type get_next_token()
54 while (*in_pos == ' ' || *in_pos == '\t' || *in_pos == '\r' || *in_pos == '\n')
61 if (*in_pos >= '0' && *in_pos <= '9' ||
62 *in_pos == '-' && in_pos[1] >= '0' && in_pos[1] <= '9')
65 while (*in_pos >= '0' && *in_pos <= '9' || *in_pos == '.' && in_pos[1] != '.')
69 token_num = stod(token, &end_pos);
70 if (end_pos < token.length())
71 parse_error("Invalid number %s", token.c_str());
75 if (*in_pos >= 'A' && *in_pos <= 'Z' ||
76 *in_pos >= 'a' && *in_pos <= 'z')
78 while (*in_pos >= 'A' && *in_pos <= 'Z' ||
79 *in_pos >= 'a' && *in_pos <= 'z' ||
80 *in_pos >= '0' && *in_pos <= '9' ||
89 while (*in_pos != '"')
92 parse_error("Unterminated string");
96 if (*in_pos != '"' && *in_pos != '\\')
97 parse_error("Unrecognized escape sequence \\%c", *in_pos);
115 return TOK_OPEN_PAREN;
117 return TOK_CLOSE_PAREN;
119 return TOK_OPEN_BRACE;
121 return TOK_CLOSE_BRACE;
123 if (c == '.' && *in_pos == '.')
128 parse_error("Unrecognized character '%c'", c);
132 static token_type next_token()
134 if (buffered_token != TOK_NONE)
135 this_token = buffered_token;
137 this_token = get_next_token();
138 buffered_token = TOK_NONE;
142 static void return_token()
144 assert(this_token != TOK_NONE);
145 assert(buffered_token == TOK_NONE);
146 buffered_token = this_token;
147 this_token = TOK_NONE;
150 static token_type peek_token()
154 return buffered_token;
157 static bool token_is_int()
159 return token_num == (double)(int) token_num;
162 /*** Argument types ***/
164 class arg_int : public arg_val {
167 arg_int(int x) { val = x; }
168 bool given() { return true; }
169 int as_int(int def UNUSED) { return val; }
170 string dump() { return "<int> " + to_string(val); }
173 class arg_double : public arg_val {
176 arg_double(double x) { val = x; }
177 bool given() { return true; }
178 double as_double(double def UNUSED) { return val; }
179 string dump() { return "<double> " + to_string(val); }
182 class arg_string : public arg_val {
185 arg_string(string x) { val = x; }
186 bool given() { return true; }
187 const string as_string(string def UNUSED) { return val; }
188 string dump() { return "<string> \"" + val + '"'; }
202 static const unit units[] = {
212 static double parse_dimen(const arg_def *adef)
214 token_type t = next_token();
216 parse_error("Parameter %s must be a dimension", adef->name);
217 double tmp = token_num;
221 parse_error("Parameter %s must have a unit", adef->name);
222 for (uint i=0; units[i].name; i++)
223 if (token == units[i].name)
224 return tmp * units[i].multiplier;
225 parse_error("Unknown unit %s", token.c_str());
228 static void parse_pipeline(cmd *c)
230 pipeline *pp = new pipeline;
233 while (peek_token() != TOK_CLOSE_BRACE)
235 if (pp->branches.size() && next_token() != TOK_COMMA)
236 parse_error("Comma expected between pipeline branches");
238 pipeline_branch *pb = new pipeline_branch;
239 pp->branches.push_back(pb);
246 parse_error("Missing close brace");
247 if (t == TOK_CLOSE_BRACE || t == TOK_COLON || t == TOK_COMMA)
250 pipeline_selector ps;
252 parse_error("Pipeline selectors must start with a number");
254 parse_error("Pipeline selectors must be integers");
255 ps.from = (int) token_num;
258 if (peek_token() == TOK_DOTDOT)
263 parse_error("Pipeline selectors must be numbers or ranges");
265 parse_error("Pipeline selectors must be integers");
266 ps.to = (int) token_num;
269 pb->selectors.push_back(ps);
273 parse_commands(pb->commands);
282 static void parse_args(cmd *c)
284 const cmd_def *cdef = c->def;
285 const arg_def *adefs = cdef->arg_defs;
287 while (adefs[num_args].name)
290 token_type t = next_token();
291 if (t != TOK_OPEN_PAREN)
297 bool saw_named = false;
302 if (t == TOK_CLOSE_PAREN)
305 bool has_value = false;
308 while (adefs[argi].name && token != adefs[argi].name)
310 if (!adefs[argi].name)
311 parse_error("Command %s has no parameter %s", cdef->name, token.c_str());
312 if (c->args.count(token))
313 parse_error("Parameter %s given multiple times", token.c_str());
322 parse_error("Positional parameters must precede named ones");
326 while (next_pos < num_args && !(adefs[next_pos].type & AT_POSITIONAL))
328 if (next_pos >= num_args)
329 parse_error("Too many positional arguments for command %s", cdef->name);
334 const arg_def *adef = &adefs[argi];
335 uint type = adef->type & AT_TYPE_MASK;
344 parse_error("Parameter %s must be a string", adef->name);
345 val = new arg_string(token);
349 if (t != TOK_NUMBER || !token_is_int())
350 parse_error("Parameter %s must be an integer", adef->name);
351 val = new arg_int((int) token_num);
356 parse_error("Parameter %s must be a number", adef->name);
357 val = new arg_double(token_num);
360 val = new arg_double(parse_dimen(adef));
364 if (t != TOK_NUMBER || !token_is_int() || ((int) token_num != 0 && (int) token_num != 1))
365 parse_error("Parameter %s must be a switch", adef->name);
366 val = new arg_int((int) token_num);
374 if (type == AT_SWITCH)
375 val = new arg_int(1);
377 parse_error("Parameter %s must have a value", adef->name);
380 c->args[adef->name] = val;
383 if (t == TOK_CLOSE_PAREN)
386 parse_error("Comma expected after parameter %s", adef->name);
389 for (uint i=0; i<num_args; i++)
390 if ((adefs[i].type & AT_MANDATORY) && !c->args.count(adefs[i].name))
391 parse_error("Command %s is missing a parameter %s", cdef->name, adefs[i].name);
394 static void debug_cmd(cmd *c, uint indent=0)
396 printf("%*sCommand %s\n", indent, "", c->def->name);
397 for (uint i=0; c->def->arg_defs[i].name; i++)
399 const arg_def *adef = &c->def->arg_defs[i];
400 string dump = c->arg(adef->name)->dump();
401 printf("%*sArg #%d: %s = %s\n", indent+4, "", (int) i, adef->name, dump.c_str());
405 printf("%*sPipeline:\n", indent+4, "");
406 for (auto pb: c->pipe->branches)
408 printf("%*sSelector:\n", indent+8, "");
409 for (auto ps: pb->selectors)
410 printf("%*s%d - %d\n", indent+12, "", ps.from, ps.to);
411 printf("%*sCommands:\n", indent+8, "");
412 for (auto cc: pb->commands)
413 debug_cmd(cc, indent+12);
418 static void debug_cmds(list<cmd *> &cmds)
424 static cmd *parse_cmd()
426 const cmd_def *cdef = cmd_table;
427 while (cdef->name && token != cdef->name)
430 parse_error("Unknown command %s", token.c_str());
438 if (peek_token() == TOK_OPEN_BRACE)
440 if (!cdef->has_pipeline)
441 parse_error("Command %s does not accept a pipeline", cdef->name);
444 else if (cdef->has_pipeline)
445 parse_error("Command %s requires a pipeline", cdef->name);
450 static void parse_commands(list<cmd *> &cmds)
454 token_type t = next_token();
461 cmd *c = parse_cmd();
466 static void instantiate(list<cmd *> &cmds)
470 c->exec = c->def->constructor(c);
473 for (auto pb: c->pipe->branches)
474 instantiate(pb->commands);
479 void parse(const char *in, list<cmd *> &cmds)
482 parse_commands(cmds);
483 if (next_token() != TOK_END)
484 parse_error("Extra tokens after commands");
492 for (int i=0; cmd_table[i].name; i++)
494 const cmd_def *def = &cmd_table[i];
495 printf("%s\n", def->name);
497 const arg_def *arg = def->arg_defs;
498 static const char * const type_names[] = {
499 "string", "int", "double", "dimen", "switch"
503 printf("\t%s (%s)%s%s\n",
505 type_names[arg->type & AT_TYPE_MASK],
506 (arg->type & AT_MANDATORY) ? " [mandatory]" : "",
507 (arg->type & AT_POSITIONAL) ? " [positional]" : "");
511 if (def->has_pipeline)
512 printf("\t{ pipeline }\n");