2013-05-27 03:12:16 +08:00
# include "expression.h"
2013-06-09 10:20:26 +08:00
# include "tokenizer.h"
2013-06-02 13:14:47 +08:00
# include <vector>
2013-06-09 10:20:26 +08:00
2013-06-02 13:14:47 +08:00
/* Fish grammar:
# A statement_list is a list of statements, separated by semicolons or newlines
statement_list = < empty > |
statement statement_list
# A statement is a normal job, or an if / while / and etc.
statement = boolean_statement | block_statement | decorated_statement
# A block is a conditional, loop, or begin / end
block_statement = block_header statement_list END arguments_or_redirections_list
block_header = if_header | for_header | while_header | function_header | begin_header
if_header = IF statement
for_header = FOR var_name IN arguments_or_redirections_list STATEMENT_TERMINATOR
while_header = WHILE statement
begin_header = BEGIN STATEMENT_TERMINATOR
function_header = FUNCTION arguments_or_redirections_list STATEMENT_TERMINATOR
# A boolean statement is AND or OR or NOT
boolean_statement = AND statement | OR statement | NOT statement
# A decorated_statement is a command with a list of arguments_or_redirections, possibly with "builtin" or "command"
decorated_statement = COMMAND plain_statement | BUILTIN plain_statement | plain_statement
plain_statement = command arguments_or_redirections_list terminator
arguments_or_redirections_list = < empty > |
argument_or_redirection arguments_or_redirections_list
argument_or_redirection = redirection | < TOK_STRING >
redirection = < TOK_REDIRECTION >
terminator = < TOK_END > | < TOK_BACKGROUND >
*/
2013-06-09 10:20:26 +08:00
typedef size_t node_offset_t ;
2013-06-02 13:14:47 +08:00
# define PARSE_ASSERT(a) assert(a)
# define PARSER_DIE() assert(0)
class parse_command_t ;
enum parse_token_type_t
{
token_type_invalid ,
// Non-terminal tokens
symbol_statement_list ,
symbol_statement ,
symbol_block_statement ,
symbol_block_header ,
symbol_if_header ,
symbol_for_header ,
symbol_while_header ,
symbol_begin_header ,
symbol_function_header ,
symbol_boolean_statement ,
symbol_decorated_statement ,
symbol_plain_statement ,
symbol_arguments_or_redirections_list ,
symbol_argument_or_redirection ,
// Terminal types
parse_token_type_string ,
parse_token_type_pipe ,
parse_token_type_redirection ,
parse_token_background ,
parse_token_type_end ,
parse_token_type_terminate ,
2013-06-09 10:20:26 +08:00
FIRST_PARSE_TOKEN_TYPE = parse_token_type_string
2013-06-02 13:14:47 +08:00
} ;
2013-06-09 10:20:26 +08:00
static wcstring token_type_description ( parse_token_type_t type )
{
switch ( type )
{
case token_type_invalid : return L " invalid " ;
case symbol_statement_list : return L " statement_list " ;
case symbol_statement : return L " statement " ;
case symbol_block_statement : return L " block_statement " ;
case symbol_block_header : return L " block_header " ;
case symbol_if_header : return L " if_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_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 " ;
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_background : return L " token_background " ;
case parse_token_type_end : return L " token_end " ;
case parse_token_type_terminate : return L " token_terminate " ;
default : return format_string ( L " Unknown token type %ld " , static_cast < long > ( type ) ) ;
}
}
2013-06-02 13:14:47 +08:00
enum parse_keyword_t
{
parse_keyword_none ,
parse_keyword_if ,
parse_keyword_else ,
parse_keyword_for ,
parse_keyword_in ,
parse_keyword_while ,
parse_keyword_begin ,
parse_keyword_function ,
parse_keyword_switch ,
parse_keyword_end ,
parse_keyword_and ,
parse_keyword_or ,
parse_keyword_not ,
parse_keyword_command ,
parse_keyword_builtin
} ;
struct parse_token_t
{
2013-06-09 10:20:26 +08:00
enum parse_token_type_t type ; // The type of the token as represented by the parser
2013-06-02 13:14:47 +08:00
enum token_type tokenizer_type ; // The type of the token as represented by the tokenizer
enum parse_keyword_t keyword ; // Any keyword represented by this parser
size_t source_start ;
2013-06-09 10:20:26 +08:00
size_t source_length ;
2013-06-02 13:14:47 +08:00
} ;
// Convert from tokenizer_t's token type to our token
static parse_token_t parse_token_from_tokenizer_token ( enum token_type tokenizer_token_type )
{
parse_token_t result = { } ;
result . tokenizer_type = tokenizer_token_type ;
switch ( tokenizer_token_type )
{
case TOK_STRING :
result . type = parse_token_type_string ;
break ;
case TOK_PIPE :
result . type = parse_token_type_pipe ;
break ;
case TOK_END :
result . type = parse_token_type_end ;
break ;
case TOK_BACKGROUND :
result . type = parse_token_background ;
break ;
default :
fprintf ( stderr , " Bad token type %d passed to %s \n " , ( int ) tokenizer_token_type , __FUNCTION__ ) ;
assert ( 0 ) ;
break ;
}
return result ;
}
/** Base class for nodes of a parse tree */
2013-06-09 10:20:26 +08:00
class parse_node_t
2013-06-02 13:14:47 +08:00
{
2013-06-09 10:20:26 +08:00
public :
2013-06-02 13:14:47 +08:00
/* Start in the source code */
2013-06-09 10:20:26 +08:00
size_t source_start ;
2013-06-02 13:14:47 +08:00
/* Length of our range in the source code */
2013-06-09 10:20:26 +08:00
size_t source_length ;
/* Children */
node_offset_t child_start ;
node_offset_t child_count ;
2013-06-07 12:49:40 +08:00
2013-06-09 10:20:26 +08:00
/* Type-dependent data */
uint32_t tag ;
2013-06-02 13:14:47 +08:00
2013-06-09 10:20:26 +08:00
/* Type of the node */
enum parse_token_type_t type ;
2013-06-02 13:14:47 +08:00
2013-06-07 12:49:40 +08:00
2013-06-09 10:20:26 +08:00
/* Description */
wcstring describe ( void ) const
2013-06-07 12:49:40 +08:00
{
2013-06-09 10:20:26 +08:00
wcstring result = token_type_description ( type ) ;
return result ;
2013-06-07 12:49:40 +08:00
}
2013-06-02 13:14:47 +08:00
2013-06-09 10:20:26 +08:00
/* Constructor */
explicit parse_node_t ( parse_token_type_t ty ) : type ( ty ) , source_start ( 0 ) , source_length ( 0 ) , child_start ( 0 ) , child_count ( 0 ) , tag ( 0 )
2013-06-02 13:14:47 +08:00
{
}
} ;
2013-06-09 10:20:26 +08:00
static void dump_tree_recursive ( const std : : vector < parse_node_t > & nodes , const wcstring & src , size_t start , size_t indent , wcstring * result , size_t * line )
2013-06-02 13:14:47 +08:00
{
2013-06-09 10:20:26 +08:00
assert ( start < nodes . size ( ) ) ;
const parse_node_t & node = nodes . at ( start ) ;
2013-06-02 13:14:47 +08:00
2013-06-09 10:20:26 +08:00
append_format ( * result , L " %2lu " , * line ) ;
result - > append ( indent , L ' ' ) ; ;
result - > append ( node . describe ( ) ) ;
if ( node . child_count > 0 )
2013-06-02 13:14:47 +08:00
{
2013-06-09 10:20:26 +08:00
append_format ( * result , L " <%lu children> " , node . child_count ) ;
2013-06-02 13:14:47 +08:00
}
2013-06-09 10:20:26 +08:00
if ( node . type = = parse_token_type_string )
2013-06-02 13:14:47 +08:00
{
2013-06-09 10:20:26 +08:00
result - > append ( L " : \" " ) ;
result - > append ( src , node . source_start , node . source_length ) ;
result - > append ( L " \" " ) ;
2013-06-02 13:14:47 +08:00
}
2013-06-09 10:20:26 +08: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 12:49:40 +08:00
{
2013-06-09 10:20:26 +08:00
dump_tree_recursive ( nodes , src , child_idx , indent + 2 , result , line ) ;
2013-06-07 12:49:40 +08:00
}
2013-06-09 10:20:26 +08:00
}
2013-06-07 12:49:40 +08:00
2013-06-09 10:20:26 +08:00
static wcstring dump_tree ( const std : : vector < parse_node_t > & nodes , const wcstring & src )
2013-06-07 12:49:40 +08:00
{
2013-06-09 10:20:26 +08:00
if ( nodes . empty ( ) )
return L " (empty!) " ;
size_t line = 0 ;
wcstring result ;
dump_tree_recursive ( nodes , src , 0 , 0 , & result , & line ) ;
return result ;
}
2013-06-07 12:49:40 +08:00
struct parse_stack_element_t
{
enum parse_token_type_t type ;
enum parse_keyword_t keyword ;
2013-06-09 10:20:26 +08:00
node_offset_t node_idx ;
2013-06-07 12:49:40 +08:00
2013-06-09 10:20:26 +08:00
parse_stack_element_t ( parse_token_type_t t ) : type ( t ) , keyword ( parse_keyword_none ) , node_idx ( - 1 )
2013-06-02 13:14:47 +08:00
{
2013-06-07 12:49:40 +08:00
}
2013-06-09 10:20:26 +08:00
parse_stack_element_t ( parse_keyword_t k ) : type ( parse_token_type_string ) , keyword ( k ) , node_idx ( - 1 )
2013-06-07 12:49:40 +08:00
{
2013-06-02 13:14:47 +08:00
}
} ;
class parse_ll_t
{
friend class parse_t ;
2013-06-07 12:49:40 +08:00
std : : vector < parse_stack_element_t > symbol_stack ; // LL parser stack
2013-06-09 10:20:26 +08:00
std : : vector < parse_node_t > nodes ;
bool errored ;
2013-06-02 13:14:47 +08:00
// Constructor
2013-06-09 10:20:26 +08:00
parse_ll_t ( ) : errored ( false )
2013-06-02 13:14:47 +08:00
{
2013-06-09 10:20:26 +08:00
// initial node
parse_stack_element_t elem = symbol_statement_list ;
elem . node_idx = 0 ;
symbol_stack . push_back ( elem ) ; // goal token
nodes . push_back ( parse_node_t ( symbol_statement_list ) ) ;
2013-06-02 13:14:47 +08:00
}
2013-06-09 10:20:26 +08:00
bool top_node_match_token ( parse_token_t token ) ;
2013-06-02 13:14:47 +08:00
// implementation of certain parser constructions
void accept_token ( parse_token_t token ) ;
void accept_token_statement_list ( parse_token_t token ) ;
void accept_token_statement ( parse_token_t token ) ;
void accept_token_block_header ( parse_token_t token ) ;
void accept_token_boolean_statement ( parse_token_t token ) ;
void accept_token_decorated_statement ( parse_token_t token ) ;
2013-06-07 12:49:40 +08:00
void accept_token_plain_statement ( parse_token_t token ) ;
2013-06-02 13:14:47 +08:00
void accept_token_arguments_or_redirections_list ( parse_token_t token ) ;
void accept_token_argument_or_redirection ( parse_token_t token ) ;
2013-06-09 10:20:26 +08:00
bool accept_token_string ( parse_token_t token ) ;
2013-06-02 13:14:47 +08:00
void token_unhandled ( parse_token_t token , const char * function ) ;
void parse_error ( const wchar_t * expected , parse_token_t token ) ;
2013-06-09 10:20:26 +08: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-06-02 13:14:47 +08:00
parse_token_type_t stack_top_type ( ) const
{
2013-06-07 12:49:40 +08:00
return symbol_stack . back ( ) . type ;
}
2013-06-09 10:20:26 +08:00
void top_node_set_tag ( uint32_t tag )
2013-06-07 12:49:40 +08:00
{
2013-06-09 10:20:26 +08:00
this - > node_for_top_symbol ( ) . tag = tag ;
2013-06-07 12:49:40 +08:00
}
2013-06-09 10:20:26 +08:00
inline void add_child_to_node ( size_t parent_node_idx , parse_stack_element_t * tok )
2013-06-07 12:49:40 +08:00
{
2013-06-09 10:20:26 +08: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 13:14:47 +08:00
}
2013-06-09 10:20:26 +08:00
2013-06-02 13:14:47 +08:00
2013-06-09 10:20:26 +08:00
// Pop from the top of the symbol stack, then push, updating node counts. Note that these are pushed in reverse order, so the first argument will be on the top of the stack.
2013-06-02 13:14:47 +08:00
inline void symbol_stack_pop_push ( parse_stack_element_t tok1 = token_type_invalid , parse_stack_element_t tok2 = token_type_invalid , parse_stack_element_t tok3 = token_type_invalid , parse_stack_element_t tok4 = token_type_invalid , parse_stack_element_t tok5 = token_type_invalid )
{
2013-06-09 10:20:26 +08:00
// Get the node for the top symbol and tell it about its children
size_t node_idx = symbol_stack . back ( ) . node_idx ;
parse_node_t & node = nodes . at ( node_idx ) ;
// Should have no children yet
PARSE_ASSERT ( node . child_count = = 0 ) ;
// Tell the node where its children start
node . child_start = nodes . size ( ) ;
// Add nodes for 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)
if ( tok1 . type ! = token_type_invalid ) add_child_to_node ( node_idx , & tok1 ) ;
if ( tok2 . type ! = token_type_invalid ) add_child_to_node ( node_idx , & tok2 ) ;
if ( tok3 . type ! = token_type_invalid ) add_child_to_node ( node_idx , & tok3 ) ;
if ( tok4 . type ! = token_type_invalid ) add_child_to_node ( node_idx , & tok4 ) ;
if ( tok5 . type ! = token_type_invalid ) add_child_to_node ( node_idx , & tok5 ) ;
// The above set the node_idx. Now replace the top of the stack.
2013-06-07 12:49:40 +08:00
symbol_stack . pop_back ( ) ;
if ( tok5 . type ! = token_type_invalid ) symbol_stack . push_back ( tok5 ) ;
if ( tok4 . type ! = token_type_invalid ) symbol_stack . push_back ( tok4 ) ;
if ( tok3 . type ! = token_type_invalid ) symbol_stack . push_back ( tok3 ) ;
if ( tok2 . type ! = token_type_invalid ) symbol_stack . push_back ( tok2 ) ;
if ( tok1 . type ! = token_type_invalid ) symbol_stack . push_back ( tok1 ) ;
2013-06-02 13:14:47 +08:00
}
} ;
void parse_ll_t : : token_unhandled ( parse_token_t token , const char * function )
{
fprintf ( stderr , " Unhandled token with type %d in function %s \n " , ( int ) token . type , function ) ;
PARSER_DIE ( ) ;
}
void parse_ll_t : : parse_error ( const wchar_t * expected , parse_token_t token )
{
fprintf ( stderr , " Expected a %ls, instead got a token of type %d \n " , expected , ( int ) token . type ) ;
}
void parse_ll_t : : accept_token_statement_list ( parse_token_t token )
{
2013-06-07 12:49:40 +08:00
PARSE_ASSERT ( stack_top_type ( ) = = symbol_statement_list ) ;
2013-06-02 13:14:47 +08:00
switch ( token . type )
{
case parse_token_type_string :
case parse_token_type_pipe :
case parse_token_type_redirection :
case parse_token_background :
case parse_token_type_end :
symbol_stack_pop_push ( symbol_statement , symbol_statement_list ) ;
break ;
case parse_token_type_terminate :
// no more commands, just transition to empty
symbol_stack_pop_push ( ) ;
break ;
default :
token_unhandled ( token , __FUNCTION__ ) ;
break ;
}
}
void parse_ll_t : : accept_token_statement ( parse_token_t token )
{
PARSE_ASSERT ( stack_top_type ( ) = = symbol_statement ) ;
switch ( token . type )
{
case parse_token_type_string :
switch ( token . keyword )
{
case parse_keyword_and :
case parse_keyword_or :
case parse_keyword_not :
symbol_stack_pop_push ( symbol_boolean_statement ) ;
break ;
case parse_keyword_if :
case parse_keyword_else :
case parse_keyword_for :
case parse_keyword_in :
case parse_keyword_while :
case parse_keyword_begin :
case parse_keyword_function :
case parse_keyword_switch :
symbol_stack_pop_push ( symbol_block_statement ) ;
2013-06-07 12:49:40 +08:00
assert ( 0 & & " Need assignment " ) ;
2013-06-02 13:14:47 +08:00
break ;
case parse_keyword_end :
// TODO
break ;
case parse_keyword_none :
case parse_keyword_command :
case parse_keyword_builtin :
symbol_stack_pop_push ( symbol_decorated_statement ) ;
break ;
}
break ;
case parse_token_type_pipe :
case parse_token_type_redirection :
case parse_token_background :
case parse_token_type_end :
case parse_token_type_terminate :
parse_error ( L " command " , token ) ;
break ;
default :
token_unhandled ( token , __FUNCTION__ ) ;
break ;
}
}
void parse_ll_t : : accept_token_block_header ( parse_token_t token )
{
PARSE_ASSERT ( stack_top_type ( ) = = symbol_block_header ) ;
switch ( token . type )
{
case parse_token_type_string :
switch ( token . keyword )
{
case parse_keyword_if :
symbol_stack_pop_push ( symbol_if_header ) ;
break ;
case parse_keyword_else :
//todo
break ;
case parse_keyword_for :
symbol_stack_pop_push ( symbol_for_header ) ;
break ;
case parse_keyword_while :
symbol_stack_pop_push ( symbol_while_header ) ;
break ;
case parse_keyword_begin :
symbol_stack_pop_push ( symbol_begin_header ) ;
break ;
case parse_keyword_function :
symbol_stack_pop_push ( symbol_function_header ) ;
break ;
default :
token_unhandled ( token , __FUNCTION__ ) ;
break ;
}
break ;
default :
token_unhandled ( token , __FUNCTION__ ) ;
break ;
}
}
void parse_ll_t : : accept_token_boolean_statement ( parse_token_t token )
{
PARSE_ASSERT ( stack_top_type ( ) = = symbol_boolean_statement ) ;
switch ( token . type )
{
case parse_token_type_string :
switch ( token . keyword )
{
case parse_keyword_and :
case parse_keyword_or :
case parse_keyword_not :
2013-06-09 10:20:26 +08:00
top_node_set_tag ( token . keyword ) ;
symbol_stack_pop_push ( token . keyword , symbol_statement ) ;
2013-06-02 13:14:47 +08:00
break ;
default :
token_unhandled ( token , __FUNCTION__ ) ;
break ;
}
default :
token_unhandled ( token , __FUNCTION__ ) ;
break ;
}
}
void parse_ll_t : : accept_token_decorated_statement ( parse_token_t token )
{
PARSE_ASSERT ( stack_top_type ( ) = = symbol_decorated_statement ) ;
switch ( token . type )
{
case parse_token_type_string :
switch ( token . keyword )
{
case parse_keyword_command :
2013-06-09 10:20:26 +08:00
top_node_set_tag ( parse_keyword_command ) ;
2013-06-07 12:49:40 +08:00
symbol_stack_pop_push ( parse_keyword_command , symbol_plain_statement ) ;
2013-06-02 13:14:47 +08:00
break ;
2013-06-07 12:49:40 +08:00
2013-06-02 13:14:47 +08:00
case parse_keyword_builtin :
2013-06-09 10:20:26 +08:00
top_node_set_tag ( parse_keyword_builtin ) ;
2013-06-07 12:49:40 +08:00
symbol_stack_pop_push ( parse_keyword_builtin , symbol_plain_statement ) ;
2013-06-02 13:14:47 +08:00
break ;
2013-06-07 12:49:40 +08:00
2013-06-02 13:14:47 +08:00
default :
2013-06-09 10:20:26 +08:00
top_node_set_tag ( parse_keyword_none ) ;
2013-06-02 13:14:47 +08:00
symbol_stack_pop_push ( symbol_plain_statement ) ;
break ;
}
2013-06-09 10:20:26 +08:00
break ;
2013-06-02 13:14:47 +08:00
default :
token_unhandled ( token , __FUNCTION__ ) ;
break ;
}
}
2013-06-07 12:49:40 +08:00
void parse_ll_t : : accept_token_plain_statement ( parse_token_t token )
{
2013-06-09 10:20:26 +08:00
PARSE_ASSERT ( stack_top_type ( ) = = symbol_plain_statement ) ;
2013-06-07 12:49:40 +08:00
symbol_stack_pop_push ( parse_token_type_string , symbol_arguments_or_redirections_list , parse_token_type_end ) ;
}
2013-06-02 13:14:47 +08:00
void parse_ll_t : : accept_token_arguments_or_redirections_list ( parse_token_t token )
{
PARSE_ASSERT ( stack_top_type ( ) = = symbol_arguments_or_redirections_list ) ;
switch ( token . type )
{
case parse_token_type_string :
case parse_token_type_redirection :
symbol_stack_pop_push ( symbol_argument_or_redirection , symbol_arguments_or_redirections_list ) ;
break ;
default :
// Some other token, end of list
symbol_stack_pop_push ( ) ;
break ;
}
}
void parse_ll_t : : accept_token_argument_or_redirection ( parse_token_t token )
{
PARSE_ASSERT ( stack_top_type ( ) = = symbol_argument_or_redirection ) ;
switch ( token . type )
{
case parse_token_type_string :
2013-06-09 10:20:26 +08:00
symbol_stack_pop_push ( parse_token_type_string ) ;
2013-06-02 13:14:47 +08:00
// Got an argument
break ;
case parse_token_type_redirection :
2013-06-09 10:20:26 +08:00
symbol_stack_pop_push ( parse_token_type_redirection ) ;
2013-06-02 13:14:47 +08:00
// Got a redirection
break ;
default :
token_unhandled ( token , __FUNCTION__ ) ;
break ;
}
}
2013-06-09 10:20:26 +08:00
bool parse_ll_t : : accept_token_string ( parse_token_t token )
2013-06-02 13:14:47 +08:00
{
2013-06-09 10:20:26 +08:00
PARSE_ASSERT ( stack_top_type ( ) = = parse_token_type_string ) ;
bool result = false ;
switch ( token . type )
2013-06-02 13:14:47 +08:00
{
2013-06-09 10:20:26 +08:00
case parse_token_type_string :
// Got our string
symbol_stack_pop_push ( ) ;
result = true ;
2013-06-02 13:14:47 +08:00
break ;
2013-06-09 10:20:26 +08:00
default :
token_unhandled ( token , __FUNCTION__ ) ;
2013-06-02 13:14:47 +08:00
break ;
2013-06-09 10:20:26 +08:00
}
return result ;
}
bool parse_ll_t : : top_node_match_token ( parse_token_t token )
{
PARSE_ASSERT ( ! symbol_stack . empty ( ) ) ;
PARSE_ASSERT ( token . type > = FIRST_PARSE_TOKEN_TYPE ) ;
bool result = false ;
parse_stack_element_t & stack_top = symbol_stack . back ( ) ;
if ( stack_top . type = = token . type )
{
// So far so good. See if we need a particular keyword.
if ( stack_top . keyword = = parse_keyword_none | | stack_top . keyword = = token . keyword )
{
// 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-06-02 13:14:47 +08:00
2013-06-09 10:20:26 +08:00
// We consumed this symbol
symbol_stack . pop_back ( ) ;
result = true ;
}
2013-06-02 13:14:47 +08:00
}
2013-06-09 10:20:26 +08:00
return result ;
2013-06-02 13:14:47 +08:00
}
2013-06-09 10:20:26 +08:00
void parse_ll_t : : accept_token ( parse_token_t token )
2013-06-02 13:14:47 +08:00
{
2013-06-09 10:20:26 +08:00
PARSE_ASSERT ( token . type > = FIRST_PARSE_TOKEN_TYPE ) ;
PARSE_ASSERT ( ! symbol_stack . empty ( ) ) ;
bool consumed = false ;
while ( ! consumed & & ! this - > errored )
{
fprintf ( stderr , " Top type %ls \n " , token_type_description ( stack_top_type ( ) ) . c_str ( ) ) ;
if ( top_node_match_token ( token ) )
{
consumed = true ;
break ;
}
switch ( stack_top_type ( ) )
{
/* Symbols */
case symbol_statement_list :
accept_token_statement_list ( token ) ;
break ;
case symbol_statement :
accept_token_statement ( token ) ;
break ;
case symbol_block_statement :
symbol_stack_pop_push ( symbol_block_header , symbol_statement_list , parse_keyword_end , symbol_arguments_or_redirections_list ) ;
break ;
case symbol_block_header :
accept_token_block_header ( token ) ;
break ;
case symbol_if_header :
break ;
case symbol_for_header :
symbol_stack_pop_push ( parse_keyword_for , parse_token_type_string , parse_keyword_in , symbol_arguments_or_redirections_list , parse_token_type_end ) ;
break ;
case symbol_while_header :
symbol_stack_pop_push ( parse_keyword_while , symbol_statement ) ;
break ;
case symbol_begin_header :
symbol_stack_pop_push ( parse_keyword_begin , parse_token_type_end ) ;
break ;
case symbol_function_header :
symbol_stack_pop_push ( parse_keyword_function , symbol_arguments_or_redirections_list , parse_token_type_end ) ;
break ;
case symbol_boolean_statement :
accept_token_boolean_statement ( token ) ;
break ;
case symbol_decorated_statement :
accept_token_decorated_statement ( token ) ;
break ;
case symbol_plain_statement :
accept_token_plain_statement ( token ) ;
break ;
case symbol_arguments_or_redirections_list :
accept_token_arguments_or_redirections_list ( token ) ;
break ;
case symbol_argument_or_redirection :
accept_token_argument_or_redirection ( token ) ;
break ;
/* Tokens */
case parse_token_type_string :
consumed = accept_token_string ( token ) ;
break ;
default :
fprintf ( stderr , " Bailing with token type %d \n " , ( int ) token . type ) ;
break ;
}
}
}
2013-06-02 13:14:47 +08:00
2013-06-09 10:20:26 +08:00
parse_t : : parse_t ( ) : parser ( new parse_ll_t ( ) )
2013-06-07 12:49:40 +08:00
{
}
2013-06-09 10:20:26 +08:00
static parse_keyword_t keyword_for_token ( token_type tok , const wchar_t * tok_txt )
2013-06-07 12:49:40 +08:00
{
2013-06-09 10:20:26 +08:00
parse_keyword_t result = parse_keyword_none ;
if ( tok = = TOK_STRING )
2013-06-07 12:49:40 +08:00
{
2013-06-09 10:20:26 +08:00
const struct {
const wchar_t * txt ;
parse_keyword_t keyword ;
} keywords [ ] = {
{ 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 } ,
{ 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 }
} ;
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 12:49:40 +08:00
}
2013-06-09 10:20:26 +08:00
return result ;
2013-06-07 12:49:40 +08:00
}
2013-06-09 10:20:26 +08:00
void parse_t : : parse ( const wcstring & str )
2013-06-02 13:14:47 +08:00
{
2013-06-09 10:20:26 +08:00
tokenizer_t tok = tokenizer_t ( str . c_str ( ) , 0 ) ;
for ( ; tok_has_next ( & tok ) ; tok_next ( & tok ) )
{
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 ) ;
if ( tok_type = = TOK_ERROR )
{
fprintf ( stderr , " Tokenizer error \n " ) ;
break ;
}
parse_token_t token = parse_token_from_tokenizer_token ( tok_type ) ;
token . tokenizer_type = tok_type ;
token . source_start = ( size_t ) tok_start ;
token . source_length = wcslen ( tok_txt ) ;
token . keyword = keyword_for_token ( tok_type , tok_txt ) ;
this - > parser - > accept_token ( token ) ;
}
wcstring result = dump_tree ( this - > parser - > nodes , str ) ;
fprintf ( stderr , " Tree (%ld nodes): \n %ls " , this - > parser - > nodes . size ( ) , result . c_str ( ) ) ;
fprintf ( stderr , " node size %ld " , sizeof ( parse_node_t ) ) ;
2013-06-02 13:14:47 +08:00
}