commit 9892f3a46e97ac793b2cbd785270f3177813d3b2
parent 8dca0ea8e3f13e1e0ed157b7110ab072df348c41
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date: Tue, 27 Feb 2024 17:52:29 +0000
Add support for intentation and C-style comments
Diffstat:
3 files changed, 34 insertions(+), 12 deletions(-)
diff --git a/src/lexer.l b/src/lexer.l
@@ -12,6 +12,10 @@ LINE_END (\n|\r|\r\n)
{LINE_END} { return EOL; }
^[\t ]*{LINE_END} { return EOL; }
+^[\t ]*
+
+^[\t ]*"//".* { yylval.n = strdup(yytext); return COMMENT; }
+
, { return COMMA; }
[^,\n]* {
while(*yytext && isspace(*yytext)) yytext++;
diff --git a/src/parser.y b/src/parser.y
@@ -16,7 +16,7 @@
char *n;
}
-%token <n> LITERAL
+%token <n> LITERAL COMMENT
%token EOL COMMA
%type <r> record
@@ -30,6 +30,11 @@
document: %empty
| EOL document
| record document { records = list_new((char *)$1, records); }
+ | COMMENT document {
+ records = list_new((char *)record_new(
+ $1, NULL, NULL, NULL
+ ), records);
+ }
;
record: name list list list { $$ = record_new($1, $2, $3, $4); }
@@ -134,6 +139,11 @@ void record_free(void *rp) {
}
void record_print_function(const struct record *r) {
+ if(!r->recipe) { // comment
+ printf("%s\n", r->name);
+ return;
+ }
+
struct list dummy, *c = &dummy;
printf("static constexpr auto %s(", r->name);
for(struct list *p = r->args; p; p = p->next) {
@@ -165,6 +175,11 @@ void record_print_function(const struct record *r) {
}
void record_print_template(const struct record *r) {
+ if(!r->recipe) { // comment
+ printf("%s\n", r->name);
+ return;
+ }
+
struct list dummy, *c = &dummy;
printf("template <");
diff --git a/src/rules b/src/rules
@@ -1,4 +1,5 @@
+// hello
foreground
int idx
@@ -6,30 +7,32 @@ limit_256
32, ';', 5, ';', idx, 'm'
-background
-int idx
-limit_256
-42, ';', 5, ';', idx, 'm'
-
-foreground
-int R, int G, int B
-limit_256
-38, ';', 5, ';', R, ';', G, ';', B, 'm'
+ background
+ int idx
+ limit_256
+ 42, ';', 5, ';', idx, 'm'
+
+ foreground
+ int R, int G, int B
+ limit_256
+ 38, ';', 5, ';', R, ';', G, ';', B, 'm'
+ // world
+ // world
background
int R, int G, int B
limit_256
38, ';', 5, ';', R, ';', G, ';', B, 'm'
-
-
+// comment
foreground
COLOR color
(int)color + 30, 'm'
+// hello there
background
COLOR color