2013-06-09 14:21:24 -07:00
/**\file parse_tree.h
2013-05-26 12:12:16 -07:00
Programmatic representation of fish code .
*/
2013-07-25 15:24:22 -07:00
# ifndef FISH_PARSE_PRODUCTIONS_H
# define FISH_PARSE_PRODUCTIONS_H
2013-05-26 12:12:16 -07:00
# include <wchar.h>
# include "config.h"
# include "util.h"
# include "common.h"
2013-06-01 22:14:47 -07:00
# include "tokenizer.h"
2013-12-08 21:54:06 -08:00
# include "parse_constants.h"
2013-06-11 09:37:51 -07:00
# include <vector>
2013-10-14 09:12:45 +02:00
# include <inttypes.h>
2013-06-11 09:37:51 -07:00
2013-06-15 14:32:38 -07:00
class parse_node_t ;
2013-06-24 12:33:40 -07:00
class parse_node_tree_t ;
2013-06-15 14:32:38 -07:00
typedef size_t node_offset_t ;
2013-06-23 02:09:46 -07:00
# define NODE_OFFSET_INVALID (static_cast<node_offset_t>(-1))
2013-06-15 14:32:38 -07:00
2013-06-15 15:21:35 -07:00
struct parse_error_t
{
/** Text of the error */
wcstring text ;
2013-12-08 21:54:06 -08:00
/** Code for the error */
enum parse_error_code_t code ;
2013-07-22 18:26:15 -07:00
2013-06-15 15:21:35 -07:00
/** Offset and length of the token in the source code that triggered this error */
size_t source_start ;
size_t source_length ;
2013-07-22 18:26:15 -07:00
2013-06-15 15:21:35 -07:00
/** Return a string describing the error, suitable for presentation to the user */
wcstring describe ( const wcstring & src ) const ;
} ;
typedef std : : vector < parse_error_t > parse_error_list_t ;
2013-05-26 12:12:16 -07:00
2013-10-12 02:46:49 -07:00
/** A struct representing the token type that we use internally */
struct parse_token_t
{
enum parse_token_type_t type ; // The type of the token as represented by the parser
enum parse_keyword_t keyword ; // Any keyword represented by this token
bool has_dash_prefix ; // Hackish: whether the source contains a dash prefix
size_t source_start ;
size_t source_length ;
2013-08-11 00:35:00 -07:00
2013-10-12 02:46:49 -07:00
wcstring describe ( ) const ;
2013-08-11 00:35:00 -07:00
} ;
enum
{
parse_flag_none = 0 ,
/* Attempt to build a "parse tree" no matter what. This may result in a 'forest' of disconnected trees. This is intended to be used by syntax highlighting. */
parse_flag_continue_after_error = 1 < < 0 ,
2013-10-06 16:23:45 -07:00
2013-08-11 00:35:00 -07:00
/* Include comment tokens */
2013-10-09 02:03:50 -07:00
parse_flag_include_comments = 1 < < 1 ,
/* Indicate that the tokenizer should accept incomplete tokens */
parse_flag_accept_incomplete_tokens = 1 < < 2
2013-06-11 09:37:51 -07:00
} ;
2013-08-11 00:35:00 -07:00
typedef unsigned int parse_tree_flags_t ;
class parse_ll_t ;
class parse_t
{
parse_ll_t * const parser ;
2013-10-12 01:17:55 -07:00
bool parse_internal ( const wcstring & str , parse_tree_flags_t flags , parse_node_tree_t * output , parse_error_list_t * errors , bool log_it = false ) ;
2013-08-11 00:35:00 -07:00
public :
parse_t ( ) ;
~ parse_t ( ) ;
2013-10-12 01:17:55 -07:00
/* Parse a string all at once */
static bool parse ( const wcstring & str , parse_tree_flags_t flags , parse_node_tree_t * output , parse_error_list_t * errors , bool log_it = false ) ;
2013-08-11 00:35:00 -07:00
/* Parse a single token */
bool parse_1_token ( parse_token_type_t token , parse_keyword_t keyword , parse_node_tree_t * output , parse_error_list_t * errors ) ;
/* Reset, ready to parse something else */
void clear ( ) ;
} ;
wcstring parse_dump_tree ( const parse_node_tree_t & tree , const wcstring & src ) ;
2013-06-11 09:37:51 -07:00
2013-06-23 02:09:46 -07:00
wcstring token_type_description ( parse_token_type_t type ) ;
wcstring keyword_description ( parse_keyword_t type ) ;
2013-10-06 16:23:45 -07:00
/** Class for nodes of a parse tree */
2013-06-11 09:37:51 -07:00
class parse_node_t
{
2013-07-22 18:26:15 -07:00
public :
2013-06-11 09:37:51 -07:00
/* Type of the node */
enum parse_token_type_t type ;
2013-07-22 18:26:15 -07:00
2013-06-11 09:37:51 -07:00
/* Start in the source code */
size_t source_start ;
2013-07-22 18:26:15 -07:00
2013-06-11 09:37:51 -07:00
/* Length of our range in the source code */
size_t source_length ;
2013-10-07 01:04:37 -07:00
/* Parent */
node_offset_t parent ;
2013-06-11 09:37:51 -07:00
/* Children */
node_offset_t child_start ;
2013-10-09 03:45:58 -07:00
uint8_t child_count ;
2013-07-22 18:26:15 -07:00
2013-07-28 15:19:38 -07:00
/* Which production was used */
uint8_t production_idx ;
2013-07-22 18:26:15 -07:00
2013-06-11 09:37:51 -07:00
/* Description */
wcstring describe ( void ) const ;
2013-07-22 18:26:15 -07:00
2013-06-11 09:37:51 -07:00
/* Constructor */
2013-10-09 02:03:50 -07:00
explicit parse_node_t ( parse_token_type_t ty ) : type ( ty ) , source_start ( - 1 ) , source_length ( 0 ) , parent ( NODE_OFFSET_INVALID ) , child_start ( 0 ) , child_count ( 0 )
2013-06-11 09:37:51 -07:00
{
}
2013-07-22 18:26:15 -07:00
2013-06-23 02:09:46 -07:00
node_offset_t child_offset ( node_offset_t which ) const
{
PARSE_ASSERT ( which < child_count ) ;
return child_start + which ;
}
2013-08-11 00:35:00 -07:00
2013-10-06 16:23:45 -07:00
/* Indicate if this node has a range of source code associated with it */
2013-08-08 15:06:46 -07:00
bool has_source ( ) const
{
return source_start ! = ( size_t ) ( - 1 ) ;
}
2013-10-08 15:05:30 -07:00
/* Gets source for the node, or the empty string if it has no source */
wcstring get_source ( const wcstring & str ) const
{
if ( ! has_source ( ) )
return wcstring ( ) ;
else
return wcstring ( str , this - > source_start , this - > source_length ) ;
}
2013-10-12 18:17:03 -07:00
/* Returns whether the given location is within the source range or at its end */
bool location_in_or_at_end_of_source_range ( size_t loc ) const
{
return has_source ( ) & & source_start < = loc & & loc - source_start < = source_length ;
}
2013-06-11 09:37:51 -07:00
} ;
2013-10-09 02:03:50 -07:00
2013-10-06 16:23:45 -07:00
/* The parse tree itself */
2013-06-24 12:33:40 -07:00
class parse_node_tree_t : public std : : vector < parse_node_t >
{
2013-08-11 00:35:00 -07:00
public :
2013-12-11 18:34:28 -08:00
/* Get the node corresponding to a child of the given node, or NULL if there is no such child. If expected_type is provided, assert that the node has that type.
*/
const parse_node_t * get_child ( const parse_node_t & parent , node_offset_t which , parse_token_type_t expected_type = token_type_invalid ) const ;
2013-10-07 01:04:37 -07:00
/* Get the node corresponding to the parent of the given node, or NULL if there is no such child. If expected_type is provided, only returns the parent if it is of that type. Note the asymmetry: get_child asserts since the children are known, but get_parent does not, since the parent may not be known. */
const parse_node_t * get_parent ( const parse_node_t & node , parse_token_type_t expected_type = token_type_invalid ) const ;
2013-12-11 18:34:28 -08:00
/* Returns the first ancestor of the given type, or NULL. */
const parse_node_t * get_first_ancestor_of_type ( const parse_node_t & node , parse_token_type_t desired_type ) const ;
2013-10-07 01:04:37 -07:00
2013-12-11 18:34:28 -08:00
/* Find all the nodes of a given type underneath a given node, up to max_count of them */
2013-08-08 15:06:46 -07:00
typedef std : : vector < const parse_node_t * > parse_node_list_t ;
2013-12-11 18:34:28 -08:00
parse_node_list_t find_nodes ( const parse_node_t & parent , parse_token_type_t type , size_t max_count = ( size_t ) ( - 1 ) ) const ;
2013-10-07 03:56:09 -07:00
2013-10-08 15:05:30 -07:00
/* Finds the last node of a given type underneath a given node, or NULL if it could not be found. If parent is NULL, this finds the last node in the tree of that type. */
const parse_node_t * find_last_node_of_type ( parse_token_type_t type , const parse_node_t * parent = NULL ) const ;
2013-10-12 18:17:03 -07:00
/* Finds a node containing the given source location */
const parse_node_t * find_node_matching_source_location ( parse_token_type_t type , size_t source_loc , const parse_node_t * parent ) const ;
2013-10-07 03:56:09 -07:00
/* Indicate if the given argument_list or arguments_or_redirections_list is a root list, or has a parent */
bool argument_list_is_root ( const parse_node_t & node ) const ;
2013-10-07 01:04:37 -07:00
2013-10-09 02:03:50 -07:00
/* Utilities */
2013-10-09 15:57:10 -07:00
/* Given a plain statement, get the decoration (from the parent node), or none if there is no decoration */
2013-10-09 02:03:50 -07:00
enum parse_statement_decoration_t decoration_for_plain_statement ( const parse_node_t & node ) const ;
2013-10-09 15:57:10 -07:00
/* Given a plain statement, get the command by reference (from the child node). Returns true if successful. Clears the command on failure. */
bool command_for_plain_statement ( const parse_node_t & node , const wcstring & src , wcstring * out_cmd ) const ;
2013-10-13 16:58:40 -07:00
2013-12-11 18:34:28 -08:00
/* Given a plain statement, return true if the statement is part of a pipeline. If include_first is set, the first command in a pipeline is considered part of it; otherwise only the second or additional commands are */
bool plain_statement_is_in_pipeline ( const parse_node_t & node , bool include_first ) const ;
2013-10-13 16:58:40 -07:00
/* Given a redirection, get the redirection type (or TOK_NONE) and target (file path, or fd) */
enum token_type type_for_redirection ( const parse_node_t & node , const wcstring & src , wcstring * out_target ) const ;
2013-12-11 18:34:28 -08:00
/* If the given node is a block statement, returns the header node (for_header, while_header, begin_header, or function_header). Otherwise returns NULL */
const parse_node_t * header_node_for_block_statement ( const parse_node_t & node ) ;
2013-10-07 01:04:37 -07:00
} ;
2013-05-26 12:12:16 -07:00
/* Fish grammar:
2013-06-23 02:09:46 -07:00
# A job_list is a list of jobs, separated by semicolons or newlines
2013-05-26 12:12:16 -07:00
2013-06-23 02:09:46 -07:00
job_list = < empty > |
job job_list
2013-08-11 00:35:00 -07:00
< TOK_END > job_list
2013-05-26 12:12:16 -07:00
2013-06-23 02:09:46 -07:00
# 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
2013-05-26 12:12:16 -07:00
2013-06-23 02:09:46 -07:00
job = statement job_continuation
2013-07-22 18:26:15 -07:00
job_continuation = < empty > |
2013-06-23 02:09:46 -07:00
< TOK_PIPE > statement job_continuation
# A statement is a normal command, or an if / while / and etc
2013-06-30 15:38:31 -07:00
statement = boolean_statement | block_statement | if_statement | switch_statement | decorated_statement
2013-07-22 18:26:15 -07:00
2013-05-26 12:12:16 -07:00
# A block is a conditional, loop, or begin / end
2013-12-11 18:34:28 -08:00
if_statement = if_clause else_clause end_command arguments_or_redirections_list
2013-06-27 15:12:27 -07:00
if_clause = < IF > job STATEMENT_TERMINATOR job_list
else_clause = < empty > |
< ELSE > else_continuation
else_continuation = if_clause else_clause |
STATEMENT_TERMINATOR job_list
2013-07-22 18:26:15 -07:00
2013-12-11 18:34:28 -08:00
switch_statement = SWITCH < TOK_STRING > STATEMENT_TERMINATOR case_item_list end_command
2013-06-30 15:38:31 -07:00
case_item_list = < empty > |
case_item case_item_list
case_item = CASE argument_list STATEMENT_TERMINATOR job_list
2013-07-22 18:26:15 -07:00
2013-12-11 18:34:28 -08:00
block_statement = block_header < TOK_END > job_list end_command arguments_or_redirections_list
2013-06-27 15:12:27 -07:00
block_header = for_header | while_header | function_header | begin_header
for_header = FOR var_name IN arguments_or_redirections_list
2013-05-26 12:12:16 -07:00
while_header = WHILE statement
2013-06-23 02:09:46 -07:00
begin_header = BEGIN
2013-06-30 15:38:31 -07:00
function_header = FUNCTION function_name argument_list
2013-07-22 18:26:15 -07:00
2013-05-26 12:12:16 -07:00
# A boolean statement is AND or OR or NOT
boolean_statement = AND statement | OR statement | NOT statement
2013-07-22 18:26:15 -07:00
2013-05-26 12:12:16 -07:00
# A decorated_statement is a command with a list of arguments_or_redirections, possibly with "builtin" or "command"
2013-10-08 15:05:30 -07:00
# TODO: we should be able to construct plain_statements out of e.g. 'command --help' or even just 'command'
2013-05-26 12:12:16 -07:00
2013-10-06 16:23:45 -07:00
decorated_statement = plain_statement | COMMAND plain_statement | BUILTIN plain_statement
plain_statement = < TOK_STRING > arguments_or_redirections_list optional_background
2013-05-26 12:12:16 -07:00
2013-10-07 01:04:37 -07:00
argument_list = < empty > | argument argument_list
2013-06-01 22:14:47 -07:00
arguments_or_redirections_list = < empty > |
argument_or_redirection arguments_or_redirections_list
2013-08-08 15:06:46 -07:00
argument_or_redirection = argument | redirection
argument = < TOK_STRING >
2013-10-13 16:58:40 -07:00
redirection = < TOK_REDIRECTION > < TOK_STRING >
2013-08-11 00:35:00 -07:00
2013-06-01 22:14:47 -07:00
terminator = < TOK_END > | < TOK_BACKGROUND >
2013-08-11 00:35:00 -07:00
2013-07-22 18:26:15 -07:00
optional_background = < empty > | < TOK_BACKGROUND >
2013-12-11 18:34:28 -08:00
end_command = END
2013-07-22 18:26:15 -07:00
2013-06-01 22:14:47 -07:00
*/
2013-05-26 12:12:16 -07:00
# endif