diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-09-05 15:32:26 +0200 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-09-05 15:32:26 +0200 |
commit | 899e79466bb46b6600e55c335b1bf5b3c6c072ff (patch) | |
tree | 0efd652b7add8bb02db7636996caa009da78c516 /src | |
parent | 4377cb485b0d03d189979766bf64676b4db9dad6 (diff) |
Allow line continuation anywhere
Diffstat (limited to 'src')
-rw-r--r-- | src/lexer.l | 7 | ||||
-rw-r--r-- | src/parser.y | 28 |
2 files changed, 16 insertions, 19 deletions
diff --git a/src/lexer.l b/src/lexer.l index 74d301a..8100405 100644 --- a/src/lexer.l +++ b/src/lexer.l @@ -15,6 +15,8 @@ extern const char *current_file; %} +ws [ \t] + %% ¬|! { return NOT; } @@ -29,12 +31,15 @@ extern const char *current_file; \\ { return '\\'; } \n { return EOL; } + /* Allow line-continuation when the newline is suffixed by a backslash */ +\n{ws}*\\ ; + [a-zA-Z] { yylval.ch = *yytext; return IDENT; } -[ \t]+ ; +{ws}+ ; . { user_error("%s:%d: Unrecognized character ‘%c’", diff --git a/src/parser.y b/src/parser.y index 072fef7..827005d 100644 --- a/src/parser.y +++ b/src/parser.y @@ -11,7 +11,6 @@ static ast_t mkunop(int, ast_t); static ast_t mkbinop(yytoken_kind_t, ast_t, ast_t); -static asts_t pushast(asts_t, ast_t); static void yyerror(const char *); extern const char *current_file; @@ -67,9 +66,14 @@ exprs: $$.buf = xmalloc(sizeof(*$$.buf) * $$.cap); $$.buf[0] = $1; } - | exprs '|' expr { $$ = pushast($1, $3); } - | exprs cont '|' expr { $$ = pushast($1, $4); } - | exprs '|' cont expr { $$ = pushast($1, $4); } + | exprs '|' expr { + $$ = $1; + if ($$.len == $$.cap) { + $$.cap *= 2; + $$.buf = xrealloc($$.buf, sizeof(*$$.buf) * $$.cap); + } + $$.buf[$$.len++] = $3; + } ; expr: @@ -79,8 +83,8 @@ expr: $$.eqn->ch = $1; $$.vars = UINT64_C(1) << (islower($1) ? $1-'a'+26 : $1-'A'); } - | NOT expr { $$ = mkunop(NOT, $2); } - | OPAR expr CPAR { $$ = mkunop(OPAR, $2); } + | NOT expr { $$ = mkunop(NOT, $2); } + | OPAR expr CPAR { $$ = mkunop(OPAR, $2); } | expr AND expr { $$ = mkbinop(AND, $1, $3); } | expr OR expr { $$ = mkbinop(OR, $1, $3); } | expr XOR expr { $$ = mkbinop(XOR, $1, $3); } @@ -88,7 +92,6 @@ expr: | expr EQUIV expr { $$ = mkbinop(EQUIV, $1, $3); } ; -cont: EOL '\\'; eol: EOL | YYEOF; %% @@ -118,17 +121,6 @@ mkbinop(yytoken_kind_t op, ast_t lhs, ast_t rhs) return a; } -asts_t -pushast(asts_t as, ast_t a) -{ - if (as.len == as.cap) { - as.cap *= 2; - as.buf = xrealloc(as.buf, sizeof(*as.buf) * as.cap); - } - as.buf[as.len++] = a; - return as; -} - void yyerror(const char *s) { |