SPECIAL_COMPARISON_OP,
SPECIAL_COMMENT_BEGIN,
SPECIAL_COMMENT_END,
+ SPECIAL_COMMENT_LINE_BEGIN,
};
static char *current_buffer_position;
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]) {
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]) {
{ "!=", SPECIAL_COMPARISON_OP },
{ "/*", SPECIAL_COMMENT_BEGIN },
{ "*/", SPECIAL_COMMENT_END },
+ { "//", SPECIAL_COMMENT_LINE_BEGIN },
{ ";", ';' },
{ "{", '{' },
{ "}", '}' },
}
done:
current_buffer_position += token->name.len;
- if (isspace(current_buffer_position[0])) {
- handle_whitespace(token);
- }
}
static void
tokens = NULL;
nr_tokens = 0;
alloc_tokens = 0;
+ bool in_single_comment = false;
current_buffer_position = 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:
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) {