scripts/helper/find-idents.c: handle single line comment tokens

This commit is contained in:
Consolatis 2023-02-09 05:33:16 +01:00 committed by Johan Malm
parent 29031a4c85
commit 309843c9c6

View 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) {