/* * Copyright (C) 2019 Vivien Kraus * * This program is free software: you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see * . */ %{ #include #include "parser.h" #include #include #include #define YY_INPUT(buf, result, max_size) { \ result = yyextra.behavior->pull_char (yyextra.behavior->data, * (yyextra.prompt_type), buf); \ } #define YY_USER_ACTION { \ * (yyextra.prompt_type) = 2; \ yyextra.behavior->before_token (yyextra.behavior->data, yytext); \ } #define YYSTYPE _MARCHANDE_STYPE %} %option extra-type="marchande_lexer_extra" %option bison-bridge %option prefix="_marchande_" %option reentrant %% %{ int ret; if (yyextra.behavior->prologue (yyextra.behavior->data, &ret, yylval)) { return ret; } %} that { return THAT; } will { return WILL; } be { return BE; } all { return ALL; } [0-9]+ { yylval->INTEGER = (unsigned int) strtoul (yytext, NULL, 10); return INTEGER; } [0-9]+\.[0-9]* { yylval->DECIMAL = strtod (yytext, NULL); return DECIMAL; } of { return OF; } kg { return KG; } a { return A; } bunch { return BUNCH; } bunches { return BUNCHES; } potatoes { return POTATOES; } radishes { return RADISHES; } [ \t\n]+ /* do nothing */ [^ \t\n.,?!0-9]+ { yylval->UNKNOWN_WORD = strdup (yytext); return UNKNOWN_WORD; } [.,?!] { return END_OF_PHRASE; } %% void marchande_parse (marchande_parser_behavior *behavior) { int prompt_type = 1; marchande_lexer_extra extra; yyscan_t scanner; extra.prompt_type = &prompt_type; extra.behavior = behavior; _marchande_lex_init_extra (extra, &scanner); _marchande_parse (behavior, &prompt_type, NULL, scanner); _marchande_lex_destroy (scanner); } void _marchande_add_completion (marchande_completer_candidate **data, const char *candidate) { marchande_completer_candidate *next = malloc (sizeof (marchande_completer_candidate)); marchande_completer_candidate *old = *data; if (next == NULL) { abort (); } next->previous = old; next->candidate = strdup (candidate); *data = next; } char ** marchande_complete (const char *left, const char *right) { behavior_data data; _marchande_pstate *ps = _marchande_pstate_new (); int done = 0; int token = 0; _MARCHANDE_STYPE lval; yyscan_t scanner; marchande_parser_behavior behavior; marchande_lexer_extra extra; int prompt_type = 1; size_t n_completions, i_completion; marchande_completer_candidate *completions = NULL; marchande_completer_candidate *i, *aux; char **ret = NULL;; data.t = MARCHANDE_BATCH; behavior.data = &data; behavior.syntax_error = &pb_syntax_error; behavior.buy_potatoes = &pb_buy_potatoes; behavior.buy_radishes = &pb_buy_radishes; behavior.value_error = &pb_value_error; behavior.pull_char = &pb_pull_char; behavior.prologue = &pb_prologue; behavior.before_token = &pb_before_token; extra.prompt_type = &prompt_type; extra.behavior = &behavior;; data.u.batch.ptr = (char *) left; _marchande_lex_init_extra (extra, &scanner); _marchande_push_parse (ps, MODE_COMPLETE, NULL, &behavior, &prompt_type, &completions, scanner); do { token = _marchande_lex (&lval, scanner); if (token != 0) { _marchande_push_parse (ps, token, &lval, &behavior, &prompt_type, &completions, scanner); } } while (token != 0); _marchande_push_parse (ps, AUTOCOMPLETE, NULL, &behavior, &prompt_type, &completions, scanner); data.u.batch.ptr = (char *) right; do { token = _marchande_lex (&lval, scanner); if (token != 0) { _marchande_push_parse (ps, token, &lval, &behavior, &prompt_type, &completions, scanner); } } while (token != 0); _marchande_push_parse (ps, 0, NULL, &behavior, &prompt_type, &completions, scanner); _marchande_pstate_delete (ps); _marchande_lex_destroy (scanner); n_completions = 0; for (i = completions; i != NULL; i = i->previous) { n_completions++; } ret = malloc ((n_completions + 1) * sizeof (char *)); if (ret == NULL) { abort (); } i_completion = 0; for (i = completions; i != NULL; i = aux) { ret[n_completions - (i_completion++) - 1] = i->candidate; aux = i->previous; free (i); } ret[n_completions] = NULL; return ret;; }