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 + '"'; }
191 static arg_val null_arg;
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 c->args.resize(num_args, &null_arg);
292 token_type t = next_token();
293 if (t != TOK_OPEN_PAREN)
299 bool saw_named = false;
304 if (t == TOK_CLOSE_PAREN)
307 bool has_value = false;
310 while (adefs[argi].name && token != adefs[argi].name)
312 if (!adefs[argi].name)
313 parse_error("Command %s has no parameter %s", cdef->name, token.c_str());
314 if (c->args.at(argi)->given())
315 parse_error("Parameter %s given multiple times", token.c_str());
324 parse_error("Positional parameters must precede named ones");
328 while (next_pos < num_args && !(adefs[next_pos].type & AT_POSITIONAL))
330 if (next_pos >= num_args)
331 parse_error("Too many positional arguments for command %s", cdef->name);
336 const arg_def *adef = &adefs[argi];
337 uint type = adef->type & AT_TYPE_MASK;
346 parse_error("Parameter %s must be a string", adef->name);
347 val = new arg_string(token);
351 if (t != TOK_NUMBER || !token_is_int())
352 parse_error("Parameter %s must be an integer", adef->name);
353 val = new arg_int((int) token_num);
358 parse_error("Parameter %s must be a number", adef->name);
359 val = new arg_double(token_num);
362 val = new arg_double(parse_dimen(adef));
366 if (t != TOK_NUMBER || !token_is_int() || ((int) token_num != 0 && (int) token_num != 1))
367 parse_error("Parameter %s must be a switch", adef->name);
368 val = new arg_int((int) token_num);
376 if (type == AT_SWITCH)
377 val = new arg_int(1);
379 parse_error("Parameter %s must have a value", adef->name);
382 c->args.at(argi) = val;
385 if (t == TOK_CLOSE_PAREN)
388 parse_error("Comma expected after parameter %s", adef->name);
391 for (uint i=0; i<num_args; i++)
392 if ((adefs[i].type & AT_MANDATORY) && !c->args.at(i)->given())
393 parse_error("Command %s is missing a parameter %s", cdef->name, adefs[i].name);
396 static void debug_cmd(cmd *c, uint indent=0)
398 printf("%*sCommand %s\n", indent, "", c->def->name);
399 for (size_t i=0; i < c->args.size(); i++)
401 const arg_def *adef = &c->def->arg_defs[i];
402 string dump = c->args.at(i)->dump();
403 printf("%*sArg #%d: %s = %s\n", indent+4, "", (int) i, adef->name, dump.c_str());
407 printf("%*sPipeline:\n", indent+4, "");
408 for (auto pb: c->pipe->branches)
410 printf("%*sSelector:\n", indent+8, "");
411 for (auto ps: pb->selectors)
412 printf("%*s%d - %d\n", indent+12, "", ps.from, ps.to);
413 printf("%*sCommands:\n", indent+8, "");
414 for (auto cc: pb->commands)
415 debug_cmd(cc, indent+12);
420 static void debug_cmds(list<cmd *> &cmds)
426 static cmd *parse_cmd()
428 const cmd_def *cdef = cmd_table;
429 while (cdef->name && token != cdef->name)
432 parse_error("Unknown command %s", token.c_str());
440 if (peek_token() == TOK_OPEN_BRACE)
442 if (!cdef->has_pipeline)
443 parse_error("Command %s does not accept a pipeline", cdef->name);
446 else if (cdef->has_pipeline)
447 parse_error("Command %s requires a pipeline", cdef->name);
452 static void parse_commands(list<cmd *> &cmds)
456 token_type t = next_token();
463 cmd *c = parse_cmd();
468 static void instantiate(list<cmd *> &cmds)
472 c->exec = c->def->constructor(c);
475 for (auto pb: c->pipe->branches)
476 instantiate(pb->commands);
481 void parse(const char *in, list<cmd *> &cmds)
484 parse_commands(cmds);
485 if (next_token() != TOK_END)
486 parse_error("Extra tokens after commands");
494 for (int i=0; cmd_table[i].name; i++)
496 const cmd_def *def = &cmd_table[i];
497 printf("%s\n", def->name);
499 const arg_def *arg = def->arg_defs;
500 static const char * const type_names[] = {
501 "string", "int", "double", "dimen", "switch"
505 printf("\t%s (%s)%s%s\n",
507 type_names[arg->type & AT_TYPE_MASK],
508 (arg->type & AT_MANDATORY) ? " [mandatory]" : "",
509 (arg->type & AT_POSITIONAL) ? " [positional]" : "");
513 if (def->has_pipeline)
514 printf("\t{ pipeline }\n");