diff --git a/include/libabacus.h b/include/libabacus.h index 0cbb079..edcbfca 100644 --- a/include/libabacus.h +++ b/include/libabacus.h @@ -4,6 +4,7 @@ #include "ht.h" #include "lexer.h" #include "table.h" +#include "parser.h" #include "libabacus_result.h" /** @@ -52,6 +53,11 @@ struct libab_s { * to tokens. */ libab_lexer lexer; + /** + * The parser used to convert + * tokens to a tree. + */ + libab_parser parser; /** * The table used to store top-level * things like functions and operators. diff --git a/include/parser.h b/include/parser.h index 19bb564..91a12f0 100644 --- a/include/parser.h +++ b/include/parser.h @@ -5,12 +5,36 @@ #include "ll.h" #include "tree.h" +/** + * The parser that is used by libabacus + * to store information for converting + * tokens into trees. + */ +struct libab_parser_s { + libab_table* base_table; +}; + +typedef struct libab_parser_s libab_parser; + +/** + * Initializes the parser. + * @param parser the parser to intialize. + * @param table the table of "reserved" entries like operators. + */ +void libab_parser_init(libab_parser* parser, libab_table* table); /** * Parses the given list of tokens into the given tree pointer. + * @param parser the parser to use for parsing text. * @param tokens the tokens to use for parsing. * @param string the string to use for determining token values. * @param store_into tree pointer to store the new data into. */ -libab_result libab_parse_tokens(ll* tokens, const char* string, libab_tree** store_into); +libab_result libab_parser_parse(libab_parser* parser, ll* tokens, + const char* string, libab_tree** store_into); +/** + * Releases the resources allocated by the parser. + * @param parser the parser to release. + */ +void libab_parser_free(libab_parser* parser); #endif diff --git a/include/tree.h b/include/tree.h index 9832ad7..2b6b06c 100644 --- a/include/tree.h +++ b/include/tree.h @@ -1,7 +1,7 @@ #ifndef LIBABACUS_TREE_H #define LIBABACUS_TREE_H -#include "libabacus.h" +#include "libabacus_result.h" #include "vec.h" /** diff --git a/src/libabacus.c b/src/libabacus.c index 4a3ee1b..f9395df 100644 --- a/src/libabacus.c +++ b/src/libabacus.c @@ -2,10 +2,12 @@ libab_result libab_init(libab* ab) { libab_table_init(&ab->table); + libab_parser_init(&ab->parser, &ab->table); return libab_lexer_init(&ab->lexer); } libab_result libab_free(libab* ab) { libab_table_free(&ab->table); + libab_parser_free(&ab->parser); return libab_lexer_free(&ab->lexer); } diff --git a/src/parser.c b/src/parser.c index ad0de4c..8266d6c 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1,6 +1,6 @@ -#include "libabacus.h" #include "parser.h" #include "libabacus_util.h" +#include "libabacus_result.h" #include "lexer.h" #include #include @@ -9,15 +9,18 @@ struct parser_state { ll_node* current_node; libab_lexer_match* current_match; const char* string; + libab_table* base_table; }; void _parser_state_update(struct parser_state* state) { state->current_match = state->current_node ? state->current_node->data : NULL; } -void _parser_state_init(struct parser_state* state, ll* tokens, const char* string) { +void _parser_state_init(struct parser_state* state, + ll* tokens, const char* string, libab_table* table) { state->current_node = tokens->head; state->string = string; + state->base_table = table; _parser_state_update(state); } @@ -109,10 +112,14 @@ libab_result _parse_block(struct parser_state* state, return result; } -libab_result libab_parse_tokens(ll* tokens, const char* string, libab_tree** store_into) { +void libab_parser_init(libab_parser* parser, libab_table* table) { + parser->base_table = table; +} +libab_result libab_parser_parse(libab_parser* parser, ll* tokens, + const char* string, libab_tree** store_into) { libab_result result = LIBAB_SUCCESS; struct parser_state state; - _parser_state_init(&state, tokens, string); + _parser_state_init(&state, tokens, string, parser->base_table); result = _parse_block(&state, store_into, 0); if(result == LIBAB_SUCCESS) { @@ -121,3 +128,6 @@ libab_result libab_parse_tokens(ll* tokens, const char* string, libab_tree** sto return result; } +void libab_parser_free(libab_parser* parser) { + parser->base_table = NULL; +}