From 5b82720814a346eae249d98ebc744fe5491a6041 Mon Sep 17 00:00:00 2001 From: anon Date: Tue, 13 Feb 2024 18:20:25 +0100 Subject: [PATCH] work on strings too --- source/lib.c | 24 ++++++++++++++++++++---- source/main.c | 1 - source/sql-parser.h | 6 ++++++ source/sql.y | 7 ++++--- 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/source/lib.c b/source/lib.c index 2a0a308..d2bd154 100644 --- a/source/lib.c +++ b/source/lib.c @@ -20,17 +20,24 @@ struct psql_state *psql_new(void) return NULL; } + st->buffer_state = NULL; + return st; } -void psql_free(struct psql_state *st) +void psql_free(struct psql_state *pstate) { - if (!st) + if (!pstate) return; - yylex_destroy(st->scanner); + if (pstate->buffer_state) { + yy_delete_buffer(pstate->buffer_state, pstate->scanner); + } - free(st); + yylex_destroy(pstate->scanner); + + + free(pstate); } void psql_set_input(struct psql_state *pstate, FILE *in_f) @@ -38,6 +45,15 @@ void psql_set_input(struct psql_state *pstate, FILE *in_f) yyset_in(in_f, pstate->scanner); } +void psql_set_string_input(struct psql_state *pstate, char *in_str) +{ + if (pstate->buffer_state) { + yy_delete_buffer(pstate->buffer_state, pstate->scanner); + } + + pstate->buffer_state = yy_scan_string(in_str, pstate->scanner); +} + int psql_parse(struct psql_state *pstate) { return yyparse(pstate->scanner, pstate); diff --git a/source/main.c b/source/main.c index 3c20412..8ea60fc 100644 --- a/source/main.c +++ b/source/main.c @@ -25,7 +25,6 @@ main(int ac, char **av) abort(); if(ac > 1) { - puts("wut?"); if((in_f = fopen(av[1], "r")) == NULL) { perror(av[1]); exit(1); diff --git a/source/sql-parser.h b/source/sql-parser.h index c29e6f1..ac5b16c 100644 --- a/source/sql-parser.h +++ b/source/sql-parser.h @@ -54,13 +54,19 @@ enum sqlp_date_intervals { SDI_HOUR_SECOND = 8, }; +#ifndef YY_TYPEDEF_YY_BUFFER_STATE +typedef void* YY_BUFFER_STATE; +#endif + struct psql_state { yyscan_t scanner; + YY_BUFFER_STATE buffer_state; }; extern struct psql_state *psql_new(void); extern void psql_free(struct psql_state *st); extern void psql_set_input(struct psql_state *st, FILE *f); +extern void psql_set_string_input(struct psql_state *pstate, char *in_str); extern int psql_parse(struct psql_state *st); extern void sqlp_alias(struct psql_state *pstate, const char *alias); diff --git a/source/sql.y b/source/sql.y index d1cb6c0..e60c9e7 100644 --- a/source/sql.y +++ b/source/sql.y @@ -325,7 +325,7 @@ struct psql_state; %start stmt_list %{ -void yyerror(YYLTYPE *, yyscan_t scanner, struct psql_state *pstate, const char *s, ...); +void yyerror(YYLTYPE *t, yyscan_t scanner, struct psql_state *pstate, const char *s, ...); void lyyerror(YYLTYPE t, const char *s, ...); %} /* free discarded tokens */ @@ -975,9 +975,10 @@ yyerror(YYLTYPE *t, yyscan_t scanner, struct psql_state *pstate, const char *s, va_list ap; va_start(ap, s); - if(t->first_line) + if (t->first_line) { fprintf(stderr, "%s:%d.%d-%d.%d: error: ", t->filename, t->first_line, t->first_column, - t->last_line, t->last_column); + t->last_line, t->last_column); + } vfprintf(stderr, s, ap); fprintf(stderr, "\n"); yyerrno = 1;