diff options
| author | gingerBill <bill@gingerbill.org> | 2021-09-06 20:15:59 +0100 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2021-09-06 20:15:59 +0100 |
| commit | 0434281f734db10ff17b68395e593d4a07b03d8d (patch) | |
| tree | e4f4ceaa09f7114121ef67215780206959245c65 /src | |
| parent | 3bf005bfc553ba59d1869c8eab66123748af0b00 (diff) | |
Strip semicolons; Make `odin strip-semicolon` replace `..` with `..=` if used as a binary operator
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.cpp | 32 | ||||
| -rw-r--r-- | src/parser.cpp | 4 | ||||
| -rw-r--r-- | src/tokenizer.cpp | 3 |
3 files changed, 35 insertions, 4 deletions
diff --git a/src/main.cpp b/src/main.cpp index 8df722437..72adfc908 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2026,7 +2026,7 @@ gbFileError write_file_with_stripped_tokens(gbFile *f, AstFile *file, i64 *writt i32 const end_offset = cast(i32)(file->tokenizer.end - file->tokenizer.start); for_array(i, file->tokens) { Token *token = &file->tokens[i]; - if (token->flags & TokenFlag_Remove) { + if (token->flags & (TokenFlag_Remove|TokenFlag_Replace)) { i32 offset = token->pos.offset; i32 to_write = offset-prev_offset; if (!gb_file_write(f, file_data+prev_offset, to_write)) { @@ -2034,6 +2034,16 @@ gbFileError write_file_with_stripped_tokens(gbFile *f, AstFile *file, i64 *writt } written += to_write; prev_offset = token_pos_end(*token).offset; + } + if (token->flags & TokenFlag_Replace) { + if (token->kind == Token_Ellipsis) { + if (!gb_file_write(f, "..=", 3)) { + return gbFileError_Invalid; + } + written += 3; + } else { + return gbFileError_Invalid; + } } } if (end_offset > prev_offset) { @@ -2054,7 +2064,6 @@ int strip_semicolons(Parser *parser) { AstPackage *pkg = parser->packages[i]; file_count += pkg->files.count; } - gb_printf_err("File count to be stripped of unneeded tokens: %td\n", file_count); auto generated_files = array_make<StripSemicolonFile>(permanent_allocator(), 0, file_count); @@ -2062,6 +2071,20 @@ int strip_semicolons(Parser *parser) { AstPackage *pkg = parser->packages[i]; for_array(j, pkg->files) { AstFile *file = pkg->files[j]; + + bool nothing_to_change = true; + for_array(i, file->tokens) { + Token *token = &file->tokens[i]; + if (token->flags) { + nothing_to_change = false; + break; + } + } + + if (nothing_to_change) { + continue; + } + String old_fullpath = copy_string(permanent_allocator(), file->fullpath); // assumes .odin extension @@ -2074,6 +2097,9 @@ int strip_semicolons(Parser *parser) { } } + gb_printf_err("File count to be stripped of unneeded tokens: %td\n", generated_files.count); + + isize generated_count = 0; bool failed = false; @@ -2169,7 +2195,7 @@ int strip_semicolons(Parser *parser) { } } - gb_printf_err("Files stripped of unneeded token: %td\n", file_count); + gb_printf_err("Files stripped of unneeded token: %td\n", generated_files.count); return cast(int)failed; diff --git a/src/parser.cpp b/src/parser.cpp index 4433a0a7f..778afbcd6 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -1376,6 +1376,10 @@ Token expect_operator(AstFile *f) { syntax_error(f->curr_token, "Expected an non-range operator, got '%.*s'", LIT(p)); } + if (f->curr_token.kind == Token_Ellipsis) { + f->tokens[f->curr_token_index].flags |= TokenFlag_Replace; + } + advance_token(f); return prev; } diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp index 3a3c40a7d..237179ca5 100644 --- a/src/tokenizer.cpp +++ b/src/tokenizer.cpp @@ -236,7 +236,8 @@ TokenPos token_pos_add_column(TokenPos pos) { } enum TokenFlag : u8 { - TokenFlag_Remove = 1<<1, + TokenFlag_Remove = 1<<1, + TokenFlag_Replace = 1<<2, }; struct Token { |