From dad8bb1e347ad293d99e374e24740f207208a20d Mon Sep 17 00:00:00 2001 From: David Phillips Date: Sun, 14 Apr 2019 20:55:10 +1200 Subject: Add support for // /**/ style comments --- lex.c | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/lex.c b/lex.c index c899fd7..8bc6640 100644 --- a/lex.c +++ b/lex.c @@ -12,6 +12,7 @@ static const char *filename = NULL; static size_t line; static size_t column; +static int context_comment; static struct token* tokens; static size_t tokens_count; static char buffer[1024]; /* XXX limitation: sources must have lines < 1024 bytes */ @@ -285,6 +286,20 @@ int lex_line(void) { struct token tok; while (column < len) { + /* special case: are we still inside a block comment? */ + if (context_comment) { + if ( column + 1 < len + && buffer[column] == '*' + && buffer[column + 1] == '/') { + context_comment = 0; + column += 2; + continue; + } + column++; + continue; + } + + /* fallthrough: outside a comment */ memset(&tok, 0, sizeof(tok)); store_location(&tok); switch (buffer[column]) { @@ -298,12 +313,19 @@ int lex_line(void) { case '\t': eat_whitespace(); continue; - /* case '/': - FIXME look ahead * or / - eat_block_comment(); + if (column + 1 < len && buffer[column + 1] == '*') { + column += 2; + context_comment = 1; + continue; + } else if (column + 1 < len && buffer[column + 1] == '/') { + ret = lex_eol(&tok); + column += 2; + return add_token(tok); + } else { + emit("Unexpected '/'\n"); + } break; - */ case ',': ret = lex_comma(&tok); break; @@ -346,6 +368,7 @@ struct token* lex(const char *filename_local, FILE *fin, size_t *len) line = 0; tokens = NULL; tokens_count = 0; + context_comment = 0; while (fgets(buffer, sizeof(buffer), fin)) { column = 0; @@ -359,6 +382,11 @@ struct token* lex(const char *filename_local, FILE *fin, size_t *len) return NULL; } + if (context_comment) { + emit("Error: unexpected EOF while still inside block comment\n"); + return NULL; + } + *len = tokens_count; return tokens; } -- cgit v1.1