This is getting complex.

This commit is contained in:
Neil 2022-07-17 22:01:34 -07:00
parent d13bf4ea1b
commit 3f81d8ea51
3 changed files with 41 additions and 24 deletions

View File

@ -218,6 +218,26 @@ catch:
return 0;
}
#define C_BLACK "\033[0;30m"
#define C_RED "\033[0;31m"
#define C_GREEN "\033[0;32m"
#define C_YELLOW "\033[0;33m"
#define C_BLUE "\033[0;34m"
#define C_PURPLE "\033[0;35m"
#define C_CYAN "\033[0;36m"
#define C_WHITE "\033[0;37m"
#define CB_BLACK "\033[1;30m"
#define CB_RED "\033[1;31m"
#define CB_GREEN "\033[1;32m"
#define CB_YELLOW "\033[1;33m"
#define CB_BLUE "\033[1;34m"
#define CB_PURPLE "\033[1;35m"
#define CB_CYAN "\033[1;36m"
#define CB_WHITE "\033[1;37m"
#define C_RESET "\033[0m"
int main(int argc, char **argv) {
int success = EXIT_SUCCESS;
char *intent = 0;
@ -310,35 +330,23 @@ int main(int argc, char **argv) {
page->entry = char_array();
page->meaning = lex_array();
if(!append_file(&page->entry, fn)) goto syntax;
struct {
int content;
enum lex_symbol expected;
} context = { 0, TEXT };
int first = 1;
for(lex_reset(page->entry.data); ; ) {
if(!(lex = lex_array_new(&page->meaning))) goto syntax;
if(!lex_next(lex)) {
if(lex->symbol != END) { errno = EILSEQ; goto syntax; }
break; /* Terminated successfully. */
}
/* Debug print. */
if(context.content && context.expected != lex->symbol) {
printf("//\n");
context.content = 0;
} else {
context.content = 1;
}
switch(lex->symbol) {
case TEXT: printf("[%.*s]",
(int)(lex->s1 - lex->s0), lex->s0);
context.expected = TEXT; break;
case PARAGRAPH: printf("\n"); break;
case BIBLE_BOOK: printf("book:[%.*s]",
(int)(lex->s1 - lex->s0), lex->s0);
context.expected = BIBLE_CHAPTER_VERSE; break;
case BIBLE_CHAPTER_VERSE: printf("[ch. %.*s]",
(int)(lex->s1 - lex->s0), lex->s0);
context.expected = BIBLE_TEXT; break;
case BIBLE_TEXT: printf("[%.*s]",
case TEXT: printf("%s%.*s",
first ? "" : " ", (int)(lex->s1 - lex->s0), lex->s0);
first = 0; break;
case PARAGRAPH: printf("\n" C_RESET); break;
case BIBLE_BOOK: printf(C_YELLOW "%.*s",
(int)(lex->s1 - lex->s0), lex->s0); break;
case BIBLE_CHAPTER_VERSE: printf(" ch. %.*s",
(int)(lex->s1 - lex->s0), lex->s0); break;
case BIBLE_TEXT: printf("%s%.*s",
(int)(lex->s1 - lex->s0), lex->s0); break;
case BIBLE_NEXT: printf("(next)\n"); break;
default:

View File

@ -9,6 +9,7 @@ int lex_looks_like_day(const char *);
/* Results. */ \
X(END), X(SYNTAX), X(ILLEGAL), X(NOT_FOUND), \
\
X(ORDERED_LIST_ITEM), X(LIST_ITEM), X(COMPLETE), X(CANCELLED), X(HEADING), \
/* Text. */ \
X(PARAGRAPH), X(TEXT), X(CAPTION), \
\

View File

@ -157,10 +157,18 @@ scan:
<expect_caption> ws* @s0 glyph (glyph | ws)* @s1 ws* / newline
=> expect_line
{ x->s0 = s0, x->s1 = s1; return x->symbol = CAPTION, 1; }
/* Recognized symbols that go at the beginning of a line. */
<line> newline { x->line = ++scan.line; return x->symbol = PARAGRAPH, 1; }
<line> "[" :=> edict
<line> "--" :=> source
<line> "->" :=> location
<line> "[" :=> edict
<line> "*" ws => text
{ return x->symbol = LIST_ITEM, 1; }
<line> @s0 natural @s1 "." ws => text
{ x->s0 = s0, x->s1 = s1; return x->symbol = ORDERED_LIST_ITEM, 1; }
<line> "!" => text { return x->symbol = COMPLETE, 1; }
<line> "^" => text { return x->symbol = CANCELLED, 1; }
<line> "#" => text { return x->symbol = HEADING, 1; }
/* Just plain text. */
<line> ws* / glyph :=> text /* match-empty-string: text takes care of it. */
@ -192,7 +200,7 @@ scan:
/* fixme: This is a hack that doesn't allow apostrophes at the end of a
word, (not sure there are any in the bible.) Is ' terminated by '';
otherwise same as glyph+ above. */
<bible> @s0 (glyph_minus+ ("'" glyph_minus+)*) | (("'" glyph_minus+)+) @s1
<bible> @s0 ("'"? glyph_minus+ ("'" glyph_minus+)*) @s1
{ x->s0 = s0, x->s1 = s1; return x->symbol = BIBLE_TEXT, 1; }
/* Multiple verses can be present, but they end in ''.
Not strictly enforced. */