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;
301 while (adefs[argi].name && token != adefs[argi].name)
303 if (!adefs[argi].name)
304 parse_error("Command %s has no parameter %s", cdef->name, token.c_str());
305 if (c->args.at(argi)->given())
306 parse_error("Parameter %s given multiple times", token.c_str());
309 parse_error("Parameter name must be followed by '='");
313 parse_error("Positional parameters must precede named ones");
317 while (next_pos < num_args && !(adefs[next_pos].type & AT_POSITIONAL))
319 if (next_pos >= num_args)
320 parse_error("Too many positional arguments for command %s", cdef->name);
324 const arg_def *adef = &adefs[argi];
326 switch (adef->type & AT_TYPE_MASK)
331 parse_error("Parameter %s must be a string", adef->name);
332 val = new arg_string(token);
336 if (t != TOK_NUMBER || !token_is_int())
337 parse_error("Parameter %s must be an integer", adef->name);
338 val = new arg_int((int) token_num);
343 parse_error("Parameter %s must be a number", adef->name);
344 val = new arg_double(token_num);
347 val = new arg_double(parse_dimen(adef));
353 c->args.at(argi) = val;
356 if (t == TOK_CLOSE_PAREN)
359 parse_error("Comma expected after parameter %s", adef->name);
362 for (uint i=0; i<num_args; i++)
363 if ((adefs[i].type & AT_MANDATORY) && !c->args.at(i)->given())
364 parse_error("Command %s is missing a parameter %s", cdef->name, adefs[i].name);
367 static void debug_cmd(cmd *c, uint indent=0)
369 printf("%*sCommand %s\n", indent, "", c->def->name);
370 for (size_t i=0; i < c->args.size(); i++)
372 const arg_def *adef = &c->def->arg_defs[i];
373 string dump = c->args.at(i)->dump();
374 printf("%*sArg #%d: %s = %s\n", indent+4, "", (int) i, adef->name, dump.c_str());
378 printf("%*sPipeline:\n", indent+4, "");
379 for (auto pb: c->pipe->branches)
381 printf("%*sSelector:\n", indent+8, "");
382 for (auto ps: pb->selectors)
383 printf("%*s%d - %d\n", indent+12, "", ps.from, ps.to);
384 printf("%*sCommands:\n", indent+8, "");
385 for (auto cc: pb->commands)
386 debug_cmd(cc, indent+12);
391 static void debug_cmds(list<cmd *> &cmds)
397 static cmd *parse_cmd()
399 const cmd_def *cdef = cmd_table;
400 while (cdef->name && token != cdef->name)
403 parse_error("Unknown command %s", token.c_str());
411 if (peek_token() == TOK_OPEN_BRACE)
413 if (!cdef->has_pipeline)
414 parse_error("Command %s does not accept a pipeline", cdef->name);
417 else if (cdef->has_pipeline)
418 parse_error("Command %s requires a pipeline", cdef->name);
423 static void parse_commands(list<cmd *> &cmds)
427 token_type t = next_token();
434 cmd *c = parse_cmd();
439 static void instantiate(list<cmd *> &cmds)
443 c->exec = c->def->constructor(c);
446 for (auto pb: c->pipe->branches)
447 instantiate(pb->commands);
452 void parse(const char *in, list<cmd *> &cmds)
455 parse_commands(cmds);
456 if (next_token() != TOK_END)
457 parse_error("Extra tokens after commands");