diff --git a/CMakeLists.txt b/CMakeLists.txt index db15468..0b95a68 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ project(libabacus) add_compile_options(-pedantic -Wall) -add_library(abacus STATIC src/lexer.c src/libabacus_util.c src/table.c) +add_library(abacus STATIC src/lexer.c src/libabacus_util.c src/table.c src/parser.c) add_executable(libabacus src/main.c) add_subdirectory(external/liblex) diff --git a/include/parser.h b/include/parser.h new file mode 100644 index 0000000..dd8d119 --- /dev/null +++ b/include/parser.h @@ -0,0 +1,16 @@ +#ifndef LIBABACUS_PARSER_H +#define LIBABACUS_PARSER_H + +#include "table.h" +#include "ll.h" +#include "tree.h" + +/** + * Parses the given list of tokens into the given tree pointer. + * @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 parse_tokens(ll* tokens, const char* string, tree** store_into); + +#endif diff --git a/src/parser.c b/src/parser.c new file mode 100644 index 0000000..6193f5d --- /dev/null +++ b/src/parser.c @@ -0,0 +1,29 @@ +struct parser_state { + ll_node* current_node; + lexer_match* current_match; + const char* string; +}; + +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) { + state->current_node = tokens->head; + state->string = string; + _parser_state_update(state); +} + +void _parser_state_step(struct parser_state* state) { + if(state->current_node) { + state->current_node = state->current_node->next; + } + _parser_state_update(state); +} +libab_result parse_tokens(ll* tokens, const char* string, tree** store_into) { + libab_result result = LIBAB_SUCCESS; + struct parser_state state; + _parser_state_init(&state, tokens, string); + + return result; +}