2023-02-02 21:53:59 -05:00
|
|
|
/** @license 2023 Neil Edelman, distributed under the terms of the
|
|
|
|
[MIT License](https://opensource.org/licenses/MIT).
|
|
|
|
@std C11 */
|
|
|
|
#define BASE
|
|
|
|
#include "../src/source.h" /* base */
|
|
|
|
#include "../src/journal.h"
|
|
|
|
#include <stdio.h>
|
2023-02-03 00:31:24 -05:00
|
|
|
#include <string.h>
|
2023-02-02 21:53:59 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2023-02-03 03:05:20 -05:00
|
|
|
|
2023-02-04 01:54:49 -05:00
|
|
|
/* `sourcelist` is an array of all the sources. */
|
2023-02-04 01:00:55 -05:00
|
|
|
static void sourcelist_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 sourcelist
|
|
|
|
#define ARRAY_TYPE struct source
|
|
|
|
#define ARRAY_TO_STRING
|
|
|
|
#include "../src/array.h"
|
|
|
|
|
|
|
|
|
2023-02-04 01:54:49 -05:00
|
|
|
/* `sourcemap` maps from substring keywords to indices in `sourcelist`. */
|
|
|
|
static void sourcemap_to_string(const struct pair key, const size_t i,
|
|
|
|
char (*const a)[12]) { (void)key; sprintf(*a, "%zu", i); }
|
|
|
|
static int sourcemap_is_equal(const struct pair a, const struct pair b)
|
2023-02-04 01:00:55 -05:00
|
|
|
{ return pair_is_equal(a, b); }
|
2023-02-04 01:54:49 -05:00
|
|
|
static uint32_t sourcemap_hash(const struct pair p) { return pair_djb2(p); }
|
|
|
|
#define TABLE_NAME sourcemap
|
2023-02-04 01:00:55 -05:00
|
|
|
#define TABLE_KEY struct pair
|
2023-02-03 00:31:24 -05:00
|
|
|
#define TABLE_UINT uint32_t
|
2023-02-04 01:54:49 -05:00
|
|
|
#define TABLE_VALUE size_t /* Index into source list. */
|
2023-02-04 01:00:55 -05:00
|
|
|
#define TABLE_DEFAULT 0
|
2023-02-04 01:54:49 -05:00
|
|
|
#define TABLE_TO_STRING
|
2023-02-03 00:31:24 -05:00
|
|
|
#include "../src/table.h"
|
2023-02-02 21:53:59 -05:00
|
|
|
|
|
|
|
|
2023-02-04 01:54:49 -05:00
|
|
|
/* `source` is a tree mapping from date-line to indices in `sourcelist`. */
|
2023-02-04 01:00:55 -05:00
|
|
|
static void source_to_string(const union line64 line, const size_t *const u,
|
2023-02-02 21:53:59 -05:00
|
|
|
char (*const a)[12]) { (void)u; date32_to_string(line.date, a); }
|
|
|
|
static int source_compare(const union line64 a, const union line64 b)
|
|
|
|
{ return a.u64 > b.u64; }
|
|
|
|
#define TREE_NAME source
|
|
|
|
#define TREE_KEY union line64
|
2023-02-04 01:54:49 -05:00
|
|
|
#define TREE_VALUE size_t /* Index into source list. */
|
2023-02-02 21:53:59 -05:00
|
|
|
#define TREE_COMPARE
|
|
|
|
#define TREE_TO_STRING
|
2023-02-04 22:06:43 -05:00
|
|
|
#define TREE_DEFAULT 0
|
2023-02-02 21:53:59 -05:00
|
|
|
#include "../src/tree.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define PROTO
|
|
|
|
#include "../src/source.h" /* proto */
|
|
|
|
|
|
|
|
|
|
|
|
/*!conditions:re2c*/
|
|
|
|
|
|
|
|
static int scan(union date32 date, const char *const buffer,
|
|
|
|
struct sources *const s) {
|
|
|
|
const char *YYCURSOR, *YYMARKER, *yyt1, *yyt2, *s0, *s1, *t0, *t1;
|
|
|
|
enum YYCONDTYPE condition = yycline;
|
|
|
|
size_t line = 1;
|
|
|
|
char datestr[12] = {0};
|
|
|
|
const char *why = "unexpected";
|
|
|
|
assert(buffer && s);
|
|
|
|
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);
|
|
|
|
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> "--" / [^-] :=> source
|
|
|
|
|
|
|
|
<source> * { why = "default source unrecognized"; goto catch; }
|
2023-02-04 01:00:55 -05:00
|
|
|
<source> @s0 keyword @s1 / "\n" => skip { also_add_to_tree: {
|
2023-02-04 22:06:43 -05:00
|
|
|
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 = sourcemap_table_get(&s->map, keyword)))
|
2023-02-04 01:00:55 -05:00
|
|
|
{ why = "keyword not introduced"; goto catch; }
|
2023-02-04 22:06:43 -05:00
|
|
|
switch(source_tree_try(&s->dates, key, &pi)) {
|
|
|
|
case TREE_PRESENT: why = "duplicate key"; /* _Sic_. */
|
|
|
|
case TREE_ERROR: goto catch;
|
|
|
|
case TREE_ABSENT: *pi = i; break;
|
|
|
|
}
|
2023-02-04 01:54:49 -05:00
|
|
|
date32_to_string(date, &datestr);
|
|
|
|
printf("%s: <%.*s>\n", datestr, (int)(s1 - s0), s0);
|
2023-02-02 21:53:59 -05:00
|
|
|
continue;
|
2023-02-04 01:00:55 -05:00
|
|
|
} }
|
2023-02-02 21:53:59 -05:00
|
|
|
/* This is lazy and will pickup trailing spaces. */
|
|
|
|
<source> @s0 keyword @s1 ":" [^\x00\n]+ / "\n" => skip {
|
2023-02-04 01:00:55 -05:00
|
|
|
struct pair keyword = pair(s0, s1);
|
|
|
|
size_t *idx;
|
|
|
|
struct source *source;
|
2023-02-04 01:54:49 -05:00
|
|
|
switch(sourcemap_table_assign(&s->map, keyword, &idx)) {
|
2023-02-04 01:00:55 -05:00
|
|
|
case TABLE_PRESENT: errno = EDOM; why = "new keyword already used";
|
|
|
|
case TABLE_ERROR: goto catch; /* /\ _Sic_. */
|
2023-02-04 22:06:43 -05:00
|
|
|
case TABLE_ABSENT: *idx = 0; break; /* Good. */
|
2023-02-04 01:00:55 -05:00
|
|
|
}
|
|
|
|
if(!(source = sourcelist_array_new(&s->list))) goto catch;
|
|
|
|
*idx = (size_t)(source - s->list.data);
|
|
|
|
source->name.a = s0, source->name.b = s1;
|
|
|
|
source->desc.a = 0, source->desc.b = 0;
|
|
|
|
fprintf(stderr, "New keyword <%.*s> stored in list at %zu.\n",
|
|
|
|
(int)(s1 - s0), s0, *idx);
|
|
|
|
goto also_add_to_tree;
|
2023-02-02 21:53:59 -05:00
|
|
|
}
|
|
|
|
*/ }
|
|
|
|
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;
|
|
|
|
}
|
2023-02-04 01:54:49 -05:00
|
|
|
/** Dynamic memory allocation for `s` will be zero. */
|
2023-02-02 21:53:59 -05:00
|
|
|
void sources_(struct sources *const s) {
|
|
|
|
if(!s) return;
|
2023-02-04 01:00:55 -05:00
|
|
|
source_tree_(&s->dates);
|
2023-02-04 01:54:49 -05:00
|
|
|
sourcemap_table_(&s->map);
|
2023-02-04 01:00:55 -05:00
|
|
|
sourcelist_array_(&s->list);
|
2023-02-02 21:53:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct sources sources(struct journal *const j) {
|
2023-02-04 01:00:55 -05:00
|
|
|
struct sources s
|
2023-02-04 01:54:49 -05:00
|
|
|
= { sourcelist_array(), sourcemap_table(), source_tree() };
|
2023-02-02 21:53:59 -05:00
|
|
|
struct journal_iterator it;
|
|
|
|
union date32 k;
|
|
|
|
const char *v;
|
|
|
|
assert(j);
|
2023-02-04 01:00:55 -05:00
|
|
|
{ /* Null is the first item for convenience, (TABLE_DEFAULT). */
|
|
|
|
struct source *nul;
|
|
|
|
if(!(nul = sourcelist_array_new(&s.list))) goto catch;
|
|
|
|
nul->name.a = nul->name.b = nul->desc.a = nul->desc.b = 0;
|
|
|
|
}
|
2023-02-02 21:53:59 -05:00
|
|
|
it = journal_begin(j);
|
|
|
|
while(journal_next(&it, &k, &v)) if(!scan(k, v, &s)) goto catch;
|
2023-02-04 01:54:49 -05:00
|
|
|
fprintf(stderr, "List of sources: %s.\n"
|
|
|
|
"Mapped to indices: %s.\n"
|
|
|
|
"Date-line tree: %s.\n", sourcelist_array_to_string(&s.list),
|
|
|
|
sourcemap_table_to_string(&s.map), source_tree_to_string(&s.dates));
|
2023-02-02 21:53:59 -05:00
|
|
|
goto finally;
|
|
|
|
catch:
|
|
|
|
sources_(&s);
|
|
|
|
finally:
|
|
|
|
return s;
|
|
|
|
}
|
2023-02-04 22:06:43 -05:00
|
|
|
|
|
|
|
/** Lookup the last source in `range` in sources `s`. They are invalidated on
|
|
|
|
adding a source, (probably fine.) */
|
|
|
|
const struct source *source_lookup(const struct sources *const s,
|
|
|
|
const union line64 range) {
|
|
|
|
struct source *looked;
|
|
|
|
assert(s);
|
|
|
|
looked = s->list.data + source_tree_left(&s->dates, range);
|
|
|
|
/* verify ... */
|
|
|
|
return looked;
|
|
|
|
}
|