aboutsummaryrefslogtreecommitdiff
path: root/src/tokenizer.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/tokenizer.c')
-rw-r--r--src/tokenizer.c57
1 files changed, 51 insertions, 6 deletions
diff --git a/src/tokenizer.c b/src/tokenizer.c
index 9077bb9..b2bd6c5 100644
--- a/src/tokenizer.c
+++ b/src/tokenizer.c
@@ -20,6 +20,11 @@ static int isAlphaDigit(int c) {
return isAlpha(c) || isDigit(c);
}
+static int tokenizer_get_start_of_line_from_index(Tokenizer *self, int index);
+static int tokenizer_get_end_of_line_from_index(Tokenizer *self, int index);
+/* Returns -1 if end of multiline comment was not found */
+static int tokenizer_get_end_of_multiline_comment(Tokenizer *self, int index);
+
int tokenizer_init(Tokenizer *self, ScopedAllocator *allocator, BufferView code, BufferView code_name) {
assert(code.size <= INT_MAX);
self->code = code;
@@ -242,6 +247,23 @@ static CHECK_RESULT int __tokenizer_next(Tokenizer *self, Token *token) {
SET_BINOP(BINOP_MUL);
} else if(c == '/') {
++self->index;
+ if(self->index < (int)self->code.size) {
+ c = tokenizer_get_char(self);
+ /* Single line comment */
+ if(c == '/') {
+ ++self->index;
+ self->index = tokenizer_get_end_of_line_from_index(self, self->index);
+ return __tokenizer_next(self, token);
+ } else if(c == '*') {
+ ++self->index;
+ self->index = tokenizer_get_end_of_multiline_comment(self, self->index);
+ if(self->index == -1) {
+ tokenizer_print_error(self, "End of multiline comment not found");
+ return TOKENIZER_ERR;
+ }
+ return __tokenizer_next(self, token);
+ }
+ }
SET_BINOP(BINOP_DIV);
} else if(c == '=') {
++self->index;
@@ -503,10 +525,10 @@ int tokenizer_consume_if(Tokenizer *self, Token expected_token, bool *result) {
return TOKENIZER_OK;
}
-static int tokenizer_get_start_of_line_from_index(Tokenizer *self, int index) {
- int c;
+int tokenizer_get_start_of_line_from_index(Tokenizer *self, int index) {
+ char c;
while(index >= 0) {
- c = self->code.data[(usize)index];
+ c = self->code.data[index];
if(c == '\n' || c == '\r') {
return index + 1;
}
@@ -515,10 +537,10 @@ static int tokenizer_get_start_of_line_from_index(Tokenizer *self, int index) {
return 0;
}
-static int tokenizer_get_end_of_line_from_index(Tokenizer *self, int index) {
- int c;
+int tokenizer_get_end_of_line_from_index(Tokenizer *self, int index) {
+ char c;
while(index < (int)self->code.size) {
- c = self->code.data[(usize)index];
+ c = self->code.data[index];
if(c == '\n' || c == '\r')
break;
++index;
@@ -526,6 +548,29 @@ static int tokenizer_get_end_of_line_from_index(Tokenizer *self, int index) {
return index;
}
+int tokenizer_get_end_of_multiline_comment(Tokenizer *self, int index) {
+ char c;
+ int comment_count;
+ comment_count = 1;
+
+ while(index < (int)self->code.size) {
+ c = self->code.data[index];
+ if(c == '*') {
+ if(index - 1 >= 0 && self->code.data[index - 1] == '/') {
+ ++comment_count;
+ }
+ } else if(c == '/') {
+ if(index - 1 >= 0 && self->code.data[index - 1] == '*') {
+ --comment_count;
+ if(comment_count == 0)
+ return index + 1;
+ }
+ }
+ ++index;
+ }
+ return -1;
+}
+
void tokenizer_print_error(Tokenizer *self, const char *fmt, ...) {
va_list args;
int line_start;