From c592ef5369a0c1dfe0feab4bff6f5186a5cbb897 Mon Sep 17 00:00:00 2001 From: overflowerror Date: Thu, 20 May 2021 17:04:16 +0200 Subject: [PATCH] basic scanner done --- src/scanner.l | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/scanner.l diff --git a/src/scanner.l b/src/scanner.l new file mode 100644 index 0000000..4bc6adc --- /dev/null +++ b/src/scanner.l @@ -0,0 +1,70 @@ +whitespace [\n\t\r ] + +params_begin "{$" +params_end "$}" + +section "%%" + +statement_begin "{%" +statement_end "%}" + +output_begin "{{" +output_end "}}" + +block_end "end" + +type_prefixes "enum"|"struct" +type_prefix {type_prefixes}{whitespace}+ +type_pointer {whitespace}*"*" +type {type_prefix}?[a-zA-Z0-9_]+{type_pointer}? + +%option noyywrap +%option nodefault +%option nounput +%option noinput +%option yylineno + +%x PARAMS +%x STATEMENT +%x OUTPUT +%x META_SECTION +%x MAIN_SECTION + +%s META_SECTION + +%% + +%{ + #include + #include + #include + + bool isMetaSection = true; +%} + +{params_begin} { BEGIN(PARAMS); return PARAMS_BEGIN; } +{statement_begin} { BEGIN(STATEMENT); return STATEMENT_BEGIN; } +{section} { isMetaSection = false; BEGIN(MAIN_SECTION); } +{output_begin} { fprintf(stderr, "error: output block not allowed in meta section (line %d)\n", yylineno); exit(1); } +. { fprintf(stderr, "error: raw text not allowed in meta section (line %d)\n", yylineno); exit(1); } + +{type} { return TEXT; } +{params_end} { BEGIN(META_SECTION); return PARAMS_END; } +"," { return COMMA; } +{whitespace}+ { /* ignore whitespaces */ } +$ { return TEXT; /* catch $ as text */ } +. { fprintf(stderr, "error: illegal token '%s' in parameter block (line %d)\n", yytext, yylineno); exit(1); } + +[^%]+ { return TEXT; } +% { return TEXT; /* catch % as text */ } +{statement_end} { if (isMetaSection) BEGIN(META_SECTION); else BEGIN(MAIN_SECTION); return STATEMENT_END; } + +{params_begin} { fprintf(stderr, "warning: parameter block not allowed in main section (line %d); assuming to be text\n", yylineno); return TEXT; } +{statement_begin} { BEGIN(STATEMENT); return STATEMENT_BEGIN; } +{output_begin} { BEGIN(OUTPUT); return OUTPUT_BEGIN; } +. { return TEXT; } + +[^}]+ { return TEXT; } +{output_end} { BEGIN(MAIN_SECTION); return OUTPUT_END; } +"}" { return TEXT; /* catch } as text */ } +