diff options
-rw-r--r-- | include/lex.h | 1 | ||||
-rw-r--r-- | src/lex.c | 26 | ||||
-rw-r--r-- | src/main.c | 3 |
3 files changed, 28 insertions, 2 deletions
diff --git a/include/lex.h b/include/lex.h index 942df9b..8050c4e 100644 --- a/include/lex.h +++ b/include/lex.h @@ -83,6 +83,7 @@ enum lexical_token { T_LAST_OPERATOR, // Tokens with additional information + T_COMMENT, T_NAME, T_NUMBER, T_STRING, @@ -215,6 +215,25 @@ lex_string(struct lexer *lexer, struct token *out, uint32_t delim) return out->token; } +static enum lexical_token +lex_comment(struct lexer *lexer, struct token *out) +{ + uint32_t c; + if ((c = next(lexer)) != C_EOF && c != ' ') { + unget(lexer); + } + + while ((c = next(lexer)) != C_EOF && c != '\n') { + push(lexer, c); + } + + out->info.str = strdup(lexer->buf); + + clear(lexer); + out->token = T_COMMENT; + return out->token; +} + enum lexical_token lex(struct lexer *lexer, struct token *out) { @@ -307,7 +326,12 @@ lex(struct lexer *lexer, struct token *out) out->token = T_COMMA; break; case '/': - out->token = T_DIV; + if (wgetc(lexer) == '/') { + return lex_comment(lexer, out); + } else { + unget(lexer); + out->token = T_DIV; + } break; case '.': out->token = T_DOT; @@ -43,7 +43,8 @@ main(int argc, char *argv[]) struct token token; while (lex(&lexer, &token) != T_EOF) { printf("%d", token.token); - if (token.token == T_NAME || token.token == T_STRING) { + if (token.token == T_COMMENT || token.token == T_NAME + || token.token == T_STRING) { printf(" %s", token.info.str); } else if (token.token == T_NUMBER && token.info.num.isfloat) { |