From 1662effca041691364de3a509ae4a34fab52b704 Mon Sep 17 00:00:00 2001 From: Mark McCulloh Date: Mon, 9 Oct 2023 11:00:25 -0400 Subject: [PATCH] chore: format tree-sitter scanner and remove implicit switch fallthrough (#4476) *By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*. --- libs/tree-sitter-wing/src/scanner.c | 153 ++++++++++++---------------- 1 file changed, 66 insertions(+), 87 deletions(-) diff --git a/libs/tree-sitter-wing/src/scanner.c b/libs/tree-sitter-wing/src/scanner.c index 02ee7e61323..c4446cd20d8 100644 --- a/libs/tree-sitter-wing/src/scanner.c +++ b/libs/tree-sitter-wing/src/scanner.c @@ -3,26 +3,30 @@ // Added comments #include "tree_sitter/parser.h" + #include -enum TokenType -{ +enum TokenType { AUTOMATIC_SEMICOLON, AUTOMATIC_BLOCK, }; -void *tree_sitter_wing_external_scanner_create() { return NULL; } -void tree_sitter_wing_external_scanner_destroy(void *p) {} -void tree_sitter_wing_external_scanner_reset(void *p) {} -unsigned tree_sitter_wing_external_scanner_serialize(void *p, char *buffer) { return 0; } -void tree_sitter_wing_external_scanner_deserialize(void *p, const char *b, unsigned n) {} +void * tree_sitter_wing_external_scanner_create() { + return NULL; +} +void tree_sitter_wing_external_scanner_destroy(void * p) {} +void tree_sitter_wing_external_scanner_reset(void * p) {} +unsigned tree_sitter_wing_external_scanner_serialize(void * p, char * buffer) { + return 0; +} +void tree_sitter_wing_external_scanner_deserialize(void * p, + const char * b, unsigned n) {} /** * Skip the current token and move to the next one (does not add consumed char to the current token) */ -static void skip(TSLexer *lexer) -{ - lexer->advance(lexer, true); +static void skip(TSLexer * lexer) { + lexer -> advance(lexer, true); } /** @@ -31,54 +35,37 @@ static void skip(TSLexer *lexer) * * @return true if we've reached a non-whitespace/comment character, false otherwise (comment is unterminated) */ -static bool scan_whitespace_and_comments(TSLexer *lexer) -{ - for (;;) - { - while (iswspace(lexer->lookahead)) - { +static bool scan_whitespace_and_comments(TSLexer * lexer) { + for (;;) { + while (iswspace(lexer -> lookahead)) { skip(lexer); } - if (lexer->lookahead == '/') - { + if (lexer -> lookahead == '/') { skip(lexer); - if (lexer->lookahead == '/') - { + if (lexer -> lookahead == '/') { skip(lexer); - while (lexer->lookahead != 0 && lexer->lookahead != '\n') - { + while (lexer -> lookahead != 0 && lexer -> lookahead != '\n') { skip(lexer); } - } - else if (lexer->lookahead == '*') - { + } else if (lexer -> lookahead == '*') { skip(lexer); - while (lexer->lookahead != 0) - { - if (lexer->lookahead == '*') - { + while (lexer -> lookahead != 0) { + if (lexer -> lookahead == '*') { skip(lexer); - if (lexer->lookahead == '/') - { + if (lexer -> lookahead == '/') { skip(lexer); break; } - } - else - { + } else { skip(lexer); } } - } - else - { + } else { return false; } - } - else - { + } else { return true; } } @@ -89,8 +76,7 @@ static bool scan_whitespace_and_comments(TSLexer *lexer) * * @return true if the character is a valid identifier character, false otherwise */ -static bool iswidentifier(int c) -{ +static bool iswidentifier(int c) { return iswalnum(c) || c == '_'; } @@ -99,22 +85,20 @@ static bool iswidentifier(int c) * * @return true if an automatic semicolon was found (aka "inserted"), false otherwise */ -static bool scan_automatic_semicolon(TSLexer *lexer) -{ - lexer->result_symbol = AUTOMATIC_SEMICOLON; - lexer->mark_end(lexer); - - for (;;) - { - if (lexer->lookahead == 0) +static bool scan_automatic_semicolon(TSLexer * lexer) { + lexer -> result_symbol = AUTOMATIC_SEMICOLON; + lexer -> mark_end(lexer); + + for (;;) { + if (lexer -> lookahead == 0) return true; - if (lexer->lookahead == '}') + if (lexer -> lookahead == '}') return true; - if (lexer->is_at_included_range_start(lexer)) + if (lexer -> is_at_included_range_start(lexer)) return true; - if (lexer->lookahead == '\n') + if (lexer -> lookahead == '\n') break; - if (!iswspace(lexer->lookahead)) + if (!iswspace(lexer -> lookahead)) return false; skip(lexer); } @@ -124,8 +108,7 @@ static bool scan_automatic_semicolon(TSLexer *lexer) if (!scan_whitespace_and_comments(lexer)) return false; - switch (lexer->lookahead) - { + switch (lexer -> lookahead) { case ',': case '.': case ':': @@ -144,38 +127,38 @@ static bool scan_automatic_semicolon(TSLexer *lexer) case '/': return false; - // Insert a semicolon before `--` and `++`, but not before binary `+` or `-`. + // Insert a semicolon before `--` and `++`, but not before binary `+` or `-`. case '+': skip(lexer); - return lexer->lookahead == '+'; + return lexer -> lookahead == '+'; case '-': skip(lexer); - return lexer->lookahead == '-'; + return lexer -> lookahead == '-'; - // Don't insert a semicolon before `!=`, but do insert one before a unary `!`. + // Don't insert a semicolon before `!=`, but do insert one before a unary `!`. case '!': skip(lexer); - return lexer->lookahead != '='; + return lexer -> lookahead != '='; - // Don't insert a semicolon before `in` (unless it's part of an identifier) + // Don't insert a semicolon before `in` (unless it's part of an identifier) case 'i': skip(lexer); - if (lexer->lookahead == 'n') - { + if (lexer -> lookahead == 'n') { skip(lexer); - if (!iswidentifier(lexer->lookahead)) + if (!iswidentifier(lexer -> lookahead)) return false; } - - // Don't insert a semicolon before `as` (unless it's part of an identifier) + break; + + // Don't insert a semicolon before `as` (unless it's part of an identifier) case 'a': skip(lexer); - if (lexer->lookahead == 's') - { + if (lexer -> lookahead == 's') { skip(lexer); - if (!iswidentifier(lexer->lookahead)) + if (!iswidentifier(lexer -> lookahead)) return false; } + break; } return true; @@ -186,20 +169,18 @@ static bool scan_automatic_semicolon(TSLexer *lexer) * * @return true if an automatic block was found (aka "inserted"), false otherwise */ -static bool scan_automatic_block(TSLexer *lexer) -{ - lexer->result_symbol = AUTOMATIC_BLOCK; - lexer->mark_end(lexer); - - for (;;) - { - if (lexer->lookahead == 0) +static bool scan_automatic_block(TSLexer * lexer) { + lexer -> result_symbol = AUTOMATIC_BLOCK; + lexer -> mark_end(lexer); + + for (;;) { + if (lexer -> lookahead == 0) return true; - if (lexer->lookahead == '}') + if (lexer -> lookahead == '}') return false; - if (lexer->is_at_included_range_start(lexer)) + if (lexer -> is_at_included_range_start(lexer)) return true; - if (!iswspace(lexer->lookahead)) + if (!iswspace(lexer -> lookahead)) return false; skip(lexer); } @@ -209,7 +190,7 @@ static bool scan_automatic_block(TSLexer *lexer) if (!scan_whitespace_and_comments(lexer)) return false; - if (lexer->lookahead != '{') + if (lexer -> lookahead != '{') return false; return true; @@ -220,11 +201,9 @@ static bool scan_automatic_block(TSLexer *lexer) * * @return true if the external scanner was able to scan a token, false otherwise */ -bool tree_sitter_wing_external_scanner_scan(void *payload, TSLexer *lexer, - const bool *valid_symbols) -{ - if (valid_symbols[AUTOMATIC_SEMICOLON]) - { +bool tree_sitter_wing_external_scanner_scan(void * payload, TSLexer * lexer, + const bool * valid_symbols) { + if (valid_symbols[AUTOMATIC_SEMICOLON]) { bool ret = scan_automatic_semicolon(lexer); return ret; } else if (valid_symbols[AUTOMATIC_BLOCK]) {