More complex; read args in commands.
This commit is contained in:
parent
794dc2206f
commit
a75385f71a
@ -135,10 +135,12 @@ int main(int argc, char **argv) {
|
|||||||
while(lex_next(&lex)) {
|
while(lex_next(&lex)) {
|
||||||
printf("%lu: %s",
|
printf("%lu: %s",
|
||||||
(unsigned long)lex.line, lex_symbols[lex.symbol]);
|
(unsigned long)lex.line, lex_symbols[lex.symbol]);
|
||||||
if(lex.symbol == TEXT) {
|
if(lex.symbol == TEXT || lex.symbol == ARG_KEYWORD
|
||||||
|
|| lex.symbol == ARG_DATE
|
||||||
|
|| lex.symbol == ARG_FREEFORM) {
|
||||||
if(lex.s0 + INT_MAX < lex.s1)
|
if(lex.s0 + INT_MAX < lex.s1)
|
||||||
{ errno = EILSEQ; goto catch; }
|
{ errno = EILSEQ; goto catch; }
|
||||||
printf(" %.*s", (int)(lex.s1 - lex.s0), lex.s0);
|
printf(" <<%.*s>>", (int)(lex.s1 - lex.s0), lex.s0);
|
||||||
}
|
}
|
||||||
printf(".\n");
|
printf(".\n");
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,8 @@ int lex_looks_like_day(const char *);
|
|||||||
|
|
||||||
#define LEX_SYMBOL X(END), X(ERROR), X(TEXT), X(BANG), X(BRACKET), X(WHITE), \
|
#define LEX_SYMBOL X(END), X(ERROR), X(TEXT), X(BANG), X(BRACKET), X(WHITE), \
|
||||||
X(MAP), \
|
X(MAP), \
|
||||||
/* Commands */ X(SIGNIFICANT), X(DEFAULT), X(SOURCE)
|
/* Commands */ X(SOURCE), X(DEFAULT), X(SIGNIFICANT), X(SCORE), \
|
||||||
|
/* Arguments */ X(ARG_KEYWORD), X(ARG_DATE), X(ARG_NATURAL), X(ARG_FREEFORM)
|
||||||
|
|
||||||
#define X(n) n
|
#define X(n) n
|
||||||
struct lex {
|
struct lex {
|
||||||
|
@ -66,6 +66,8 @@ int lex_looks_like_day(const char *const a) {
|
|||||||
/* This defines `enum condition`. */
|
/* This defines `enum condition`. */
|
||||||
/*!types:re2c*/
|
/*!types:re2c*/
|
||||||
|
|
||||||
|
#define EXPECT X(KEYWORD), X(DATE), X(FREEFORM)
|
||||||
|
|
||||||
/** scanner reads a file and extracts semantic information. Valid to access
|
/** scanner reads a file and extracts semantic information. Valid to access
|
||||||
only while underlying pointers do not change. */
|
only while underlying pointers do not change. */
|
||||||
static struct scan {
|
static struct scan {
|
||||||
@ -75,7 +77,12 @@ static struct scan {
|
|||||||
const char *label, *buffer;
|
const char *label, *buffer;
|
||||||
enum condition condition;
|
enum condition condition;
|
||||||
size_t line;
|
size_t line;
|
||||||
int is_ws_expected;
|
int is_ws_expected, is_source;
|
||||||
|
#define X(n) EXPECT_ ## n
|
||||||
|
/* "[something: expect; there; to; be; args]", in this case, `size = 5` and
|
||||||
|
expect would be 5 `EXPECT_KEYWORD`. */
|
||||||
|
struct { unsigned size; enum { EXPECT } expect[20]; } command;
|
||||||
|
#undef X
|
||||||
} scan; /* Terrible, gah. */
|
} scan; /* Terrible, gah. */
|
||||||
|
|
||||||
void lex_reset(const char *const buffer) {
|
void lex_reset(const char *const buffer) {
|
||||||
@ -108,8 +115,19 @@ glyphs = glyph+;
|
|||||||
natural = [1-9][0-9]*;
|
natural = [1-9][0-9]*;
|
||||||
decimal = "-"? ([1-9][0-9]* | [0])? "." [0-9]+ | [1-9][0-9]* | [0];
|
decimal = "-"? ([1-9][0-9]* | [0])? "." [0-9]+ | [1-9][0-9]* | [0];
|
||||||
id = [a-zA-Z_][a-zA-Z_\-0-9]{0,63};
|
id = [a-zA-Z_][a-zA-Z_\-0-9]{0,63};
|
||||||
|
date = "-"? natural "-" [0-1][0-9] "-" [0-1][0-9];
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
static void expect_pop(void) {
|
||||||
|
printf("<expect_pop>");
|
||||||
|
if(!scan.command.size) { printf("nostack\n"); scan.condition = yyccommand_end; return; }
|
||||||
|
switch(scan.command.expect[--scan.command.size]) {
|
||||||
|
case EXPECT_KEYWORD: printf("keyword\n");scan.condition = yyccommand_keyword; break;
|
||||||
|
case EXPECT_DATE: printf("date\n");scan.condition = yyccommand_date; break;
|
||||||
|
case EXPECT_FREEFORM: printf("freeform\n");scan.condition = yyccommand_freeform; break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int lex_next(struct lex *const x) {
|
int lex_next(struct lex *const x) {
|
||||||
const char *s0, *s1;
|
const char *s0, *s1;
|
||||||
const size_t prev_line = scan.line;
|
const size_t prev_line = scan.line;
|
||||||
@ -123,7 +141,8 @@ int lex_next(struct lex *const x) {
|
|||||||
scan:
|
scan:
|
||||||
/*!re2c
|
/*!re2c
|
||||||
<text> end { printf("end\n");return x->symbol = END, 0; }
|
<text> end { printf("end\n");return x->symbol = END, 0; }
|
||||||
<image, command, command_args> end { return x->symbol = ERROR, 0; }
|
<image, command, command_keyword, command_date, command_end> end
|
||||||
|
{ return x->symbol = ERROR, 0; }
|
||||||
|
|
||||||
<text> newline
|
<text> newline
|
||||||
{ x->line = ++scan.line;
|
{ x->line = ++scan.line;
|
||||||
@ -147,20 +166,58 @@ scan:
|
|||||||
}
|
}
|
||||||
<image> * { printf("image(broken)\n");return 0; }
|
<image> * { printf("image(broken)\n");return 0; }
|
||||||
|
|
||||||
<command> ws+ { scan.is_ws_expected = 0; goto scan; }
|
// source
|
||||||
<command> @s0 natural @s1
|
|
||||||
{ if(scan.is_ws_expected) return x->symbol = ERROR, 0;
|
|
||||||
return x->symbol = SIGNIFICANT, scan.is_ws_expected = 1,
|
|
||||||
x->s0 = s0, x->s1 = s1, 1; }
|
|
||||||
<command> "source"
|
<command> "source"
|
||||||
{ if(scan.is_ws_expected) return x->symbol = ERROR, 0;
|
{ if(scan.is_ws_expected || scan.command.size)
|
||||||
return scan.is_ws_expected = 1, x->symbol = SOURCE, 1; }
|
return x->symbol = ERROR, 0;
|
||||||
|
scan.is_ws_expected = 1, scan.is_source = 1;
|
||||||
|
scan.command.size = 2;
|
||||||
|
scan.command.expect[1] = EXPECT_KEYWORD;
|
||||||
|
scan.command.expect[0] = EXPECT_FREEFORM;
|
||||||
|
return x->symbol = SOURCE, 1; }
|
||||||
<command> "default"
|
<command> "default"
|
||||||
{ if(scan.is_ws_expected) return x->symbol = ERROR, 0;
|
{ if(scan.is_ws_expected || !scan.is_source)
|
||||||
return scan.is_ws_expected = 1, x->symbol = DEFAULT, 1; }
|
return x->symbol = ERROR, 0;
|
||||||
<command> ":" => command_args { scan.is_ws_expected = 0; goto scan; }
|
scan.is_ws_expected = 1, scan.is_source = 0;
|
||||||
<command_args> [^\n\r\]\x00]+ { printf("Command args fixme\n"); goto scan; }
|
return x->symbol = DEFAULT, 1; }
|
||||||
<command, command_args> "]" :=> text
|
|
||||||
<command, command_args> * { return x->symbol = ERROR, 0; }
|
// score
|
||||||
*/
|
<command> "significant"
|
||||||
|
{ if(scan.is_ws_expected || scan.command.size)
|
||||||
|
return x->symbol = ERROR, 0;
|
||||||
|
scan.is_ws_expected = 1;
|
||||||
|
scan.command.size = 2;
|
||||||
|
scan.command.expect[1] = EXPECT_FREEFORM;
|
||||||
|
scan.command.expect[0] = EXPECT_DATE;
|
||||||
|
return x->symbol = SIGNIFICANT, 1; }
|
||||||
|
<command> @s0 natural @s1
|
||||||
|
{ if(scan.is_ws_expected || scan.command.size)
|
||||||
|
return x->symbol = ERROR, 0;
|
||||||
|
scan.is_ws_expected = 1;
|
||||||
|
x->s0 = s0, x->s1 = s1;
|
||||||
|
return x->symbol = SCORE, 1; }
|
||||||
|
|
||||||
|
// general command stuff
|
||||||
|
<command> ws+ { scan.is_ws_expected = 0; goto scan; }
|
||||||
|
<command> ":"
|
||||||
|
{ if(!scan.command.size) return x->symbol = ERROR, 0;
|
||||||
|
scan.is_ws_expected = 0, scan.is_source = 0;
|
||||||
|
expect_pop(); goto scan; }
|
||||||
|
<command_keyword> ws* @s0 id @s1 ws* ";"? / "]"?
|
||||||
|
{ x->s0 = s0, x->s1 = s1; expect_pop();
|
||||||
|
return x->symbol = ARG_KEYWORD, 1; }
|
||||||
|
<command_date> ws* @s0 date @s1 ws* ";"? / "]"?
|
||||||
|
{ x->s0 = s0, x->s1 = s1; expect_pop();
|
||||||
|
return x->symbol = ARG_DATE, 1; }
|
||||||
|
<command_freeform> ws* @s0 [^\t\n\r\v\f;[\]\x00]* @s1 ws* ";"? / "]"?
|
||||||
|
{ x->s0 = s0, x->s1 = s1; expect_pop();
|
||||||
|
return x->symbol = ARG_FREEFORM, 1; }
|
||||||
|
<command, command_end> "]" => text
|
||||||
|
{ printf("]\n"); if(scan.command.size) { printf("huh?\n"); return 0; }
|
||||||
|
goto scan;
|
||||||
|
}
|
||||||
|
<command, command_keyword, command_date, command_freeform, command_end> *
|
||||||
|
{ return x->symbol = ERROR, 0; }
|
||||||
|
*/
|
||||||
|
assert(0);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user