X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=parse.cc;h=48a32c717c0318a806ce6edfb5eb077a0a4dc06d;hb=bd1389b30f2a5addf0570c5f3358567df8134fc6;hp=a68dd366dd788d0fc9aa351c59231d387bba6a63;hpb=33a127c6d1e89d83af157f260fb26f475603df6b;p=paperjam.git diff --git a/parse.cc b/parse.cc index a68dd36..48a32c7 100644 --- a/parse.cc +++ b/parse.cc @@ -1,3 +1,9 @@ +/* + * PaperJam -- Command parser + * + * (c) 2018 Martin Mares + */ + #include #include #include @@ -71,7 +77,8 @@ static token_type get_next_token() { while (*in_pos >= 'A' && *in_pos <= 'Z' || *in_pos >= 'a' && *in_pos <= 'z' || - *in_pos >= '0' && *in_pos <= '9') + *in_pos >= '0' && *in_pos <= '9' || + *in_pos == '_') token += *in_pos++; return TOK_IDENT; } @@ -160,7 +167,7 @@ public: arg_int(int x) { val = x; } bool given() { return true; } int as_int(int def UNUSED) { return val; } - string dump() { return to_string(val); } + string dump() { return " " + to_string(val); } }; class arg_double : public arg_val { @@ -169,7 +176,7 @@ public: arg_double(double x) { val = x; } bool given() { return true; } double as_double(double def UNUSED) { return val; } - string dump() { return to_string(val); } + string dump() { return " " + to_string(val); } }; class arg_string : public arg_val { @@ -177,8 +184,8 @@ class arg_string : public arg_val { public: arg_string(string x) { val = x; } bool given() { return true; } - string as_string(string def UNUSED) { return val; } - string dump() { return '"' + val + '"'; } + const string as_string(string def UNUSED) { return val; } + string dump() { return " \"" + val + '"'; } }; static arg_val null_arg; @@ -231,23 +238,15 @@ static void parse_pipeline(cmd *c) pipeline_branch *pb = new pipeline_branch; pp->branches.push_back(pb); + token_type t; for (;;) { - token_type t = next_token(); - if (t == TOK_CLOSE_BRACE || t == TOK_END) - parse_error("Premature end of pipeline"); - if (t == TOK_COLON) + t = next_token(); + if (t == TOK_END) + parse_error("Missing close brace"); + if (t == TOK_CLOSE_BRACE || t == TOK_COLON || t == TOK_COMMA) break; - if (pb->selectors.size()) - { - if (t != TOK_COMMA) - parse_error("Invalid pipeline selector"); - t = next_token(); - if (t == TOK_CLOSE_BRACE || t == TOK_END) - parse_error("Premature end of pipeline"); - } - pipeline_selector ps; if (t != TOK_NUMBER) parse_error("Pipeline selectors must start with a number"); @@ -270,7 +269,10 @@ static void parse_pipeline(cmd *c) pb->selectors.push_back(ps); } - parse_commands(pb->commands); + if (t == TOK_COLON) + parse_commands(pb->commands); + else + return_token(); } c->pipe = pp; @@ -299,7 +301,10 @@ static void parse_args(cmd *c) for (;;) { t = next_token(); + if (t == TOK_CLOSE_PAREN) + break; uint argi = 0; + bool has_value = false; if (t == TOK_IDENT) { while (adefs[argi].name && token != adefs[argi].name) @@ -309,8 +314,10 @@ static void parse_args(cmd *c) if (c->args.at(argi)->given()) parse_error("Parameter %s given multiple times", token.c_str()); t = next_token(); - if (t != TOK_EQUAL) - parse_error("Parameter name must be followed by '='"); + if (t == TOK_EQUAL) + has_value = true; + else + return_token(); saw_named = true; } else if (saw_named) @@ -323,35 +330,53 @@ static void parse_args(cmd *c) if (next_pos >= num_args) parse_error("Too many positional arguments for command %s", cdef->name); argi = next_pos++; + has_value = true; } const arg_def *adef = &adefs[argi]; + uint type = adef->type & AT_TYPE_MASK; arg_val *val = NULL; - switch (adef->type & AT_TYPE_MASK) - { - case AT_STRING: - t = next_token(); - if (t != TOK_STRING) - parse_error("Parameter %s must be a string", adef->name); - val = new arg_string(token); - break; - case AT_INT: - t = next_token(); - if (t != TOK_NUMBER || !token_is_int()) - parse_error("Parameter %s must be an integer", adef->name); - val = new arg_int((int) token_num); - break; - case AT_DOUBLE: - t = next_token(); - if (t != TOK_NUMBER) - parse_error("Parameter %s must be a number", adef->name); - val = new arg_double(token_num); - break; - case AT_DIMEN: - val = new arg_double(parse_dimen(adef)); - break; - default: - abort(); + if (has_value) + { + switch (type) + { + case AT_STRING: + t = next_token(); + if (t != TOK_STRING) + parse_error("Parameter %s must be a string", adef->name); + val = new arg_string(token); + break; + case AT_INT: + t = next_token(); + if (t != TOK_NUMBER || !token_is_int()) + parse_error("Parameter %s must be an integer", adef->name); + val = new arg_int((int) token_num); + break; + case AT_DOUBLE: + t = next_token(); + if (t != TOK_NUMBER) + parse_error("Parameter %s must be a number", adef->name); + val = new arg_double(token_num); + break; + case AT_DIMEN: + val = new arg_double(parse_dimen(adef)); + break; + case AT_SWITCH: + t = next_token(); + if (t != TOK_NUMBER || !token_is_int() || ((int) token_num != 0 && (int) token_num != 1)) + parse_error("Parameter %s must be a switch", adef->name); + val = new arg_int((int) token_num); + break; + default: + abort(); + } + } + else + { + if (type == AT_SWITCH) + val = new arg_int(1); + else + parse_error("Parameter %s must have a value", adef->name); } c->args.at(argi) = val; @@ -463,3 +488,29 @@ void parse(const char *in, list &cmds) debug_cmds(cmds); instantiate(cmds); } + +void parser_help() +{ + for (int i=0; cmd_table[i].name; i++) + { + const cmd_def *def = &cmd_table[i]; + printf("%s\n", def->name); + + const arg_def *arg = def->arg_defs; + static const char * const type_names[] = { + "string", "int", "double", "dimen", "switch" + }; + while (arg->name) + { + printf("\t%s (%s)%s%s\n", + arg->name, + type_names[arg->type & AT_TYPE_MASK], + (arg->type & AT_MANDATORY) ? " [mandatory]" : "", + (arg->type & AT_POSITIONAL) ? " [positional]" : ""); + arg++; + } + + if (def->has_pipeline) + printf("\t{ pipeline }\n"); + } +}