From 25e5956e4d8c2a275f48653e56c207d99913fcf6 Mon Sep 17 00:00:00 2001 From: Neil Date: Sun, 12 Mar 2023 16:48:29 -0700 Subject: [PATCH] scan score; ineffecient --- src/scan_score.h | 4 ++ src/scan_score.re.c | 94 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 src/scan_score.h create mode 100644 src/scan_score.re.c diff --git a/src/scan_score.h b/src/scan_score.h new file mode 100644 index 0000000..6385b8f --- /dev/null +++ b/src/scan_score.h @@ -0,0 +1,4 @@ +#include "helper.h" +#include "flight.h" + +int scan_score(union date32, const char *, struct flight_tree *); diff --git a/src/scan_score.re.c b/src/scan_score.re.c new file mode 100644 index 0000000..e9ebdd4 --- /dev/null +++ b/src/scan_score.re.c @@ -0,0 +1,94 @@ +/** @license 2022 Neil Edelman, distributed under the terms of the + [MIT License](https://opensource.org/licenses/MIT). + + Scan journal entries for kjv references. */ + +#include "../src/journal.h" +#include "../src/scan_flight.h" +#include /* C99 */ +#include +#include +#include +#include + +/*!conditions:re2c*/ + +int flight_scan(union date32 date, const char *const buffer, + struct flights *const f) { + const char *YYCURSOR, *YYMARKER, *yyt1, *yyt2, *yyt3, *s0, *s1, *t0, *t1; + enum flight_type type = Glider; + enum YYCONDTYPE condition = yycline; + size_t line = 1; + char datestr[12] = {0}; + const char *why = "unexpected"; + assert(buffer && f); + YYCURSOR = YYMARKER = yyt1 = buffer; + /*!re2c /**/ + re2c:define:YYCTYPE = char; + re2c:yyfill:enable = 0; + re2c:define:YYGETCONDITION = "condition"; + re2c:define:YYSETCONDITION = "condition = @@;"; + re2c:define:YYGETCONDITION:naked = 1; + re2c:define:YYSETCONDITION:naked = 1; + + unix_control = [\x01-\x08\x0b-\x1f\x7f]; + ws = [ \t]; + glyph = [^] \ ("\x00" | "\n" | unix_control | ws); + natural = [1-9][0-9]*; + keyword = [A-Za-z0-9][A-Za-z0-9_-]*; + */ + for( ; ; ) { /*!re2c /**/ + /* Default ignore. */ + [^\n\x00] { continue; } + "\x00" { why = "no newline at end of file"; goto catch; } + "\x00" { return 1; } + "\n" => line { line++; continue; } + * :=> skip + "::" / [^:] :=> score + + * { why = "default score unrecognized"; goto catch; } + @s0 keyword @s1 / "\n" => skip { also_add_to_tree: { + const struct pair keyword = pair(s0, s1); + const union line64 key = { { (uint32_t)line, date } }; + size_t i, *pi; + if(line > UINT32_MAX) + { errno = ERANGE; why = "too many lines of text"; goto catch; } + if(!(i = scoremap_table_get(&s->map, keyword))) + { why = "keyword not introduced"; goto catch; } + switch(score_tree_try(&s->dates, key, &pi)) { + case TREE_PRESENT: why = "duplicate key"; /* _Sic_. */ + case TREE_ERROR: goto catch; + case TREE_ABSENT: *pi = i; break; + } + /*date32_to_string(date, &datestr); + printf("%s: <%.*s>\n", datestr, (int)(s1 - s0), s0);*/ + continue; + } } + /* This is lazy and will pickup trailing spaces. */ + @s0 keyword @s1 ":" [^\x00\n]+ / "\n" => skip { + struct pair keyword = pair(s0, s1); + size_t *idx; + struct score *score; + switch(scoremap_table_assign(&s->map, keyword, &idx)) { + case TABLE_PRESENT: errno = EDOM; why = "new keyword already used"; + case TABLE_ERROR: goto catch; /* /\ _Sic_. */ + case TABLE_ABSENT: *idx = 0; break; /* Good. */ + } + if(!(source = scorelist_array_new(&s->list))) goto catch; + *idx = (size_t)(score - s->list.data); + score->name.a = s0, score->name.b = s1; + score->desc.a = 0, score->desc.b = 0; + date32_to_string(date, &datestr); + fprintf(stderr, "%s: new source <%.*s> stored in list at %zu.\n", + datestr, (int)(s1 - s0), s0, *idx); + goto also_add_to_tree; + } + */ } + assert(0); /* Never gets here. */ +catch: + if(!errno) errno = EILSEQ; + date32_to_string(date, &datestr); + fprintf(stderr, "%s\n" + "%s line %zu: %s.\n", buffer, datestr, line, why); + return 0; +}