// Programmatic representation of fish grammar #ifndef FISH_PARSE_GRAMMAR_H #define FISH_PARSE_GRAMMAR_H #include #include #include #include "parse_constants.h" #include "tokenizer.h" struct parse_token_t; typedef uint8_t parse_node_tag_t; using parse_node_tag_t = uint8_t; struct parse_token_t; namespace grammar { using production_element_t = uint8_t; enum { // The maximum length of any seq production. MAX_PRODUCTION_LENGTH = 6 }; // Define primitive types. template struct primitive { using type_tuple = std::tuple<>; static constexpr parse_token_type_t token = Token; static constexpr production_element_t element() { return Token; } }; using tok_end = primitive; using tok_string = primitive; using tok_pipe = primitive; using tok_background = primitive; using tok_redirection = primitive; using tok_andand = primitive; using tok_oror = primitive; // Define keyword types. template struct keyword { using type_tuple = std::tuple<>; static constexpr parse_token_type_t token = parse_token_type_string; static constexpr production_element_t element() { // Convert a parse_keyword_t enum to a production_element_t enum. return Keyword + LAST_TOKEN_OR_SYMBOL + 1; } }; // Define special types. // Comments are not emitted as part of productions, but specially by the parser. struct comment { using type_tuple = std::tuple<>; static constexpr parse_token_type_t token = parse_special_type_comment; }; // Forward declare all the symbol types. #define ELEM(T) struct T; #include "parse_grammar_elements.inc" // A production is a sequence of production elements. // +1 to hold the terminating token_type_invalid template using production_t = std::array; // This is an ugly hack to avoid ODR violations // Given some type, return a pointer to its production. template const production_element_t *production_for() { static constexpr auto prod = T::production; return prod.data(); } // Get some production element. template constexpr production_element_t element() { return T::element(); } // Template goo. namespace detail { template struct tuple_contains; template struct tuple_contains> : std::false_type {}; template struct tuple_contains> : tuple_contains> {}; template struct tuple_contains> : std::true_type {}; struct void_type { using type = void; }; // Support for checking whether the index N is valid for T::type_tuple. template static constexpr bool index_valid() { return N < std::tuple_size::value; } // Get the Nth type of T::type_tuple. template using tuple_element = std::tuple_element; // Get the Nth type of T::type_tuple, or void if N is out of bounds. template using tuple_element_or_void = typename std::conditional(), tuple_element, void_type>::type::type; // Make a tuple by mapping the Nth item of a list of 'seq's. template struct tuple_nther { // A tuple of the Nth types of tuples (or voids). using type = std::tuple...>; }; // Given a list of Options, each one a seq, check to see if any of them contain type Desired at // index Index. template inline constexpr bool type_possible() { using nths = typename tuple_nther::type; return tuple_contains::value; } } // namespace detail // Partial specialization hack. #define ELEM(T) \ template <> \ constexpr production_element_t element() { \ return symbol_##T; \ } #include "parse_grammar_elements.inc" // Empty produces nothing. struct empty { using type_tuple = std::tuple<>; static constexpr production_t<0> production = {{token_type_invalid}}; static const production_element_t *resolve(const parse_token_t &, const parse_token_t &, parse_node_tag_t *) { return production_for(); } }; // Sequence represents a list of (at least two) productions. template struct seq { static constexpr production_t<1 + sizeof...(Ts)> production = { {element(), element()..., token_type_invalid}}; static_assert(1 + sizeof...(Ts) <= MAX_PRODUCTION_LENGTH, "MAX_PRODUCTION_LENGTH too small"); using type_tuple = std::tuple; template static constexpr bool type_possible() { using element_t = detail::tuple_element_or_void; return std::is_same::value; } static const production_element_t *resolve(const parse_token_t &, const parse_token_t &, parse_node_tag_t *) { return production_for(); } }; template using produces_sequence = seq; // Ergonomic way to create a production for a single element. template using single = seq; template using produces_single = single; // Alternative represents a choice. struct alternative {}; // Following are the grammar productions. #define BODY(T) static constexpr parse_token_type_t token = symbol_##T; #define DEF(T) struct T : public #define DEF_ALT(T) struct T : public alternative #define ALT_BODY(T, ...) \ BODY(T) \ using type_tuple = std::tuple<>; \ template \ static constexpr bool type_possible() { \ return detail::type_possible(); \ } \ static const production_element_t *resolve(const parse_token_t &, const parse_token_t &, \ parse_node_tag_t *) // A job_list is a list of job_conjunctions, separated by semicolons or newlines DEF_ALT(job_list) { using normal = seq; using empty_line = seq; using empty = grammar::empty; ALT_BODY(job_list, normal, empty_line, empty); }; // Job decorators are 'and' and 'or'. These apply to the whole job. DEF_ALT(job_decorator) { using ands = single>; using ors = single>; using empty = grammar::empty; ALT_BODY(job_decorator, ands, ors, empty); }; // A job_conjunction is a job followed by a continuation. DEF(job_conjunction) produces_sequence{BODY(job_conjunction)}; DEF_ALT(job_conjunction_continuation) { using andands = seq; using orors = seq; using empty = grammar::empty; ALT_BODY(job_conjunction_continuation, andands, orors, empty); }; // A job is a non-empty list of statements, separated by pipes. (Non-empty is useful for cases // like if statements, where we require a command). To represent "non-empty", we require a // statement, followed by a possibly empty job_continuation, and then optionally a background // specifier '&' DEF(job) produces_sequence{BODY(job)}; DEF_ALT(job_continuation) { using piped = seq; using empty = grammar::empty; ALT_BODY(job_continuation, piped, empty); }; // A statement is a normal command, or an if / while / and etc DEF_ALT(statement) { using nots = single; using block = single; using ifs = single; using switchs = single; using decorated = single; ALT_BODY(statement, nots, block, ifs, switchs, decorated); }; // A block is a conditional, loop, or begin/end DEF(if_statement) produces_sequence{ BODY(if_statement)}; DEF(if_clause) produces_sequence, job_conjunction, tok_end, andor_job_list, job_list>{ BODY(if_clause)}; DEF_ALT(else_clause) { using empty = grammar::empty; using else_cont = seq, else_continuation>; ALT_BODY(else_clause, empty, else_cont); }; DEF_ALT(else_continuation) { using else_if = seq; using else_only = seq; ALT_BODY(else_continuation, else_if, else_only); }; DEF(switch_statement) produces_sequence, argument, tok_end, case_item_list, end_command, arguments_or_redirections_list>{BODY(switch_statement)}; DEF_ALT(case_item_list) { using empty = grammar::empty; using case_items = seq; using blank_line = seq; ALT_BODY(case_item_list, empty, case_items, blank_line); }; DEF(case_item) produces_sequence, argument_list, tok_end, job_list>{BODY(case_item)}; DEF(block_statement) produces_sequence{ BODY(block_statement)}; DEF_ALT(block_header) { using forh = single; using whileh = single; using funch = single; using beginh = single; ALT_BODY(block_header, forh, whileh, funch, beginh); }; DEF(for_header) produces_sequence, tok_string, keyword, argument_list, tok_end>{BODY(for_header)}; DEF(while_header) produces_sequence, job_conjunction, tok_end, andor_job_list>{ BODY(while_header)}; DEF(begin_header) produces_single>{BODY(begin_header)}; // Functions take arguments, and require at least one (the name). No redirections allowed. DEF(function_header) produces_sequence, argument, argument_list, tok_end>{ BODY(function_header)}; DEF_ALT(not_statement) { using nots = seq, statement>; using exclams = seq, statement>; ALT_BODY(not_statement, nots, exclams); }; // An andor_job_list is zero or more job lists, where each starts with an `and` or `or` boolean // statement. DEF_ALT(andor_job_list) { using empty = grammar::empty; using andor_job = seq; using empty_line = seq; ALT_BODY(andor_job_list, empty, andor_job, empty_line); }; // A decorated_statement is a command with a list of arguments_or_redirections, possibly with // "builtin" or "command" or "exec" DEF_ALT(decorated_statement) { using plains = single; using cmds = seq, plain_statement>; using builtins = seq, plain_statement>; using execs = seq, plain_statement>; ALT_BODY(decorated_statement, plains, cmds, builtins, execs); }; DEF(plain_statement) produces_sequence{BODY(plain_statement)}; DEF_ALT(argument_list) { using empty = grammar::empty; using arg = seq; ALT_BODY(argument_list, empty, arg); }; DEF_ALT(arguments_or_redirections_list) { using empty = grammar::empty; using arg = seq; using redir = seq; ALT_BODY(arguments_or_redirections_list, empty, arg, redir); }; DEF(argument) produces_single{BODY(argument)}; DEF(redirection) produces_sequence{BODY(redirection)}; DEF_ALT(optional_background) { using empty = grammar::empty; using background = single; ALT_BODY(optional_background, empty, background); }; DEF(end_command) produces_single>{BODY(end_command)}; // Note optional_newlines only allows newline-style tok_end, not semicolons. DEF_ALT(optional_newlines) { using empty = grammar::empty; using newlines = seq; ALT_BODY(optional_newlines, empty, newlines); }; // A freestanding_argument_list is equivalent to a normal argument list, except it may contain // TOK_END (newlines, and even semicolons, for historical reasons) DEF_ALT(freestanding_argument_list) { using empty = grammar::empty; using arg = seq; using semicolon = seq; ALT_BODY(freestanding_argument_list, empty, arg, semicolon); }; } // namespace grammar #endif