scan encompasses source and score now

This commit is contained in:
Neil 2023-04-23 00:20:50 -07:00
parent 2d0dcdd937
commit 322abe3740
3 changed files with 158 additions and 89 deletions

View File

@ -6,7 +6,7 @@
@std C99 */
#include "journal.h"
#include "scores.h"
#include "scan.h"
#include <stdio.h>
#include <errno.h>
#include <assert.h>
@ -14,7 +14,7 @@
int main(void) {
const char *intent = "start";
struct journal jrnl = {0};
struct scores scrs = {0};
struct scan scn = {0};
errno = 0;
intent = "../journal";
@ -23,17 +23,18 @@ int main(void) {
if(errno) goto catch;
intent = "parse";
scrs = scores(&jrnl);
fprintf(stderr, "Scores: %s.\n", scores_to_string(&scrs));
if(scores_is_empty(&scrs)) goto catch;
if(!scores_graph(&scrs)) goto catch;
scn = scan(&jrnl);
//fprintf(stderr, "Scores: %s.\n", scores_to_string(&scrs));
if(errno) goto catch;
if(!scan_scores_graph(&scn)) goto catch;
intent = 0;
goto finally;
catch:
perror(intent);
finally:
scores_(&scrs);
scan_(&scn);
journal_(&jrnl);
return intent ? EXIT_FAILURE : EXIT_SUCCESS;
}

View File

@ -1,33 +1,43 @@
#include "pair.h" /* pair */
#include "journal.h" /* size_t, date32, line64 */
/* Map from line to index in array. */
#define TREE_NAME linetoindex
#define TREE_NAME linemap
#define TREE_KEY union line64
#define TREE_VALUE size_t
#define TREE_HEAD
#include "../src/tree.h"
/* Score array. */
struct source { struct pair name, desc; };
#define ARRAY_NAME source
#define ARRAY_TYPE struct source
#define ARRAY_HEAD
#include "../src/array.h"
struct sources {
struct source_array array;
struct pairmap_table map;
struct linemap_tree dates;
};
struct score {
struct pair key, name;
union date32 date, last/* update */;
union date32 date, last/* update; need to compare leading value */;
unsigned edges, score/* update */;
};
#define ARRAY_NAME score
#define ARRAY_TYPE struct score
#define ARRAY_HEAD
#include "../src/array.h"
struct scores {
struct score_array array;
struct pairmap_table map;
struct linetoindex_tree dates;
struct linemap_tree dates;
};
void scores_(struct scores *);
struct scores scores(struct journal *);
int scores_is_empty(const struct scores *);
int scores_graph(struct scores *);
const char *scores_to_string(const struct scores *);
struct scan {
struct sources sources;
struct scores scores;
};
void scan_(struct scan *);
struct scan scan(struct journal *);
int scan_scores_graph(struct scan *);

View File

@ -8,13 +8,12 @@
#include <stdio.h>
#include <assert.h>
/* Tree mapping from date-line in the journal to indices in whatever array. */
static void linetoindex_to_string(const union line64 line, const size_t *const u,
static void linemap_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 linetoindex_compare(const union line64 a, const union line64 b)
static int linemap_compare(const union line64 a, const union line64 b)
{ return a.u64 > b.u64; }
#define TREE_NAME linetoindex
#define TREE_NAME linemap
#define TREE_KEY union line64
#define TREE_VALUE size_t
#define TREE_COMPARE
@ -23,8 +22,22 @@ static int linetoindex_compare(const union line64 a, const union line64 b)
#define TREE_BODY
#include "../src/tree.h"
/* Array of sources. */
static void source_to_string(const struct source *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 source
#define ARRAY_TYPE struct source
#define ARRAY_TO_STRING
#define ARRAY_BODY
#include "../src/array.h"
/* Score array, one of the endpoints to index tree. */
/* Array of scores. */
static void score_to_string(const struct score *const s,
char (*const z)[12]) {
const char *a = s->name.a, *b;
@ -44,15 +57,15 @@ static void score_to_string(const struct score *const s,
/*!conditions:re2c*/
static int scan(union date32 date, const char *const buffer,
struct scores *const scores) {
const char *YYCURSOR = buffer, *YYMARKER, *yyt1, *yyt2, *s0, *s1;
static int scan_day(struct scan *const scan, union date32 date,
const char *const buffer) {
const char *YYCURSOR, *YYMARKER, *yyt1, *yyt2, *s0, *s1;
enum YYCONDTYPE condition = yycline;
size_t line = 1;
char datestr[12] = {0};
const char *why = "unexpected";
const char *fail = "perhaps a bat?";
struct score *score = 0;
assert(buffer && scores);
assert(scan && date.u32 && buffer);
YYCURSOR = YYMARKER = yyt1 = buffer;
/*!re2c /**/
re2c:define:YYCTYPE = char;
@ -70,48 +83,92 @@ static int scan(union date32 date, const char *const buffer,
keyword = [A-Za-z0-9][A-Za-z0-9_-]*;
date = natural "-" [0-1][0-9] "-" [0-3][0-9];
*/
for( ; ; ) { /*!re2c /**/
for( ; ; ) {
/*!re2c /**/
/* Default ignore. */
<skip> [^\n\x00] { continue; }
<skip> "\x00" { why = "no newline at end of file"; goto catch; }
<line> "\x00" { return 1; }
<skip> "\x00" { fail = "no newline at end of file"; goto catch; }
<line> "\x00" { return 1; } /* End of day. */
<line, skip> "\n" => line { line++; continue; }
<line> * :=> skip
<line> "--" / [^-] :=> source
<line> "::" / [^:] :=> score
<score> * { why = "score unrecognized"; goto catch; }
<source> * { fail = "source unrecognized"; goto catch; }
<source> @s0 keyword @s1 / "\n" => skip { also_source: {
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; fail = "too many lines of text"; goto catch; }
if(!(i = pair_map_table_get(&scan->sources.map, keyword)))
{ fail = "keyword not introduced"; goto catch; }
switch(linemap_tree_try(&scan->sources.dates, key, &pi)) {
case TREE_PRESENT: fail = "duplicate key"; /* _Sic_. */
case TREE_ERROR: goto catch;
case TREE_ABSENT: *pi = i; break;
}
date32_to_string(date, &datestr);
fprintf(stderr, "%s: source <<%.*s>>\n",
datestr, (int)(s1 - s0), s0);
continue;
} }
/* New source. fixme: desc not set. */
<source> @s0 keyword @s1 ":" [^\x00\n]+ / "\n" => skip {
struct pair keyword = pair(s0, s1);
size_t *idx;
struct source *source;
switch(pair_map_table_assign(&scan->sources.map, keyword, &idx)) {
case TABLE_PRESENT: errno = EDOM; fail = "new keyword already used";
case TABLE_ERROR: goto catch; /* /\ _Sic_. */
case TABLE_ABSENT: *idx = 0; break; /* Good. */
}
if(!(source = source_array_new(&scan->sources.array))) goto catch;
*idx = (size_t)(source - scan->sources.array.data);
source->name.a = s0, source->name.b = s1;
source->desc.a = 0, source->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_source;
}
<score> * { fail = "score unrecognized"; goto catch; }
/* Already there. Use the map to get the index from the keyword and
then stick a marker in the tree with that index. */
<score> @s0 keyword @s1 / "\n" => skip { also_add_to_tree: {
<score> @s0 keyword @s1 / "\n" => skip { also_score: {
const struct pair keyword = pair(s0, s1);
const union line64 key = { { (uint32_t)line, date } };
size_t idx, *pidx;
if(line > UINT32_MAX)
{ errno = ERANGE; why = "too many lines of text"; goto catch; }
if(!(idx = pair_map_table_get(&scores->map, keyword)))
{ why = "keyword not introduced"; goto catch; }
if(scores->array.data[idx].last.u32 >= date.u32)
{ why = "duplicate key in same day"; goto catch; }
scores->array.data[idx].last.u32 = date.u32;
switch(linetoindex_tree_bulk_try(&scores->dates, key, &pidx)) {
case TREE_PRESENT: assert(0); why = "duplicate key"; /* _Sic_. */
{ errno = ERANGE; fail = "too many lines of text"; goto catch; }
if(!(idx = pair_map_table_get(&scan->scores.map, keyword)))
{ fail = "keyword not introduced"; goto catch; }
if(scan->scores.array.data[idx].last.u32 >= date.u32)
{ fail = "duplicate key in same day"; goto catch; }
scan->scores.array.data[idx].last.u32 = date.u32;
switch(linemap_tree_bulk_try(&scan->scores.dates, key, &pidx)) {
case TREE_PRESENT: assert(0); fail = "duplicate key"; /* _Sic_. */
case TREE_ERROR: goto catch;
case TREE_ABSENT: *pidx = idx; break;
}
date32_to_string(date, &datestr);
fprintf(stderr, "%s: <%.*s>\n", datestr, (int)(s1 - s0), s0);
fprintf(stderr, "%s: score <%.*s>\n", datestr, (int)(s1 - s0), s0);
continue;
} }
/* New score. */
<score> @s0 keyword @s1 ":" => score_name {
size_t *idx;
switch(pair_map_table_assign(&scores->map, pair(s0, s1), &idx)) {
case TABLE_PRESENT: errno = EDOM; why = "new keyword already used";
switch(pair_map_table_assign(&scan->scores.map,
pair(s0, s1), &idx)) {
case TABLE_PRESENT: errno = EDOM; fail = "new keyword already used";
case TABLE_ERROR: goto catch; /* _Sic_. */
case TABLE_ABSENT: *idx = 0; break;
}
if(!(score = score_array_new(&scores->array))) goto catch;
*idx = (size_t)(score - scores->array.data);
if(!(score = score_array_new(&scan->scores.array))) goto catch;
*idx = (size_t)(score - scan->scores.array.data); /* Offset. */
/*struct pair key, name; union date32 date, last; unsigned edges;*/
score->key.a = s0, score->key.b = s1;
score->name.a = 0, score->name.b = 0;
@ -120,11 +177,11 @@ static int scan(union date32 date, const char *const buffer,
date32_to_string(date, &datestr);
fprintf(stderr, "%s: new score <%.*s> stored in list at %zu.\n",
datestr, (int)(s1 - s0), s0, *idx);
goto also_add_to_tree;
goto also_score;
}
<score_name> * { why = "name unrecognized"; goto catch; }
<score_date> * { why = "date unrecognized"; goto catch; }
<score_edges> * { why = "edges unrecognized"; goto catch; }
<score_name> * { fail = "name unrecognized"; goto catch; }
<score_date> * { fail = "date unrecognized"; goto catch; }
<score_edges> * { fail = "edges unrecognized"; goto catch; }
<score_name> ws* @s0 semitext+ (" " semitext+)* @s1 /* ws* */ ";"
=> score_date {
assert(score);
@ -139,63 +196,64 @@ static int scan(union date32 date, const char *const buffer,
if(!pair_to_natural(s0, s1, &score->edges)) goto catch;
score = 0; /* Done. */
}
*/ }
assert(0); /* Never gets here. */
catch:
if(!errno) errno = EILSEQ;
date32_to_string(date, &datestr);
fprintf(stderr, "%s line %zu: %s.\n", datestr, line, why);
fprintf(stderr, "%s line %zu: %s.\n", datestr, line, fail);
return 0;
}
void scores_(struct scores *const s) {
if(!s) return;
linetoindex_tree_(&s->dates);
pair_map_table_(&s->map);
score_array_(&s->array);
void scan_(struct scan *const scan) {
if(!scan) return;
linemap_tree_(&scan->scores.dates);
pair_map_table_(&scan->scores.map);
score_array_(&scan->scores.array);
}
struct scores scores(struct journal *const j) {
struct scores s
= { score_array(), pair_map_table(), linetoindex_tree() };
/** @param[jrnl] Must be constant throughout the use of the returned value. */
struct scan scan(struct journal *const jrnl) {
struct scan scan = {0};
struct journal_iterator it;
union date32 date;
const char *text;
assert(j);
{ /* Null is the first item for convenience, (TABLE_DEFAULT). */
assert(jrnl);
/* Null is the first item for convenience, (TABLE_DEFAULT). */
{
struct source *nul;
if(!(nul = source_array_new(&scan.sources.array))) goto catch;
*nul = (struct source){0};
}
{
struct score *nul;
if(!(nul = score_array_new(&s.array))) goto catch;
nul->key.a = nul->key.b = nul->name.a = nul->name.b = 0;
nul->date.u32 = 0;
nul->edges = 0;
}
it = journal_iterator(j);
while(journal_next(&it, &date, &text)) {
char a[12];
date32_to_string(date, &a);
printf("<debug>: %s\n", a);
printf("<<%s>>\n", text);
if(!scan(date, text, &s)) goto catch;
if(!(nul = score_array_new(&scan.scores.array))) goto catch;
*nul = (struct score){0};
}
/* Scan all. */
it = journal_iterator(jrnl);
while(journal_next(&it, &date, &text))
if(!scan_day(&scan, date, text)) goto catch;
fprintf(stderr, "List of scores: %s.\n"
"Mapped to indices: %s.\n"
"Date-line tree: %s.\n", score_array_to_string(&s.array),
pair_map_table_to_string(&s.map), linetoindex_tree_to_string(&s.dates));
"Date-line tree: %s.\n",
score_array_to_string(&scan.scores.array),
pair_map_table_to_string(&scan.scores.map),
linemap_tree_to_string(&scan.scores.dates));
goto finally;
catch:
scores_(&s);
scan_(&scan);
finally:
return s;
return scan;
}
int scores_is_empty(const struct scores *const s)
{ return !s || !s->dates.root.node; }
const char *scores_to_string(const struct scores *const s)
{ return assert(s), score_array_to_string(&s->array); }
int scores_graph(struct scores *const scrs) {
struct linetoindex_tree_iterator it = linetoindex_tree_iterator(&scrs->dates);
int scan_scores_graph(struct scan *const scan) {
struct scores *const scrs = &scan->scores;
struct linemap_tree_iterator it = linemap_tree_iterator(&scrs->dates);
union line64 line;
struct score *score;
@ -208,9 +266,9 @@ int scores_graph(struct scores *const scrs) {
"set output \"score.png\"\n");
printf("$Data <<EOD\n"
"# date, key, key score\n");
while(linetoindex_tree_next(&it)) {
line = linetoindex_tree_key(&it);
score = scrs->array.data + *linetoindex_tree_value(&it);
while(linemap_tree_next(&it)) {
line = linemap_tree_key(&it);
score = scrs->array.data + *linemap_tree_value(&it);
char datestr[12];
date32_to_string(line.date, &datestr);
score->score++;