Mercurial > lbo > hg > ylisp
changeset 88:d197a9c11953
parse: Implement helper for parsing strings
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Mon, 26 Aug 2019 14:54:21 +0200 |
parents | 8156b5031546 |
children | 50cbad7d31da |
files | src/parse.c src/parse.h |
diffstat | 2 files changed, 19 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/src/parse.c Mon Aug 26 12:19:25 2019 +0200 +++ b/src/parse.c Mon Aug 26 14:54:21 2019 +0200 @@ -3,6 +3,14 @@ // From y.yy int y_bison_parse(FILE* input, yvec_t* out, ystr_t* err); +bool yparse_str(ystr_t* input, yexpr_t* result, ystr_t* error) { + char* contents = ystr_str(input); + FILE* stream = fmemopen(contents, ystr_len(input), "r"); + bool ret = yparse(stream, result, error); + fclose(stream); + return ret; +} + bool yparse(FILE* input, yexpr_t* result, ystr_t* error) { yvec_t exprs = YVEC_NEW(0, 0, yexpr_t); ystr_init(error, NULL);
--- a/src/parse.h Mon Aug 26 12:19:25 2019 +0200 +++ b/src/parse.h Mon Aug 26 14:54:21 2019 +0200 @@ -9,17 +9,25 @@ */ #include <gen/y.tab.h> +#include <src/base/str.h> #include <src/expr.h> -#include <src/base/str.h> #include <stdio.h> /** + * @brief Parse contents of `input`. Thin helper around `yparse()`. Does not + * take ownership of `input`. + */ +bool yparse_str(ystr_t* input, yexpr_t* result, ystr_t* error); + +/** * @brief Parse `input` (which can be a string, see `fmemopen(3)`). * - * If successful, `true` is returned, and the top-level s-exprs are stored as list into `result`. + * If successful, `true` is returned, and the top-level s-exprs are stored as + * list into `result`. * - * If not successful, `false` is returned, and `error` will contain a description of the error. + * If not successful, `false` is returned, and `error` will contain a + * description of the error. */ bool yparse(FILE* input, yexpr_t* result, ystr_t* error);