]> git.mdlowis.com Git - proto/labwc.git/commitdiff
scripts/helper/find-idents.c: handle single line comment tokens
authorConsolatis <35009135+Consolatis@users.noreply.github.com>
Thu, 9 Feb 2023 04:33:16 +0000 (05:33 +0100)
committerJohan Malm <johanmalm@users.noreply.github.com>
Thu, 6 Jul 2023 17:04:55 +0000 (18:04 +0100)
scripts/helper/find-idents.c

index 69556ae479f45a4baef7760a1e56c8198ae4610f..b7b36a84d8dd49feec12fd5111d87b93d886d64f 100644 (file)
@@ -54,6 +54,7 @@ enum {
        SPECIAL_COMPARISON_OP,
        SPECIAL_COMMENT_BEGIN,
        SPECIAL_COMMENT_END,
+       SPECIAL_COMMENT_LINE_BEGIN,
 };
 
 static char *current_buffer_position;
@@ -111,25 +112,12 @@ add_token(void)
        return token;
 }
 
-static void
-handle_whitespace(struct token *token)
-{
-       if (current_buffer_position[0] == '\n') {
-               ++current_line;
-       }
-       current_buffer_position++;
-       if (isspace(current_buffer_position[0])) {
-               handle_whitespace(token);
-       }
-}
-
 static void
 get_identifier_token(struct token *token)
 {
        buf_add_char(&token->name, current_buffer_position[0]);
        current_buffer_position++;
        if (isspace(current_buffer_position[0])) {
-               handle_whitespace(token);
                return;
        }
        switch (current_buffer_position[0]) {
@@ -153,7 +141,6 @@ get_number_token(struct token *token)
        buf_add_char(&token->name, current_buffer_position[0]);
        current_buffer_position++;
        if (isspace(current_buffer_position[0])) {
-               handle_whitespace(token);
                return;
        }
        switch (current_buffer_position[0]) {
@@ -198,6 +185,7 @@ struct {
        { "!=", SPECIAL_COMPARISON_OP },
        { "/*", SPECIAL_COMMENT_BEGIN },
        { "*/", SPECIAL_COMMENT_END },
+       { "//", SPECIAL_COMMENT_LINE_BEGIN },
        { ";", ';' },
        { "{", '{' },
        { "}", '}' },
@@ -255,9 +243,6 @@ get_special_token(struct token *token)
        }
 done:
        current_buffer_position += token->name.len;
-       if (isspace(current_buffer_position[0])) {
-               handle_whitespace(token);
-       }
 }
 
 static void
@@ -282,6 +267,7 @@ lex(char *buffer)
        tokens = NULL;
        nr_tokens = 0;
        alloc_tokens = 0;
+       bool in_single_comment = false;
 
        current_buffer_position = buffer;
 
@@ -309,11 +295,21 @@ lex(char *buffer)
                        token = add_token();
                        get_special_token(token);
                        token->kind = TOKEN_SPECIAL;
+                       if (token->special == SPECIAL_COMMENT_LINE_BEGIN) {
+                               token->special = SPECIAL_COMMENT_BEGIN;
+                               in_single_comment = true;
+                       }
                        continue;
                case '#':
                        handle_preprocessor_directive();
                        break;
                case '\n':
+                       if (in_single_comment) {
+                               token = add_token();
+                               token->kind = TOKEN_SPECIAL;
+                               token->special = SPECIAL_COMMENT_END;
+                               in_single_comment = false;
+                       }
                        ++current_line;
                        break;
                default:
@@ -350,14 +346,14 @@ static bool
 grep(struct token *tokens, const char *pattern)
 {
        bool found = false;
-       bool in_comment = false;
+       unsigned int in_comment = 0;
 
        for (struct token *t = tokens; t->kind; t++) {
                if (t->kind == TOKEN_SPECIAL) {
                        if (t->special == SPECIAL_COMMENT_BEGIN) {
-                               in_comment = true;
+                               ++in_comment;
                        } else if (t->special == SPECIAL_COMMENT_END) {
-                               in_comment = false;
+                               --in_comment;
                        }
                }
                if (in_comment) {