interpret/src/scan_kjv.re.c

101 lines
3.4 KiB
C
Raw Normal View History

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"
#include "../src/helper.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <limits.h>
/*!conditions:re2c*/
static int scan(union date32 date, const char *const buffer) {
const char *YYCURSOR, *YYMARKER, *yyt1, *yyt2, *yyt3, *s0, *s1, *t0, *t1;
enum kjv_book book = Revelation;
uint32_t chapter, verse;
enum YYCONDTYPE condition = yycline;
size_t line = 1;
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\x0a-\x1f\x7f];
ws = [ \t];
glyph = [^] \ ("\x00" | "\n" | unix_control | ws);
natural = [1-9][0-9]*;
lookat = ws* natural ":" natural [ab]?
("-" (natural ":")? natural [ab]?)? ws* "--";
*/
for( ; ; ) { /*!re2c /**/
<skip, book> * { printf("*! %zu\n", line);goto error; }
<line> "\x00" { printf("yes!\n");return 1; }
<line> * :=> skip
<line> "Genesis" / lookat => book { book = Genesis; continue; }
<line> "Exodus" / lookat => book { book = Exodus; continue; }
/*| "Leviticus" | "Numbers" | "Deuteronomy"
| "Joshua" | "Judges" | "Ruth" | "I"{1,2} " Samuel" | "I"{1,2} " Kings"
| "I"{1,2} " Chronicles" | "Ezra" | "Nehemiah" | "Esther" | "Job"
| "Psalms" | "Proverbs" | "Ecclesiastes" | "Song of Solomon" | "Isaiah"
| "Jeremiah" | "Lamentations" | "Ezekiel" | "Daniel" | "Hosea" | "Joel"
| "Amos" | "Obadiah" | "Jonah" | "Micah" | "Nahum" | "Habakkuk"
| "Zephaniah" | "Haggai" | "Zechariah" | "Malachi" | "Matthew" | "Mark"
| "Luke" | "John" | "Acts" | "Romans" | "I"{1,2} " Corinthians"
| "Galatians" | "Ephesians" | "Philippians" | "Colossians"
| "I"{1,2} " Thessalonians" | "I"{1,2} " Timothy" | "Titus" | "Philemon"
| "Hebrews" | "James" | "I"{1,2} " Peter" | "I"{1,3} " John" | "Jude"
| "Revelation") @s1 ws* / bible_ref ws+ "--" ws+ "``" */
//<line> [^\n\x00]* newline { printf("throw\n"); line++; continue; }
<book> ws+ @s0 natural @s1 ":" @t0 natural @t1 [ab]? => skip {
if(!helper_natural(s0, s1, &chapter)
|| !helper_natural(t0, t1, &verse)) goto error;
union kjvcite c
= { .book = book, .chapter = chapter, .verse = verse };
char a[12];
kjvcite_to_string(c, &a), printf("Parsed %s\n", a);
continue;
}
<skip> [^\n\x00]* "\n" => line { line++; continue; }
//=> bible { x->s0 = s0, x->s1 = s1; return x->symbol = KJV_BOOK, 1; }
*/ }
error:
{
char a[12];
date32_to_string(date, &a);
fprintf(stderr, "%s line %zu: unexpected.\n", a, line);
}
return 0;
}
int main(void) {
struct journal j = journal();
int success = EXIT_SUCCESS;
struct journal_iterator it;
union date32 k;
union load *v;
if(!journal_is_valid(&j)) goto catch;
printf("Journal: %s.\n", journal_to_string(&j));
if(!scan((union date32){ .u32 = 42 }, "Genesis 1:1 -- fdgYo.\n"
"Exodus 1:2 -- fuck\n"
"no\n")) fprintf(stderr, "Error :[\n");
it = journal_begin(&j); while(journal_next(&it, &k, &v)) {
char a[12];
date32_to_string(k, &a), printf("%s: %s\n", a, v->text);
}
goto finally;
catch:
success = EXIT_FAILURE;
perror("journal");
finally:
journal_(&j);
return success;
}