Skip to content

Commit

Permalink
fix: automatically semicolon incorrectly inserted into as and in
Browse files Browse the repository at this point in the history
…expressions (#4254)

Fixes #4247

*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 Sep 22, 2023
1 parent f676dad commit aa5ca2f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
30 changes: 30 additions & 0 deletions libs/tree-sitter-wing/src/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ static bool scan_whitespace_and_comments(TSLexer *lexer)
}
}

/**
* Check if a character is a valid identifier character
*
* @return true if the character is a valid identifier character, false otherwise
*/
static bool iswidentifier(int c)
{
return iswalnum(c) || c == '_';
}

/**
* Check if an automatic semicolon could be inserted
*
Expand Down Expand Up @@ -146,6 +156,26 @@ static bool scan_automatic_semicolon(TSLexer *lexer)
case '!':
skip(lexer);
return lexer->lookahead != '=';

// Don't insert a semicolon before `in` (unless it's part of an identifier)
case 'i':
skip(lexer);
if (lexer->lookahead == 'n')
{
skip(lexer);
if (!iswidentifier(lexer->lookahead))
return false;
}

// Don't insert a semicolon before `as` (unless it's part of an identifier)
case 'a':
skip(lexer);
if (lexer->lookahead == 's')
{
skip(lexer);
if (!iswidentifier(lexer->lookahead))
return false;
}
}

return true;
Expand Down
22 changes: 22 additions & 0 deletions libs/tree-sitter-wing/test/corpus/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,28 @@ new A() as "b" in c;

--------------------------------------------------------------------------------

(source
(expression_statement
(new_expression
class: (custom_type
object: (type_identifier))
args: (argument_list)
id: (string)
scope: (reference
(reference_identifier)))))

================================================================================
New expression with id and scope on different lines
================================================================================

new A()
as
"b"
in
c;

--------------------------------------------------------------------------------

(source
(expression_statement
(new_expression
Expand Down

0 comments on commit aa5ca2f

Please sign in to comment.