Skip to content

Commit

Permalink
chore: format tree-sitter scanner and remove implicit switch fallthro…
Browse files Browse the repository at this point in the history
…ugh (winglang#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)*.
  • Loading branch information
MarkMcCulloh authored Oct 9, 2023
1 parent 9700b4d commit 1662eff
Showing 1 changed file with 66 additions and 87 deletions.
153 changes: 66 additions & 87 deletions libs/tree-sitter-wing/src/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,30 @@
// Added comments

#include "tree_sitter/parser.h"

#include <wctype.h>

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);
}

/**
Expand All @@ -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;
}
}
Expand All @@ -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 == '_';
}

Expand All @@ -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);
}
Expand All @@ -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 ':':
Expand All @@ -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;
Expand All @@ -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);
}
Expand All @@ -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;
Expand All @@ -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]) {
Expand Down

0 comments on commit 1662eff

Please sign in to comment.