interpret/src/source.re.c

155 lines
4.6 KiB
C
Raw Normal View History

2023-02-03 02:53:59 +00: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 05:31:24 +00:00
#include <string.h>
2023-02-03 02:53:59 +00:00
#include <stdlib.h>
#include <assert.h>
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"
static int sourcelook_is_equal(const struct pair a, const struct pair b)
{ return pair_is_equal(a, b); }
static uint32_t sourcelook_hash(const struct pair p) { return pair_djb2(p); }
#define TABLE_NAME sourcelook
#define TABLE_KEY struct pair
2023-02-03 05:31:24 +00:00
#define TABLE_UINT uint32_t
#define TABLE_VALUE size_t /* Index into `lookup_sources`. */
#define TABLE_DEFAULT 0
2023-02-03 05:31:24 +00:00
#include "../src/table.h"
2023-02-03 02:53:59 +00:00
static void source_to_string(const union line64 line, const size_t *const u,
2023-02-03 02:53:59 +00: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
#define TREE_VALUE size_t /* Index into `lookup_sources`. */
2023-02-03 02:53:59 +00:00
#define TREE_COMPARE
#define TREE_TO_STRING
#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; }
<source> @s0 keyword @s1 / "\n" => skip { also_add_to_tree: {
2023-02-03 19:50:10 +00:00
struct pair p = pair(s0, s1);
size_t i;
if(!(i = sourcelook_table_get(&s->look, p)))
{ why = "keyword not introduced"; goto catch; }
2023-02-03 02:53:59 +00:00
printf("extracted <%.*s>\n", (int)(s1 - s0), s0);
continue;
} }
2023-02-03 02:53:59 +00:00
/* 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 source *source;
switch(sourcelook_table_assign(&s->look, keyword, &idx)) {
case TABLE_PRESENT: errno = EDOM; why = "new keyword already used";
case TABLE_ERROR: goto catch; /* /\ _Sic_. */
case TABLE_ABSENT: break; /* Good. */
}
*idx = 0; /* Clean. */
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-03 02:53:59 +00: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;
}
/** Dynamic memory allocation for `s` will be zero, <fn:sources_is_valid> will
be false. */
void sources_(struct sources *const s) {
if(!s) return;
source_tree_(&s->dates);
sourcelook_table_(&s->look);
sourcelist_array_(&s->list);
2023-02-03 02:53:59 +00:00
}
struct sources sources(struct journal *const j) {
struct sources s
= { sourcelist_array(), sourcelook_table(), source_tree() };
2023-02-03 02:53:59 +00:00
struct journal_iterator it;
union date32 k;
const char *v;
assert(j);
{ /* 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-03 02:53:59 +00:00
it = journal_begin(j);
while(journal_next(&it, &k, &v)) if(!scan(k, v, &s)) goto catch;
printf("%s\n", sourcelist_array_to_string(&s.list));
2023-02-03 02:53:59 +00:00
goto finally;
catch:
sources_(&s);
finally:
return s;
}