diff --git a/src/interpret.c b/src/interpret.c index c7d6f15..1a46365 100644 --- a/src/interpret.c +++ b/src/interpret.c @@ -15,9 +15,9 @@ static void int_to_string(const int *const n, #define ARRAY_NAME int #define ARRAY_TYPE int #define ARRAY_EXPECT_TRAIT -#include "../src/array.h" +#include "array.h" #define ARRAY_TO_STRING &int_to_string -#include "../src/array.h" +#include "array.h" static int int_cmp(const int *const a, const int *const b) { return (*b < *a) - (*a < *b); } static int void_int_cmp(const void *const a, const void *const b) diff --git a/src/lex.h b/src/lex.h index ab82088..436f779 100644 --- a/src/lex.h +++ b/src/lex.h @@ -4,10 +4,11 @@ int lex_looks_like_year(const char *, int *); int lex_looks_like_month(const char *); int lex_looks_like_day(const char *); -#define LEX_SYMBOL X(END), X(ERROR), X(TEXT), X(BANG), X(BRACKET), X(WHITE), \ - X(MAP), \ - /* Commands */ X(SOURCE), X(DEFAULT), X(SIGNIFICANT), X(SCORE), \ - /* Arguments */ X(ARG_KEYWORD), X(ARG_DATE), X(ARG_NATURAL), X(ARG_FREEFORM) +#define LEX_SYMBOL \ + /* Results. */ X(END), X(SYNTAX), X(ILLEGAL), X(NOT_FOUND), \ + /* Text */ X(PARAGRAPH), X(TEXT), \ + /* Directive. */ X(SOURCE), X(DEFAULT), X(SIGNIFICANT), X(SCORE), X(MAP), \ + /* Arguments. */ X(ARG_KEYWORD), X(ARG_DATE), X(ARG_NATURAL), X(ARG_FREEFORM) #define X(n) n struct lex { diff --git a/src/lex.re_c.c b/src/lex.re_c.c index 943e191..3182503 100644 --- a/src/lex.re_c.c +++ b/src/lex.re_c.c @@ -20,6 +20,7 @@ re2c:define:YYCTYPE = char; int lex_looks_like_year(const char *const a, int *const year) { const char *YYCURSOR = a, *YYMARKER = a, *s0; /*!stags:re2c format = 'const char *@@;\n'; */ + (void)yyt1; assert(a && year); /*!re2c @s0 ("-"? [1-9][0-9]* | "0") "\x00" { @@ -81,7 +82,7 @@ static struct scan { #define X(n) EXPECT_ ## n /* "[something: expect; there; to; be; args]", in this case, expect would be a stack of `size = 5` `EXPECT_KEYWORD`. */ - struct { unsigned size; enum { EXPECT } expect[16]; } command; + struct { unsigned size; enum { EXPECT } expect[16]; } edict; #undef X } scan; /* Terrible, gah. */ @@ -92,45 +93,30 @@ void lex_reset(const char *const buffer) { scan.line = 1; } -/*!re2c -re2c:flags:tags = 1; -re2c:define:YYCURSOR = scan.cursor; -re2c:define:YYMARKER = scan.marker; -re2c:define:YYCTXMARKER = scan.ctx_marker; -re2c:define:YYCONDTYPE = 'condition'; -re2c:define:YYGETCONDITION = 'scan.condition'; -re2c:define:YYGETCONDITION:naked = 1; -re2c:define:YYSETCONDITION = 'scan.condition = @@;'; -re2c:define:YYSETCONDITION:naked = 1; - -// Eof is marked by null when preparing files for lexing. -// Mutually exclusive; only !, [, are not covered. -end = "\x00"; -newline = "\n" | "\r" "\n"?; -ws = [ \t\v\f]; -glyph = [^ \t\n\r\v\f![\x00]; -glyphs = glyph+; - -// inside the block -natural = [1-9][0-9]*; -decimal = "-"? ([1-9][0-9]* | [0])? "." [0-9]+ | [1-9][0-9]* | [0]; -id = [a-zA-Z_][a-zA-Z_\-0-9]{0,63}; -date = "-"? natural "-" [0-1][0-9] "-" [0-1][0-9]; -*/ - /** I don't think `re2c` supports branching on variable conditions. It does now? */ static void expect_pop(void) { printf(""); - if(!scan.command.size) { printf("allfinished\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; + if(!scan.edict.size) { printf("allfinished\n"); scan.condition = yycedict_end; return; } + switch(scan.edict.expect[--scan.edict.size]) { + case EXPECT_KEYWORD: printf("keyword\n");scan.condition = yycedict_keyword; break; + case EXPECT_DATE: printf("date\n");scan.condition = yycedict_date; break; + case EXPECT_FREEFORM: printf("freeform\n");scan.condition = yycedict_freeform; break; } } int lex_next(struct lex *const x) { + /*!re2c + re2c:flags:tags = 1; + re2c:define:YYCURSOR = scan.cursor; + re2c:define:YYMARKER = scan.marker; + re2c:define:YYCTXMARKER = scan.ctx_marker; + re2c:define:YYCONDTYPE = 'condition'; + re2c:define:YYGETCONDITION = 'scan.condition'; + re2c:define:YYGETCONDITION:naked = 1; + re2c:define:YYSETCONDITION = 'scan.condition = @@;'; + re2c:define:YYSETCONDITION:naked = 1; + */ const char *s0, *s1; const size_t prev_line = scan.line; /*!stags:re2c format = 'const char *@@;\n'; */ @@ -142,24 +128,27 @@ int lex_next(struct lex *const x) { x->new_paragraph = 0; scan: /*!re2c - end { printf("end\n");return x->symbol = END, 0; } - end - { return x->symbol = ERROR, 0; } + end = "\x00"; + illegal = [\x01-\x08\x0a-\x1f\x7f]; // unix-style control characters + newline = "\n"; + ws = [ \t]; + glyph = [^\x00-\x1f \x7f]; + <*> illegal { return x->symbol = ILLEGAL, 0; } + end { return x->symbol = END, 0; } + + end { return x->symbol = ILLEGAL, 0; } + newline { x->line = ++scan.line; return x->symbol = PARAGRAPH, 1; } + "![" :=> image + "[" :=> edict + "" / glyph :=> text + * { return x->symbol = SYNTAX, 1; } - newline - { x->line = ++scan.line; - x->new_paragraph = prev_line + 2 >= scan.line; - x->ws_before = 1; - goto scan; } - ws+ { x->ws_before = 1; goto scan; } + newline => line { x->line = ++scan.line; goto scan; } + ws+ { goto scan; } @s0 glyph+ @s1 { x->s0 = s0, x->s1 = s1; return x->symbol = TEXT, 1; } - @s0 "!" @s1 { x->s0 = s0, x->s1 = s1; - return x->symbol = BANG, 1; } - "\\" @s0 "[" @s1 { x->s0 = s0, x->s1 = s1; - return x->symbol = BRACKET, 1; } - "![" :=> image - "[" :=> command + + decimal = "-"? ([1-9][0-9]* | [0])? "." [0-9]+ | [1-9][0-9]* | [0]; ws* "osm" ws* "](geo:" @s0 decimal "," @s1 decimal ")" => text { x->symbol = MAP, x->s0 = s0, x->s1 = s1; @@ -168,58 +157,60 @@ scan: } * { printf("image(broken)\n");return 0; } + natural = [1-9][0-9]*; + id = [a-zA-Z_][a-zA-Z_\-0-9]{0,63}; + date = "-"? natural "-" [0-1][0-9] "-" [0-1][0-9]; // source - "source" - { if(scan.is_ws_expected || scan.command.size) - return x->symbol = ERROR, 0; + "source" + { if(scan.is_ws_expected || scan.edict.size) + return x->symbol = SYNTAX, 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; + scan.edict.size = 2; + scan.edict.expect[1] = EXPECT_KEYWORD; + scan.edict.expect[0] = EXPECT_FREEFORM; return x->symbol = SOURCE, 1; } - "default" + "default" { if(scan.is_ws_expected || !scan.is_source) - return x->symbol = ERROR, 0; + return x->symbol = SYNTAX, 0; scan.is_ws_expected = 1, scan.is_source = 0; return x->symbol = DEFAULT, 1; } // score - "significant" - { if(scan.is_ws_expected || scan.command.size) - return x->symbol = ERROR, 0; + "significant" + { if(scan.is_ws_expected || scan.edict.size) + return x->symbol = SYNTAX, 0; scan.is_ws_expected = 1; - scan.command.size = 2; - scan.command.expect[1] = EXPECT_FREEFORM; - scan.command.expect[0] = EXPECT_DATE; + scan.edict.size = 2; + scan.edict.expect[1] = EXPECT_FREEFORM; + scan.edict.expect[0] = EXPECT_DATE; return x->symbol = SIGNIFICANT, 1; } - @s0 natural @s1 - { if(scan.is_ws_expected || scan.command.size) - return x->symbol = ERROR, 0; + @s0 natural @s1 + { if(scan.is_ws_expected || scan.edict.size) + return x->symbol = SYNTAX, 0; scan.is_ws_expected = 1; x->s0 = s0, x->s1 = s1; return x->symbol = SCORE, 1; } - // general command stuff - ws+ { scan.is_ws_expected = 0; goto scan; } - ":" - { if(!scan.command.size) return x->symbol = ERROR, 0; + ws+ { scan.is_ws_expected = 0; goto scan; } + ":" + { if(!scan.edict.size) return x->symbol = SYNTAX, 0; scan.is_ws_expected = 0, scan.is_source = 0; expect_pop(); goto scan; } - ws* @s0 id @s1 ws* ";"? / "]"? + ws* @s0 id @s1 ws* ";"? / "]"? { x->s0 = s0, x->s1 = s1; expect_pop(); return x->symbol = ARG_KEYWORD, 1; } - ws* @s0 date @s1 ws* ";"? / "]"? + ws* @s0 date @s1 ws* ";"? / "]"? { x->s0 = s0, x->s1 = s1; expect_pop(); return x->symbol = ARG_DATE, 1; } - ws* @s0 + ws* @s0 [^ \t\n\r\v\f;[\]\x00][^\t\n\r\v\f;[\]\x00]*[^ \t\n\r\v\f;[\]\x00]* @s1 ws* ";"? / "]"? { x->s0 = s0, x->s1 = s1; expect_pop(); return x->symbol = ARG_FREEFORM, 1; } - "]" => text - { if(scan.command.size) return 0; + "]" => text + { if(scan.edict.size) return 0; goto scan; } - * - { return x->symbol = ERROR, 0; } + * + { return x->symbol = SYNTAX, 0; } */ }