27 static token_type this_token = TOK_NONE;
28 static token_type buffered_token = TOK_NONE;
30 static double token_num;
32 static void NONRET parse_error(const char *msg, ...);
33 static void parse_commands(list<cmd *> &cmds);
35 static void parse_error(const char *msg, ...)
39 fprintf(stderr, "Parse error: ");
40 vfprintf(stderr, msg, args);
41 fprintf(stderr, "\n");
46 static token_type get_next_token()
48 while (*in_pos == ' ' || *in_pos == '\t' || *in_pos == '\r' || *in_pos == '\n')
55 if (*in_pos >= '0' && *in_pos <= '9' ||
56 *in_pos == '-' && in_pos[1] >= '0' && in_pos[1] <= '9')
59 while (*in_pos >= '0' && *in_pos <= '9' || *in_pos == '.' && in_pos[1] != '.')
63 token_num = stod(token, &end_pos);
64 if (end_pos < token.length())
65 parse_error("Invalid number %s", token.c_str());
69 if (*in_pos >= 'A' && *in_pos <= 'Z' ||
70 *in_pos >= 'a' && *in_pos <= 'z')
72 while (*in_pos >= 'A' && *in_pos <= 'Z' ||
73 *in_pos >= 'a' && *in_pos <= 'z' ||
74 *in_pos >= '0' && *in_pos <= '9' ||
83 while (*in_pos != '"')
86 parse_error("Unterminated string");
90 if (*in_pos != '"' && *in_pos != '\\')
91 parse_error("Unrecognized escape sequence \\%c", *in_pos);
109 return TOK_OPEN_PAREN;
111 return TOK_CLOSE_PAREN;
113 return TOK_OPEN_BRACE;
115 return TOK_CLOSE_BRACE;
117 if (c == '.' && *in_pos == '.')
122 parse_error("Unrecognized character '%c'", c);
126 static token_type next_token()
128 if (buffered_token != TOK_NONE)
129 this_token = buffered_token;
131 this_token = get_next_token();
132 buffered_token = TOK_NONE;
136 static void return_token()
138 assert(this_token != TOK_NONE);
139 assert(buffered_token == TOK_NONE);
140 buffered_token = this_token;
141 this_token = TOK_NONE;
144 static token_type peek_token()
148 return buffered_token;
151 static bool token_is_int()
153 return token_num == (double)(int) token_num;
156 /*** Argument types ***/
158 class arg_int : public arg_val {
161 arg_int(int x) { val = x; }
162 bool given() { return true; }
163 int as_int(int def UNUSED) { return val; }
164 string dump() { return "<int> " + to_string(val); }
167 class arg_double : public arg_val {
170 arg_double(double x) { val = x; }
171 bool given() { return true; }
172 double as_double(double def UNUSED) { return val; }
173 string dump() { return "<double> " + to_string(val); }
176 class arg_string : public arg_val {
179 arg_string(string x) { val = x; }
180 bool given() { return true; }
181 const string as_string(string def UNUSED) { return val; }
182 string dump() { return "<string> \"" + val + '"'; }
185 static arg_val null_arg;
196 static const unit units[] = {
206 static double parse_dimen(const arg_def *adef)
208 token_type t = next_token();
210 parse_error("Parameter %s must be a dimension", adef->name);
211 double tmp = token_num;
215 parse_error("Parameter %s must have a unit", adef->name);
216 for (uint i=0; units[i].name; i++)
217 if (token == units[i].name)
218 return tmp * units[i].multiplier;
219 parse_error("Unknown unit %s", token.c_str());
222 static void parse_pipeline(cmd *c)
224 pipeline *pp = new pipeline;
227 while (peek_token() != TOK_CLOSE_BRACE)
229 if (pp->branches.size() && next_token() != TOK_COMMA)
230 parse_error("Comma expected between pipeline branches");
232 pipeline_branch *pb = new pipeline_branch;
233 pp->branches.push_back(pb);
240 parse_error("Missing close brace");
241 if (t == TOK_CLOSE_BRACE || t == TOK_COLON || t == TOK_COMMA)
244 pipeline_selector ps;
246 parse_error("Pipeline selectors must start with a number");
248 parse_error("Pipeline selectors must be integers");
249 ps.from = (int) token_num;
252 if (peek_token() == TOK_DOTDOT)
257 parse_error("Pipeline selectors must be numbers or ranges");
259 parse_error("Pipeline selectors must be integers");
260 ps.to = (int) token_num;
263 pb->selectors.push_back(ps);
267 parse_commands(pb->commands);
276 static void parse_args(cmd *c)
278 const cmd_def *cdef = c->def;
279 const arg_def *adefs = cdef->arg_defs;
281 while (adefs[num_args].name)
284 c->args.resize(num_args, &null_arg);
286 token_type t = next_token();
287 if (t != TOK_OPEN_PAREN)
293 bool saw_named = false;
298 if (t == TOK_CLOSE_PAREN)
301 bool has_value = false;
304 while (adefs[argi].name && token != adefs[argi].name)
306 if (!adefs[argi].name)
307 parse_error("Command %s has no parameter %s", cdef->name, token.c_str());
308 if (c->args.at(argi)->given())
309 parse_error("Parameter %s given multiple times", token.c_str());
318 parse_error("Positional parameters must precede named ones");
322 while (next_pos < num_args && !(adefs[next_pos].type & AT_POSITIONAL))
324 if (next_pos >= num_args)
325 parse_error("Too many positional arguments for command %s", cdef->name);
330 const arg_def *adef = &adefs[argi];
331 uint type = adef->type & AT_TYPE_MASK;
340 parse_error("Parameter %s must be a string", adef->name);
341 val = new arg_string(token);
345 if (t != TOK_NUMBER || !token_is_int())
346 parse_error("Parameter %s must be an integer", adef->name);
347 val = new arg_int((int) token_num);
352 parse_error("Parameter %s must be a number", adef->name);
353 val = new arg_double(token_num);
356 val = new arg_double(parse_dimen(adef));
360 if (t != TOK_NUMBER || !token_is_int() || ((int) token_num != 0 && (int) token_num != 1))
361 parse_error("Parameter %s must be a switch", adef->name);
362 val = new arg_int((int) token_num);
370 if (type == AT_SWITCH)
371 val = new arg_int(1);
373 parse_error("Parameter %s must have a value", adef->name);
376 c->args.at(argi) = val;
379 if (t == TOK_CLOSE_PAREN)
382 parse_error("Comma expected after parameter %s", adef->name);
385 for (uint i=0; i<num_args; i++)
386 if ((adefs[i].type & AT_MANDATORY) && !c->args.at(i)->given())
387 parse_error("Command %s is missing a parameter %s", cdef->name, adefs[i].name);
390 static void debug_cmd(cmd *c, uint indent=0)
392 printf("%*sCommand %s\n", indent, "", c->def->name);
393 for (size_t i=0; i < c->args.size(); i++)
395 const arg_def *adef = &c->def->arg_defs[i];
396 string dump = c->args.at(i)->dump();
397 printf("%*sArg #%d: %s = %s\n", indent+4, "", (int) i, adef->name, dump.c_str());
401 printf("%*sPipeline:\n", indent+4, "");
402 for (auto pb: c->pipe->branches)
404 printf("%*sSelector:\n", indent+8, "");
405 for (auto ps: pb->selectors)
406 printf("%*s%d - %d\n", indent+12, "", ps.from, ps.to);
407 printf("%*sCommands:\n", indent+8, "");
408 for (auto cc: pb->commands)
409 debug_cmd(cc, indent+12);
414 static void debug_cmds(list<cmd *> &cmds)
420 static cmd *parse_cmd()
422 const cmd_def *cdef = cmd_table;
423 while (cdef->name && token != cdef->name)
426 parse_error("Unknown command %s", token.c_str());
434 if (peek_token() == TOK_OPEN_BRACE)
436 if (!cdef->has_pipeline)
437 parse_error("Command %s does not accept a pipeline", cdef->name);
440 else if (cdef->has_pipeline)
441 parse_error("Command %s requires a pipeline", cdef->name);
446 static void parse_commands(list<cmd *> &cmds)
450 token_type t = next_token();
457 cmd *c = parse_cmd();
462 static void instantiate(list<cmd *> &cmds)
466 c->exec = c->def->constructor(c);
469 for (auto pb: c->pipe->branches)
470 instantiate(pb->commands);
475 void parse(const char *in, list<cmd *> &cmds)
478 parse_commands(cmds);
479 if (next_token() != TOK_END)
480 parse_error("Extra tokens after commands");
488 for (int i=0; cmd_table[i].name; i++)
490 const cmd_def *def = &cmd_table[i];
491 printf("%s\n", def->name);
493 const arg_def *arg = def->arg_defs;
494 static const char * const type_names[] = {
495 "string", "int", "double", "dimen", "switch"
499 printf("\t%s (%s)%s%s\n",
501 type_names[arg->type & AT_TYPE_MASK],
502 (arg->type & AT_MANDATORY) ? " [mandatory]" : "",
503 (arg->type & AT_POSITIONAL) ? " [positional]" : "");
507 if (def->has_pipeline)
508 printf("\t{ pipeline }\n");