interpret/kjv/src/kjv.re_c.c

72 lines
1.6 KiB
C
Raw Normal View History

/** @license 2022 Neil Edelman, distributed under the terms of the
[MIT License](https://opensource.org/licenses/MIT).
Is intended to use
<https://github.com/scrollmapper/bible_databases/master/txt/KJV/>. */
#include "../src/kjv.h"
#include <assert.h>
#include <errno.h>
#include <stdio.h> /* debug */
2022-12-13 05:25:28 +00:00
/** [`s`,`e`) => `n` */
static int parse_natural(const char *s, const char *const e, unsigned *const n) {
2022-12-12 08:45:41 +00:00
unsigned accum = 0;
while(s < e) {
2022-12-12 08:45:41 +00:00
unsigned next = accum * 10 + (unsigned)(*s - '0');
if(accum >= next) return errno = ERANGE, 0;
accum = next;
s++;
}
2022-12-12 08:45:41 +00:00
*n = accum;
return 1;
}
/*!re2c /**/
re2c:yyfill:enable = 0;
re2c:define:YYCTYPE = char;
natural = [1-9][0-9]*;
2022-12-13 05:25:28 +00:00
whitespace = [ \t\v\f];
word = [^ \t\v\f\n\x00]+;
*/
2022-12-13 05:25:28 +00:00
/** `fn` contains "<number>[*].txt", sticks that in `book_no`, otherwise
returns false. */
int kjv_filename(const char *fn, unsigned *book_no) {
const char *YYCURSOR = fn, *YYMARKER, *yyt1, *yyt2, *s0, *s1;
assert(fn && book_no);
/*!re2c /**/
*
{ return 0; }
2022-12-13 05:25:28 +00:00
@s0 natural @s1 [^.\x00]* ".txt" "\x00"
{ return parse_natural(s0, s1, book_no); }
*/
}
2022-12-13 05:25:28 +00:00
#if 0
/*!conditions:re2c*/
enum kjv_status kjv_chapter(const char *YYCURSOR, struct book *const book) {
const char *YYMARKER, *s0, *s1;
int c = yycinit;
/*!re2c /**/
re2c:define:YYGETCONDITION = "c";
re2c:define:YYSETCONDITION = "c = @@;";
*/
assert(book);
/*YYCURSOR = book;*/
return KJV_ERROR;
scan:
/*!re2c /**/
<*> * { return KJV_ERROR; }
<*> "\x00" { return KJV_DONE; }
<line> [^\n\x00]* "\n" { goto scan; }
<line> "[" natural ":" natural "]" :=> verse
<verse> whitespace+ { goto scan; }
<verse> @s0 word @s1 {
}
*/
}
#endif