2013-07-23 09:26:15 +08:00
/**\file parse_tree.h
Programmatic representation of fish code .
*/
# ifndef FISH_PARSE_TREE_CONSTRUCTION_H
# define FISH_PARSE_TREE_CONSTRUCTION_H
# include "parse_tree.h"
2013-10-14 15:12:45 +08:00
# include <inttypes.h>
2013-07-23 09:26:15 +08:00
2013-07-26 06:24:22 +08:00
namespace parse_productions
{
# define MAX_PRODUCTIONS 5
2013-12-27 19:58:42 +08:00
# define MAX_SYMBOLS_PER_PRODUCTION 6
2013-07-26 06:24:22 +08:00
2013-07-29 06:19:38 +08:00
typedef uint32_t production_tag_t ;
2013-07-26 06:24:22 +08:00
2013-07-27 14:59:12 +08:00
/* A production is an array of unsigned char. Symbols are encoded directly as their symbol value. Keywords are encoded with an offset of LAST_TOKEN_OR_SYMBOL + 1. So essentially we glom together keywords and symbols. */
2013-07-29 06:19:38 +08:00
typedef uint8_t production_element_t ;
2013-07-23 09:26:15 +08:00
2013-07-29 06:19:38 +08:00
/* An index into a production option list */
typedef uint8_t production_option_idx_t ;
2013-07-23 09:26:15 +08:00
2013-07-29 06:44:09 +08:00
/* A production is an array of production elements */
typedef production_element_t const production_t [ MAX_SYMBOLS_PER_PRODUCTION ] ;
/* A production options is an array of (possible) productions */
typedef production_t production_options_t [ MAX_PRODUCTIONS ] ;
/* Resolve the type from a production element */
2013-07-29 06:19:38 +08:00
inline parse_token_type_t production_element_type ( production_element_t elem )
2013-07-23 09:26:15 +08:00
{
2013-07-29 06:19:38 +08:00
if ( elem > LAST_TOKEN_OR_SYMBOL )
2013-07-23 09:26:15 +08:00
{
2013-07-29 06:19:38 +08:00
return parse_token_type_string ;
2013-07-23 09:26:15 +08:00
}
2013-07-29 06:19:38 +08:00
else
2013-07-23 09:26:15 +08:00
{
2013-07-29 06:19:38 +08:00
return static_cast < parse_token_type_t > ( elem ) ;
2013-07-23 09:26:15 +08:00
}
2013-07-29 06:19:38 +08:00
}
2013-07-23 09:26:15 +08:00
2013-07-29 06:44:09 +08:00
/* Resolve the keyword from a production element */
2013-07-29 06:19:38 +08:00
inline parse_keyword_t production_element_keyword ( production_element_t elem )
2013-07-23 09:26:15 +08:00
{
2013-07-29 06:19:38 +08:00
if ( elem > LAST_TOKEN_OR_SYMBOL )
2013-07-23 09:26:15 +08:00
{
2013-07-29 06:19:38 +08:00
// First keyword is LAST_TOKEN_OR_SYMBOL + 1
return static_cast < parse_keyword_t > ( elem - LAST_TOKEN_OR_SYMBOL - 1 ) ;
2013-07-23 09:26:15 +08:00
}
2013-07-29 06:19:38 +08:00
else
2013-07-23 09:26:15 +08:00
{
2013-07-29 06:19:38 +08:00
return parse_keyword_none ;
2013-07-23 09:26:15 +08:00
}
2013-07-29 06:19:38 +08:00
}
2013-07-23 09:26:15 +08:00
2013-07-29 06:44:09 +08:00
/* Check if an element is valid */
2013-07-29 06:19:38 +08:00
inline bool production_element_is_valid ( production_element_t elem )
2013-07-23 09:26:15 +08:00
{
2013-07-29 06:19:38 +08:00
return elem ! = token_type_invalid ;
}
2013-07-23 09:26:15 +08:00
2013-10-10 06:57:10 +08:00
/* Fetch a production. We are passed two input tokens. The first input token is guaranteed to not be invalid; the second token may be invalid if there's no more tokens. */
2013-10-12 17:46:49 +08:00
const production_t * production_for_token ( parse_token_type_t node_type , const parse_token_t & input1 , const parse_token_t & input2 , production_option_idx_t * out_which_production , wcstring * out_error_text ) ;
2013-07-23 09:26:15 +08:00
}
2013-07-29 06:19:38 +08:00
2013-07-23 09:26:15 +08:00
# endif