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

Move custom operator handling to scanner.c #220

Merged
merged 2 commits into from
Sep 4, 2022
Merged
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions .github/workflows/top-repos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ jobs:
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
Expand Down
21 changes: 21 additions & 0 deletions corpus/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1142,3 +1142,24 @@ async(async: async, qos: qos, flags: flags) {
(simple_identifier)
(call_suffix
(value_arguments))))))))

================================================================================
Assigning to the result of a force unwrap
================================================================================

stat[lang]! += 1

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

(source_file
(assignment
(directly_assignable_expression
(postfix_expression
(call_expression
(simple_identifier)
(call_suffix
(value_arguments
(value_argument
(simple_identifier)))))
(bang)))
(integer_literal)))
6 changes: 5 additions & 1 deletion corpus/functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ precedencegroup MyPrecedence {

infix operator -=- : MyPrecedence

infix operator •

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

(source_file
Expand All @@ -371,7 +373,9 @@ infix operator -=- : MyPrecedence
(simple_identifier))))
(operator_declaration
(custom_operator)
(simple_identifier)))
(simple_identifier))
(operator_declaration
(custom_operator)))

================================================================================
Custom operator with another operator as a prefix
Expand Down
46 changes: 14 additions & 32 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,6 @@ if (tree_sitter_version_supports_emoji()) {
LEXICAL_IDENTIFIER = /[_\p{XID_Start}][_\p{XID_Continue}]*/;
}

const CUSTOM_OPERATORS = token(
choice(
// https://docs.swift.org/swift-book/ReferenceManual/LexicalStructure.html#ID418
// This supports a subset of the operators that Swift does but I'm really not concerned about the esoteric ones.
// Someone who wants unicode support can add it. What this does do is:
// * Avoid the reserved operators by saying that certain characters are only available if you don't start with them.
// * Entirely forbid `<` as the last char because it creates ambiguity with type arguments
/[\\<>&?=][\/=\-+!*%<>&|^?~\.]*[\/=\-+!*%>&|^?~]+/,
/[\-+!*%|^~]+[\/=\-+!*%<>&|^?~]*[\/=\-+!*%>&|^?~]+/,
/[\-+!*%|^~\.]+[\/=\-+!*%<>&|^?~\.]*[\/=\-+!*%>&|^?~\.]+/,
/[\/]+[=\-+!*%<>&|^?~]*[=\-+!*%>&|^?~]+/,
/[\/]+[=\-+!*%<>&|^?~\.]*[=\-+!*%>&|^?~\.]+/
)
);
// XXX need custom scanner for:
// * Custom operators and `<` for type arguments
module.exports = grammar({
name: "swift",
conflicts: ($) => [
Expand All @@ -115,12 +99,10 @@ module.exports = grammar({
// After a `{` in a function or switch context, it's ambigous whether we're starting a set of local statements or
// applying some modifiers to a capture or pattern.
[$.modifiers],
// Custom operators get weird special handling for `<` characters in silly stuff like `func =<<<<T>(...)`
[$.custom_operator],
[$._prefix_unary_operator, $._referenceable_operator],
// `+(...)` is ambigously either "call the function produced by a reference to the operator `+`" or "use the unary
// operator `+` on the result of the parenthetical expression."
[$._additive_operator, $._prefix_unary_operator],
[$._referenceable_operator, $._prefix_unary_operator],
// `{ [self, b, c] ...` could be a capture list or an array literal depending on what else happens.
[$.capture_list_item, $.self_expression],
[$.capture_list_item, $._expression],
Expand Down Expand Up @@ -249,6 +231,7 @@ module.exports = grammar({
$._as_quest_custom,
$._as_bang_custom,
$._async_keyword_custom,
$._custom_operator,
],
inline: ($) => [$._locally_permitted_modifiers],
rules: {
Expand Down Expand Up @@ -658,7 +641,7 @@ module.exports = grammar({
field("rhs", $._expr_hack_at_ternary_binary_suffix)
)
),
custom_operator: ($) => seq(CUSTOM_OPERATORS, optional("<")),
custom_operator: ($) => choice(token(/[\/]+[*]+/), $._custom_operator),
// Suffixes
navigation_suffix: ($) =>
seq(
Expand Down Expand Up @@ -870,7 +853,7 @@ module.exports = grammar({
prec.left(
PRECS.lambda,
seq(
"{",
choice("{", "^{"),
optional($._lambda_type_declaration),
optional($.statements),
"}"
Expand Down Expand Up @@ -1066,7 +1049,8 @@ module.exports = grammar({
$.navigation_expression,
$.call_expression,
$.tuple_expression,
$.self_expression
$.self_expression,
$.postfix_expression // Since `x[...]! = y` is legal
),
////////////////////////////////
// Statements - https://docs.swift.org/swift-book/ReferenceManual/Statements.html
Expand Down Expand Up @@ -1423,14 +1407,7 @@ module.exports = grammar({
_non_constructor_function_decl: ($) =>
seq(
"func",
field(
"name",
choice(
$.simple_identifier,
$._referenceable_operator,
$._bitwise_binary_operator
)
)
field("name", choice($.simple_identifier, $._referenceable_operator))
),
_referenceable_operator: ($) =>
choice(
Expand All @@ -1440,10 +1417,15 @@ module.exports = grammar({
$._multiplicative_operator,
$._equality_operator,
$._comparison_operator,
$._assignment_and_operator,
"++",
"--",
$.bang,
"~"
"~",
"|",
"^",
"<<",
">>"
),
// Hide the fact that certain symbols come from the custom scanner by aliasing them to their
// string variants. This keeps us from having to see them in the syntax tree (which would be
Expand Down Expand Up @@ -1587,7 +1569,7 @@ module.exports = grammar({
seq(
choice("prefix", "infix", "postfix"),
"operator",
$.custom_operator,
$._referenceable_operator,
optional(seq(":", $.simple_identifier)),
optional($.deprecated_operator_declaration_body)
),
Expand Down
2 changes: 1 addition & 1 deletion script-data/known_failures.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
firefox-ios/Shared/Functions.swift
ReactKit/ReactKitTests/OperationTests.swift
10 changes: 10 additions & 0 deletions script-data/top-repositories.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,13 @@ Nuke kean/Nuke 11.1.1
Swinject Swinject/Swinject 2.8.2
GRDB groue/GRDB.swift v6.0.0-beta.3 0 2
GRDB groue/GRDB.swift v6.0.0-beta.3 1 2
Dance saoudrizwan/Dance v1.0.7
StyleKit 146BC/StyleKit 0.7.0
ReactKit ReactKit/ReactKit 0.12.0
JASON delba/JASON 3.1.1
Side-Menu Yalantis/Side-Menu.iOS 2.0.2
C4iOS C4Labs/C4iOS 3.0.1
Taylor izqui/Taylor 0.4.5
Runes thoughtbot/Runes v5.1.0
Overdrive saidsikira/Overdrive 0.3
Tactile delba/Tactile 3.0.1
Loading