interpret/src/scan.re.c

1522 lines
57 KiB
C

/** @license 2022 Neil Edelman, distributed under the terms of the
[MIT License](https://opensource.org/licenses/MIT).
Scan journal. */
#include "../src/journal.h"
#include "../src/scan.h"
#include <stdio.h>
#include <assert.h>
#include <inttypes.h>
/* Tree mapping from date-line in the journal to indices in whatever array. */
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 linemap_compare(const union line64 a, const union line64 b)
{ return a.u64 > b.u64; }
#define TREE_NAME linemap
#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"
/** `p` to `z`. */
static void pair_to_string(const struct pair *const p,
char (*const z)[12]) {
const char *a = p->a, *b;
char *y = *z;
b = p->b <= a + 11 ? p->b : a + 11;
while(a < b) *(y++) = *(a++);
*y = '\0';
}
static void linepair_to_string(const union line64 line,
const struct pair *const pair,
char (*const a)[12]) { (void)pair; date32_to_string(line.date, a); }
static int linepair_compare(const union line64 a, const union line64 b)
{ return a.u64 > b.u64; }
#define TREE_NAME linepair
#define TREE_KEY union line64
#define TREE_VALUE struct pair
#define TREE_COMPARE
#define TREE_TO_STRING
#define TREE_DEFAULT {0,0}
#define TREE_BODY
#include "../src/tree.h"
/* Array of kvpair. */
static void kvpair_to_string(const struct kvpair *const s,
char (*const z)[12]) { pair_to_string(&s->key, z); }
#define ARRAY_NAME kvpair
#define ARRAY_TYPE struct kvpair
#define ARRAY_TO_STRING
#define ARRAY_BODY
#include "../src/array.h"
static void linekvpair_to_string(const union line64 line,
const struct kvpair *const kv,
char (*const a)[12]) { (void)kv; date32_to_string(line.date, a); }
static int linekvpair_compare(const union line64 a, const union line64 b)
{ return a.u64 > b.u64; }
#define TREE_NAME linekvpair
#define TREE_KEY union line64
#define TREE_VALUE struct kvpair
#define TREE_COMPARE
#define TREE_TO_STRING
#define TREE_DEFAULT {{0,0},{0,0}}
#define TREE_BODY
#include "../src/tree.h"
static void linekvdate_to_string(const union line64 line,
const struct kvdate *const kv,
char (*const a)[12]) { (void)kv; date32_to_string(line.date, a); }
static int linekvdate_compare(const union line64 a, const union line64 b)
{ return a.u64 > b.u64; }
#define TREE_NAME linekvdate
#define TREE_KEY union line64
#define TREE_VALUE struct kvdate
#define TREE_COMPARE
#define TREE_TO_STRING
#define TREE_DEFAULT {0}
#define TREE_BODY
#include "../src/tree.h"
static void linekvmoney_to_string(const union line64 line,
const struct kvmoney *const kv,
char (*const a)[12]) { (void)kv; date32_to_string(line.date, a); }
static int linekvmoney_compare(const union line64 a, const union line64 b)
{ return a.u64 > b.u64; }
#define TREE_NAME linekvmoney
#define TREE_KEY union line64
#define TREE_VALUE struct kvmoney
#define TREE_COMPARE
#define TREE_TO_STRING
#define TREE_DEFAULT {{0,0},{0,CAD}}
#define TREE_BODY
#include "../src/tree.h"
/* Array of places. */
static void place_to_string(const struct place *const p,
char (*const z)[12]) { pair_to_string(&p->name, z); }
#define ARRAY_NAME place
#define ARRAY_TYPE struct place
#define ARRAY_TO_STRING
#define ARRAY_BODY
#include "../src/array.h"
/* Array of scores. */
static void score_to_string(const struct score *const s,
char (*const z)[12]) { pair_to_string(&s->name, z); }
#define ARRAY_NAME score
#define ARRAY_TYPE struct score
#define ARRAY_TO_STRING
#define ARRAY_BODY
#include "../src/array.h"
/* Array of edits. */
static void edit_to_string(const struct edit *const e,
char (*const z)[12]) { pair_to_string(&e->desc, z); }
#define ARRAY_NAME edit
#define ARRAY_TYPE struct edit
#define ARRAY_TO_STRING
#define ARRAY_BODY
#include "../src/array.h"
/* Glider tree. */
static void glider_to_string(const union line64 line, const struct glider *g,
char (*const a)[12]) { (void)g; date32_to_string(line.date, a); }
static int glider_compare(const union line64 a, const union line64 b)
{ return a.u64 > b.u64; }
#define TREE_NAME glider
#define TREE_KEY union line64
#define TREE_VALUE struct glider
#define TREE_COMPARE
#define TREE_TO_STRING
#define TREE_BODY
#include "../src/tree.h"
/* Flight tree. */
static void flight_to_string(const union line64 line, const struct flight *f,
char (*const a)[12]) { (void)f; date32_to_string(line.date, a); }
static int flight_compare(const union line64 a, const union line64 b)
{ return a.u64 > b.u64; }
#define TREE_NAME flight
#define TREE_KEY union line64
#define TREE_VALUE struct flight
#define TREE_COMPARE
#define TREE_TO_STRING
#define TREE_BODY
#include "../src/tree.h"
/* KJV tree. */
static void kjv_to_string(const union line64 line, const struct kjvrange *u,
char (*const a)[12]) { (void)u; date32_to_string(line.date, a); }
static int kjv_compare(const union line64 a, const union line64 b)
{ return a.u64 > b.u64; }
#define TREE_NAME kjv
#define TREE_KEY union line64
#define TREE_VALUE struct kjvrange
#define TREE_COMPARE
#define TREE_TO_STRING
#define TREE_BODY
#include "../src/tree.h"
/*!conditions:re2c*/
static int scan_day(struct scan *const scan, union date32 date,
const char *const buffer) {
const char *YYCURSOR, *YYMARKER, *yyt1, *yyt2, *yyt3, *s0, *s1, *t0, *t1;
enum YYCONDTYPE condition = yycline;
/* All '\n' might overflow on last, but it wouldn't matter because there's
no content. */
size_t line = 1;
char datestr[12] = {0};
const char *fail = "perhaps a bat?", *lastnl = buffer;
struct {
enum YYCONDTYPE future;
union {
struct pair *pair;
struct money *money;
};
} input;
struct score *new_score = 0;
struct glider *new_glider = 0;
struct flight *new_flight = 0;
struct {
enum kjv_book book; uint32_t chapter, verse, verse_end;
} kjv = { Revelation, 0, 0, 0 };
assert(scan && date.u32 && buffer);
YYCURSOR = YYMARKER = yyt1 = buffer;
date32_to_string(date, &datestr);
/*!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;
ws = [ \t];
glyph = [^\x00-\x20\x7f] | [\x80-\xff]; // [^\x00\n\t ] + all weird
semichar = glyph \ ";";
brachar = semichar \ [[\]];
parchar = glyph \ [()];
// label: possibly separated by spaces; used in freestanding last
anylabel = glyph+ (ws+ glyph+)*;
// label except ";"; used in freestanding
semilabel = semichar+ (ws+ semichar+)*;
// label except ";[]"; used in []
bralabel = brachar+ (ws+ brachar+)*;
// label except start "()"; used in location
parlabel = (parchar glyph*) (ws+ glyph+)*;
keyword = [A-Za-z0-9][A-Za-z0-9_-]*;
uint = [0-9]+;
natural = [1-9][0-9]*;
decimal = "-"? [0-9]+ ("." [0-9]*)?;
date = natural "-" [0-1][0-9] "-" [0-3][0-9];
minutes = [0-5][0-9];
airport = [A-Z0-9]{4,4};
moneyamount = "-"? [0-9]+ ("." [0-9][0-9])?; // fixme?
kjvlookat = ws* natural ":" natural [ab]? ("-" natural [ab]?)? ws+ "--" ws+;
first = ("I" | "1") " "?;
second = ("II" | "2") " "?;
third = ("III" | "3") " "?;
*/
for( ; ; ) {
/*!re2c /**/
<skip> [^\n\x00] { continue; } /* Default ignore. */
<newline> * { fail = "newline expected"; goto catch; }
<skip> "\x00" { fail = "no newline at end of file"; goto catch; }
<line> "\x00" { return 1; } /* End of day. */
<line, skip, newline> "\n" @s1 => line
{ line++; lastnl = s1; continue; }
<line> * :=> skip
<line> "->" :=> place
<line> "--" / [^-] :=> source
<line> "::" / [^:] :=> score
<line> "[" :=> bracket
/* ^"[" ... */
<bracket> * { fail = "bracket unrecognized"; goto catch; }
<bracket> "glider] " :=> glider_type
<bracket> "flight]" :=> flight_type
<bracket> "document: " :=> document
<bracket> "rem: " :=> remark
<bracket> "contact: " :=> contact
<bracket> "book: " :=> book
<bracket> "tv: " :=> tv
<bracket> "movie: " :=> movie
<bracket> "idea: " :=> idea
<bracket> "vaccine: " :=> vaccine
<bracket> "medication: " :=> medication
<bracket> "lab: " :=> lab
<bracket> "tax: " :=> tax
<bracket> "in: " :=> income
<bracket> "mail: " :=> mail
<bracket> "couch: " :=> couch
<bracket> "from: " :=> from
<line> "- " => input_text {
const union line64 key = { { (uint32_t)line, date } };
struct pair *pair;
switch(linepair_tree_bulk_assign(&scan->dreams, key, &pair)) {
case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
case TREE_ABSENT: break;
}
pair->a = pair->b = 0;
input.future = yycnewline, input.pair = pair;
fprintf(stderr, "%s:%zu: new dream.\n", datestr, line);
continue;
}
/* Whatsapp messages ignored. [11-03, 04:19] Contact: massage
Hopefully they don't have a ':' in their name. */
<bracket> [0-1][0-9] "-" [0-3][0-9] ", " [0-2][0-9] ":" [0-5][0-9] "] "
@s0 [^\x00\n:]+ @s1 ":" [^\x00\n]* / "\n" => skip {
fprintf(stderr, "Whatsapp message from %.*s ignored.\n",
(int)(s1 - s0), s0);
continue;
}
<document> * { fail = "document title"; goto catch; }
<document> @s0 bralabel @s1 "]" => input_text {
const union line64 key = { { (uint32_t)line, date } };
size_t *pi;
struct kvpair *doc;
if(!(doc = kvpair_array_new(&scan->documents.array))) goto catch;
doc->key.a = s0, doc->key.b = s1;
doc->value.a = 0, doc->value.b = 0;
input.future = yycnewline, input.pair = &doc->value;
switch(linemap_tree_bulk_assign(&scan->documents.dates, key, &pi)) {
case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
case TREE_ABSENT:
*pi = (size_t)(doc - scan->documents.array.data); break;
}
fprintf(stderr, "%s:%zu: new document <<%.*s>> stored at %zu.\n",
datestr, line, (int)(s1 - s0), s0, *pi);
continue;
}
<remark> * { fail = "remark"; goto catch; }
<remark> @s0 date "]" => input_text {
const union line64 key = { { (uint32_t)line, date } };
size_t *pi;
struct edit *ed;
if(!(ed = edit_array_new(&scan->edits.array))) goto catch;
ed->edit.u32 = 0;
ed->desc.a = 0, ed->desc.b = 0;
input.future = yycnewline, input.pair = &ed->desc;
if(!pair_to_date(s0, &ed->edit)) goto catch;
switch(linemap_tree_bulk_assign(&scan->edits.dates, key, &pi)) {
case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
case TREE_ABSENT:
*pi = (size_t)(ed - scan->edits.array.data); break;
}
fprintf(stderr, "%s:%zu: new remark from %"
PRIu32 "-%.2" PRIu32 "-%.2" PRIu32 " stored at %zu.\n",
datestr, line, ed->edit.year, ed->edit.month, ed->edit.day, *pi);
continue;
}
<contact> * { fail = "contact unrecognized"; goto catch; }
<contact> @s0 bralabel @s1 "]" => input_text {
const union line64 key = { { (uint32_t)line, date } };
struct kvpair *pair;
switch(linekvpair_tree_bulk_assign(&scan->contacts, key, &pair)) {
case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
case TREE_ABSENT: break;
}
pair->key.a = s0, pair->key.b = s1;
pair->value.a = pair->value.b = 0;
input.future = yycnewline, input.pair = &pair->value;
fprintf(stderr, "%s:%zu: new contact <<%.*s>>.\n",
datestr, line, (int)(s1 - s0), s0);
continue;
}
<book> * { fail = "book unrecognized"; goto catch; }
<book> @s0 bralabel @s1 "]" => input_text {
const union line64 key = { { (uint32_t)line, date } };
struct kvpair *pair;
switch(linekvpair_tree_bulk_assign(&scan->books, key, &pair)) {
case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
case TREE_ABSENT: break;
}
pair->key.a = s0, pair->key.b = s1;
pair->value.a = pair->value.b = 0;
input.future = yycnewline, input.pair = &pair->value;
fprintf(stderr, "%s:%zu: new book <<%.*s>>.\n",
datestr, line, (int)(s1 - s0), s0);
continue;
}
<tv> * { fail = "tv unrecognized"; goto catch; }
<tv> @s0 bralabel @s1 "]" => input_text {
const union line64 key = { { (uint32_t)line, date } };
struct kvpair *pair;
switch(linekvpair_tree_bulk_assign(&scan->tvs, key, &pair)) {
case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
case TREE_ABSENT: break;
}
pair->key.a = s0, pair->key.b = s1;
pair->value.a = pair->value.b = 0;
input.future = yycnewline, input.pair = &pair->value;
fprintf(stderr, "%s:%zu: new tv <<%.*s>>.\n",
datestr, line, (int)(s1 - s0), s0);
continue;
}
<movie> * { fail = "movie unrecognized"; goto catch; }
<movie> @s0 bralabel @s1 "]" => input_text {
const union line64 key = { { (uint32_t)line, date } };
struct kvpair *pair;
switch(linekvpair_tree_bulk_assign(&scan->contacts, key, &pair)) {
case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
case TREE_ABSENT: break;
}
pair->key.a = s0, pair->key.b = s1;
pair->value.a = pair->value.b = 0;
input.future = yycnewline, input.pair = &pair->value;
fprintf(stderr, "%s:%zu: new movie <<%.*s>>.\n",
datestr, line, (int)(s1 - s0), s0);
continue;
}
<idea> * { fail = "idea unrecognized"; goto catch; }
<idea> @s0 bralabel @s1 "]" => input_text {
const union line64 key = { { (uint32_t)line, date } };
struct kvpair *pair;
switch(linekvpair_tree_bulk_assign(&scan->contacts, key, &pair)) {
case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
case TREE_ABSENT: break;
}
pair->key.a = s0, pair->key.b = s1;
pair->value.a = pair->value.b = 0;
input.future = yycnewline, input.pair = &pair->value;
fprintf(stderr, "%s:%zu: new idea <<%.*s>>.\n",
datestr, line, (int)(s1 - s0), s0);
continue;
}
<vaccine> * { fail = "vaccine unrecognized"; goto catch; }
<vaccine> @s0 bralabel @s1 "]" => input_text {
const union line64 key = { { (uint32_t)line, date } };
struct kvpair *pair;
switch(linekvpair_tree_bulk_assign(&scan->vaccines, key, &pair)) {
case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
case TREE_ABSENT: break;
}
pair->key.a = s0, pair->key.b = s1;
pair->value.a = pair->value.b = 0;
input.future = yycnewline, input.pair = &pair->value;
fprintf(stderr, "%s:%zu: new vaccine <<%.*s>>.\n",
datestr, line, (int)(s1 - s0), s0);
continue;
}
<medication> * { fail = "medication unrecognized"; goto catch; }
<medication> @s0 bralabel @s1 "]" => input_text {
const union line64 key = { { (uint32_t)line, date } };
struct kvpair *pair;
switch(linekvpair_tree_bulk_assign(&scan->medications, key, &pair)){
case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
case TREE_ABSENT: break;
}
pair->key.a = s0, pair->key.b = s1;
pair->value.a = pair->value.b = 0;
input.future = yycnewline, input.pair = &pair->value;
fprintf(stderr, "%s:%zu: new medication <<%.*s>>.\n",
datestr, line, (int)(s1 - s0), s0);
continue;
}
<lab> * { fail = "lab unrecognized"; goto catch; }
<lab> @s0 bralabel @s1 "]" => input_text {
const union line64 key = { { (uint32_t)line, date } };
struct kvpair *pair;
switch(linekvpair_tree_bulk_assign(&scan->labs, key, &pair)) {
case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
case TREE_ABSENT: break;
}
pair->key.a = s0, pair->key.b = s1;
pair->value.a = pair->value.b = 0;
input.future = yycnewline, input.pair = &pair->value;
fprintf(stderr, "%s:%zu: new lab <<%.*s>>.\n",
datestr, line, (int)(s1 - s0), s0);
continue;
}
<tax> * { fail = "tax unrecognized"; goto catch; }
<tax> @s0 bralabel @s1 "]" => input_money {
const union line64 key = { { (uint32_t)line, date } };
struct kvmoney *pair;
switch(linekvmoney_tree_bulk_assign(&scan->taxes, key, &pair)) {
case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
case TREE_ABSENT: break;
}
pair->key.a = s0, pair->key.b = s1;
pair->value = (struct money){0, CAD};
input.future = yycnewline, input.money = &pair->value;
fprintf(stderr, "%s:%zu: new tax <<%.*s>>.\n",
datestr, line, (int)(s1 - s0), s0);
continue;
}
<income> * { fail = "income unrecognized"; goto catch; }
<income> @s0 bralabel @s1 "]" => input_money {
const union line64 key = { { (uint32_t)line, date } };
struct kvmoney *pair;
switch(linekvmoney_tree_bulk_assign(&scan->incomes, key, &pair)) {
case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
case TREE_ABSENT: break;
}
pair->key.a = s0, pair->key.b = s1;
pair->value = (struct money){0, CAD};
input.future = yycline, input.money = &pair->value;
fprintf(stderr, "%s:%zu: new income <<%.*s>>.\n",
datestr, line, (int)(s1 - s0), s0);
continue;
}
<mail> * { fail = "mail unrecognized"; goto catch; }
<mail> @s0 bralabel @s1 "]" => input_text {
const union line64 key = { { (uint32_t)line, date } };
struct kvpair *pair;
switch(linekvpair_tree_bulk_assign(&scan->mails, key, &pair)) {
case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
case TREE_ABSENT: break;
}
pair->key.a = s0, pair->key.b = s1;
pair->value.a = pair->value.b = 0;
input.future = yycnewline, input.pair = &pair->value;
fprintf(stderr, "%s:%zu: new mail <<%.*s>>.\n",
datestr, line, (int)(s1 - s0), s0);
continue;
}
<couch> * { fail = "couch unrecognized"; goto catch; }
<couch> @s0 bralabel @s1 "]" => input_text {
const union line64 key = { { (uint32_t)line, date } };
struct kvpair *pair;
switch(linekvpair_tree_bulk_assign(&scan->couches, key, &pair)) {
case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
case TREE_ABSENT: break;
}
pair->key.a = s0, pair->key.b = s1;
pair->value.a = pair->value.b = 0;
input.future = yycnewline, input.pair = &pair->value;
fprintf(stderr, "%s:%zu: new couch <<%.*s>>.\n",
datestr, line, (int)(s1 - s0), s0);
continue;
}
<from> * { fail = "from unrecognized"; goto catch; }
<from> "~"? @s0 date "]" => input_text {
const union line64 key = { { (uint32_t)line, date } };
struct kvdate *pair;
switch(linekvdate_tree_bulk_assign(&scan->froms, key, &pair)) {
case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
case TREE_ABSENT: break;
}
if(!pair_to_date(s0, &pair->key)) goto catch;
pair->value.a = pair->value.b = 0;
input.future = yycnewline, input.pair = &pair->value;
fprintf(stderr, "%s:%zu: new from.\n", datestr, line);
continue;
}
<place> * { fail = "place unrecognized"; goto catch; }
<place> @s0 parlabel @s1 / "\n" => skip { also_place: {
const struct pair keyword = pair(s0, s1);
const union line64 key = { { (uint32_t)line, date } };
size_t i, *pi;
/*fprintf(stderr, "map: <<%.*s>> out of %s\n", (int)(s1 - s0), s0,
pair_map_table_to_string(&scan->places.map));*/
if(!(i = pair_map_table_get(&scan->places.map, keyword)))
{ fail = "keyword not introduced"; goto catch; }
switch(linemap_tree_bulk_assign(&scan->places.dates, key, &pi)) {
case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
case TREE_ABSENT: *pi = i; break;
}
fprintf(stderr, "%s:%zu: place <<%.*s>>\n",
datestr, line, (int)(s1 - s0), s0);
} continue; }
<place> "(" @t0 decimal "," @t1 decimal ")"
ws+ @s0 parlabel @s1 / "\n" => skip {
const struct pair keyword = pair(s0, s1);
const double x = strtod(t0, 0), y = strtod(t1, 0); /* Safe? */
size_t *idx;
struct place *place;
switch(pair_map_table_assign(&scan->places.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(!(place = place_array_new(&scan->places.array))) goto catch;
*idx = (size_t)(place - scan->places.array.data);
place->name.a = s0, place->name.b = s1;
place->x = x, place->y = y;
fprintf(stderr,
"%s:%zu: new place: <<%.*s>> at (%f,%f) stored at %zu.\n",
datestr, line, (int)(s1 - s0), s0, x, y, *idx);
goto also_place;
}
// anonymous place: bypass map and have a 1:1 correspondence
<place> "(" @t0 decimal "," @t1 decimal ")" / "\n" => skip {
size_t *idx;
struct place *place;
const double x = strtod(t0, 0), y = strtod(t1, 0); /* Safe? */
const union line64 key = { { (uint32_t)line, date } };
if(!(place = place_array_new(&scan->places.array))) goto catch;
place->name.a = place->name.b = 0;
place->x = x, place->y = y;
switch(linemap_tree_bulk_assign(&scan->places.dates, key, &idx)) {
case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
case TREE_ABSENT: break;
}
*idx = (size_t)(place - scan->places.array.data);
continue;
}
// fixme: place allows multiple places to be named the same thing?
<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(!(i = pair_map_table_get(&scan->sources.map, keyword)))
{ fail = "keyword not introduced"; goto catch; }
switch(linemap_tree_bulk_assign(&scan->sources.dates, key, &pi)) {
case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
case TREE_ABSENT: *pi = i; break;
}
fprintf(stderr, "%s:%zu: source <<%.*s>>\n",
datestr, line, (int)(s1 - s0), s0);
} continue; }
/* New source. */
<source> @s0 keyword @s1 ":" => input_text {
struct pair keyword = pair(s0, s1);
size_t *idx;
struct kvpair *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 = kvpair_array_new(&scan->sources.array))) goto catch;
*idx = (size_t)(source - scan->sources.array.data);
source->key.a = s0, source->key.b = s1;
source->value.a = 0, source->value.b = 0;
input.future = yycnewline, input.pair = &source->value;
fprintf(stderr, "%s:%zu: new source <<%.*s>> stored at %zu.\n",
datestr, line, (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 { new_score: {
const struct pair keyword = pair(s0, s1);
const union line64 key = { { (uint32_t)line, date } };
size_t idx, *pidx;
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_assign(&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;
}
fprintf(stderr, "%s:%zu: score <%.*s>\n",
datestr, line, (int)(s1 - s0), s0);
} continue; }
/* New score. */
<score> @s0 keyword @s1 ":" => score_name {
size_t *idx;
assert(!new_score);
/* Create a new mapping from dateline to scores array. */
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;
}
/* New entry in the scores array for this map to point to. */
if(!(new_score = score_array_new(&scan->scores.array))) goto catch;
*idx = (size_t)(new_score - scan->scores.array.data); /* Offset. */
/*struct pair key, name; union date32 date, last; unsigned edges;*/
new_score->key.a = s0, new_score->key.b = s1;
new_score->name.a = 0, new_score->name.b = 0;
new_score->date.u32 = new_score->last.u32 = 0;
new_score->edges = 0, new_score->score = 0;
fprintf(stderr, "%s:%zu: new score <%.*s> stored at %zu.\n",
datestr, line, (int)(s1 - s0), s0, *idx);
goto new_score;
}
<score_name> * { fail = "name unrecognized"; goto catch; }
<score_name> ws* @s0 semilabel @s1 ";" => score_date {
assert(new_score);
new_score->name.a = s0, new_score->name.b = s1;
continue;
}
<score_date> * { fail = "date unrecognized"; goto catch; }
<score_date> ws* "~"? @s0 date ";" => score_edges {
assert(new_score);
if(!pair_to_date(s0, &new_score->date)) goto catch;
continue;
}
<score_edges> * { fail = "edges unrecognized"; goto catch; }
<score_edges> ws* "~"? @s0 uint @s1 / "\n" => skip {
assert(new_score);
if(!pair_to_natural(s0, s1, &new_score->edges)) goto catch;
new_score = 0; /* Done. */
continue;
}
/* type, reg, launch, how, height, landing, pilot, dual, instr, remarks
eg, [glider] 2-33A; C-GCLK; CYQQ; A; 2000'; CYQQ; ;:13;; Peters D1 */
<glider_type> * { fail = "glider type"; goto catch; }
<glider_type> ws* @s0 semilabel @s1 ";" => glider_reg {
const union line64 key = {{ (uint32_t)line, date }};
assert(!new_glider);
switch(glider_tree_bulk_assign(&scan->gliders, key, &new_glider)) {
case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
case TREE_ABSENT: break;
}
new_glider->type.a = s0, new_glider->type.b = s1;
continue;
}
<glider_reg> * { fail = "glider reg"; goto catch; }
<glider_reg> ws* @s0 semilabel @s1 ";" => glider_launch
{ new_glider->reg.a = s0, new_glider->reg.b = s1; continue; }
<glider_launch> * { fail = "glider launch"; goto catch; }
<glider_launch> ws* @s0 airport @s1 ";" => glider_how {
new_glider->launch.a = s0, new_glider->launch.b = s1;
fprintf(stderr, "%s:%zu: glider <<%.*s>> at <<%.*s>>\n",
datestr, line, (int)(new_glider->reg.b - new_glider->reg.a),
new_glider->reg.a, (int)(s1 - s0), s0);
continue;
}
<glider_how> * { fail = "glider how"; goto catch; }
<glider_how> ws* @s0 [MWA] ";" => glider_height {
switch(*s0) {
case 'M': new_glider->how = MotorCarTow; break;
case 'W': new_glider->how = Winch; break;
case 'A': new_glider->how = AeroTow; break;
}
continue;
}
<glider_height> * { fail = "glider height"; goto catch; }
<glider_height> ws* @s0 natural @s1 "';" => glider_landing
{ if(!pair_to_natural(s0, s1, &new_glider->height_ft)); continue; }
<glider_landing> * { fail = "glider landing"; goto catch; }
<glider_landing> ws* @s0 airport @s1 ";" => glider_pilot
{ new_glider->landing.a = s0, new_glider->landing.b = s1; continue;}
<glider_pilot> * { fail = "glider pilot time"; goto catch; }
<glider_pilot> ws* ";" => glider_dual /* not PIC */
{ new_glider->pilot_min = 0; continue; }
<glider_pilot> ws* @s0 natural? @s1 ":" @t0 minutes @t1 ws* ";"
=> glider_dual { if(!pair_colon_to_minutes(s0, s1, t0, t1,
&new_glider->pilot_min)) { fail = "glider pilot time"; goto catch; }
continue; }
<glider_dual> * { fail = "glider dual time"; goto catch; }
<glider_dual> ws* ";" => glider_instr
{ new_glider->dual_min = 0; continue; }
<glider_dual> ws* @s0 natural? @s1 ":" @t0 minutes @t1 ws* ";"
=> glider_instr { if(!pair_colon_to_minutes(s0, s1, t0, t1,
&new_glider->dual_min)) { fail = "glider dual time"; goto catch; }
continue; }
<glider_instr> * { fail = "glider instr time"; goto catch; }
<glider_instr> ws* ";" => glider_remarks
{ new_glider->instr_min = 0; continue; }
<glider_instr> ws* @s0 natural? @s1 ":" @t0 minutes @t1 ws* ";"
=> glider_remarks { if(!pair_hours_to_minutes(s0, s1, t0, t1,
&new_glider->instr_min)) { fail = "glider instr time"; goto catch; }
continue; }
<glider_remarks> * { fail = "glider remarks"; goto catch; }
<glider_remarks> "\n" @s1 => line
{ new_glider->remarks.a = new_glider->remarks.b = 0;
new_glider = 0; line++; lastnl = s1; continue; }
<glider_remarks> ws* @s0 anylabel @s1 "\n" @t1 => line
{ new_glider->remarks.a = s0, new_glider->remarks.b = s1;
new_glider = 0; line++; lastnl = t1; continue; }
/* type; registration; launch -- landing; pic; sic;
single engine day dual; pilot; instrument simulated; actual; remarks */
<flight_type> * { fail = "flight type"; goto catch; }
<flight_type> ws* @s0 semilabel @s1 ";" => flight_reg {
const union line64 key = {{ (uint32_t)line, date }};
assert(!new_flight);
switch(flight_tree_bulk_assign(&scan->flights, key, &new_flight)) {
case TREE_PRESENT: fail = "duplicate key";
case TREE_ERROR: goto catch;
case TREE_ABSENT: break;
}
new_flight->type.a = s0, new_flight->type.b = s1;
continue;
}
<flight_reg> * { fail = "flight reg"; goto catch; }
<flight_reg> ws* @s0 semilabel @s1 ";" => flight_airports
{ new_flight->reg.a = s0, new_flight->reg.b = s1; continue; }
<flight_airports> * { fail = "flight airports"; goto catch; }
<flight_airports> ws* @s0 airport @s1 ws* "--"
ws* @t0 airport @t1 ws* ";" => flight_pic {
new_flight->launch.a = s0, new_flight->launch.b = s1;
new_flight->landing.a = t0, new_flight->landing.b = t1;
fprintf(stderr, "%s:%zu: flight <<%.*s>> at <<%.*s>>\n",
datestr, line, (int)(new_flight->reg.b - new_flight->reg.a),
new_flight->reg.a, (int)(s1 - s0), s0);
continue;
}
<flight_pic> * { fail = "flight pic"; goto catch; }
<flight_pic> ws* @s0 semilabel @s1 ";" => flight_sic
{ new_flight->pilot.a = s0, new_flight->pilot.b = s1; continue; }
<flight_sic> * { fail = "flight sic"; goto catch; }
<flight_sic> ws* ";" => flight_dual
{ new_flight->copilot.a = new_flight->copilot.b = 0; continue; }
<flight_sic> ws* @s0 semilabel @s1 ";" => flight_dual
{ new_flight->copilot.a = s0, new_flight->copilot.b = s1; continue; }
<flight_dual> * { fail = "flight dual time"; goto catch; }
<flight_dual> ws* ";" => flight_pilot
{ new_flight->dual_min = 0; continue; }
<flight_dual> ws* @s0 [0-9]* @s1 "." @t0 [0-9] @t1 "h" ws* ";"
=> flight_pilot { if(!pair_hours_to_minutes(s0, s1, t0, t1,
&new_flight->dual_min)) { fail = "flight dual time"; goto catch; }
continue; }
<flight_pilot> * { fail = "flight pilot time"; goto catch; }
<flight_pilot> ws* ";" => flight_ifrsim
{ new_flight->pilot_min = 0; continue; }
<flight_pilot> ws* @s0 [0-9]* @s1 "." @t0 [0-9] @t1 "h" ws* ";"
=> flight_ifrsim { if(!pair_hours_to_minutes(s0, s1, t0, t1,
&new_flight->pilot_min)) { fail = "flight pilot time";
goto catch; } continue; }
<flight_ifrsim> * { fail = "flight simulated ifr time"; goto catch; }
<flight_ifrsim> ws* ";" => flight_ifr
{ new_flight->ifrsim_min = 0; continue; }
<flight_ifrsim> ws* @s0 [0-9]* @s1 "." @t0 [0-9] @t1 "h" ws* ";"
=> flight_ifr { if(!pair_hours_to_minutes(s0, s1, t0, t1,
&new_flight->ifrsim_min)) { fail = "flight simulated ifr time";
goto catch; } continue; }
<flight_ifr> * { fail = "flight ifr time"; goto catch; }
<flight_ifr> ws* ";" => flight_remarks
{ new_flight->ifr_min = 0; continue; }
<flight_ifr> ws* @s0 [0-9]* @s1 "." @t0 [0-9] @t1 "h" ws* ";"
=> flight_remarks { if(!pair_hours_to_minutes(s0, s1, t0, t1,
&new_flight->ifr_min)) { fail = "flight ifr time"; goto catch; }
continue; }
<flight_remarks> * { fail = "flight remarks"; goto catch; }
<flight_remarks> "\n" @s1 => line
{ new_flight->remarks.a = new_flight->remarks.b = 0;
new_flight = 0; line++; lastnl = s1; continue; }
<flight_remarks> ws* @s0 anylabel @s1 "\n" @t1 => line
{ new_flight->remarks.a = s0, new_flight->remarks.b = s1;
new_flight = 0; line++; lastnl = t1; continue; }
/* Books in KJV. */
<line> "Genesis" / kjvlookat => kjvbook { kjv.book = Genesis; continue; }
<line> "Exodus" / kjvlookat => kjvbook { kjv.book = Exodus; continue; }
<line> "Leviticus" / kjvlookat => kjvbook { kjv.book = Leviticus; continue; }
<line> "Numbers" / kjvlookat => kjvbook { kjv.book = Numbers; continue; }
<line> "Deuteronomy" / kjvlookat => kjvbook { kjv.book = Deuteronomy; continue; }
<line> "Joshua" / kjvlookat => kjvbook { kjv.book = Joshua; continue; }
<line> "Judges" / kjvlookat => kjvbook { kjv.book = Judges; continue; }
<line> "Ruth" / kjvlookat => kjvbook { kjv.book = Ruth; continue; }
<line> first "Samuel" / kjvlookat => kjvbook { kjv.book = ISamuel; continue; }
<line> second "Samuel" / kjvlookat => kjvbook { kjv.book = IISamuel; continue; }
<line> first "Kings" / kjvlookat => kjvbook { kjv.book = IKings; continue; }
<line> second "Kings" / kjvlookat => kjvbook { kjv.book = IIKings; continue; }
<line> first "Chronicles" / kjvlookat => kjvbook { kjv.book = IChronicles; continue; }
<line> second "Chronicles" / kjvlookat => kjvbook { kjv.book = IIChronicles; continue; }
<line> "Ezra" / kjvlookat => kjvbook { kjv.book = Ezra; continue; }
<line> "Nehemiah" / kjvlookat => kjvbook { kjv.book = Nehemiah; continue; }
<line> "Esther" / kjvlookat => kjvbook { kjv.book = Esther; continue; }
<line> "Job" / kjvlookat => kjvbook { kjv.book = Job; continue; }
<line> "Psalms" / kjvlookat => kjvbook { kjv.book = Psalms; continue; }
<line> "Proverbs" / kjvlookat => kjvbook { kjv.book = Proverbs; continue; }
<line> "Ecclesiastes" / kjvlookat => kjvbook
{ kjv.book = Ecclesiastes; continue; }
<line> "Song of Solomon" / kjvlookat => kjvbook
{ kjv.book = Song_of_Solomon; continue; }
<line> "Isaiah" / kjvlookat => kjvbook { kjv.book = Isaiah; continue; }
<line> "Jeremiah" / kjvlookat => kjvbook { kjv.book = Jeremiah; continue; }
<line> "Lamentations" / kjvlookat => kjvbook { kjv.book = Lamentations; continue; }
<line> "Ezekiel" / kjvlookat => kjvbook { kjv.book = Ezekiel; continue; }
<line> "Daniel" / kjvlookat => kjvbook { kjv.book = Daniel; continue; }
<line> "Hosea" / kjvlookat => kjvbook { kjv.book = Hosea; continue; }
<line> "Joel" / kjvlookat => kjvbook { kjv.book = Joel; continue; }
<line> "Amos" / kjvlookat => kjvbook { kjv.book = Amos; continue; }
<line> "Obadiah" / kjvlookat => kjvbook { kjv.book = Obadiah; continue; }
<line> "Jonah" / kjvlookat => kjvbook { kjv.book = Jonah; continue; }
<line> "Micah" / kjvlookat => kjvbook { kjv.book = Micah; continue; }
<line> "Nahum" / kjvlookat => kjvbook { kjv.book = Nahum; continue; }
<line> "Habakkuk" / kjvlookat => kjvbook { kjv.book = Habakkuk; continue; }
<line> "Zephaniah" / kjvlookat => kjvbook { kjv.book = Zephaniah; continue; }
<line> "Haggai" / kjvlookat => kjvbook { kjv.book = Haggai; continue; }
<line> "Zechariah" / kjvlookat => kjvbook { kjv.book = Zechariah; continue; }
<line> "Malachi" / kjvlookat => kjvbook { kjv.book = Malachi; continue; }
<line> "Matthew" / kjvlookat => kjvbook { kjv.book = Matthew; continue; }
<line> "Mark" / kjvlookat => kjvbook { kjv.book = Mark; continue; }
<line> "Luke" / kjvlookat => kjvbook { kjv.book = Luke; continue; }
<line> "John" / kjvlookat => kjvbook { kjv.book = John; continue; }
<line> "Acts" / kjvlookat => kjvbook { kjv.book = Acts; continue; }
<line> "Romans" / kjvlookat => kjvbook { kjv.book = Romans; continue; }
<line> first "Corinthians" / kjvlookat => kjvbook { kjv.book = ICorinthians; continue; }
<line> second "Corinthians" / kjvlookat => kjvbook { kjv.book = IICorinthians; continue; }
<line> "Galatians" / kjvlookat => kjvbook { kjv.book = Galatians; continue; }
<line> "Ephesians" / kjvlookat => kjvbook { kjv.book = Ephesians; continue; }
<line> "Philippians" / kjvlookat => kjvbook { kjv.book = Philippians; continue; }
<line> "Colossians" / kjvlookat => kjvbook { kjv.book = Colossians; continue; }
<line> first "Thessalonians" / kjvlookat => kjvbook { kjv.book = IThessalonians; continue; }
<line> second "Thessalonians" / kjvlookat => kjvbook { kjv.book = IIThessalonians; continue; }
<line> first "Timothy" / kjvlookat => kjvbook { kjv.book = ITimothy; continue; }
<line> second "Timothy" / kjvlookat => kjvbook { kjv.book = IITimothy; continue; }
<line> "Titus" / kjvlookat => kjvbook { kjv.book = Titus; continue; }
<line> "Philemon" / kjvlookat => kjvbook { kjv.book = Philemon; continue; }
<line> "Hebrews" / kjvlookat => kjvbook { kjv.book = Hebrews; continue; }
<line> "James" / kjvlookat => kjvbook { kjv.book = James; continue; }
<line> first "Peter" / kjvlookat => kjvbook { kjv.book = IPeter; continue; }
<line> second "Peter" / kjvlookat => kjvbook { kjv.book = IIPeter; continue; }
<line> first "John" / kjvlookat => kjvbook { kjv.book = IJohn; continue; }
<line> second "John" / kjvlookat => kjvbook { kjv.book = IIJohn; continue; }
<line> third "John" / kjvlookat => kjvbook { kjv.book = IIIJohn; continue; }
<line> "Jude" / kjvlookat => kjvbook { kjv.book = Jude; continue; }
<line> "Revelation" / kjvlookat => kjvbook { kjv.book = Revelation; continue; }
<kjvbook> * { fail = "kjv unrecognized"; goto catch; }
/* 19:15a, just ignore the a. */
<kjvbook> " " @s0 natural @s1 ":" @t0 natural @t1 [ab]? {
if(kjv.chapter || kjv.verse || kjv.verse_end)
{ fail = "kjv reference"; goto catch; }
if(!pair_to_natural(s0, s1, &kjv.chapter)
|| !pair_to_natural(t0, t1, &kjv.verse))
{ fail = "kjv reference numerical error"; goto catch; }
continue;
}
<kjvbook> "-" @s0 natural @s1 [ab]? { /* Verse range. */
if(!kjv.chapter || !kjv.verse || kjv.verse_end)
{ fail = "kjv range unrecognized"; goto catch; }
if(!pair_to_natural(s0, s1, &kjv.verse_end))
{ fail = "kjv range numerical error"; goto catch; }
continue;
}
<kjvbook> " -- " => skip {
if(!kjv.chapter || !kjv.verse)
{ fail = "kjv missing information"; goto catch; }
if(kjv.verse_end && kjv.verse_end <= kjv.verse)
{ fail = "kjv interval error"; goto catch; }
const union line64 key = {{ (uint32_t)line, date }};
struct kjvrange *value;
switch(kjv_tree_bulk_assign(&scan->kjvs, key, &value)) {
case TREE_PRESENT: fail = "kjv duplicate key";
case TREE_ERROR: goto catch;
case TREE_ABSENT:
value->start.book = kjv.book;
value->start.chapter = kjv.chapter;
value->start.verse = kjv.verse;
value->verse_end = kjv.verse_end;
break;
}
fprintf(stderr, "%s:%zu: KJV %s %" PRIu32 ":%" PRIu32, datestr,
line, kjv_book_string[kjv.book], kjv.chapter, kjv.verse);
if(kjv.verse_end) fprintf(stderr, "-%u", kjv.verse_end);
fprintf(stderr, "\n");
kjv.book = Revelation, kjv.chapter = 0, kjv.verse = 0,
kjv.verse_end = 0;
continue;
}
/* fixme: This is a cool way of doing things. Avoid repetition, make
all the things this way. */
<input_text, input_text_multi> * { fail = "text input"; goto catch; }
<input_text> ws { continue; }
<input_text> "<<\n" @s0 => input_text_multi { // multi-line
input.pair->a = s0;
line++; lastnl = s0;
continue;
}
<input_text> @s0 anylabel? @s1 "\n" @t0 => line { // one line; last one
/*fprintf(stderr, "text: <<%.*s>>\n", (int)(s1 - s0), s0);*/
input.pair->a = s0, input.pair->b = s1;
if(input.future != yycnewline)
{ fail = "use <<text>>"; goto catch; }
line++; lastnl = t0;
continue;
}
<input_text_multi> [^\x00\n] { continue; }
<input_text_multi> [\x00]
{ fail = "missing closing \">>\""; goto catch; }
<input_text_multi> "\n" @s1 { line++; lastnl = s1; continue; }
<input_text_multi> @s1 ">>" / [^>] /*future*/ {
/*fprintf(stderr, "text: <<\n%.*s>>\n",
(int)(s1 - input.pair->a), input.pair->a);*/
input.pair->b = s1;
condition = input.future;
continue;
}
<input_money, input_money_currency> *
{ fail = "money input"; goto catch; }
<input_money> ws { continue; }
<input_money> @s0 moneyamount @s1 => input_money_currency {
if(!pair_to_cents(s0, s1, &input.money->cents)) goto catch;
printf("input_money: amount %" PRId64 "\n", input.money->cents);
continue;
}
<input_money_currency> "$" {
input.money->currency = CAD;
condition = input.future;
continue;
}
*/ }
assert(0); /* Never gets here. */
catch:
if(!errno) errno = EILSEQ;
date32_to_string(date, &datestr);
if(s1 = strchr(lastnl, '\n')) {
fprintf(stderr, "%.*s\n", (int)(s1 - lastnl), lastnl);
}
fprintf(stderr,
"%s:%zu fail: %s" /*" condition %d"*/ ".\n",
datestr, line, fail/*, condition*/);
return 0;
}
void scan_(struct scan *const scan) {
if(!scan) return;
kjv_tree_(&scan->kjvs);
flight_tree_(&scan->flights);
glider_tree_(&scan->gliders);
linekvmoney_tree_(&scan->incomes);
linekvmoney_tree_(&scan->taxes);
linekvdate_tree_(&scan->froms);
linekvpair_tree_(&scan->couches);
linekvpair_tree_(&scan->labs);
linekvpair_tree_(&scan->medications);
linekvpair_tree_(&scan->vaccines);
linekvpair_tree_(&scan->ideas);
linekvpair_tree_(&scan->movies);
linekvpair_tree_(&scan->tvs);
linekvpair_tree_(&scan->books);
linekvpair_tree_(&scan->contacts);
linepair_tree_(&scan->dreams);
linemap_tree_(&scan->scores.dates);
pair_map_table_(&scan->scores.map);
score_array_(&scan->scores.array);
linemap_tree_(&scan->places.dates);
pair_map_table_(&scan->places.map);
place_array_(&scan->places.array);
linemap_tree_(&scan->edits.dates);
pair_map_table_(&scan->edits.map);
edit_array_(&scan->edits.array);
linemap_tree_(&scan->documents.dates);
pair_map_table_(&scan->documents.map);
kvpair_array_(&scan->documents.array);
linemap_tree_(&scan->sources.dates);
pair_map_table_(&scan->sources.map);
kvpair_array_(&scan->sources.array);
}
/** @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(jrnl);
/* Null is the first item for convenience, (TABLE_DEFAULT). */
{
struct kvpair *nul;
if(!(nul = kvpair_array_new(&scan.sources.array))) goto catch;
*nul = (struct kvpair){0};
if(!(nul = kvpair_array_new(&scan.documents.array))) goto catch;
*nul = (struct kvpair){0};
}
{
struct edit *nul;
if(!(nul = edit_array_new(&scan.edits.array))) goto catch;
*nul = (struct edit){0};
}
{
struct place *nul;
if(!(nul = place_array_new(&scan.places.array))) goto catch;
*nul = (struct place){0};
}
{
struct score *nul;
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;
/* Scans make trees bulk-loaded; fix to real tree. */
if(!linemap_tree_bulk_finish(&scan.sources.dates)
|| !linemap_tree_bulk_finish(&scan.documents.dates)
|| !linemap_tree_bulk_finish(&scan.places.dates)
|| !linemap_tree_bulk_finish(&scan.scores.dates)
|| !linemap_tree_bulk_finish(&scan.edits.dates)
|| !linepair_tree_bulk_finish(&scan.dreams)
|| !linekvpair_tree_bulk_finish(&scan.contacts)
|| !linekvpair_tree_bulk_finish(&scan.books)
|| !linekvpair_tree_bulk_finish(&scan.tvs)
|| !linekvpair_tree_bulk_finish(&scan.movies)
|| !linekvpair_tree_bulk_finish(&scan.ideas)
|| !linekvpair_tree_bulk_finish(&scan.vaccines)
|| !linekvpair_tree_bulk_finish(&scan.medications)
|| !linekvpair_tree_bulk_finish(&scan.labs)
|| !linekvpair_tree_bulk_finish(&scan.couches)
|| !linekvdate_tree_bulk_finish(&scan.froms)
|| !linekvmoney_tree_bulk_finish(&scan.taxes)
|| !linekvmoney_tree_bulk_finish(&scan.incomes)
|| !glider_tree_bulk_finish(&scan.gliders)
|| !flight_tree_bulk_finish(&scan.flights)
|| !kjv_tree_bulk_finish(&scan.kjvs)) goto catch;
goto finally;
catch:
scan_(&scan);
finally:
return scan;
}
/** Lookup the source of `x` in `scan`, if any. */
static const struct kvpair *source_lookup(struct scan *const scan,
const union line64 x) {
struct linemap_tree_iterator it;
assert(scan);
it = linemap_tree_less(&scan->sources.dates, x);
/* If it's before all elements of the journal or is not on the same date as
the source, this has no source, which is `array[0]`. */
return scan->sources.array.data + (linemap_tree_has_element(&it)
&& x.date.u32 == linemap_tree_key(&it).date.u32
? *linemap_tree_value(&it) : 0);
}
/** Lookup the source of `x` in `scan`, if any; public function for testing. */
const struct kvpair *scan_source_lookup(struct scan *const scan,
const union line64 x) { return source_lookup(scan, x); }
void scan_score_graph(struct scan *const scan) {
struct linemap_tree_iterator it
= linemap_tree_iterator(&scan->scores.dates);
union line64 line;
struct score *score;
unsigned total = 0;
assert(scan);
fprintf(stderr, "*** Score graph %s.\n",
linemap_tree_to_string(&scan->scores.dates));
/* Set score to zero to verify count with paper journal. */
for(struct score *i = scan->scores.array.data,
*const z = i + scan->scores.array.size; i < z; i++) i->score = 0;
/* 840 with legend; only useful to me. */
printf("set terminal pngcairo dashed transparent truecolor"
" size 840, 480 fontscale 1\n"
"set output \"score.png\"\n");
printf("$Data <<EOD\n"
"# date, key, score, total, place\n");
while(linemap_tree_next(&it)) {
line = linemap_tree_key(&it);
assert(*linemap_tree_value(&it) < scan->scores.array.size);
score = scan->scores.array.data + *linemap_tree_value(&it);
/* Get a place. */
struct linemap_tree_iterator place_it
= linemap_tree_less(&scan->places.dates, line);
struct pair place_name;
if(!linemap_tree_has_element(&place_it)) {
place_name = (struct pair){ 0, 0 };
} else {
assert(*linemap_tree_value(&place_it) < scan->places.array.size);
place_name
= scan->places.array.data[*linemap_tree_value(&place_it)].name;
}
char datestr[12];
date32_to_string(line.date, &datestr);
score->score++; /* Keep track of the score per key. */
total++; /* Keep track of the total. */
printf("%s, %.*s, %u, %u, %.*s\n",
datestr,
(int)(score->key.b - score->key.a), score->key.a,
score->score, total,
(int)(place_name.b - place_name.a), place_name.a);
}
printf("EOD\n"
"# theozh https://stackoverflow.com/a/75466214/2472827\n"
"# get a unique list from datablock\n"
"addToList(list,col) = list.( strstrt(list,'\"'.strcol(col).'\"') \\\n"
" > 0 ? '' : ' \"'.strcol(col).'\"')\n"
"Uniqs = ''\n"
"stats $Data u (Uniqs=addToList(Uniqs,2)) nooutput\n"
"Uniq(i) = word(Uniqs,i)\n"
"getIndex(s) = sum [_i=1:words(Uniqs)] s eq word(Uniqs,_i) ? _i : 0\n"
"\n"
/*"stats $Data u 3 nooutput\n"
"cumsum = STATS_sum\n"
"stats $Data u 4 nooutput\n"
"setsum = STATS_sum\n"
"\n"*/
"myTimeFmt = \"%%Y-%%m-%%d\"\n"
"set format x myTimeFmt timedate\n"
"set xtics format myTimeFmt rotate by -30\n"
"set ylabel \"person·day cdf\"\n"
"set grid\n"
"set key out reverse Left noautotitle\n"
"set style fill solid 0.5\n"
"unset border\n"
"unset key\n"
"set autoscale xfix # max? hack: can't get x to extend further\n"
"\n"
/*"set label sprintf(\"%%u cumulative words (duplicate verses counted)\","
" cumsum) center at graph 0.5, first cumsum*100/%zu offset 0,0.5\n"
"set label sprintf(\"%%u unique KJV verse words memorized\", setsum) "
"center at graph 0.5, first setsum*100/%zu offset 0,0.5\n"
"\n"*/
"plot \\\n"
" total=0 $Data u"
" (timecolumn(1,myTimeFmt)):(total=total+1) \\\n"
" w steps lc \"black\" dt 1 lw 1, \\\n"
" total=0 '' u \\\n"
" (timecolumn(1,myTimeFmt)): \\\n"
" (total=total+1,total/2.): \\\n"
" (43200): \\\n"
" (total/2.): \\\n"
" (getIndex(strcol(2))) w boxxy lc var lw 1, \\\n"
" for [i=1:words(Uniqs)] keyentry w boxxy lc i ti Uniq(i)\n");
fprintf(stderr, "\n");
}
void scan_labs_graph(struct scan *const scan) {
struct linekvpair_tree_iterator it
= linekvpair_tree_iterator(&scan->labs);
union line64 line;
struct kvpair *kv;
assert(scan);
fprintf(stderr, "*** Labs graph %s.\n",
linekvpair_tree_to_string(&scan->labs));
while(linekvpair_tree_next(&it)) {
char datestr[12];
line = linekvpair_tree_key(&it);
kv = linekvpair_tree_value(&it);
date32_to_string(line.date, &datestr);
printf("%s, \"%.*s\", \"%.*s\"\n",
datestr, (int)(kv->key.b - kv->key.a), kv->key.a,
(int)(kv->value.b - kv->value.a), kv->value.a);
}
fprintf(stderr, "\n");
}
#include <inttypes.h>
void scan_glider_graph(struct scan *const scan) {
assert(scan);
fprintf(stderr, "*** Glider graph %s.\n",
glider_tree_to_string(&scan->gliders));
printf("set terminal pngcairo dashed transparent truecolor"
" size 840, 480 fontscale 1\n"
"set output \"glider.png\"\n");
/*printf("set terminal cairolatex standalone pdf size 16cm,10.5cm"
" dashed transparent\n"
"set output \"flight.tex\"\n");*/
/*printf("set term postscript eps enhanced\n"
"set output \"flighthours.eps\"\n");*/
printf("$Data <<EOD\n"
"# date, reg, sic, pic, source\n");
struct glider_tree_iterator it = glider_tree_iterator(&scan->gliders);
while(glider_tree_next(&it)) {
const union line64 line = glider_tree_key(&it);
const struct glider *glider = glider_tree_value(&it);
char datestr[12];
date32_to_string(line.date, &datestr);
const struct kvpair *src = source_lookup(scan, line);
assert(src);
if(!src->key.a) { fprintf(stderr,
"Glider has no source at %s; ignoring.\n", datestr); continue; }
printf("%s, ", datestr);
printf("%.*s, %" PRIu32 ", %" PRIu32,
(int)(glider->reg.b - glider->reg.a), glider->reg.a,
glider->dual_min, glider->pilot_min + glider->instr_min);
/* case POWER:
printf("%.*s, %" PRIu32 ", %" PRIu32,
(int)(flight->power.reg.b - flight->power.reg.a),
flight->power.reg.a,
flight->power.dual_min,
flight->power.pilot_min);
break; */
printf(", %.*s\n", (int)(src->key.b - src->key.a), src->key.a);
}
printf("EOD\n"
"# theozh https://stackoverflow.com/a/75466214/2472827\n"
"# get a unique list from datablock\n"
"addToList(list,col) = list.( strstrt(list,'\"'.strcol(col).'\"') \\\n"
" > 0 ? '' : ' \"'.strcol(col).'\"')\n"
"Uniqs = ''\n"
"stats $Data u (Uniqs=addToList(Uniqs,2)) nooutput\n"
"Uniq(i) = word(Uniqs,i)\n"
"getIndex(s) = sum [_i=1:words(Uniqs)] s eq word(Uniqs,_i) ? _i : 0\n"
"\n"
"stats $Data u 3 nooutput\n"
"sicsum = STATS_sum\n"
"stats $Data u 4 nooutput\n"
"picsum = STATS_sum\n"
"\n"
"myTimeFmt = \"%%Y-%%m-%%d\"\n"
"set format x myTimeFmt timedate\n"
"set xtics format myTimeFmt rotate by -30\n"
"set format y \"%%tH:%%tM\" timedate\n"
"set grid\n"
"set key out reverse Left noautotitle\n"
"set style fill solid 0.5\n"
"unset border\n"
"plot total=0 $Data u"
" (timecolumn(1,myTimeFmt)):(dy=($3+$4)*60,total=total+dy)"
" w steps lc \"black\" dt 3, \\\n"
" total=0 '' u (timecolumn(1,myTimeFmt)):"
"(dy=($3+$4)*60,total=total+dy,total/2.): \\\n"
" (43200):(total/2.):(getIndex(strcol(2))) w boxxy lc var, \\\n"
" for [i=1:words(Uniqs)] keyentry w boxxy lc i ti Uniq(i)\n"
/*"set xrange [*:'2001-09-11']\n"*/
/*"#set style fill solid 0.1 #pattern 5 (better, but restarts)\n"
"plot $Data using 1:($6/60) with fillsteps lw 2\n"*/);
fprintf(stderr, "\n");
}
void scan_flight_graph(struct scan *const scan) {
assert(scan);
fprintf(stderr, "*** Flight graph: %s.\n",
flight_tree_to_string(&scan->flights));
printf("set terminal pngcairo dashed transparent truecolor"
" size 840, 480 fontscale 1\n"
"set output \"flight.png\"\n");
printf("$Data <<EOD\n"
"# date, reg, sic, pic, source\n");
struct flight_tree_iterator it = flight_tree_iterator(&scan->flights);
while(flight_tree_next(&it)) {
const union line64 line = flight_tree_key(&it);
const struct flight *flight = flight_tree_value(&it);
char datestr[12];
date32_to_string(line.date, &datestr);
const struct kvpair *src = source_lookup(scan, line);
assert(src);
if(!src->key.a) { fprintf(stderr,
"Source has no source at %s; ignoring.\n", datestr); continue; }
printf("%s, ", datestr);
printf("%.*s, %" PRIu32 ", %" PRIu32,
(int)(flight->reg.b - flight->reg.a), flight->reg.a,
flight->dual_min, flight->pilot_min);
printf(", %.*s\n", (int)(src->key.b - src->key.a), src->key.a);
}
printf("EOD\n"
"# theozh https://stackoverflow.com/a/75466214/2472827\n"
"# get a unique list from datablock\n"
"addToList(list,col) = list.( strstrt(list,'\"'.strcol(col).'\"') \\\n"
" > 0 ? '' : ' \"'.strcol(col).'\"')\n"
"Uniqs = ''\n"
"stats $Data u (Uniqs=addToList(Uniqs,2)) nooutput\n"
"Uniq(i) = word(Uniqs,i)\n"
"getIndex(s) = sum [_i=1:words(Uniqs)] s eq word(Uniqs,_i) ? _i : 0\n"
"\n"
"stats $Data u 3 nooutput\n"
"sicsum = STATS_sum\n"
"stats $Data u 4 nooutput\n"
"picsum = STATS_sum\n"
"\n"
"myTimeFmt = \"%%Y-%%m-%%d\"\n"
"set format x myTimeFmt timedate\n"
"set xtics format myTimeFmt rotate by -30\n"
"set format y \"%%tH:%%tM\" timedate\n"
"set grid\n"
"set key out reverse Left noautotitle\n"
"set style fill solid 0.5\n"
"unset border\n"
"plot total=0 $Data u"
" (timecolumn(1,myTimeFmt)):(dy=($3+$4)*60,total=total+dy)"
" w steps lc \"black\" dt 3, \\\n"
" total=0 '' u (timecolumn(1,myTimeFmt)):"
"(dy=($3+$4)*60,total=total+dy,total/2.): \\\n"
" (43200):(total/2.):(getIndex(strcol(2))) w boxxy lc var, \\\n"
" for [i=1:words(Uniqs)] keyentry w boxxy lc i ti Uniq(i)\n");
fprintf(stderr, "\n");
}
void scan_kjv_graph(struct scan *const scan) {
struct kjvcount_table count = {0};
struct kjvset_table set = kjv_set();
size_t no_total;
const char *reason = 0;
count = kjv_count(&no_total);
fprintf(stderr, "*** KJV graph, count: %s; lines: %s.\n",
kjv_count_to_string(&count), kjv_tree_to_string(&scan->kjvs));
if(!no_total) { reason = "kjv failed to load"; goto catch; }
struct kjv_tree_iterator it = kjv_tree_iterator(&scan->kjvs);
/* https://stackoverflow.com/a/12601553 */
printf("set terminal pngcairo dashed transparent truecolor"
" size 840, 480 fontscale 1\n"
"set output \"kjv.png\"\n");
printf("$Data <<EOD\n"
"# date, verse, words, set / %zu, source\n", no_total);
while(kjv_tree_next(&it)) {
char citestr[12], datestr[12];
const union line64 line = kjv_tree_key(&it);
const struct kjvrange *const range = kjv_tree_value(&it);
size_t words = 0, newwords = 0;
const struct kvpair *src = source_lookup(scan, line);
if(!src || !src->key.a) { errno = EDOM; goto catch; }
date32_to_string(line.date, &datestr); /* Date. */
kjvcite_to_string(range->start, &citestr); /* KJV cite. */
for(union kjvcite c = range->start; ; c.verse++) {
size_t w = kjv_count_get(&count, c);
words += w;
switch (kjv_set_add(&set, c)) {
case TABLE_ERROR: goto catch;
case TABLE_ABSENT: newwords += w;
case TABLE_PRESENT: break;
} /* while(); */
if(!range->verse_end || range->verse_end <= c.verse) break;
}
printf("%s, %s", datestr, citestr);
if(range->verse_end) printf("-%" PRIu32, range->verse_end);
printf(", %zu, %zu, %.*s\n",
words, newwords, (int)(src->key.b - src->key.a), src->key.a);
}
printf("EOD\n"
"# theozh https://stackoverflow.com/a/75466214/2472827\n"
"# get a unique list from datablock\n"
"addToList(list,col) = list.( strstrt(list,'\"'.strcol(col).'\"') \\\n"
" > 0 ? '' : ' \"'.strcol(col).'\"')\n"
"Uniqs = ''\n"
"stats $Data u (Uniqs=addToList(Uniqs,5)) nooutput\n"
"Uniq(i) = word(Uniqs,i)\n"
"getIndex(s) = sum [_i=1:words(Uniqs)] s eq word(Uniqs,_i) ? _i : 0\n"
"\n"
"stats $Data u 3 nooutput\n"
"cumsum = STATS_sum\n"
"stats $Data u 4 nooutput\n"
"setsum = STATS_sum\n"
"\n"
"myTimeFmt = \"%%Y-%%m-%%d\"\n"
"set format x myTimeFmt timedate\n"
"set xtics format myTimeFmt rotate by -30\n"
"set format y \"%%g%%%%\"\n"
"set grid\n"
"set key out reverse Left noautotitle\n"
"set style fill solid 0.5\n"
"unset border\n"
"set autoscale xfix # max? hack: can't get x to extend further\n"
"\n"
"set label sprintf(\"%%u cumulative words (duplicate verses counted)\","
" cumsum) center at graph 0.5, first cumsum*100/%zu offset 0,0.5\n"
"set label sprintf(\"%%u unique KJV verse words memorized\", setsum) "
"center at graph 0.5, first setsum*100/%zu offset 0,0.5\n"
"\n"
"plot \\\n"
" cumsum*100/%zu w l lc \"grey\" dt 2 lw 1, \\\n"
" total=0 $Data u"
" (timecolumn(1,myTimeFmt)):(dy=$3*100/%zu,total=total+dy) \\\n"
" w steps lc \"grey\" dt 2 lw 1, \\\n" /* `pngcairo` has trouble :[. */
" total=0 $Data u"
" (timecolumn(1,myTimeFmt)):(dy=$4*100/%zu,total=total+dy) \\\n"
" w steps lc \"black\" dt 1 lw 1, \\\n"
" setsum*100/%zu w l lc \"black\" dt 1 lw 1, \\\n"
" total=0 '' u \\\n"
" (timecolumn(1,myTimeFmt)): \\\n"
" (dy=$4*100/%zu,total=total+dy,total/2.): \\\n"
" (43200): \\\n"
" (total/2.): \\\n"
" (getIndex(strcol(5))) w boxxy lc var lw 1, \\\n"
" for [i=1:words(Uniqs)] keyentry w boxxy lc i ti Uniq(i)\n",
no_total, no_total, no_total, no_total, no_total, no_total, no_total);
goto finally;
catch:
perror(reason);
finally:
kjv_count_(&count);
fprintf(stderr, "\n");
}
void scan_dream_graph(struct scan *const scan) {
assert(scan);
fprintf(stderr, "*** Dream graph: %s.\n",
linepair_tree_to_string(&scan->dreams));
struct linepair_tree_iterator it = linepair_tree_iterator(&scan->dreams);
/* https://stackoverflow.com/a/12601553 */
printf("set terminal pngcairo dashed transparent truecolor"
" size 840, 480 fontscale 1\n"
"set output \"dream.png\"\n"
"$Data <<EOD\n"
"# date\n");
struct { union date32 date; size_t count; } accum = { {{0}}, 0 };
while(linepair_tree_next(&it)) {
/* Bin by month. */
const union date32
now = { .u32 = linepair_tree_key(&it).date.u32 & ~31u };
/* Next month; output the last. */
if(accum.date.u32 < now.u32) {
if(accum.count) {
printf("%" PRIu32 "-%.2" PRIu32 ", %zu\n",
accum.date.year, accum.date.month, accum.count);
/* If we skip a month, output zero as dummy. */
const union date32 next = date32_next_month(accum.date);
if(next.u32 < now.u32) {
printf("%" PRIu32 "-%.2" PRIu32 ", 0\n",
next.year, next.month);
/* Skip more than a month, place dummy output on both. */
const union date32 last = date32_last_month(now);
if(next.u32 < last.u32)
printf("%" PRIu32 "-%.2" PRIu32 ", 0\n",
last.year, last.month);
}
}
accum.date = now;
accum.count = 1;
} else {
accum.count++;
}
}
/* Force. */
if(accum.count) printf("%" PRIu32 "-%.2" PRIu32 ", %zu\n",
accum.date.year, accum.date.month, accum.count);
printf("EOD\n"
"\n"
"set xdata time\n"
"set timefmt \"%%Y-%%m\"\n"
"set format x \"%%Y-%%m\"\n"
"set style fill transparent solid 0.3\n"
"set ylabel \"remembered dreams binned by month\"\n"
"\n"
"plot $Data using 1:2 with boxes notitle lc rgb \"blue\"\n");
fprintf(stderr, "\n");
}