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')
82 while (*in_pos != '"')
85 parse_error("Unterminated string");
89 if (*in_pos != '"' && *in_pos != '\\')
90 parse_error("Unrecognized escape sequence \\%c", *in_pos);
108 return TOK_OPEN_PAREN;
110 return TOK_CLOSE_PAREN;
112 return TOK_OPEN_BRACE;
114 return TOK_CLOSE_BRACE;
116 if (c == '.' && *in_pos == '.')
121 parse_error("Unrecognized character '%c'", c);
125 static token_type next_token()
127 if (buffered_token != TOK_NONE)
128 this_token = buffered_token;
130 this_token = get_next_token();
131 buffered_token = TOK_NONE;
135 static void return_token()
137 assert(this_token != TOK_NONE);
138 assert(buffered_token == TOK_NONE);
139 buffered_token = this_token;
140 this_token = TOK_NONE;
143 static token_type peek_token()
147 return buffered_token;
150 static bool token_is_int()
152 return token_num == (double)(int) token_num;
155 /*** Argument types ***/
157 class arg_int : public arg_val {
160 arg_int(int x) { val = x; }
161 bool given() { return true; }
162 int as_int(int def UNUSED) { return val; }
163 string dump() { return "<int> " + to_string(val); }
166 class arg_double : public arg_val {
169 arg_double(double x) { val = x; }
170 bool given() { return true; }
171 double as_double(double def UNUSED) { return val; }
172 string dump() { return "<double> " + to_string(val); }
175 class arg_string : public arg_val {
178 arg_string(string x) { val = x; }
179 bool given() { return true; }
180 const string as_string(string def UNUSED) { return val; }
181 string dump() { return "<string> \"" + val + '"'; }
184 static arg_val null_arg;
195 static const unit units[] = {
205 static double parse_dimen(const arg_def *adef)
207 token_type t = next_token();
209 parse_error("Parameter %s must be a dimension", adef->name);
210 double tmp = token_num;
214 parse_error("Parameter %s must have a unit", adef->name);
215 for (uint i=0; units[i].name; i++)
216 if (token == units[i].name)
217 return tmp * units[i].multiplier;
218 parse_error("Unknown unit %s", token.c_str());
221 static void parse_pipeline(cmd *c)
223 pipeline *pp = new pipeline;
226 while (peek_token() != TOK_CLOSE_BRACE)
228 if (pp->branches.size() && next_token() != TOK_COMMA)
229 parse_error("Comma expected between pipeline branches");
231 pipeline_branch *pb = new pipeline_branch;
232 pp->branches.push_back(pb);
239 parse_error("Missing close brace");
240 if (t == TOK_CLOSE_BRACE || t == TOK_COLON || t == TOK_COMMA)
243 pipeline_selector ps;
245 parse_error("Pipeline selectors must start with a number");
247 parse_error("Pipeline selectors must be integers");
248 ps.from = (int) token_num;
251 if (peek_token() == TOK_DOTDOT)
256 parse_error("Pipeline selectors must be numbers or ranges");
258 parse_error("Pipeline selectors must be integers");
259 ps.to = (int) token_num;
262 pb->selectors.push_back(ps);
266 parse_commands(pb->commands);
275 static void parse_args(cmd *c)
277 const cmd_def *cdef = c->def;
278 const arg_def *adefs = cdef->arg_defs;
280 while (adefs[num_args].name)
283 c->args.resize(num_args, &null_arg);
285 token_type t = next_token();
286 if (t != TOK_OPEN_PAREN)
292 bool saw_named = false;
300 while (adefs[argi].name && token != adefs[argi].name)
302 if (!adefs[argi].name)
303 parse_error("Command %s has no parameter %s", cdef->name, token.c_str());
304 if (c->args.at(argi)->given())
305 parse_error("Parameter %s given multiple times", token.c_str());
308 parse_error("Parameter name must be followed by '='");
312 parse_error("Positional parameters must precede named ones");
316 while (next_pos < num_args && !(adefs[next_pos].type & AT_POSITIONAL))
318 if (next_pos >= num_args)
319 parse_error("Too many positional arguments for command %s", cdef->name);
323 const arg_def *adef = &adefs[argi];
325 switch (adef->type & AT_TYPE_MASK)
330 parse_error("Parameter %s must be a string", adef->name);
331 val = new arg_string(token);
335 if (t != TOK_NUMBER || !token_is_int())
336 parse_error("Parameter %s must be an integer", adef->name);
337 val = new arg_int((int) token_num);
342 parse_error("Parameter %s must be a number", adef->name);
343 val = new arg_double(token_num);
346 val = new arg_double(parse_dimen(adef));
352 c->args.at(argi) = val;
355 if (t == TOK_CLOSE_PAREN)
358 parse_error("Comma expected after parameter %s", adef->name);
361 for (uint i=0; i<num_args; i++)
362 if ((adefs[i].type & AT_MANDATORY) && !c->args.at(i)->given())
363 parse_error("Command %s is missing a parameter %s", cdef->name, adefs[i].name);
366 static void debug_cmd(cmd *c, uint indent=0)
368 printf("%*sCommand %s\n", indent, "", c->def->name);
369 for (size_t i=0; i < c->args.size(); i++)
371 const arg_def *adef = &c->def->arg_defs[i];
372 string dump = c->args.at(i)->dump();
373 printf("%*sArg #%d: %s = %s\n", indent+4, "", (int) i, adef->name, dump.c_str());
377 printf("%*sPipeline:\n", indent+4, "");
378 for (auto pb: c->pipe->branches)
380 printf("%*sSelector:\n", indent+8, "");
381 for (auto ps: pb->selectors)
382 printf("%*s%d - %d\n", indent+12, "", ps.from, ps.to);
383 printf("%*sCommands:\n", indent+8, "");
384 for (auto cc: pb->commands)
385 debug_cmd(cc, indent+12);
390 static void debug_cmds(list<cmd *> &cmds)
396 static cmd *parse_cmd()
398 const cmd_def *cdef = cmd_table;
399 while (cdef->name && token != cdef->name)
402 parse_error("Unknown command %s", token.c_str());
410 if (peek_token() == TOK_OPEN_BRACE)
412 if (!cdef->has_pipeline)
413 parse_error("Command %s does not accept a pipeline", cdef->name);
416 else if (cdef->has_pipeline)
417 parse_error("Command %s requires a pipeline", cdef->name);
422 static void parse_commands(list<cmd *> &cmds)
426 token_type t = next_token();
433 cmd *c = parse_cmd();
438 static void instantiate(list<cmd *> &cmds)
442 c->exec = c->def->constructor(c);
445 for (auto pb: c->pipe->branches)
446 instantiate(pb->commands);
451 void parse(const char *in, list<cmd *> &cmds)
454 parse_commands(cmds);
455 if (next_token() != TOK_END)
456 parse_error("Extra tokens after commands");