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_double : public arg_val {
160 bool given() { return true; }
161 explicit operator double () { return val; }
162 arg_double(double x) { val = x; }
163 string dump() { return to_string(val); }
166 class arg_string : public arg_val {
169 bool given() { return true; }
170 explicit operator string () { return val; }
171 arg_string(string x) { val = x; }
172 string dump() { return '"' + val + '"'; }
175 static arg_val null_arg;
186 static const unit units[] = {
196 static double parse_dimen(const arg_def *adef)
198 token_type t = next_token();
200 parse_error("Parameter %s must be a dimension", adef->name);
201 double tmp = token_num;
205 parse_error("Parameter %s must have a unit", adef->name);
206 for (uint i=0; units[i].name; i++)
207 if (token == units[i].name)
208 return tmp * units[i].multiplier;
209 parse_error("Unknown unit %s", token.c_str());
212 static void parse_pipeline(cmd *c)
214 pipeline *pp = new pipeline;
217 while (peek_token() != TOK_CLOSE_BRACE)
219 if (pp->branches.size() && next_token() != TOK_COMMA)
220 parse_error("Comma expected between pipeline branches");
222 pipeline_branch *pb = new pipeline_branch;
223 pp->branches.push_back(pb);
227 token_type t = next_token();
228 if (t == TOK_CLOSE_BRACE || t == TOK_END)
229 parse_error("Premature end of pipeline");
233 if (pb->selectors.size())
236 parse_error("Invalid pipeline selector");
238 if (t == TOK_CLOSE_BRACE || t == TOK_END)
239 parse_error("Premature end of pipeline");
242 pipeline_selector ps;
244 parse_error("Pipeline selectors must start with a number");
246 parse_error("Pipeline selectors must be integers");
247 ps.from = (int) token_num;
250 if (peek_token() == TOK_DOTDOT)
255 parse_error("Pipeline selectors must be numbers or ranges");
257 parse_error("Pipeline selectors must be integers");
258 ps.to = (int) token_num;
261 pb->selectors.push_back(ps);
264 parse_commands(&pb->commands);
271 static void parse_args(cmd *c)
273 const cmd_def *cdef = c->def;
274 const arg_def *adefs = cdef->arg_defs;
276 while (adefs[num_args].name)
279 c->args.resize(num_args, &null_arg);
281 token_type t = next_token();
282 if (t != TOK_OPEN_PAREN)
288 bool saw_named = false;
296 while (adefs[argi].name && token != adefs[argi].name)
298 if (!adefs[argi].name)
299 parse_error("Command %s has no parameter %s", cdef->name, token.c_str());
300 if (c->args.at(argi)->given())
301 parse_error("Parameter %s given multiple times", token.c_str());
304 parse_error("Parameter name must be followed by '='");
308 parse_error("Positional parameters must precede named ones");
312 while (next_pos < num_args && !(adefs[next_pos].type & AT_POSITIONAL))
314 if (next_pos >= num_args)
315 parse_error("Too many positional arguments for command %s", cdef->name);
319 const arg_def *adef = &adefs[argi];
321 switch (adef->type & AT_TYPE_MASK)
326 parse_error("Parameter %s must be a string", adef->name);
327 val = new arg_string(token);
332 parse_error("Parameter %s must be a number", adef->name);
333 val = new arg_double(token_num);
336 val = new arg_double(parse_dimen(adef));
342 c->args.at(argi) = val;
345 if (t == TOK_CLOSE_PAREN)
348 parse_error("Comma expected after parameter %s", adef->name);
351 for (uint i=0; i<num_args; i++)
352 if ((adefs[i].type & AT_MANDATORY) && !c->args.at(i)->given())
353 parse_error("Command %s is missing a parameter %s", cdef->name, adefs[i].name);
356 static void debug_cmd(cmd *c, uint indent=0)
358 printf("%*sCommand %s\n", indent, "", c->def->name);
359 for (size_t i=0; i < c->args.size(); i++)
361 const arg_def *adef = &c->def->arg_defs[i];
362 string dump = c->args.at(i)->dump();
363 printf("%*sArg #%d: %s = %s\n", indent+4, "", (int) i, adef->name, dump.c_str());
367 printf("%*sPipeline:\n", indent+4, "");
368 for (auto pb: c->pipe->branches)
370 printf("%*sSelector:\n", indent+8, "");
371 for (auto ps: pb->selectors)
372 printf("%*s%d - %d\n", indent+12, "", ps.from, ps.to);
373 printf("%*sCommands:\n", indent+8, "");
374 for (auto cc: pb->commands)
375 debug_cmd(cc, indent+12);
380 static void debug_cmds(list<cmd *> *cmds)
386 static cmd *parse_cmd()
388 const cmd_def *cdef = cmd_table;
389 while (cdef->name && token != cdef->name)
392 parse_error("Unknown command %s", token.c_str());
400 if (peek_token() == TOK_OPEN_BRACE)
402 if (!cdef->has_pipeline)
403 parse_error("Command %s does not accept a pipeline", cdef->name);
406 else if (cdef->has_pipeline)
407 parse_error("Command %s requires a pipeline", cdef->name);
412 static void parse_commands(list<cmd *> *cmds)
416 token_type t = next_token();
423 cmd *c = parse_cmd();
428 void parse(const char *in, list<cmd *> *cmds)
431 parse_commands(cmds);
432 if (next_token() != TOK_END)
433 parse_error("Extra tokens after commands");