151 lines
4.6 KiB
C
151 lines
4.6 KiB
C
/** @license 2022 Neil Edelman, distributed under the terms of the
|
|
[MIT License](https://opensource.org/licenses/MIT).
|
|
|
|
Scan journal entries for score. */
|
|
|
|
#if 0
|
|
#include "../src/journal.h"
|
|
#include "../src/scan_score.h"
|
|
#include <inttypes.h> /* C99 */
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <assert.h>
|
|
#include <limits.h>
|
|
#endif
|
|
|
|
#include "../src/journal.h"
|
|
#include "../src/scan_score.h"
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <assert.h>
|
|
|
|
|
|
/* One array. */
|
|
static void scorelist_to_string(const struct score *const s,
|
|
char (*const z)[12]) {
|
|
const char *a = s->name.a, *b;
|
|
char *y = *z;
|
|
b = s->name.b <= a + 11 ? s->name.b : a + 11;
|
|
while(a < b) *(y++) = *(a++);
|
|
*y = '\0';
|
|
}
|
|
#define ARRAY_NAME scorelist
|
|
#define ARRAY_TYPE struct score
|
|
#define ARRAY_TO_STRING
|
|
#define ARRAY_BODY
|
|
#include "../src/array.h"
|
|
|
|
|
|
/* Maps from substring keywords to indices. */
|
|
static void scoremap_to_string(const struct pair key, const size_t i,
|
|
char (*const a)[12]) { (void)key; sprintf(*a, "%zu", i); }
|
|
static int scoremap_is_equal(const struct pair a, const struct pair b)
|
|
{ return pair_is_equal(a, b); }
|
|
static uint32_t scoremap_hash(const struct pair p) { return pair_djb2(p); }
|
|
#define TABLE_NAME scoremap
|
|
#define TABLE_KEY struct pair
|
|
#define TABLE_UINT uint32_t
|
|
#define TABLE_VALUE size_t /* Index into array. */
|
|
#define TABLE_DEFAULT 0 /* Default set at zero. */
|
|
#define TABLE_TO_STRING
|
|
#define TABLE_BODY
|
|
#include "../src/table.h"
|
|
|
|
|
|
/* Tree mapping from date-line to indices. */
|
|
static void score_to_string(const union line64 line, const size_t *const u,
|
|
char (*const a)[12]) { (void)u; date32_to_string(line.date, a); }
|
|
static int score_compare(const union line64 a, const union line64 b)
|
|
{ return a.u64 > b.u64; }
|
|
#define TREE_NAME score
|
|
#define TREE_KEY union line64
|
|
#define TREE_VALUE size_t
|
|
#define TREE_COMPARE
|
|
#define TREE_TO_STRING
|
|
#define TREE_DEFAULT 0
|
|
#define TREE_BODY
|
|
#include "../src/tree.h"
|
|
|
|
|
|
/*!conditions:re2c*/
|
|
|
|
int scan_score(union date32 date, const char *const buffer,
|
|
struct score_tree *const score) {
|
|
const char *YYCURSOR, *YYMARKER, *yyt1, *yyt2, *yyt3, *s0, *s1, *t0, *t1;
|
|
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. */
|
|
<skip> [^\n\x00] { continue; }
|
|
<skip> "\x00" { why = "no newline at end of file"; goto catch; }
|
|
<line> "\x00" { return 1; }
|
|
<line, skip> "\n" => line { line++; continue; }
|
|
<line> * :=> skip
|
|
<line> "::" / [^:] :=> score
|
|
|
|
<score> * { why = "default score unrecognized"; goto catch; }
|
|
<score> @s0 keyword @s1 / "\n" => skip { also_add_to_tree: {
|
|
const struct pair keyword = pair(s0, s1);
|
|
const union line64 key = { { line.u32, 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. */
|
|
<source> @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;
|
|
}
|