2022-12-29 07:54:59 +00:00
|
|
|
/** @license 2022 Neil Edelman, distributed under the terms of the
|
|
|
|
[MIT License](https://opensource.org/licenses/MIT).
|
|
|
|
|
|
|
|
Scan journal entries for kjv references. */
|
|
|
|
|
|
|
|
#include "../src/journal.h"
|
|
|
|
#include "../src/kjv.h"
|
2023-02-03 19:50:10 +00:00
|
|
|
#include "../src/pair.h"
|
2022-12-29 22:01:10 +00:00
|
|
|
#include <inttypes.h> /* C99 */
|
2022-12-29 07:54:59 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2023-02-03 19:50:10 +00:00
|
|
|
#include <errno.h>
|
2022-12-29 07:54:59 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
/*!conditions:re2c*/
|
|
|
|
|
2022-12-29 08:51:45 +00:00
|
|
|
static int scan(union date32 date, const char *const buffer,
|
|
|
|
struct kjv *const kj) {
|
2022-12-30 05:32:40 +00:00
|
|
|
const char *YYCURSOR, *YYMARKER, *yyt1, *yyt2, *yyt3, *s0, *s1, *t0, *t1;
|
2022-12-29 07:54:59 +00:00
|
|
|
enum kjv_book book = Revelation;
|
2022-12-29 22:01:10 +00:00
|
|
|
uint32_t chapter = 0, verse = 0, verse_end = 0;
|
2022-12-29 07:54:59 +00:00
|
|
|
enum YYCONDTYPE condition = yycline;
|
|
|
|
size_t line = 1;
|
2022-12-29 18:10:41 +00:00
|
|
|
char datestr[12] = {0};
|
2022-12-30 05:32:40 +00:00
|
|
|
const char *why = "unexpected";
|
2022-12-29 08:51:45 +00:00
|
|
|
assert(buffer && kj);
|
2022-12-30 05:32:40 +00:00
|
|
|
YYCURSOR = YYMARKER = yyt1 = buffer;
|
2022-12-29 07:54:59 +00:00
|
|
|
/*!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;
|
|
|
|
|
2023-01-25 06:15:04 +00:00
|
|
|
unix_control = [\x01-\x08\x0b-\x1f\x7f];
|
2022-12-29 07:54:59 +00:00
|
|
|
ws = [ \t];
|
|
|
|
glyph = [^] \ ("\x00" | "\n" | unix_control | ws);
|
|
|
|
natural = [1-9][0-9]*;
|
2022-12-29 22:01:10 +00:00
|
|
|
engage = ws+ "--" ws+;
|
2022-12-30 05:32:40 +00:00
|
|
|
/* (natural ":")? Don't use for memorizing and use for reading, I think? */
|
|
|
|
/*("``"|"\"") This is not in the next book. */
|
|
|
|
lookat = ws* natural ":" natural [ab]? ("-" natural [ab]?)? engage;
|
2022-12-29 22:40:43 +00:00
|
|
|
first = ("I" | "1") " "?;
|
|
|
|
second = ("II" | "2") " "?;
|
|
|
|
third = ("III" | "3") " "?;
|
2022-12-29 07:54:59 +00:00
|
|
|
*/
|
|
|
|
for( ; ; ) { /*!re2c /**/
|
2023-02-01 08:58:51 +00:00
|
|
|
/* Default ignore. */
|
2022-12-30 05:32:40 +00:00
|
|
|
<skip> [^\n\x00] { continue; }
|
|
|
|
<skip> "\x00" { why = "no newline at end of file"; goto catch; }
|
2022-12-29 18:10:41 +00:00
|
|
|
<line> "\x00" { return 1; }
|
2023-02-01 08:58:51 +00:00
|
|
|
<line, skip> "\n" => line { line++; continue; }
|
|
|
|
<line> * :=> skip
|
|
|
|
|
|
|
|
/* Books. */
|
2022-12-29 07:54:59 +00:00
|
|
|
<line> "Genesis" / lookat => book { book = Genesis; continue; }
|
|
|
|
<line> "Exodus" / lookat => book { book = Exodus; continue; }
|
2022-12-29 22:40:43 +00:00
|
|
|
<line> "Leviticus" / lookat => book { book = Leviticus; continue; }
|
|
|
|
<line> "Numbers" / lookat => book { book = Numbers; continue; }
|
|
|
|
<line> "Deuteronomy" / lookat => book { book = Deuteronomy; continue; }
|
|
|
|
<line> "Joshua" / lookat => book { book = Joshua; continue; }
|
|
|
|
<line> "Judges" / lookat => book { book = Judges; continue; }
|
|
|
|
<line> "Ruth" / lookat => book { book = Ruth; continue; }
|
|
|
|
<line> first "Samuel" / lookat => book { book = ISamuel; continue; }
|
|
|
|
<line> second "Samuel" / lookat => book { book = IISamuel; continue; }
|
|
|
|
<line> first "Kings" / lookat => book { book = IKings; continue; }
|
|
|
|
<line> second "Kings" / lookat => book { book = IIKings; continue; }
|
|
|
|
<line> first "Chronicles" / lookat
|
|
|
|
=> book { book = IChronicles; continue; }
|
|
|
|
<line> second "Chronicles" / lookat
|
|
|
|
=> book { book = IIChronicles; continue; }
|
|
|
|
<line> "Ezra" / lookat => book { book = Ezra; continue; }
|
|
|
|
<line> "Nehemiah" / lookat => book { book = Nehemiah; continue; }
|
|
|
|
<line> "Esther" / lookat => book { book = Esther; continue; }
|
|
|
|
<line> "Job" / lookat => book { book = Job; continue; }
|
|
|
|
<line> "Psalms" / lookat => book { book = Psalms; continue; }
|
|
|
|
<line> "Proverbs" / lookat => book { book = Proverbs; continue; }
|
|
|
|
<line> "Ecclesiastes" / lookat
|
|
|
|
=> book { book = Ecclesiastes; continue; }
|
|
|
|
<line> "Song of Solomon" / lookat
|
|
|
|
=> book { book = Song_of_Solomon; continue; }
|
|
|
|
<line> "Isaiah" / lookat => book { book = Isaiah; continue; }
|
|
|
|
<line> "Jeremiah" / lookat => book { book = Jeremiah; continue; }
|
|
|
|
<line> "Lamentations" / lookat
|
|
|
|
=> book { book = Lamentations; continue; }
|
|
|
|
<line> "Ezekiel" / lookat => book { book = Ezekiel; continue; }
|
|
|
|
<line> "Daniel" / lookat => book { book = Daniel; continue; }
|
|
|
|
<line> "Hosea" / lookat => book { book = Hosea; continue; }
|
|
|
|
<line> "Joel" / lookat => book { book = Joel; continue; }
|
|
|
|
<line> "Amos" / lookat => book { book = Amos; continue; }
|
|
|
|
<line> "Obadiah" / lookat => book { book = Obadiah; continue; }
|
|
|
|
<line> "Jonah" / lookat => book { book = Jonah; continue; }
|
|
|
|
<line> "Micah" / lookat => book { book = Micah; continue; }
|
|
|
|
<line> "Nahum" / lookat => book { book = Nahum; continue; }
|
|
|
|
<line> "Habakkuk" / lookat => book { book = Habakkuk; continue; }
|
|
|
|
<line> "Zephaniah" / lookat => book { book = Zephaniah; continue; }
|
|
|
|
<line> "Haggai" / lookat => book { book = Haggai; continue; }
|
|
|
|
<line> "Zechariah" / lookat => book { book = Zechariah; continue; }
|
|
|
|
<line> "Malachi" / lookat => book { book = Malachi; continue; }
|
|
|
|
<line> "Matthew" / lookat => book { book = Matthew; continue; }
|
|
|
|
<line> "Mark" / lookat => book { book = Mark; continue; }
|
|
|
|
<line> "Luke" / lookat => book { book = Luke; continue; }
|
|
|
|
<line> "John" / lookat => book { book = John; continue; }
|
|
|
|
<line> "Acts" / lookat => book { book = Acts; continue; }
|
2022-12-29 08:51:45 +00:00
|
|
|
<line> "Romans" / lookat => book { book = Romans; continue; }
|
2022-12-29 22:40:43 +00:00
|
|
|
<line> first "Corinthians" / lookat
|
|
|
|
=> book { book = ICorinthians; continue; }
|
|
|
|
<line> second "Corinthians" / lookat
|
|
|
|
=> book { book = IICorinthians; continue; }
|
|
|
|
<line> "Galatians" / lookat => book { book = Galatians; continue; }
|
|
|
|
<line> "Ephesians" / lookat => book { book = Ephesians; continue; }
|
|
|
|
<line> "Philippians" / lookat => book { book = Philippians; continue; }
|
|
|
|
<line> "Colossians" / lookat => book { book = Colossians; continue; }
|
|
|
|
<line> first "Thessalonians" / lookat
|
|
|
|
=> book { book = IThessalonians; continue; }
|
|
|
|
<line> second "Thessalonians" / lookat
|
|
|
|
=> book { book = IIThessalonians; continue; }
|
|
|
|
<line> first "Timothy" / lookat => book { book = ITimothy; continue; }
|
|
|
|
<line> second "Timothy" / lookat => book { book = IITimothy; continue; }
|
|
|
|
<line> "Titus" / lookat => book { book = Titus; continue; }
|
|
|
|
<line> "Philemon" / lookat => book { book = Philemon; continue; }
|
|
|
|
<line> "Hebrews" / lookat => book { book = Hebrews; continue; }
|
|
|
|
<line> "James" / lookat => book { book = James; continue; }
|
|
|
|
<line> first "Peter" / lookat => book { book = IPeter; continue; }
|
|
|
|
<line> second "Peter" / lookat => book { book = IIPeter; continue; }
|
|
|
|
<line> first "John" / lookat => book { book = IJohn; continue; }
|
|
|
|
<line> second "John" / lookat => book { book = IIJohn; continue; }
|
|
|
|
<line> third "John" / lookat => book { book = IIIJohn; continue; }
|
|
|
|
<line> "Jude" / lookat => book { book = Jude; continue; }
|
|
|
|
<line> "Revelation" / lookat => book { book = Revelation; continue; }
|
2023-02-01 08:58:51 +00:00
|
|
|
|
|
|
|
/* Extract further information. */
|
2022-12-30 05:32:40 +00:00
|
|
|
<book> * { why = "default unrecognized"; goto catch; }
|
|
|
|
/* 19:15a, just ignore the a. */
|
2022-12-29 22:01:10 +00:00
|
|
|
<book> ws+ @s0 natural @s1 ":" @t0 natural @t1 [ab]? {
|
2022-12-30 05:32:40 +00:00
|
|
|
if(chapter || verse || verse_end)
|
|
|
|
{ why = "reference unrecognized"; goto catch; }
|
2023-02-03 19:50:10 +00:00
|
|
|
if(!pair_to_natural(s0, s1, &chapter)
|
|
|
|
|| !pair_to_natural(t0, t1, &verse))
|
2022-12-30 05:32:40 +00:00
|
|
|
{ why = "reference numerical error"; goto catch; }
|
2022-12-29 22:01:10 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
<book> "-" @s0 natural @s1 [ab]? { /* Verse range. */
|
2022-12-30 05:32:40 +00:00
|
|
|
if(!chapter || !verse || verse_end)
|
|
|
|
{ why = "range unrecognized"; goto catch; }
|
2023-02-03 19:50:10 +00:00
|
|
|
if(!pair_to_natural(s0, s1, &verse_end))
|
2022-12-30 05:32:40 +00:00
|
|
|
{ why = "range numerical error"; goto catch; }
|
2022-12-29 22:01:10 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
<book> engage => skip {
|
2022-12-29 18:10:41 +00:00
|
|
|
char citestr[12];
|
2022-12-30 05:32:40 +00:00
|
|
|
if(!chapter || !verse) { why = "missing information"; goto catch; }
|
|
|
|
if(verse_end && verse_end <= verse)
|
|
|
|
{ why = "interval error"; goto catch; }
|
2022-12-29 22:01:10 +00:00
|
|
|
union kjvcite cite
|
|
|
|
= { .book = book, .chapter = chapter, .verse = verse };
|
|
|
|
if(!datestr[0]) date32_to_string(date, &datestr); /* Only once. */
|
2022-12-29 18:10:41 +00:00
|
|
|
kjvcite_to_string(cite, &citestr);
|
2022-12-29 22:01:10 +00:00
|
|
|
for( ; ; verse++, cite.verse++) {
|
2022-12-30 05:32:40 +00:00
|
|
|
if(!kjv_add(kj, cite)) { why = "add to set"; goto catch; }
|
2022-12-29 22:01:10 +00:00
|
|
|
if(!verse_end || verse_end <= verse) break;
|
|
|
|
}
|
2022-12-30 07:38:58 +00:00
|
|
|
printf("%s\t%zu\t%zu\t%zu\t# ",
|
|
|
|
datestr, kj->words.verse, kj->words.set, kj->words.cumulative);
|
2022-12-29 22:01:10 +00:00
|
|
|
if(verse_end) {
|
2022-12-30 07:38:58 +00:00
|
|
|
printf("%s-%" PRIu32 "\n", citestr, verse_end);
|
2022-12-29 22:01:10 +00:00
|
|
|
} else {
|
2022-12-30 07:38:58 +00:00
|
|
|
printf("%s\n", citestr);
|
2022-12-29 22:01:10 +00:00
|
|
|
}
|
|
|
|
book = Revelation, chapter = 0, verse = 0, verse_end = 0;
|
2022-12-29 07:54:59 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*/ }
|
2022-12-30 05:32:40 +00:00
|
|
|
assert(0); /* Never gets here. */
|
2022-12-29 18:10:41 +00:00
|
|
|
catch:
|
|
|
|
if(!errno) errno = EILSEQ;
|
2022-12-30 05:32:40 +00:00
|
|
|
date32_to_string(date, &datestr);
|
|
|
|
fprintf(stderr, "%s\n"
|
|
|
|
"%s line %zu: %s.\n", buffer, datestr, line, why);
|
2022-12-29 07:54:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void) {
|
|
|
|
int success = EXIT_SUCCESS;
|
2023-02-17 22:06:21 +00:00
|
|
|
errno = 0;
|
2022-12-29 18:10:41 +00:00
|
|
|
struct journal j;
|
2022-12-29 07:54:59 +00:00
|
|
|
struct journal_iterator it;
|
2023-03-11 04:06:11 +00:00
|
|
|
printf("1. kjv\n");
|
2023-02-02 07:18:20 +00:00
|
|
|
struct kjv bible = kjv();
|
2023-03-11 04:06:11 +00:00
|
|
|
if(errno) { printf("kjv?\n"); goto catch; }
|
2022-12-29 07:54:59 +00:00
|
|
|
union date32 k;
|
2023-02-02 07:18:20 +00:00
|
|
|
const char *v;
|
2023-03-11 04:06:11 +00:00
|
|
|
printf("2. journal\n");
|
2022-12-29 18:10:41 +00:00
|
|
|
j = journal();
|
2023-02-17 22:06:21 +00:00
|
|
|
if(errno) goto catch;
|
2022-12-29 18:10:41 +00:00
|
|
|
fprintf(stderr, "Journal: %s.\n", journal_to_string(&j));
|
2022-12-29 22:01:10 +00:00
|
|
|
printf("set term postscript eps enhanced\n"
|
|
|
|
"set output \"kjv.eps\"\n"
|
|
|
|
"$Data <<EOD\n"
|
2023-02-02 07:18:20 +00:00
|
|
|
"# date\tverse\tset\tcumulative / %zu\n", bible.words.total);
|
2023-02-12 05:21:29 +00:00
|
|
|
it = journal_iterator(&j);
|
2023-02-02 07:18:20 +00:00
|
|
|
while(journal_next(&it, &k, &v)) if(!scan(k, v, &bible)) goto catch;
|
2022-12-29 22:01:10 +00:00
|
|
|
printf("EOD\n"
|
|
|
|
"set monochrome\n"
|
|
|
|
"set xdata time\n"
|
|
|
|
"set timefmt \"%%Y-%%m-%%d\"\n"
|
|
|
|
"set xtics format \"%%Y-%%m-%%d\" rotate by -30\n"
|
2022-12-31 03:37:53 +00:00
|
|
|
"set ylabel \"words in KJV\"\n"
|
2022-12-29 22:01:10 +00:00
|
|
|
"set format y \"%%g%%%%\"\n"
|
2022-12-30 07:38:58 +00:00
|
|
|
"set key top left\n"
|
2022-12-29 22:01:10 +00:00
|
|
|
"set grid\n"
|
|
|
|
"unset border\n"
|
2022-12-30 23:01:32 +00:00
|
|
|
"#set style fill solid 0.1 #pattern 5 (better, but restarts)\n"
|
|
|
|
"plot $Data using 1:($3)*100/%zu with fillsteps lw 2 title \"set\", \\\n"
|
2022-12-30 07:38:58 +00:00
|
|
|
"$Data using 1:($4)*100/%zu with steps lw 1 title \"cumulative\"\n",
|
2023-02-02 07:18:20 +00:00
|
|
|
bible.words.total, bible.words.total);
|
2022-12-29 07:54:59 +00:00
|
|
|
goto finally;
|
|
|
|
catch:
|
|
|
|
success = EXIT_FAILURE;
|
|
|
|
perror("journal");
|
|
|
|
finally:
|
2023-03-11 04:06:11 +00:00
|
|
|
kjv_(&bible);
|
2022-12-29 07:54:59 +00:00
|
|
|
journal_(&j);
|
|
|
|
return success;
|
|
|
|
}
|