2013-07-25 22:24:22 +00:00
# include "parse_productions.h"
2013-06-09 02:20:26 +00:00
# include "tokenizer.h"
2013-06-02 05:14:47 +00:00
# include <vector>
2013-07-28 22:19:38 +00:00
using namespace parse_productions ;
2013-06-02 05:14:47 +00:00
2013-08-11 07:35:00 +00:00
/** Returns a string description of this parse error */
2013-06-15 22:21:35 +00:00
wcstring parse_error_t : : describe ( const wcstring & src ) const
{
wcstring result = text ;
if ( source_start < src . size ( ) & & source_start + source_length < = src . size ( ) )
{
// Locate the beginning of this line of source
size_t line_start = 0 ;
2013-07-23 01:26:15 +00:00
2013-06-15 22:21:35 +00:00
// Look for a newline prior to source_start. If we don't find one, start at the beginning of the string; otherwise start one past the newline
size_t newline = src . find_last_of ( L ' \n ' , source_start ) ;
2013-07-28 22:44:09 +00:00
//fprintf(stderr, "newline: %lu, source_start %lu, source_length %lu\n", newline, source_start, source_length);
2013-06-15 22:21:35 +00:00
if ( newline ! = wcstring : : npos )
{
2013-07-23 01:26:15 +00:00
line_start = newline ; // + 1;
2013-06-15 22:21:35 +00:00
}
2013-07-23 01:26:15 +00:00
2013-06-15 22:21:35 +00:00
size_t line_end = src . find ( L ' \n ' , source_start + source_length ) ;
if ( line_end = = wcstring : : npos )
{
line_end = src . size ( ) ;
}
assert ( line_end > = line_start ) ;
2013-07-28 22:44:09 +00:00
//fprintf(stderr, "source start: %lu, line start %lu\n", source_start, line_start);
2013-06-15 22:21:35 +00:00
assert ( source_start > = line_start ) ;
2013-07-23 01:26:15 +00:00
2013-06-15 22:21:35 +00:00
// Append the line of text
result . push_back ( L ' \n ' ) ;
result . append ( src , line_start , line_end - line_start ) ;
2013-07-23 01:26:15 +00:00
2013-06-15 22:21:35 +00:00
// Append the caret line
result . push_back ( L ' \n ' ) ;
result . append ( source_start - line_start , L ' ' ) ;
result . push_back ( L ' ^ ' ) ;
}
return result ;
}
2013-06-02 05:14:47 +00:00
2013-08-11 07:35:00 +00:00
/** Returns a string description of the given token type */
2013-06-23 09:09:46 +00:00
wcstring token_type_description ( parse_token_type_t type )
2013-06-09 02:20:26 +00:00
{
switch ( type )
{
2013-07-23 01:26:15 +00:00
case token_type_invalid :
return L " invalid " ;
case symbol_job_list :
return L " job_list " ;
case symbol_job :
return L " job " ;
case symbol_job_continuation :
return L " job_continuation " ;
case symbol_statement :
return L " statement " ;
case symbol_block_statement :
return L " block_statement " ;
case symbol_block_header :
return L " block_header " ;
case symbol_for_header :
return L " for_header " ;
case symbol_while_header :
return L " while_header " ;
case symbol_begin_header :
return L " begin_header " ;
case symbol_function_header :
return L " function_header " ;
case symbol_if_statement :
return L " if_statement " ;
case symbol_if_clause :
return L " if_clause " ;
case symbol_else_clause :
return L " else_clause " ;
case symbol_else_continuation :
return L " else_continuation " ;
case symbol_switch_statement :
return L " switch_statement " ;
case symbol_case_item_list :
return L " case_item_list " ;
case symbol_case_item :
return L " case_item " ;
case symbol_argument_list_nonempty :
return L " argument_list_nonempty " ;
case symbol_argument_list :
return L " argument_list " ;
case symbol_boolean_statement :
return L " boolean_statement " ;
case symbol_decorated_statement :
return L " decorated_statement " ;
case symbol_plain_statement :
return L " plain_statement " ;
case symbol_arguments_or_redirections_list :
return L " arguments_or_redirections_list " ;
case symbol_argument_or_redirection :
return L " argument_or_redirection " ;
2013-08-08 22:06:46 +00:00
case symbol_argument :
return L " symbol_argument " ;
case symbol_redirection :
return L " symbol_redirection " ;
2013-07-23 01:26:15 +00:00
case parse_token_type_string :
return L " token_string " ;
case parse_token_type_pipe :
return L " token_pipe " ;
case parse_token_type_redirection :
return L " token_redirection " ;
case parse_token_type_background :
return L " token_background " ;
case parse_token_type_end :
return L " token_end " ;
case parse_token_type_terminate :
return L " token_terminate " ;
case symbol_optional_background :
return L " optional_background " ;
2013-08-11 07:35:00 +00:00
2013-08-08 22:06:46 +00:00
case parse_special_type_parse_error :
return L " parse_error " ;
case parse_special_type_tokenizer_error :
return L " tokenizer_error " ;
case parse_special_type_comment :
return L " comment " ;
2013-06-09 02:20:26 +00:00
}
2013-07-23 01:26:15 +00:00
return format_string ( L " Unknown token type %ld " , static_cast < long > ( type ) ) ;
2013-06-09 02:20:26 +00:00
}
2013-06-23 09:09:46 +00:00
wcstring keyword_description ( parse_keyword_t k )
{
switch ( k )
{
2013-07-23 01:26:15 +00:00
case parse_keyword_none :
return L " none " ;
case parse_keyword_if :
return L " if " ;
case parse_keyword_else :
return L " else " ;
case parse_keyword_for :
return L " for " ;
case parse_keyword_in :
return L " in " ;
case parse_keyword_while :
return L " while " ;
case parse_keyword_begin :
return L " begin " ;
case parse_keyword_function :
return L " function " ;
case parse_keyword_switch :
return L " switch " ;
case parse_keyword_end :
return L " end " ;
case parse_keyword_and :
return L " and " ;
case parse_keyword_or :
return L " or " ;
case parse_keyword_not :
return L " not " ;
case parse_keyword_command :
return L " command " ;
case parse_keyword_builtin :
return L " builtin " ;
2013-06-23 09:09:46 +00:00
default :
return format_string ( L " Unknown keyword type %ld " , static_cast < long > ( k ) ) ;
}
}
2013-08-11 07:35:00 +00:00
/** Returns a string description of the given parse node */
2013-06-11 16:37:51 +00:00
wcstring parse_node_t : : describe ( void ) const
2013-06-02 05:14:47 +00:00
{
2013-06-11 16:37:51 +00:00
wcstring result = token_type_description ( type ) ;
return result ;
}
2013-06-02 05:14:47 +00:00
2013-08-11 07:35:00 +00:00
/** A struct representing the token type passed to */
2013-06-02 05:14:47 +00:00
struct parse_token_t
{
2013-06-09 02:20:26 +00:00
enum parse_token_type_t type ; // The type of the token as represented by the parser
2013-06-02 05:14:47 +00:00
enum parse_keyword_t keyword ; // Any keyword represented by this parser
size_t source_start ;
2013-06-09 02:20:26 +00:00
size_t source_length ;
2013-07-23 01:26:15 +00:00
2013-08-11 07:35:00 +00:00
wcstring describe ( ) const
2013-06-23 09:09:46 +00:00
{
2013-08-11 07:35:00 +00:00
wcstring result = token_type_description ( type ) ;
if ( keyword ! = parse_keyword_none )
{
append_format ( result , L " <%ls> " , keyword_description ( keyword ) . c_str ( ) ) ;
}
return result ;
2013-06-23 09:09:46 +00:00
}
2013-08-11 07:35:00 +00:00
} ;
2013-06-23 09:09:46 +00:00
2013-08-11 07:35:00 +00:00
/* Convert from tokenizer_t's token type to a parse_token_t type */
static parse_token_type_t parse_token_type_from_tokenizer_token ( enum token_type tokenizer_token_type )
2013-06-02 05:14:47 +00:00
{
2013-08-11 07:35:00 +00:00
parse_token_type_t result = token_type_invalid ;
2013-06-02 05:14:47 +00:00
switch ( tokenizer_token_type )
{
case TOK_STRING :
2013-08-11 07:35:00 +00:00
result = parse_token_type_string ;
2013-06-02 05:14:47 +00:00
break ;
2013-07-23 01:26:15 +00:00
2013-06-02 05:14:47 +00:00
case TOK_PIPE :
2013-08-11 07:35:00 +00:00
result = parse_token_type_pipe ;
2013-06-02 05:14:47 +00:00
break ;
2013-07-23 01:26:15 +00:00
2013-06-02 05:14:47 +00:00
case TOK_END :
2013-08-11 07:35:00 +00:00
result = parse_token_type_end ;
2013-06-02 05:14:47 +00:00
break ;
2013-07-23 01:26:15 +00:00
2013-06-02 05:14:47 +00:00
case TOK_BACKGROUND :
2013-08-11 07:35:00 +00:00
result = parse_token_type_background ;
2013-06-02 05:14:47 +00:00
break ;
2013-08-11 07:35:00 +00:00
2013-07-23 01:26:15 +00:00
case TOK_REDIRECT_OUT :
case TOK_REDIRECT_APPEND :
case TOK_REDIRECT_IN :
case TOK_REDIRECT_FD :
case TOK_REDIRECT_NOCLOB :
2013-08-11 07:35:00 +00:00
result = parse_token_type_redirection ;
2013-07-23 01:26:15 +00:00
break ;
2013-08-11 07:35:00 +00:00
2013-08-08 22:06:46 +00:00
case TOK_ERROR :
2013-08-11 07:35:00 +00:00
result = parse_special_type_tokenizer_error ;
2013-08-08 22:06:46 +00:00
break ;
2013-08-11 07:35:00 +00:00
2013-08-08 22:06:46 +00:00
case TOK_COMMENT :
2013-08-11 07:35:00 +00:00
result = parse_special_type_comment ;
2013-08-08 22:06:46 +00:00
break ;
2013-07-23 01:26:15 +00:00
2013-06-02 05:14:47 +00:00
default :
fprintf ( stderr , " Bad token type %d passed to %s \n " , ( int ) tokenizer_token_type , __FUNCTION__ ) ;
assert ( 0 ) ;
break ;
}
return result ;
}
2013-08-11 07:35:00 +00:00
/* Helper function for dump_tree */
2013-06-09 21:21:24 +00:00
static void dump_tree_recursive ( const parse_node_tree_t & nodes , const wcstring & src , size_t start , size_t indent , wcstring * result , size_t * line )
2013-06-02 05:14:47 +00:00
{
2013-06-09 02:20:26 +00:00
assert ( start < nodes . size ( ) ) ;
const parse_node_t & node = nodes . at ( start ) ;
2013-07-23 01:26:15 +00:00
2013-06-19 06:35:04 +00:00
const size_t spacesPerIndent = 2 ;
2013-07-23 01:26:15 +00:00
2013-06-19 06:35:04 +00:00
// unindent statement lists by 1 to flatten them
2013-06-23 09:09:46 +00:00
if ( node . type = = symbol_job_list | | node . type = = symbol_arguments_or_redirections_list )
2013-06-19 06:35:04 +00:00
{
if ( indent > 0 ) indent - = 1 ;
}
2013-07-23 01:26:15 +00:00
2013-06-23 09:09:46 +00:00
append_format ( * result , L " %2lu - %l2u " , * line , start ) ;
2013-06-19 06:35:04 +00:00
result - > append ( indent * spacesPerIndent , L ' ' ) ; ;
2013-06-09 02:20:26 +00:00
result - > append ( node . describe ( ) ) ;
if ( node . child_count > 0 )
2013-06-02 05:14:47 +00:00
{
2013-06-09 02:20:26 +00:00
append_format ( * result , L " <%lu children> " , node . child_count ) ;
2013-06-02 05:14:47 +00:00
}
2013-06-09 02:20:26 +00:00
if ( node . type = = parse_token_type_string )
2013-06-02 05:14:47 +00:00
{
2013-08-08 22:06:46 +00:00
if ( node . source_start = = - 1 )
{
append_format ( * result , L " (no source) " ) ;
}
else
{
result - > append ( L " : \" " ) ;
result - > append ( src , node . source_start , node . source_length ) ;
result - > append ( L " \" " ) ;
}
2013-06-02 05:14:47 +00:00
}
2013-06-09 02:20:26 +00:00
result - > push_back ( L ' \n ' ) ;
+ + * line ;
for ( size_t child_idx = node . child_start ; child_idx < node . child_start + node . child_count ; child_idx + + )
2013-06-07 04:49:40 +00:00
{
2013-06-19 06:35:04 +00:00
dump_tree_recursive ( nodes , src , child_idx , indent + 1 , result , line ) ;
2013-06-07 04:49:40 +00:00
}
2013-06-09 02:20:26 +00:00
}
2013-06-07 04:49:40 +00:00
2013-08-11 07:35:00 +00:00
/* Gives a debugging textual description of a parse tree */
wcstring parse_dump_tree ( const parse_node_tree_t & nodes , const wcstring & src )
2013-06-07 04:49:40 +00:00
{
2013-06-09 02:20:26 +00:00
if ( nodes . empty ( ) )
return L " (empty!) " ;
2013-07-23 01:26:15 +00:00
2013-06-09 02:20:26 +00:00
size_t line = 0 ;
wcstring result ;
dump_tree_recursive ( nodes , src , 0 , 0 , & result , & line ) ;
return result ;
}
2013-06-07 04:49:40 +00:00
2013-08-11 07:35:00 +00:00
/* Struct representing elements of the symbol stack, used in the internal state of the LL parser */
2013-06-07 04:49:40 +00:00
struct parse_stack_element_t
{
enum parse_token_type_t type ;
enum parse_keyword_t keyword ;
2013-06-09 02:20:26 +00:00
node_offset_t node_idx ;
2013-07-23 01:26:15 +00:00
2013-07-28 22:19:38 +00:00
explicit parse_stack_element_t ( parse_token_type_t t , node_offset_t idx ) : type ( t ) , keyword ( parse_keyword_none ) , node_idx ( idx )
2013-06-02 05:14:47 +00:00
{
2013-06-07 04:49:40 +00:00
}
2013-08-11 07:35:00 +00:00
2013-07-28 22:19:38 +00:00
explicit parse_stack_element_t ( production_element_t e , node_offset_t idx ) : type ( production_element_type ( e ) ) , keyword ( production_element_keyword ( e ) ) , node_idx ( idx )
2013-06-07 04:49:40 +00:00
{
2013-06-02 05:14:47 +00:00
}
2013-07-23 01:26:15 +00:00
2013-06-27 22:12:27 +00:00
wcstring describe ( void ) const
{
wcstring result = token_type_description ( type ) ;
if ( keyword ! = parse_keyword_none )
{
append_format ( result , L " <%ls> " , keyword_description ( keyword ) . c_str ( ) ) ;
}
return result ;
}
2013-06-02 05:14:47 +00:00
} ;
2013-08-11 07:35:00 +00:00
/* The parser itself, private implementation of class parse_t. This is a hand-coded table-driven LL parser. Most hand-coded LL parsers are recursive descent, but recursive descent parsers are difficult to "pause", unlike table-driven parsers. */
2013-06-02 05:14:47 +00:00
class parse_ll_t
{
2013-08-11 07:35:00 +00:00
/* Traditional symbol stack of the LL parser */
std : : vector < parse_stack_element_t > symbol_stack ;
/* Parser output. This is a parse tree, but stored in an array. */
2013-06-09 21:21:24 +00:00
parse_node_tree_t nodes ;
2013-07-23 01:26:15 +00:00
2013-08-11 07:35:00 +00:00
/* Whether we ran into a fatal error, including parse errors or tokenizer errors */
2013-06-15 22:21:35 +00:00
bool fatal_errored ;
2013-08-11 07:35:00 +00:00
2013-10-06 23:23:45 +00:00
/* Whether we should collect error messages or not */
bool should_generate_error_messages ;
2013-08-11 07:35:00 +00:00
/* List of errors we have encountered */
2013-06-15 22:21:35 +00:00
parse_error_list_t errors ;
2013-10-06 23:23:45 +00:00
2013-08-11 07:35:00 +00:00
/* The symbol stack can contain terminal types or symbols. Symbols go on to do productions, but terminal types are just matched against input tokens. */
bool top_node_handle_terminal_types ( parse_token_t token ) ;
2013-07-23 01:26:15 +00:00
2013-06-02 05:14:47 +00:00
void parse_error ( const wchar_t * expected , parse_token_t token ) ;
2013-07-23 01:26:15 +00:00
void parse_error ( parse_token_t token , const wchar_t * format , . . . ) ;
2013-06-15 22:21:35 +00:00
void append_error_callout ( wcstring & error_message , parse_token_t token ) ;
2013-07-23 01:26:15 +00:00
2013-06-27 22:12:27 +00:00
void dump_stack ( void ) const ;
2013-07-23 01:26:15 +00:00
2013-06-09 02:20:26 +00:00
// Get the node corresponding to the top element of the stack
parse_node_t & node_for_top_symbol ( )
{
PARSE_ASSERT ( ! symbol_stack . empty ( ) ) ;
const parse_stack_element_t & top_symbol = symbol_stack . back ( ) ;
PARSE_ASSERT ( top_symbol . node_idx ! = - 1 ) ;
PARSE_ASSERT ( top_symbol . node_idx < nodes . size ( ) ) ;
return nodes . at ( top_symbol . node_idx ) ;
}
2013-07-23 01:26:15 +00:00
2013-06-02 05:14:47 +00:00
parse_token_type_t stack_top_type ( ) const
{
2013-06-07 04:49:40 +00:00
return symbol_stack . back ( ) . type ;
}
2013-07-23 01:26:15 +00:00
2013-06-09 02:20:26 +00:00
void top_node_set_tag ( uint32_t tag )
2013-06-07 04:49:40 +00:00
{
2013-06-09 02:20:26 +00:00
this - > node_for_top_symbol ( ) . tag = tag ;
2013-06-07 04:49:40 +00:00
}
2013-07-23 01:26:15 +00:00
2013-06-09 02:20:26 +00:00
inline void add_child_to_node ( size_t parent_node_idx , parse_stack_element_t * tok )
2013-06-07 04:49:40 +00:00
{
2013-06-09 02:20:26 +00:00
PARSE_ASSERT ( tok - > type ! = token_type_invalid ) ;
tok - > node_idx = nodes . size ( ) ;
nodes . push_back ( parse_node_t ( tok - > type ) ) ;
nodes . at ( parent_node_idx ) . child_count + = 1 ;
2013-06-02 05:14:47 +00:00
}
2013-07-23 01:26:15 +00:00
2013-07-21 22:22:11 +00:00
inline void symbol_stack_pop ( )
{
symbol_stack . pop_back ( ) ;
}
2013-06-09 02:20:26 +00:00
2013-07-28 22:19:38 +00:00
// Pop from the top of the symbol stack, then push the given production, updating node counts. Note that production_t has type "pointer to array" so some care is required.
inline void symbol_stack_pop_push_production ( const production_t * production )
2013-06-02 05:14:47 +00:00
{
2013-07-28 22:19:38 +00:00
bool logit = false ;
if ( logit )
2013-06-19 06:35:04 +00:00
{
2013-07-28 22:19:38 +00:00
size_t count = 0 ;
fprintf ( stderr , " Applying production: \n " ) ;
for ( size_t i = 0 ; i < MAX_SYMBOLS_PER_PRODUCTION ; i + + )
{
production_element_t elem = ( * production ) [ i ] ;
if ( production_element_is_valid ( elem ) )
{
parse_token_type_t type = production_element_type ( elem ) ;
parse_keyword_t keyword = production_element_keyword ( elem ) ;
fprintf ( stderr , " \t %ls <%ls> \n " , token_type_description ( type ) . c_str ( ) , keyword_description ( keyword ) . c_str ( ) ) ;
count + + ;
}
}
if ( ! count ) fprintf ( stderr , " \t <empty> \n " ) ;
2013-06-19 06:35:04 +00:00
}
2013-07-23 01:26:15 +00:00
2013-08-11 07:35:00 +00:00
2013-07-28 22:19:38 +00:00
// Add the children. Confusingly, we want our nodes to be in forwards order (last token last, so dumps look nice), but the symbols should be reverse order (last token first, so it's lowest on the stack)
const size_t child_start = nodes . size ( ) ;
size_t child_count = 0 ;
for ( size_t i = 0 ; i < MAX_SYMBOLS_PER_PRODUCTION ; i + + )
{
production_element_t elem = ( * production ) [ i ] ;
2013-08-11 07:35:00 +00:00
if ( ! production_element_is_valid ( elem ) )
{
// All done, bail out
break ;
}
else
2013-07-28 22:19:38 +00:00
{
// Generate the parse node. Note that this push_back may invalidate node.
2013-08-11 07:35:00 +00:00
parse_token_type_t child_type = production_element_type ( elem ) ;
nodes . push_back ( parse_node_t ( child_type ) ) ;
child_count + + ;
2013-07-28 22:19:38 +00:00
}
}
2013-08-11 07:35:00 +00:00
2013-07-28 22:19:38 +00:00
// Update the parent
const size_t parent_node_idx = symbol_stack . back ( ) . node_idx ;
parse_node_t & parent_node = nodes . at ( parent_node_idx ) ;
2013-08-11 07:35:00 +00:00
2013-06-09 02:20:26 +00:00
// Should have no children yet
2013-07-28 22:19:38 +00:00
PARSE_ASSERT ( parent_node . child_count = = 0 ) ;
2013-07-23 01:26:15 +00:00
2013-07-28 22:19:38 +00:00
// Tell the node about its children
parent_node . child_start = child_start ;
parent_node . child_count = child_count ;
2013-08-11 07:35:00 +00:00
2013-07-28 22:19:38 +00:00
// Replace the top of the stack with new stack elements corresponding to our new nodes. Note that these go in reverse order.
2013-06-07 04:49:40 +00:00
symbol_stack . pop_back ( ) ;
2013-07-28 22:19:38 +00:00
symbol_stack . reserve ( symbol_stack . size ( ) + child_count ) ;
size_t idx = child_count ;
while ( idx - - )
2013-07-11 06:45:09 +00:00
{
2013-07-28 22:19:38 +00:00
production_element_t elem = ( * production ) [ idx ] ;
PARSE_ASSERT ( production_element_is_valid ( elem ) ) ;
symbol_stack . push_back ( parse_stack_element_t ( elem , child_start + idx ) ) ;
2013-07-11 06:45:09 +00:00
}
}
2013-07-21 22:22:11 +00:00
2013-08-11 07:35:00 +00:00
public :
/* Constructor */
2013-10-06 23:23:45 +00:00
parse_ll_t ( ) : fatal_errored ( false ) , should_generate_error_messages ( true )
2013-08-11 07:35:00 +00:00
{
this - > symbol_stack . reserve ( 16 ) ;
this - > nodes . reserve ( 64 ) ;
this - > reset_symbols_and_nodes ( ) ;
}
/* Input */
void accept_token ( parse_token_t token ) ;
/* Indicate if we hit a fatal error */
bool has_fatal_error ( void ) const
{
return this - > fatal_errored ;
}
2013-10-06 23:23:45 +00:00
/* Indicate whether we want to generate error messages */
void set_should_generate_error_messages ( bool flag )
{
this - > should_generate_error_messages = flag ;
}
2013-08-11 07:35:00 +00:00
/* Clear the parse symbol stack (but not the node tree). Add a new job_list_t goal node. This is called from the constructor */
void reset_symbols ( void ) ;
/* Clear the parse symbol stack and the node tree. Add a new job_list_t goal node. This is called from the constructor. */
void reset_symbols_and_nodes ( void ) ;
/* Once parsing is complete, determine the ranges of intermediate nodes */
void determine_node_ranges ( ) ;
/* Acquire output after parsing. This transfers directly from within self */
void acquire_output ( parse_node_tree_t * output , parse_error_list_t * errors ) ;
2013-06-02 05:14:47 +00:00
} ;
2013-06-27 22:12:27 +00:00
void parse_ll_t : : dump_stack ( void ) const
{
// Walk backwards from the top, looking for parents
wcstring_list_t lines ;
if ( symbol_stack . empty ( ) )
{
lines . push_back ( L " (empty) " ) ;
}
else
{
node_offset_t child = symbol_stack . back ( ) . node_idx ;
node_offset_t cursor = child ;
lines . push_back ( nodes . at ( cursor ) . describe ( ) ) ;
while ( cursor - - )
{
const parse_node_t & node = nodes . at ( cursor ) ;
if ( node . child_start < = child & & node . child_start + node . child_count > child )
{
lines . push_back ( node . describe ( ) ) ;
child = cursor ;
}
}
}
2013-07-23 01:26:15 +00:00
2013-06-27 22:12:27 +00:00
fprintf ( stderr , " Stack dump (%lu elements): \n " , symbol_stack . size ( ) ) ;
for ( size_t idx = 0 ; idx < lines . size ( ) ; idx + + )
{
fprintf ( stderr , " %ls \n " , lines . at ( idx ) . c_str ( ) ) ;
}
}
2013-08-08 22:06:46 +00:00
// Give each node a source range equal to the union of the ranges of its children
// Terminal nodes already have source ranges (and no children)
// Since children always appear after their parents, we can implement this very simply by walking backwards
void parse_ll_t : : determine_node_ranges ( void )
{
const size_t source_start_invalid = - 1 ;
size_t idx = nodes . size ( ) ;
while ( idx - - )
{
parse_node_t * parent = & nodes . at ( idx ) ;
2013-08-11 07:35:00 +00:00
2013-08-08 22:06:46 +00:00
// Skip nodes that already have a source range. These are terminal nodes.
if ( parent - > source_start ! = source_start_invalid )
continue ;
2013-08-11 07:35:00 +00:00
2013-08-08 22:06:46 +00:00
// Ok, this node needs a source range. Get all of its children, and then set its range.
size_t min_start = source_start_invalid , max_end = 0 ; //note source_start_invalid is huge
for ( node_offset_t i = 0 ; i < parent - > child_count ; i + + )
{
const parse_node_t & child = nodes . at ( parent - > child_offset ( i ) ) ;
min_start = std : : min ( min_start , child . source_start ) ;
max_end = std : : max ( max_end , child . source_start + child . source_length ) ;
}
2013-08-11 07:35:00 +00:00
if ( min_start ! = source_start_invalid )
{
2013-08-08 22:06:46 +00:00
assert ( max_end > = min_start ) ;
parent - > source_start = min_start ;
parent - > source_length = max_end - min_start ;
}
}
}
2013-08-11 07:35:00 +00:00
void parse_ll_t : : acquire_output ( parse_node_tree_t * output , parse_error_list_t * errors )
{
if ( output ! = NULL )
{
std : : swap ( * output , this - > nodes ) ;
}
this - > nodes . clear ( ) ;
if ( errors ! = NULL )
{
std : : swap ( * errors , this - > errors ) ;
}
this - > errors . clear ( ) ;
this - > symbol_stack . clear ( ) ;
}
2013-07-23 01:26:15 +00:00
void parse_ll_t : : parse_error ( parse_token_t token , const wchar_t * fmt , . . . )
{
2013-10-06 23:23:45 +00:00
this - > fatal_errored = true ;
if ( this - > should_generate_error_messages )
{
//this->dump_stack();
parse_error_t err ;
2013-08-11 07:35:00 +00:00
2013-10-06 23:23:45 +00:00
va_list va ;
va_start ( va , fmt ) ;
err . text = vformat_string ( fmt , va ) ;
va_end ( va ) ;
2013-07-23 01:26:15 +00:00
2013-10-06 23:23:45 +00:00
err . source_start = token . source_start ;
err . source_length = token . source_length ;
this - > errors . push_back ( err ) ;
}
2013-06-02 05:14:47 +00:00
}
2013-07-23 01:26:15 +00:00
2013-06-02 05:14:47 +00:00
void parse_ll_t : : parse_error ( const wchar_t * expected , parse_token_t token )
{
2013-06-15 22:21:35 +00:00
fatal_errored = true ;
2013-10-06 23:23:45 +00:00
if ( this - > should_generate_error_messages )
{
wcstring desc = token_type_description ( token . type ) ;
parse_error_t error ;
error . text = format_string ( L " Expected a %ls, instead got a token of type %ls " , expected , desc . c_str ( ) ) ;
error . source_start = token . source_start ;
error . source_start = token . source_length ;
errors . push_back ( error ) ;
}
2013-06-02 05:14:47 +00:00
}
2013-08-11 07:35:00 +00:00
void parse_ll_t : : reset_symbols ( void )
2013-08-08 22:06:46 +00:00
{
2013-08-11 07:35:00 +00:00
/* Add a new job_list node, and then reset our symbol list to point at it */
2013-08-08 22:06:46 +00:00
node_offset_t where = nodes . size ( ) ;
nodes . push_back ( parse_node_t ( symbol_job_list ) ) ;
2013-08-11 07:35:00 +00:00
2013-08-08 22:06:46 +00:00
symbol_stack . clear ( ) ;
symbol_stack . push_back ( parse_stack_element_t ( symbol_job_list , where ) ) ; // goal token
this - > fatal_errored = false ;
}
2013-08-11 07:35:00 +00:00
/* Reset both symbols and nodes */
void parse_ll_t : : reset_symbols_and_nodes ( void )
{
nodes . clear ( ) ;
this - > reset_symbols ( ) ;
}
static bool type_is_terminal_type ( parse_token_type_t type )
{
switch ( type )
{
case parse_token_type_string :
case parse_token_type_pipe :
case parse_token_type_redirection :
case parse_token_type_background :
case parse_token_type_end :
case parse_token_type_terminate :
return true ;
default :
return false ;
}
}
2013-08-08 22:06:46 +00:00
2013-08-11 07:35:00 +00:00
bool parse_ll_t : : top_node_handle_terminal_types ( parse_token_t token )
2013-06-09 02:20:26 +00:00
{
2013-08-08 22:06:46 +00:00
if ( symbol_stack . empty ( ) )
{
// This can come about with an unbalanced 'end' or 'else', which causes us to terminate the outermost job list.
this - > fatal_errored = true ;
return false ;
}
2013-08-11 07:35:00 +00:00
2013-06-09 02:20:26 +00:00
PARSE_ASSERT ( ! symbol_stack . empty ( ) ) ;
PARSE_ASSERT ( token . type > = FIRST_PARSE_TOKEN_TYPE ) ;
2013-08-11 07:35:00 +00:00
bool handled = false ;
2013-06-09 02:20:26 +00:00
parse_stack_element_t & stack_top = symbol_stack . back ( ) ;
2013-08-11 07:35:00 +00:00
if ( type_is_terminal_type ( stack_top . type ) )
2013-06-09 02:20:26 +00:00
{
2013-08-11 07:35:00 +00:00
// The top of the stack is terminal. We are going to handle this (because we can't produce from a terminal type)
handled = true ;
// Now see if we actually matched
bool matched = false ;
if ( stack_top . type = = token . type )
{
switch ( stack_top . type )
{
case parse_token_type_string :
// We matched if the keywords match, or no keyword was required
matched = ( stack_top . keyword = = parse_keyword_none | | stack_top . keyword = = token . keyword ) ;
break ;
default :
// For other types, we only require that the types match
matched = true ;
break ;
}
}
if ( matched )
2013-06-09 02:20:26 +00:00
{
// Success. Tell the node that it matched this token
parse_node_t & node = node_for_top_symbol ( ) ;
node . source_start = token . source_start ;
node . source_length = token . source_length ;
}
2013-08-11 07:35:00 +00:00
else
2013-06-23 09:09:46 +00:00
{
2013-08-11 07:35:00 +00:00
// Failure
this - > fatal_errored = true ;
2013-06-23 09:09:46 +00:00
}
2013-08-11 07:35:00 +00:00
// We handled the token, so pop the symbol stack
symbol_stack . pop_back ( ) ;
2013-06-02 05:14:47 +00:00
}
2013-08-11 07:35:00 +00:00
return handled ;
2013-06-02 05:14:47 +00:00
}
2013-08-11 07:35:00 +00:00
void parse_ll_t : : accept_token ( parse_token_t token )
2013-06-02 05:14:47 +00:00
{
2013-07-23 01:26:15 +00:00
bool logit = false ;
2013-06-23 09:09:46 +00:00
if ( logit )
2013-06-19 06:35:04 +00:00
{
2013-06-23 09:09:46 +00:00
fprintf ( stderr , " Accept token %ls \n " , token . describe ( ) . c_str ( ) ) ;
2013-06-19 06:35:04 +00:00
}
2013-06-09 02:20:26 +00:00
PARSE_ASSERT ( token . type > = FIRST_PARSE_TOKEN_TYPE ) ;
2013-08-11 07:35:00 +00:00
2013-06-09 02:20:26 +00:00
bool consumed = false ;
2013-08-11 07:35:00 +00:00
2013-08-08 22:06:46 +00:00
// Handle special types specially. Note that these are the only types that can be pushed if the symbol stack is empty.
if ( token . type = = parse_special_type_parse_error | | token . type = = parse_special_type_tokenizer_error | | token . type = = parse_special_type_comment )
{
parse_node_t err_node ( token . type ) ;
err_node . source_start = token . source_start ;
err_node . source_length = token . source_length ;
nodes . push_back ( err_node ) ;
consumed = true ;
}
2013-06-15 22:21:35 +00:00
while ( ! consumed & & ! this - > fatal_errored )
2013-06-09 02:20:26 +00:00
{
2013-08-08 22:06:46 +00:00
PARSE_ASSERT ( ! symbol_stack . empty ( ) ) ;
2013-08-11 07:35:00 +00:00
if ( top_node_handle_terminal_types ( token ) )
2013-06-09 02:20:26 +00:00
{
2013-06-23 09:09:46 +00:00
if ( logit )
{
fprintf ( stderr , " Consumed token %ls \n " , token . describe ( ) . c_str ( ) ) ;
}
2013-06-09 02:20:26 +00:00
consumed = true ;
break ;
}
2013-08-11 07:35:00 +00:00
2013-08-08 22:06:46 +00:00
// top_node_match_token may indicate an error if our stack is empty
if ( this - > fatal_errored )
break ;
2013-08-11 07:35:00 +00:00
2013-07-28 22:19:38 +00:00
// Get the production for the top of the stack
parse_stack_element_t & stack_elem = symbol_stack . back ( ) ;
parse_node_t & node = nodes . at ( stack_elem . node_idx ) ;
2013-07-28 22:44:09 +00:00
const production_t * production = production_for_token ( stack_elem . type , token . type , token . keyword , & node . production_idx , & node . tag , NULL /* error text */ ) ;
if ( production = = NULL )
{
2013-10-06 23:23:45 +00:00
if ( should_generate_error_messages )
{
this - > parse_error ( token , L " Unable to produce a '%ls' from input '%ls' " , stack_elem . describe ( ) . c_str ( ) , token . describe ( ) . c_str ( ) ) ;
}
else
{
this - > parse_error ( token , NULL ) ;
}
2013-07-28 22:44:09 +00:00
// parse_error sets fatal_errored, which ends the loop
}
else
{
// Manipulate the symbol stack.
// Note that stack_elem is invalidated by popping the stack.
symbol_stack_pop_push_production ( production ) ;
2013-08-11 07:35:00 +00:00
2013-08-08 22:06:46 +00:00
// If we end up with an empty stack, something bad happened, like an unbalanced end
if ( symbol_stack . empty ( ) )
{
2013-10-06 23:23:45 +00:00
this - > parse_error ( token , L " All symbols removed from symbol stack. Likely unbalanced else or end? " ) ;
2013-08-08 22:06:46 +00:00
}
2013-07-28 22:44:09 +00:00
}
2013-06-09 02:20:26 +00:00
}
}
2013-06-02 05:14:47 +00:00
2013-06-09 02:20:26 +00:00
parse_t : : parse_t ( ) : parser ( new parse_ll_t ( ) )
2013-06-07 04:49:40 +00:00
{
}
2013-08-08 22:06:46 +00:00
parse_t : : ~ parse_t ( )
{
delete parser ;
}
2013-06-09 02:20:26 +00:00
static parse_keyword_t keyword_for_token ( token_type tok , const wchar_t * tok_txt )
2013-06-07 04:49:40 +00:00
{
2013-06-09 02:20:26 +00:00
parse_keyword_t result = parse_keyword_none ;
if ( tok = = TOK_STRING )
2013-06-07 04:49:40 +00:00
{
2013-07-23 01:26:15 +00:00
const struct
{
2013-06-09 02:20:26 +00:00
const wchar_t * txt ;
parse_keyword_t keyword ;
2013-07-23 01:26:15 +00:00
} keywords [ ] =
{
2013-06-09 02:20:26 +00:00
{ L " if " , parse_keyword_if } ,
{ L " else " , parse_keyword_else } ,
{ L " for " , parse_keyword_for } ,
{ L " in " , parse_keyword_in } ,
{ L " while " , parse_keyword_while } ,
{ L " begin " , parse_keyword_begin } ,
{ L " function " , parse_keyword_function } ,
{ L " switch " , parse_keyword_switch } ,
2013-06-30 22:38:31 +00:00
{ L " case " , parse_keyword_case } ,
2013-06-09 02:20:26 +00:00
{ L " end " , parse_keyword_end } ,
{ L " and " , parse_keyword_and } ,
{ L " or " , parse_keyword_or } ,
{ L " not " , parse_keyword_not } ,
{ L " command " , parse_keyword_command } ,
{ L " builtin " , parse_keyword_builtin }
} ;
2013-07-23 01:26:15 +00:00
2013-06-09 02:20:26 +00:00
for ( size_t i = 0 ; i < sizeof keywords / sizeof * keywords ; i + + )
{
if ( ! wcscmp ( keywords [ i ] . txt , tok_txt ) )
{
result = keywords [ i ] . keyword ;
break ;
}
}
2013-06-07 04:49:40 +00:00
}
2013-06-09 02:20:26 +00:00
return result ;
2013-06-07 04:49:40 +00:00
}
2013-10-06 23:23:45 +00:00
// Set type-specific tags for nodes
// This is not in parse_ll_t because it knows about different node types
static void tag_nodes ( const wcstring & src , parse_node_tree_t * tree )
{
size_t count = tree - > size ( ) ;
for ( size_t i = 0 ; i < count ; i + + )
{
const parse_node_t & node = tree - > at ( i ) ;
switch ( node . type )
{
case symbol_decorated_statement :
{
// Set a tag on the plain statement to indicate the decoration type
// The decoration types matches the production
bool is_decorated = ( node . production_idx > 0 ) ;
// Get the plain statement and set the tag equal to the production index we used
// This is an enum parse_statement_decoration_t
node_offset_t statement_idx = ( is_decorated ? 1 : 0 ) ;
parse_node_t * plain_statement = tree - > get_child ( node , statement_idx , symbol_plain_statement ) ;
if ( plain_statement ! = NULL )
{
plain_statement - > tag = static_cast < enum parse_statement_decoration_t > ( node . production_idx ) ;
}
}
break ;
default :
break ;
}
}
}
2013-08-08 22:06:46 +00:00
bool parse_t : : parse ( const wcstring & str , parse_tree_flags_t parse_flags , parse_node_tree_t * output , parse_error_list_t * errors , bool log_it )
2013-06-02 05:14:47 +00:00
{
2013-08-08 22:06:46 +00:00
tok_flags_t tok_options = TOK_SQUASH_ERRORS ;
if ( parse_flags & parse_flag_include_comments )
tok_options | = TOK_SHOW_COMMENTS ;
2013-10-06 23:23:45 +00:00
this - > parser - > set_should_generate_error_messages ( errors ! = NULL ) ;
2013-08-11 07:35:00 +00:00
2013-08-08 22:06:46 +00:00
tokenizer_t tok = tokenizer_t ( str . c_str ( ) , tok_options ) ;
2013-08-11 07:35:00 +00:00
for ( ; tok_has_next ( & tok ) & & ! this - > parser - > has_fatal_error ( ) ; tok_next ( & tok ) )
2013-06-09 02:20:26 +00:00
{
token_type tok_type = static_cast < token_type > ( tok_last_type ( & tok ) ) ;
const wchar_t * tok_txt = tok_last ( & tok ) ;
int tok_start = tok_get_pos ( & tok ) ;
2013-07-23 01:26:15 +00:00
size_t tok_extent = tok_get_extent ( & tok ) ;
2013-08-08 22:06:46 +00:00
assert ( tok_extent < 10000000 ) ; //paranoia
2013-07-23 01:26:15 +00:00
2013-08-11 07:35:00 +00:00
parse_token_t token ;
token . type = parse_token_type_from_tokenizer_token ( tok_type ) ;
2013-06-09 02:20:26 +00:00
token . source_start = ( size_t ) tok_start ;
2013-07-23 01:26:15 +00:00
token . source_length = tok_extent ;
2013-06-09 02:20:26 +00:00
token . keyword = keyword_for_token ( tok_type , tok_txt ) ;
2013-08-11 07:35:00 +00:00
this - > parser - > accept_token ( token ) ;
if ( this - > parser - > has_fatal_error ( ) )
2013-08-08 22:06:46 +00:00
{
if ( parse_flags & parse_flag_continue_after_error )
{
/* Mark an error and then keep going */
token . type = parse_special_type_parse_error ;
token . keyword = parse_keyword_none ;
2013-08-11 07:35:00 +00:00
this - > parser - > accept_token ( token ) ;
this - > parser - > reset_symbols ( ) ;
2013-08-08 22:06:46 +00:00
}
else
{
/* Bail out */
break ;
}
}
2013-06-09 02:20:26 +00:00
}
2013-06-15 22:21:35 +00:00
2013-08-08 22:06:46 +00:00
// Teach each node where its source range is
this - > parser - > determine_node_ranges ( ) ;
2013-10-06 23:23:45 +00:00
// Tag nodes
2013-08-08 22:06:46 +00:00
#if 0
2013-07-28 22:44:09 +00:00
wcstring result = dump_tree ( this - > parser - > nodes , str ) ;
2013-06-09 02:20:26 +00:00
fprintf ( stderr , " Tree (%ld nodes): \n %ls " , this - > parser - > nodes . size ( ) , result . c_str ( ) ) ;
2013-06-15 21:32:38 +00:00
fprintf ( stderr , " %lu nodes, node size %lu, %lu bytes \n " , this - > parser - > nodes . size ( ) , sizeof ( parse_node_t ) , this - > parser - > nodes . size ( ) * sizeof ( parse_node_t ) ) ;
2013-08-08 22:06:46 +00:00
# endif
2013-07-23 01:26:15 +00:00
2013-08-11 07:35:00 +00:00
// Acquire the output from the parser
this - > parser - > acquire_output ( output , errors ) ;
2013-10-06 23:23:45 +00:00
// Set node tags
tag_nodes ( str , output ) ;
2013-08-11 07:35:00 +00:00
// Indicate if we had a fatal error
return ! this - > parser - > has_fatal_error ( ) ;
}
2013-07-23 01:26:15 +00:00
2013-08-11 07:35:00 +00:00
bool parse_t : : parse_1_token ( parse_token_type_t token_type , parse_keyword_t keyword , parse_node_tree_t * output , parse_error_list_t * errors )
{
// Only strings can have keywords. So if we have a keyword, the type must be a string
assert ( keyword = = parse_keyword_none | | token_type = = parse_token_type_string ) ;
parse_token_t token ;
token . type = token_type ;
token . keyword = keyword ;
token . source_start = - 1 ;
token . source_length = 0 ;
2013-10-06 23:23:45 +00:00
bool wants_errors = ( errors ! = NULL ) ;
this - > parser - > set_should_generate_error_messages ( wants_errors ) ;
2013-07-23 01:26:15 +00:00
2013-08-11 07:35:00 +00:00
this - > parser - > accept_token ( token ) ;
return ! this - > parser - > has_fatal_error ( ) ;
}
void parse_t : : clear ( )
{
this - > parser - > reset_symbols_and_nodes ( ) ;
2013-06-02 05:14:47 +00:00
}
2013-08-08 22:06:46 +00:00
const parse_node_t * parse_node_tree_t : : get_child ( const parse_node_t & parent , node_offset_t which , parse_token_type_t expected_type ) const
{
const parse_node_t * result = NULL ;
PARSE_ASSERT ( which < parent . child_count ) ;
node_offset_t child_offset = parent . child_offset ( which ) ;
if ( child_offset < this - > size ( ) )
{
result = & this - > at ( child_offset ) ;
}
2013-08-11 07:35:00 +00:00
2013-08-08 22:06:46 +00:00
// If we are given an expected type, then the node must be null or that type
if ( result ! = NULL )
{
assert ( expected_type = = token_type_invalid | | expected_type = = result - > type ) ;
}
2013-08-11 07:35:00 +00:00
2013-08-08 22:06:46 +00:00
return result ;
}
2013-10-06 23:23:45 +00:00
/* Hackish non-const version of get_child */
parse_node_t * parse_node_tree_t : : get_child ( const parse_node_t & parent , node_offset_t which , parse_token_type_t expected_type )
{
const parse_node_tree_t * const_this = this ;
const parse_node_t * result = const_this - > get_child ( parent , which , expected_type ) ;
return const_cast < parse_node_t * > ( result ) ;
}
2013-08-08 22:06:46 +00:00
static void find_nodes_recursive ( const parse_node_tree_t & tree , const parse_node_t & parent , parse_token_type_t type , parse_node_tree_t : : parse_node_list_t * result )
{
if ( parent . type = = type ) result - > push_back ( & parent ) ;
for ( size_t i = 0 ; i < parent . child_count ; i + + )
{
const parse_node_t * child = tree . get_child ( parent , i ) ;
assert ( child ! = NULL ) ;
find_nodes_recursive ( tree , * child , type , result ) ;
}
}
parse_node_tree_t : : parse_node_list_t parse_node_tree_t : : find_nodes ( const parse_node_t & parent , parse_token_type_t type ) const
{
parse_node_list_t result ;
find_nodes_recursive ( * this , parent , type , & result ) ;
return result ;
}