fish-shell/src/parse_productions.cpp

404 lines
13 KiB
C++
Raw Normal View History

#include "config.h" // IWYU pragma: keep
2015-07-25 23:14:25 +08:00
#include <stdio.h>
#include "common.h"
#include "parse_constants.h"
2018-01-08 05:34:04 +08:00
#include "parse_grammar.h"
#include "parse_productions.h"
#include "parse_tree.h"
2013-07-26 06:24:22 +08:00
using namespace parse_productions;
2018-01-08 07:53:36 +08:00
using namespace grammar;
2013-07-26 06:24:22 +08:00
#define NO_PRODUCTION NULL
2013-07-29 06:19:38 +08:00
// Herein are encoded the productions for our LL2 fish grammar.
//
// Each symbol (e.g. symbol_job_list) has a corresponding function (e.g. resolve_job_lits). The
// function accepts two tokens, representing the first and second lookahead, and returns returns a
// production representing the rule, or NULL on error. There is also a tag value which is returned
// by reference; the tag is a sort of node annotation.
//
// Productions are generally a static const array, and we return a pointer to the array (yes,
// really).
2018-01-08 07:53:36 +08:00
#define RESOLVE(SYM) \
const production_element_t *SYM::resolve( \
const parse_token_t &token1, const parse_token_t &token2, parse_node_tag_t *out_tag)
/// A job_list is a list of jobs, separated by semicolons or newlines.
RESOLVE(job_list) {
UNUSED(token2);
UNUSED(out_tag);
switch (token1.type) {
case parse_token_type_string: {
// Some keywords are special.
switch (token1.keyword) {
2013-07-27 14:59:12 +08:00
case parse_keyword_end:
case parse_keyword_else:
case parse_keyword_case: {
2018-01-08 07:53:36 +08:00
return production_for<empty>(); // end this job list
}
default: {
2018-01-08 07:53:36 +08:00
return production_for<normal>(); // normal string
}
2013-07-27 14:59:12 +08:00
}
}
2013-07-27 14:59:12 +08:00
case parse_token_type_pipe:
case parse_token_type_redirection:
case parse_token_type_background: {
2018-01-08 07:53:36 +08:00
return production_for<normal>();
}
case parse_token_type_end: {
2018-01-08 07:53:36 +08:00
return production_for<empty_line>();
}
case parse_token_type_terminate: {
2018-01-08 07:53:36 +08:00
return production_for<empty>(); // no more commands, just transition to empty
}
default: { return NO_PRODUCTION; }
2013-07-27 14:59:12 +08:00
}
}
// A job decorator is AND or OR
RESOLVE(job_decorator) {
UNUSED(token2);
switch (token1.keyword) {
case parse_keyword_and: {
*out_tag = parse_bool_and;
return production_for<ands>();
}
case parse_keyword_or: {
*out_tag = parse_bool_or;
return production_for<ors>();
}
default: {
*out_tag = parse_bool_none;
return production_for<empty>();
}
}
}
RESOLVE(job_conjunction_continuation) {
UNUSED(token2);
UNUSED(out_tag);
switch (token1.type) {
case parse_token_type_andand:
*out_tag = parse_bool_and;
return production_for<andands>();
case parse_token_type_oror:
*out_tag = parse_bool_or;
return production_for<orors>();
default:
return production_for<empty>();
}
}
RESOLVE(job_continuation) {
UNUSED(token2);
UNUSED(out_tag);
switch (token1.type) {
case parse_token_type_pipe: {
2018-01-08 07:53:36 +08:00
return production_for<piped>(); // pipe, continuation
}
default: {
2018-01-08 07:53:36 +08:00
return production_for<empty>(); // not a pipe, no job continuation
}
2013-07-27 14:59:12 +08:00
}
}
// A statement is a normal command, or an if / while / and etc.
RESOLVE(statement) {
UNUSED(out_tag);
// The only block-like builtin that takes any parameters is 'function' So go to decorated
// statements if the subsequent token looks like '--'. The logic here is subtle:
//
// If we are 'begin', then we expect to be invoked with no arguments.
// If we are 'function', then we are a non-block if we are invoked with -h or --help
// If we are anything else, we require an argument, so do the same thing if the subsequent token
// is a statement terminator.
if (token1.type == parse_token_type_string) {
// If we are a function, then look for help arguments. Otherwise, if the next token looks
// like an option (starts with a dash), then parse it as a decorated statement.
if (token1.keyword == parse_keyword_function && token2.is_help_argument) {
2018-01-08 07:53:36 +08:00
return production_for<decorated>();
} else if (token1.keyword != parse_keyword_function && token2.has_dash_prefix) {
2018-01-08 07:53:36 +08:00
return production_for<decorated>();
}
// Likewise if the next token doesn't look like an argument at all. This corresponds to e.g.
// a "naked if".
bool naked_invocation_invokes_help =
(token1.keyword != parse_keyword_begin && token1.keyword != parse_keyword_end);
if (naked_invocation_invokes_help &&
(token2.type == parse_token_type_end || token2.type == parse_token_type_terminate)) {
2018-01-08 07:53:36 +08:00
return production_for<decorated>();
}
}
switch (token1.type) {
case parse_token_type_string: {
switch (token1.keyword) {
case parse_keyword_not: {
return production_for<nots>();
}
2013-07-27 14:59:12 +08:00
case parse_keyword_for:
case parse_keyword_while:
case parse_keyword_function:
case parse_keyword_begin: {
2018-01-08 07:53:36 +08:00
return production_for<block>();
}
case parse_keyword_if: {
2018-01-08 07:53:36 +08:00
return production_for<ifs>();
}
case parse_keyword_else: {
2013-07-27 14:59:12 +08:00
return NO_PRODUCTION;
}
case parse_keyword_switch: {
2018-01-08 07:53:36 +08:00
return production_for<switchs>();
}
case parse_keyword_end: {
2013-07-27 14:59:12 +08:00
return NO_PRODUCTION;
}
// All other keywords fall through to decorated statement.
2018-01-08 07:53:36 +08:00
default: { return production_for<decorated>(); }
2013-07-27 14:59:12 +08:00
}
break;
}
2013-07-27 14:59:12 +08:00
case parse_token_type_pipe:
case parse_token_type_redirection:
case parse_token_type_background:
case parse_token_type_terminate: {
2013-07-27 14:59:12 +08:00
return NO_PRODUCTION;
}
default: { return NO_PRODUCTION; }
2013-07-27 14:59:12 +08:00
}
}
RESOLVE(else_clause) {
UNUSED(token2);
UNUSED(out_tag);
switch (token1.keyword) {
case parse_keyword_else: {
2018-01-08 07:53:36 +08:00
return production_for<else_cont>();
}
2018-01-08 07:53:36 +08:00
default: { return production_for<empty>(); }
2013-07-27 14:59:12 +08:00
}
}
RESOLVE(else_continuation) {
UNUSED(token2);
UNUSED(out_tag);
switch (token1.keyword) {
case parse_keyword_if: {
2018-01-08 07:53:36 +08:00
return production_for<else_if>();
}
2018-01-08 07:53:36 +08:00
default: { return production_for<else_only>(); }
2013-07-27 14:59:12 +08:00
}
}
RESOLVE(case_item_list) {
UNUSED(token2);
UNUSED(out_tag);
if (token1.keyword == parse_keyword_case)
2018-01-08 07:53:36 +08:00
return production_for<case_items>();
else if (token1.type == parse_token_type_end)
2018-01-08 07:53:36 +08:00
return production_for<blank_line>();
else
2018-01-08 07:53:36 +08:00
return production_for<empty>();
2013-07-27 14:59:12 +08:00
}
RESOLVE(andor_job_list) {
UNUSED(out_tag);
if (token1.type == parse_token_type_end) {
2018-01-08 07:53:36 +08:00
return production_for<empty_line>();
} else if (token1.keyword == parse_keyword_and || token1.keyword == parse_keyword_or) {
// Check that the argument to and/or is a string that's not help. Otherwise it's either 'and
// --help' or a naked 'and', and not part of this list.
if (token2.type == parse_token_type_string && !token2.is_help_argument) {
2018-01-08 07:53:36 +08:00
return production_for<andor_job>();
}
}
// All other cases end the list.
2018-01-08 07:53:36 +08:00
return production_for<empty>();
}
RESOLVE(argument_list) {
UNUSED(token2);
UNUSED(out_tag);
switch (token1.type) {
case parse_token_type_string: {
2018-01-08 07:53:36 +08:00
return production_for<arg>();
}
2018-01-08 07:53:36 +08:00
default: { return production_for<empty>(); }
2013-07-27 14:59:12 +08:00
}
}
RESOLVE(freestanding_argument_list) {
UNUSED(token2);
UNUSED(out_tag);
switch (token1.type) {
case parse_token_type_string: {
2018-01-08 07:53:36 +08:00
return production_for<arg>();
}
case parse_token_type_end: {
2018-01-08 07:53:36 +08:00
return production_for<semicolon>();
}
2018-01-08 07:53:36 +08:00
default: { return production_for<empty>(); }
}
}
RESOLVE(block_header) {
UNUSED(token2);
UNUSED(out_tag);
switch (token1.keyword) {
case parse_keyword_for: {
2018-01-08 07:53:36 +08:00
return production_for<forh>();
}
case parse_keyword_while: {
2018-01-08 07:53:36 +08:00
return production_for<whileh>();
}
case parse_keyword_function: {
2018-01-08 07:53:36 +08:00
return production_for<funch>();
}
case parse_keyword_begin: {
2018-01-08 07:53:36 +08:00
return production_for<beginh>();
}
default: { return NO_PRODUCTION; }
2013-07-27 14:59:12 +08:00
}
}
RESOLVE(decorated_statement) {
// If this is e.g. 'command --help' then the command is 'command' and not a decoration. If the
// second token is not a string, then this is a naked 'command' and we should execute it as
// undecorated.
if (token2.type != parse_token_type_string || token2.has_dash_prefix) {
2018-01-08 07:53:36 +08:00
return production_for<plains>();
}
switch (token1.keyword) {
case parse_keyword_command: {
*out_tag = parse_statement_decoration_command;
2018-01-08 07:53:36 +08:00
return production_for<cmds>();
}
case parse_keyword_builtin: {
*out_tag = parse_statement_decoration_builtin;
2018-01-08 07:53:36 +08:00
return production_for<builtins>();
}
case parse_keyword_exec: {
*out_tag = parse_statement_decoration_exec;
2018-01-08 07:53:36 +08:00
return production_for<execs>();
}
default: {
*out_tag = parse_statement_decoration_none;
2018-01-08 07:53:36 +08:00
return production_for<plains>();
}
2013-07-27 14:59:12 +08:00
}
}
RESOLVE(arguments_or_redirections_list) {
UNUSED(token2);
UNUSED(out_tag);
switch (token1.type) {
2013-07-27 14:59:12 +08:00
case parse_token_type_string:
2018-01-08 07:53:36 +08:00
return production_for<arg>();
case parse_token_type_redirection:
2018-01-08 07:53:36 +08:00
return production_for<redir>();
default:
return production_for<empty>();
2013-07-29 06:19:38 +08:00
}
}
RESOLVE(optional_newlines) {
UNUSED(token2);
if (token1.is_newline) return production_for<newlines>();
return production_for<empty>();
}
RESOLVE(optional_background) {
UNUSED(token2);
switch (token1.type) {
case parse_token_type_background: {
*out_tag = parse_background;
2018-01-08 07:53:36 +08:00
return production_for<background>();
}
default: {
*out_tag = parse_no_background;
2018-01-08 07:53:36 +08:00
return production_for<empty>();
}
2013-07-29 06:19:38 +08:00
}
}
2016-11-02 10:12:14 +08:00
const production_element_t *parse_productions::production_for_token(parse_token_type_t node_type,
const parse_token_t &input1,
const parse_token_t &input2,
parse_node_tag_t *out_tag) {
debug(5, "Resolving production for %ls with input token <%ls>",
2016-10-30 10:01:19 +08:00
token_type_description(node_type), input1.describe().c_str());
2013-08-11 15:35:00 +08:00
// Fetch the function to resolve the list of productions.
const production_element_t *(*resolver)(const parse_token_t &input1, //!OCLINT(unused param)
const parse_token_t &input2, //!OCLINT(unused param)
parse_node_tag_t *out_tag) = //!OCLINT(unused param)
NULL;
switch (node_type) {
// Handle all of our grammar elements
#define ELEM(SYM) \
case (symbol_##SYM): \
resolver = SYM::resolve; \
break;
#include "parse_grammar_elements.inc"
2013-08-11 15:35:00 +08:00
// Everything else is an error.
2013-07-29 06:19:38 +08:00
case parse_token_type_string:
case parse_token_type_pipe:
case parse_token_type_redirection:
case parse_token_type_background:
case parse_token_type_andand:
case parse_token_type_oror:
2013-07-29 06:19:38 +08:00
case parse_token_type_end:
case parse_token_type_terminate: {
debug(0, "Terminal token type %ls passed to %s", token_type_description(node_type),
__FUNCTION__);
2013-07-29 06:19:38 +08:00
PARSER_DIE();
break;
}
2013-08-09 06:06:46 +08:00
case parse_special_type_parse_error:
case parse_special_type_tokenizer_error:
case parse_special_type_comment: {
debug(0, "Special type %ls passed to %s\n", token_type_description(node_type),
__FUNCTION__);
2013-08-09 06:06:46 +08:00
PARSER_DIE();
break;
}
case token_type_invalid: {
debug(0, "token_type_invalid passed to %s", __FUNCTION__);
2013-07-29 06:19:38 +08:00
PARSER_DIE();
break;
}
2013-07-29 06:19:38 +08:00
}
PARSE_ASSERT(resolver != NULL);
2013-08-11 15:35:00 +08:00
const production_element_t *result = resolver(input1, input2, out_tag);
2016-10-30 10:01:19 +08:00
if (result == NULL) {
debug(5, "Node type '%ls' has no production for input '%ls' (in %s)",
2016-10-30 10:01:19 +08:00
token_type_description(node_type), input1.describe().c_str(), __FUNCTION__);
2013-07-29 06:19:38 +08:00
}
2013-07-29 06:19:38 +08:00
return result;
}