Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add next token #17

Merged
merged 7 commits into from
Jun 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/lexer/lex.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,48 @@ int unlex(Lexer *l, Token *t) {
return 0;
}

int skip_to_token(Lexer *l) {
char cur, prev;
int in_block = 0, pass = 0;

// Read the first character
if ((cur = fgetc(l->fp)) != EOF) {
prev = cur;
if (!(cur == ' ' || cur == '\t' || cur == '/')) {
fseek(l->fp, -1, SEEK_CUR);
return 0; // Token begins immediately
}
} else {
return -1; // File done, no more tokens
}

// Read each character from the file until EOF
while ((cur = fgetc(l->fp)) != EOF) {
if (cur == '/' && prev == '/' && in_block == 0) {
in_block = 1; // Single line comment
} else if (cur == '*' && prev == '/' && in_block == 0) {
in_block = 2; // Block comment
pass = 2;
} else if ((in_block == 1 && cur == '\n') ||
(in_block == 2 && cur == '/' && prev == '*' && pass <= 0)) {
in_block = 0; // Out of comment
} else if (prev == '/' && !(cur == '*' || cur == '/') && in_block == 0) {
fseek(l->fp, -1, SEEK_CUR);
return 0; // Token was a slash without a * or / following it
}

if (!(cur == ' ' || cur == '\t' || cur == '/') && in_block == 0) {
fseek(l->fp, -1, SEEK_CUR);
return 0; // Token is next
}

pass -= 1;
prev = cur;
}

return -1; // EOF was reached
}

TokenType ttype_one_char(char c) {
switch (c) {
case '(':
Expand Down
Loading