From 72b683d63c610b1540e9440926c8a1e480760460 Mon Sep 17 00:00:00 2001 From: Asger F Date: Wed, 13 May 2026 00:01:18 +0200 Subject: [PATCH] Unified: Add Swift corpus tests Add corpus test cases for Swift covering closures, collections, control flow, functions, literals, loops, operators, optionals/errors, types, and variables. Update existing desugar.txt with raw parse sections. Note: operator nodes currently render their node ID instead of the actual operator text (e.g. operator "3" instead of operator "+"). This will be fixed in the next commit. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../extractor/tests/corpus/swift/closures.txt | 188 +++++++ .../tests/corpus/swift/collections.txt | 234 +++++++++ .../tests/corpus/swift/control-flow.txt | 333 ++++++++++++ .../extractor/tests/corpus/swift/desugar.txt | 26 +- .../tests/corpus/swift/functions.txt | 297 +++++++++++ .../extractor/tests/corpus/swift/literals.txt | 121 +++++ .../extractor/tests/corpus/swift/loops.txt | 208 ++++++++ .../tests/corpus/swift/operators.txt | 264 ++++++++++ .../corpus/swift/optionals-and-errors.txt | 236 +++++++++ .../extractor/tests/corpus/swift/types.txt | 490 ++++++++++++++++++ .../tests/corpus/swift/variables.txt | 187 +++++++ 11 files changed, 2582 insertions(+), 2 deletions(-) create mode 100644 unified/extractor/tests/corpus/swift/closures.txt create mode 100644 unified/extractor/tests/corpus/swift/collections.txt create mode 100644 unified/extractor/tests/corpus/swift/control-flow.txt create mode 100644 unified/extractor/tests/corpus/swift/functions.txt create mode 100644 unified/extractor/tests/corpus/swift/literals.txt create mode 100644 unified/extractor/tests/corpus/swift/loops.txt create mode 100644 unified/extractor/tests/corpus/swift/operators.txt create mode 100644 unified/extractor/tests/corpus/swift/optionals-and-errors.txt create mode 100644 unified/extractor/tests/corpus/swift/types.txt create mode 100644 unified/extractor/tests/corpus/swift/variables.txt diff --git a/unified/extractor/tests/corpus/swift/closures.txt b/unified/extractor/tests/corpus/swift/closures.txt new file mode 100644 index 00000000000..08af80c3f65 --- /dev/null +++ b/unified/extractor/tests/corpus/swift/closures.txt @@ -0,0 +1,188 @@ +=== +Closure with explicit parameters +=== + +let f = { (x: Int) -> Int in x * 2 } + +--- + +source_file + property_declaration + name: + pattern + bound_identifier: simple_identifier "f" + value: + lambda_literal + type: + lambda_function_type + name: + user_type + type_identifier "Int" + lambda_function_type_parameters + lambda_parameter + name: + simple_identifier "x" + user_type + type_identifier "Int" + statements + multiplicative_expression + lhs: simple_identifier "x" + op: * + rhs: integer_literal "2" + value_binding_pattern + mutability: let + +--- + +top_level + body: unsupported_node "let f = { (x: Int) -> Int in x * 2 }" + +=== +Closure with shorthand parameters +=== + +let f = { $0 + $1 } + +--- + +source_file + property_declaration + name: + pattern + bound_identifier: simple_identifier "f" + value: + lambda_literal + statements + additive_expression + lhs: simple_identifier "$0" + op: + + rhs: simple_identifier "$1" + value_binding_pattern + mutability: let + +--- + +top_level + body: unsupported_node "let f = { $0 + $1 }" + +=== +Trailing closure +=== + +xs.map { $0 * 2 } + +--- + +source_file + call_expression + navigation_expression + suffix: + navigation_suffix + suffix: simple_identifier "map" + target: simple_identifier "xs" + call_suffix + lambda_literal + statements + multiplicative_expression + lhs: simple_identifier "$0" + op: * + rhs: integer_literal "2" + +--- + +top_level + body: unsupported_node "xs.map { $0 * 2 }" + +=== +Closure with capture list +=== + +let f = { [weak self] in self?.doThing() } + +--- + +source_file + property_declaration + name: + pattern + bound_identifier: simple_identifier "f" + value: + lambda_literal + captures: + capture_list + capture_list_item + name: simple_identifier "self" + ownership_modifier + statements + call_expression + navigation_expression + suffix: + navigation_suffix + suffix: simple_identifier "doThing" + target: + self_expression + ? + call_suffix + value_arguments + value_binding_pattern + mutability: let + +--- + +top_level + body: unsupported_node "let f = { [weak self] in self?.doThing() }" + +=== +Multi-statement closure +=== + +let f = { (x: Int) -> Int in + let y = x + 1 + return y * 2 +} + +--- + +source_file + property_declaration + name: + pattern + bound_identifier: simple_identifier "f" + value: + lambda_literal + type: + lambda_function_type + name: + user_type + type_identifier "Int" + lambda_function_type_parameters + lambda_parameter + name: + simple_identifier "x" + user_type + type_identifier "Int" + statements + property_declaration + name: + pattern + bound_identifier: simple_identifier "y" + value: + additive_expression + lhs: simple_identifier "x" + op: + + rhs: integer_literal "1" + value_binding_pattern + mutability: let + control_transfer_statement + result: + multiplicative_expression + lhs: simple_identifier "y" + op: * + rhs: integer_literal "2" + value_binding_pattern + mutability: let + +--- + +top_level + body: unsupported_node "let f = { (x: Int) -> Int in\n let y = x + 1\n return y * 2\n}" diff --git a/unified/extractor/tests/corpus/swift/collections.txt b/unified/extractor/tests/corpus/swift/collections.txt new file mode 100644 index 00000000000..76015e75baa --- /dev/null +++ b/unified/extractor/tests/corpus/swift/collections.txt @@ -0,0 +1,234 @@ +=== +Array literal +=== + +let xs = [1, 2, 3] + +--- + +source_file + property_declaration + name: + pattern + bound_identifier: simple_identifier "xs" + value: + array_literal + element: + integer_literal "1" + integer_literal "2" + integer_literal "3" + value_binding_pattern + mutability: let + +--- + +top_level + body: unsupported_node "let xs = [1, 2, 3]" + +=== +Empty array literal with type +=== + +let xs: [Int] = [] + +--- + +source_file + property_declaration + name: + pattern + bound_identifier: simple_identifier "xs" + value: + array_literal + value_binding_pattern + mutability: let + type_annotation + name: + array_type + name: + user_type + type_identifier "Int" + +--- + +top_level + body: unsupported_node "let xs: [Int] = []" + +=== +Dictionary literal +=== + +let d = ["a": 1, "b": 2] + +--- + +source_file + property_declaration + name: + pattern + bound_identifier: simple_identifier "d" + value: + dictionary_literal + key: + line_string_literal + text: line_str_text "a" + line_string_literal + text: line_str_text "b" + value: + integer_literal "1" + integer_literal "2" + value_binding_pattern + mutability: let + +--- + +top_level + body: unsupported_node "let d = [\"a\": 1, \"b\": 2]" + +=== +Set literal +=== + +let s: Set = [1, 2, 3] + +--- + +source_file + property_declaration + name: + pattern + bound_identifier: simple_identifier "s" + value: + array_literal + element: + integer_literal "1" + integer_literal "2" + integer_literal "3" + value_binding_pattern + mutability: let + type_annotation + name: + user_type + type_identifier "Set" + type_arguments + name: + user_type + type_identifier "Int" + +--- + +top_level + body: unsupported_node "let s: Set = [1, 2, 3]" + +=== +Tuple literal +=== + +let t = (1, "two", 3.0) + +--- + +source_file + property_declaration + name: + pattern + bound_identifier: simple_identifier "t" + value: + tuple_expression + value: + integer_literal "1" + line_string_literal + text: line_str_text "two" + real_literal "3.0" + value_binding_pattern + mutability: let + +--- + +top_level + body: unsupported_node "let t = (1, \"two\", 3.0)" + +=== +Subscript access +=== + +let first = xs[0] + +--- + +source_file + property_declaration + name: + pattern + bound_identifier: simple_identifier "first" + value: + call_expression + simple_identifier "xs" + call_suffix + value_arguments + value_argument + value: integer_literal "0" + value_binding_pattern + mutability: let + +--- + +top_level + body: unsupported_node "let first = xs[0]" + +=== +Dictionary subscript +=== + +let v = d["key"] + +--- + +source_file + property_declaration + name: + pattern + bound_identifier: simple_identifier "v" + value: + call_expression + simple_identifier "d" + call_suffix + value_arguments + value_argument + value: + line_string_literal + text: line_str_text "key" + value_binding_pattern + mutability: let + +--- + +top_level + body: unsupported_node "let v = d[\"key\"]" + +=== +Tuple member access +=== + +let n = t.0 + +--- + +source_file + property_declaration + name: + pattern + bound_identifier: simple_identifier "n" + value: + navigation_expression + suffix: + navigation_suffix + suffix: integer_literal "0" + target: simple_identifier "t" + value_binding_pattern + mutability: let + +--- + +top_level + body: unsupported_node "let n = t.0" diff --git a/unified/extractor/tests/corpus/swift/control-flow.txt b/unified/extractor/tests/corpus/swift/control-flow.txt new file mode 100644 index 00000000000..ec54f9481f1 --- /dev/null +++ b/unified/extractor/tests/corpus/swift/control-flow.txt @@ -0,0 +1,333 @@ +=== +If statement +=== + +if x > 0 { + print(x) +} + +--- + +source_file + if_statement + condition: + comparison_expression + lhs: simple_identifier "x" + op: > + rhs: integer_literal "0" + statements + call_expression + simple_identifier "print" + call_suffix + value_arguments + value_argument + value: simple_identifier "x" + +--- + +top_level + body: unsupported_node "if x > 0 {\n print(x)\n}" + +=== +If-else +=== + +if x > 0 { + print(x) +} else { + print(-x) +} + +--- + +source_file + if_statement + condition: + comparison_expression + lhs: simple_identifier "x" + op: > + rhs: integer_literal "0" + statements + call_expression + simple_identifier "print" + call_suffix + value_arguments + value_argument + value: simple_identifier "x" + else "else" + statements + call_expression + simple_identifier "print" + call_suffix + value_arguments + value_argument + value: + prefix_expression + operation: - + target: simple_identifier "x" + +--- + +top_level + body: unsupported_node "if x > 0 {\n print(x)\n} else {\n print(-x)\n}" + +=== +If-else-if chain +=== + +if x > 0 { + print(1) +} else if x < 0 { + print(2) +} else { + print(3) +} + +--- + +source_file + if_statement + condition: + comparison_expression + lhs: simple_identifier "x" + op: > + rhs: integer_literal "0" + statements + call_expression + simple_identifier "print" + call_suffix + value_arguments + value_argument + value: integer_literal "1" + else "else" + if_statement + condition: + comparison_expression + lhs: simple_identifier "x" + op: < + rhs: integer_literal "0" + statements + call_expression + simple_identifier "print" + call_suffix + value_arguments + value_argument + value: integer_literal "2" + else "else" + statements + call_expression + simple_identifier "print" + call_suffix + value_arguments + value_argument + value: integer_literal "3" + +--- + +top_level + body: unsupported_node "if x > 0 {\n print(1)\n} else if x < 0 {\n print(2)\n} else {\n print(3)\n}" + +=== +If-let optional binding +=== + +if let value = optional { + print(value) +} + +--- + +source_file + if_statement + bound_identifier: simple_identifier "value" + condition: + value_binding_pattern + mutability: let + = + simple_identifier "optional" + statements + call_expression + simple_identifier "print" + call_suffix + value_arguments + value_argument + value: simple_identifier "value" + +--- + +top_level + body: unsupported_node "if let value = optional {\n print(value)\n}" + +=== +Guard let +=== + +guard let value = optional else { return } + +--- + +source_file + guard_statement + bound_identifier: simple_identifier "value" + condition: + value_binding_pattern + mutability: let + = + simple_identifier "optional" + else "else" + statements + control_transfer_statement + +--- + +top_level + body: unsupported_node "guard let value = optional else { return }" + +=== +Ternary expression +=== + +let y = x > 0 ? 1 : -1 + +--- + +source_file + property_declaration + name: + pattern + bound_identifier: simple_identifier "y" + value: + ternary_expression + condition: + comparison_expression + lhs: simple_identifier "x" + op: > + rhs: integer_literal "0" + if_false: + prefix_expression + operation: - + target: integer_literal "1" + if_true: integer_literal "1" + value_binding_pattern + mutability: let + +--- + +top_level + body: unsupported_node "let y = x > 0 ? 1 : -1" + +=== +Switch statement +=== + +switch x { +case 1: + print("one") +case 2, 3: + print("two or three") +default: + print("other") +} + +--- + +source_file + switch_statement + expr: simple_identifier "x" + switch_entry + switch_pattern + pattern + integer_literal "1" + statements + call_expression + simple_identifier "print" + call_suffix + value_arguments + value_argument + value: + line_string_literal + text: line_str_text "one" + switch_entry + switch_pattern + pattern + integer_literal "2" + switch_pattern + pattern + integer_literal "3" + statements + call_expression + simple_identifier "print" + call_suffix + value_arguments + value_argument + value: + line_string_literal + text: line_str_text "two or three" + switch_entry + default_keyword "default" + statements + call_expression + simple_identifier "print" + call_suffix + value_arguments + value_argument + value: + line_string_literal + text: line_str_text "other" + +--- + +top_level + body: unsupported_node "switch x {\ncase 1:\n print(\"one\")\ncase 2, 3:\n print(\"two or three\")\ndefault:\n print(\"other\")\n}" + +=== +Switch with binding pattern +=== + +switch shape { +case .circle(let r): + print(r) +case .square(let s): + print(s) +} + +--- + +source_file + switch_statement + expr: simple_identifier "shape" + switch_entry + switch_pattern + pattern + simple_identifier "circle" + pattern + bound_identifier: simple_identifier "r" + value_binding_pattern + mutability: let + statements + call_expression + simple_identifier "print" + call_suffix + value_arguments + value_argument + value: simple_identifier "r" + switch_entry + switch_pattern + pattern + simple_identifier "square" + pattern + bound_identifier: simple_identifier "s" + value_binding_pattern + mutability: let + statements + call_expression + simple_identifier "print" + call_suffix + value_arguments + value_argument + value: simple_identifier "s" + +--- + +top_level + body: unsupported_node "switch shape {\ncase .circle(let r):\n print(r)\ncase .square(let s):\n print(s)\n}" diff --git a/unified/extractor/tests/corpus/swift/desugar.txt b/unified/extractor/tests/corpus/swift/desugar.txt index 1ea0e260aad..e90fe444800 100644 --- a/unified/extractor/tests/corpus/swift/desugar.txt +++ b/unified/extractor/tests/corpus/swift/desugar.txt @@ -7,8 +7,19 @@ Additive expression is desugared --- source_file - simple_identifier "blah" + additive_expression + lhs: integer_literal "1" + op: + + rhs: integer_literal "2" +--- + +top_level + body: + binary_expr + operator: operator "3" + left: unsupported_node "1" + right: unsupported_node "2" === Another additive expression is desugared @@ -19,5 +30,16 @@ foo + bar --- source_file - simple_identifier "blah" + additive_expression + lhs: simple_identifier "foo" + op: + + rhs: simple_identifier "bar" +--- + +top_level + body: + binary_expr + operator: operator "3" + left: name_expr "foo" + right: name_expr "bar" diff --git a/unified/extractor/tests/corpus/swift/functions.txt b/unified/extractor/tests/corpus/swift/functions.txt new file mode 100644 index 00000000000..5943b75d04d --- /dev/null +++ b/unified/extractor/tests/corpus/swift/functions.txt @@ -0,0 +1,297 @@ +=== +Function with no parameters +=== + +func greet() { + print("hello") +} + +--- + +source_file + function_declaration + body: + function_body + statements + call_expression + simple_identifier "print" + call_suffix + value_arguments + value_argument + value: + line_string_literal + text: line_str_text "hello" + name: simple_identifier "greet" + +--- + +top_level + body: unsupported_node "func greet() {\n print(\"hello\")\n}" + +=== +Function with parameters and return type +=== + +func add(_ a: Int, _ b: Int) -> Int { + return a + b +} + +--- + +source_file + function_declaration + body: + function_body + statements + control_transfer_statement + result: + additive_expression + lhs: simple_identifier "a" + op: + + rhs: simple_identifier "b" + name: + simple_identifier "add" + user_type + type_identifier "Int" + parameter + external_name: simple_identifier "_" + name: + simple_identifier "a" + user_type + type_identifier "Int" + parameter + external_name: simple_identifier "_" + name: + simple_identifier "b" + user_type + type_identifier "Int" + +--- + +top_level + body: unsupported_node "func add(_ a: Int, _ b: Int) -> Int {\n return a + b\n}" + +=== +Function with named parameters +=== + +func greet(person name: String) { + print(name) +} + +--- + +source_file + function_declaration + body: + function_body + statements + call_expression + simple_identifier "print" + call_suffix + value_arguments + value_argument + value: simple_identifier "name" + name: simple_identifier "greet" + parameter + external_name: simple_identifier "person" + name: + simple_identifier "name" + user_type + type_identifier "String" + +--- + +top_level + body: unsupported_node "func greet(person name: String) {\n print(name)\n}" + +=== +Function with default parameter value +=== + +func greet(name: String = "world") { + print(name) +} + +--- + +source_file + function_declaration + body: + function_body + statements + call_expression + simple_identifier "print" + call_suffix + value_arguments + value_argument + value: simple_identifier "name" + default_value: + line_string_literal + text: line_str_text "world" + name: simple_identifier "greet" + parameter + name: + simple_identifier "name" + user_type + type_identifier "String" + +--- + +top_level + body: unsupported_node "func greet(name: String = \"world\") {\n print(name)\n}" + +=== +Variadic function +=== + +func sum(_ values: Int...) -> Int { + return values.reduce(0, +) +} + +--- + +source_file + function_declaration + body: + function_body + statements + control_transfer_statement + result: + call_expression + navigation_expression + suffix: + navigation_suffix + suffix: simple_identifier "reduce" + target: simple_identifier "values" + call_suffix + value_arguments + value_argument + value: integer_literal "0" + value_argument + value: + + name: + simple_identifier "sum" + user_type + type_identifier "Int" + parameter + external_name: simple_identifier "_" + name: + simple_identifier "values" + user_type + type_identifier "Int" + +--- + +top_level + body: unsupported_node "func sum(_ values: Int...) -> Int {\n return values.reduce(0, +)\n}" + +=== +Function call +=== + +foo(1, 2) + +--- + +source_file + call_expression + simple_identifier "foo" + call_suffix + value_arguments + value_argument + value: integer_literal "1" + value_argument + value: integer_literal "2" + +--- + +top_level + body: unsupported_node "foo(1, 2)" + +=== +Function call with labelled arguments +=== + +greet(person: "Bob") + +--- + +source_file + call_expression + simple_identifier "greet" + call_suffix + value_arguments + value_argument + name: + value_argument_label + simple_identifier "person" + value: + line_string_literal + text: line_str_text "Bob" + +--- + +top_level + body: unsupported_node "greet(person: \"Bob\")" + +=== +Method call +=== + +list.append(1) + +--- + +source_file + call_expression + navigation_expression + suffix: + navigation_suffix + suffix: simple_identifier "append" + target: simple_identifier "list" + call_suffix + value_arguments + value_argument + value: integer_literal "1" + +--- + +top_level + body: unsupported_node "list.append(1)" + +=== +Generic function +=== + +func identity(_ x: T) -> T { + return x +} + +--- + +source_file + function_declaration + body: + function_body + statements + control_transfer_statement + result: simple_identifier "x" + name: + simple_identifier "identity" + user_type + type_identifier "T" + type_parameters + type_parameter + type_identifier "T" + parameter + external_name: simple_identifier "_" + name: + simple_identifier "x" + user_type + type_identifier "T" + +--- + +top_level + body: unsupported_node "func identity(_ x: T) -> T {\n return x\n}" diff --git a/unified/extractor/tests/corpus/swift/literals.txt b/unified/extractor/tests/corpus/swift/literals.txt new file mode 100644 index 00000000000..5e42e60e89f --- /dev/null +++ b/unified/extractor/tests/corpus/swift/literals.txt @@ -0,0 +1,121 @@ +=== +Integer literal +=== + +42 + +--- + +source_file + integer_literal "42" + +--- + +top_level + body: unsupported_node "42" + +=== +Negative integer literal +=== + +-7 + +--- + +source_file + prefix_expression + operation: - + target: integer_literal "7" + +--- + +top_level + body: unsupported_node "-7" + +=== +Floating-point literal +=== + +3.14 + +--- + +source_file + real_literal "3.14" + +--- + +top_level + body: unsupported_node "3.14" + +=== +Boolean literals +=== + +true +false + +--- + +source_file + boolean_literal + boolean_literal + +--- + +top_level + body: + unsupported_node "true" + unsupported_node "false" + +=== +Nil literal +=== + +nil + +--- + +source_file + +--- + +top_level + body: + +=== +String literal +=== + +"hello" + +--- + +source_file + line_string_literal + text: line_str_text "hello" + +--- + +top_level + body: unsupported_node "\"hello\"" + +=== +String with interpolation +=== + +"hello \(name)" + +--- + +source_file + line_string_literal + interpolation: + interpolated_expression + value: simple_identifier "name" + text: line_str_text "hello " + +--- + +top_level + body: unsupported_node "\"hello \\(name)\"" diff --git a/unified/extractor/tests/corpus/swift/loops.txt b/unified/extractor/tests/corpus/swift/loops.txt new file mode 100644 index 00000000000..68c0a86198e --- /dev/null +++ b/unified/extractor/tests/corpus/swift/loops.txt @@ -0,0 +1,208 @@ +=== +For-in over array literal +=== + +for x in [1, 2, 3] { + print(x) +} + +--- + +source_file + for_statement + collection: + array_literal + element: + integer_literal "1" + integer_literal "2" + integer_literal "3" + item: + pattern + bound_identifier: simple_identifier "x" + statements + call_expression + simple_identifier "print" + call_suffix + value_arguments + value_argument + value: simple_identifier "x" + +--- + +top_level + body: unsupported_node "for x in [1, 2, 3] {\n print(x)\n}" + +=== +For-in over range +=== + +for i in 0..<10 { + print(i) +} + +--- + +source_file + for_statement + collection: + range_expression + end: integer_literal "10" + op: ..< + start: integer_literal "0" + item: + pattern + bound_identifier: simple_identifier "i" + statements + call_expression + simple_identifier "print" + call_suffix + value_arguments + value_argument + value: simple_identifier "i" + +--- + +top_level + body: unsupported_node "for i in 0..<10 {\n print(i)\n}" + +=== +For-in with where clause +=== + +for x in xs where x > 0 { + print(x) +} + +--- + +source_file + for_statement + collection: simple_identifier "xs" + item: + pattern + bound_identifier: simple_identifier "x" + where_clause + where_keyword "where" + comparison_expression + lhs: simple_identifier "x" + op: > + rhs: integer_literal "0" + statements + call_expression + simple_identifier "print" + call_suffix + value_arguments + value_argument + value: simple_identifier "x" + +--- + +top_level + body: unsupported_node "for x in xs where x > 0 {\n print(x)\n}" + +=== +While loop +=== + +while x > 0 { + x -= 1 +} + +--- + +source_file + while_statement + condition: + comparison_expression + lhs: simple_identifier "x" + op: > + rhs: integer_literal "0" + statements + assignment + operator: -= + result: integer_literal "1" + target: + directly_assignable_expression + simple_identifier "x" + +--- + +top_level + body: unsupported_node "while x > 0 {\n x -= 1\n}" + +=== +Repeat-while loop +=== + +repeat { + x -= 1 +} while x > 0 + +--- + +source_file + repeat_while_statement + condition: + comparison_expression + lhs: simple_identifier "x" + op: > + rhs: integer_literal "0" + statements + assignment + operator: -= + result: integer_literal "1" + target: + directly_assignable_expression + simple_identifier "x" + +--- + +top_level + body: unsupported_node "repeat {\n x -= 1\n} while x > 0" + +=== +Break and continue +=== + +for x in xs { + if x < 0 { continue } + if x > 100 { break } + print(x) +} + +--- + +source_file + for_statement + collection: simple_identifier "xs" + item: + pattern + bound_identifier: simple_identifier "x" + statements + if_statement + condition: + comparison_expression + lhs: simple_identifier "x" + op: < + rhs: integer_literal "0" + statements + control_transfer_statement + if_statement + condition: + comparison_expression + lhs: simple_identifier "x" + op: > + rhs: integer_literal "100" + statements + control_transfer_statement + call_expression + simple_identifier "print" + call_suffix + value_arguments + value_argument + value: simple_identifier "x" + +--- + +top_level + body: unsupported_node "for x in xs {\n if x < 0 { continue }\n if x > 100 { break }\n print(x)\n}" diff --git a/unified/extractor/tests/corpus/swift/operators.txt b/unified/extractor/tests/corpus/swift/operators.txt new file mode 100644 index 00000000000..caa88c3cef4 --- /dev/null +++ b/unified/extractor/tests/corpus/swift/operators.txt @@ -0,0 +1,264 @@ +=== +Addition +=== + +a + b + +--- + +source_file + additive_expression + lhs: simple_identifier "a" + op: + + rhs: simple_identifier "b" + +--- + +top_level + body: + binary_expr + operator: operator "3" + left: name_expr "a" + right: name_expr "b" + +=== +Subtraction +=== + +a - b + +--- + +source_file + additive_expression + lhs: simple_identifier "a" + op: - + rhs: simple_identifier "b" + +--- + +top_level + body: + binary_expr + operator: operator "3" + left: name_expr "a" + right: name_expr "b" + +=== +Multiplication +=== + +a * b + +--- + +source_file + multiplicative_expression + lhs: simple_identifier "a" + op: * + rhs: simple_identifier "b" + +--- + +top_level + body: + binary_expr + operator: operator "3" + left: name_expr "a" + right: name_expr "b" + +=== +Division +=== + +a / b + +--- + +source_file + multiplicative_expression + lhs: simple_identifier "a" + op: / + rhs: simple_identifier "b" + +--- + +top_level + body: + binary_expr + operator: operator "3" + left: name_expr "a" + right: name_expr "b" + +=== +Operator precedence: addition and multiplication +=== + +a + b * c + +--- + +source_file + additive_expression + lhs: simple_identifier "a" + op: + + rhs: + multiplicative_expression + lhs: simple_identifier "b" + op: * + rhs: simple_identifier "c" + +--- + +top_level + body: + binary_expr + operator: operator "3" + left: name_expr "a" + right: + binary_expr + operator: operator "6" + left: name_expr "b" + right: name_expr "c" + +=== +Parenthesised expression +=== + +(a + b) * c + +--- + +source_file + multiplicative_expression + lhs: + tuple_expression + value: + additive_expression + lhs: simple_identifier "a" + op: + + rhs: simple_identifier "b" + op: * + rhs: simple_identifier "c" + +--- + +top_level + body: + binary_expr + operator: operator "9" + left: unsupported_node "(a + b)" + right: name_expr "c" + +=== +Comparison +=== + +a < b + +--- + +source_file + comparison_expression + lhs: simple_identifier "a" + op: < + rhs: simple_identifier "b" + +--- + +top_level + body: unsupported_node "a < b" + +=== +Equality +=== + +a == b + +--- + +source_file + equality_expression + lhs: simple_identifier "a" + op: == + rhs: simple_identifier "b" + +--- + +top_level + body: unsupported_node "a == b" + +=== +Logical and +=== + +a && b + +--- + +source_file + conjunction_expression + lhs: simple_identifier "a" + op: && + rhs: simple_identifier "b" + +--- + +top_level + body: unsupported_node "a && b" + +=== +Logical or +=== + +a || b + +--- + +source_file + disjunction_expression + lhs: simple_identifier "a" + op: || + rhs: simple_identifier "b" + +--- + +top_level + body: unsupported_node "a || b" + +=== +Logical not +=== + +!a + +--- + +source_file + prefix_expression + operation: bang "!" + target: simple_identifier "a" + +--- + +top_level + body: unsupported_node "!a" + +=== +Range operator +=== + +1...10 + +--- + +source_file + range_expression + end: integer_literal "10" + op: ... + start: integer_literal "1" + +--- + +top_level + body: unsupported_node "1...10" diff --git a/unified/extractor/tests/corpus/swift/optionals-and-errors.txt b/unified/extractor/tests/corpus/swift/optionals-and-errors.txt new file mode 100644 index 00000000000..3b3310c4fb9 --- /dev/null +++ b/unified/extractor/tests/corpus/swift/optionals-and-errors.txt @@ -0,0 +1,236 @@ +=== +Optional type annotation +=== + +let x: Int? = nil + +--- + +source_file + property_declaration + name: + pattern + bound_identifier: simple_identifier "x" + value: nil + value_binding_pattern + mutability: let + type_annotation + name: + optional_type + wrapped: + user_type + type_identifier "Int" + +--- + +top_level + body: unsupported_node "let x: Int? = nil" + +=== +Optional chaining +=== + +let n = obj?.foo?.bar + +--- + +source_file + property_declaration + name: + pattern + bound_identifier: simple_identifier "n" + value: + navigation_expression + suffix: + navigation_suffix + suffix: simple_identifier "bar" + target: + navigation_expression + suffix: + navigation_suffix + suffix: simple_identifier "foo" + target: + simple_identifier "obj" + ? + ? + value_binding_pattern + mutability: let + +--- + +top_level + body: unsupported_node "let n = obj?.foo?.bar" + +=== +Force unwrap +=== + +let n = opt! + +--- + +source_file + property_declaration + name: + pattern + bound_identifier: simple_identifier "n" + value: + postfix_expression + operation: bang "!" + target: simple_identifier "opt" + value_binding_pattern + mutability: let + +--- + +top_level + body: unsupported_node "let n = opt!" + +=== +Nil-coalescing +=== + +let n = opt ?? 0 + +--- + +source_file + property_declaration + name: + pattern + bound_identifier: simple_identifier "n" + value: + nil_coalescing_expression + if_nil: integer_literal "0" + value: simple_identifier "opt" + value_binding_pattern + mutability: let + +--- + +top_level + body: unsupported_node "let n = opt ?? 0" + +=== +Throwing function +=== + +func read() throws -> String { + return "" +} + +--- + +source_file + function_declaration + body: + function_body + statements + control_transfer_statement + result: + line_string_literal + name: + simple_identifier "read" + user_type + type_identifier "String" + throws "throws" + +--- + +top_level + body: unsupported_node "func read() throws -> String {\n return \"\"\n}" + +=== +Do-catch +=== + +do { + try foo() +} catch { + print(error) +} + +--- + +source_file + do_statement + statements + try_expression + expr: + call_expression + simple_identifier "foo" + call_suffix + value_arguments + try_operator + catch_block + catch_keyword "catch" + statements + call_expression + simple_identifier "print" + call_suffix + value_arguments + value_argument + value: simple_identifier "error" + +--- + +top_level + body: unsupported_node "do {\n try foo()\n} catch {\n print(error)\n}" + +=== +Try? expression +=== + +let result = try? foo() + +--- + +source_file + property_declaration + name: + pattern + bound_identifier: simple_identifier "result" + value: + try_expression + expr: + call_expression + simple_identifier "foo" + call_suffix + value_arguments + try_operator + value_binding_pattern + mutability: let + +--- + +top_level + body: unsupported_node "let result = try? foo()" + +=== +Try! expression +=== + +let result = try! foo() + +--- + +source_file + property_declaration + name: + pattern + bound_identifier: simple_identifier "result" + value: + try_expression + expr: + call_expression + simple_identifier "foo" + call_suffix + value_arguments + try_operator + value_binding_pattern + mutability: let + +--- + +top_level + body: unsupported_node "let result = try! foo()" diff --git a/unified/extractor/tests/corpus/swift/types.txt b/unified/extractor/tests/corpus/swift/types.txt new file mode 100644 index 00000000000..c4af07383b8 --- /dev/null +++ b/unified/extractor/tests/corpus/swift/types.txt @@ -0,0 +1,490 @@ +=== +Empty class +=== + +class Foo {} + +--- + +source_file + class_declaration + body: + class_body + declaration_kind: class + name: type_identifier "Foo" + +--- + +top_level + body: unsupported_node "class Foo {}" + +=== +Class with stored properties +=== + +class Point { + var x: Int + var y: Int +} + +--- + +source_file + class_declaration + body: + class_body + property_declaration + name: + pattern + bound_identifier: simple_identifier "x" + value_binding_pattern + mutability: var + type_annotation + name: + user_type + type_identifier "Int" + property_declaration + name: + pattern + bound_identifier: simple_identifier "y" + value_binding_pattern + mutability: var + type_annotation + name: + user_type + type_identifier "Int" + declaration_kind: class + name: type_identifier "Point" + +--- + +top_level + body: unsupported_node "class Point {\n var x: Int\n var y: Int\n}" + +=== +Class with initializer +=== + +class Point { + var x: Int + init(x: Int) { + self.x = x + } +} + +--- + +source_file + class_declaration + body: + class_body + property_declaration + name: + pattern + bound_identifier: simple_identifier "x" + value_binding_pattern + mutability: var + type_annotation + name: + user_type + type_identifier "Int" + init_declaration + body: + function_body + statements + assignment + operator: = + result: simple_identifier "x" + target: + directly_assignable_expression + navigation_expression + suffix: + navigation_suffix + suffix: simple_identifier "x" + target: + self_expression + name: init + parameter + name: + simple_identifier "x" + user_type + type_identifier "Int" + declaration_kind: class + name: type_identifier "Point" + +--- + +top_level + body: unsupported_node "class Point {\n var x: Int\n init(x: Int) {\n self.x = x\n }\n}" + +=== +Class with method +=== + +class Counter { + var n = 0 + func bump() { + n += 1 + } +} + +--- + +source_file + class_declaration + body: + class_body + property_declaration + name: + pattern + bound_identifier: simple_identifier "n" + value: integer_literal "0" + value_binding_pattern + mutability: var + function_declaration + body: + function_body + statements + assignment + operator: += + result: integer_literal "1" + target: + directly_assignable_expression + simple_identifier "n" + name: simple_identifier "bump" + declaration_kind: class + name: type_identifier "Counter" + +--- + +top_level + body: unsupported_node "class Counter {\n var n = 0\n func bump() {\n n += 1\n }\n}" + +=== +Class inheritance +=== + +class Dog: Animal {} + +--- + +source_file + class_declaration + body: + class_body + declaration_kind: class + name: type_identifier "Dog" + inheritance_specifier + inherits_from: + user_type + type_identifier "Animal" + +--- + +top_level + body: unsupported_node "class Dog: Animal {}" + +=== +Struct +=== + +struct Point { + let x: Int + let y: Int +} + +--- + +source_file + class_declaration + body: + class_body + property_declaration + name: + pattern + bound_identifier: simple_identifier "x" + value_binding_pattern + mutability: let + type_annotation + name: + user_type + type_identifier "Int" + property_declaration + name: + pattern + bound_identifier: simple_identifier "y" + value_binding_pattern + mutability: let + type_annotation + name: + user_type + type_identifier "Int" + declaration_kind: struct + name: type_identifier "Point" + +--- + +top_level + body: unsupported_node "struct Point {\n let x: Int\n let y: Int\n}" + +=== +Enum with cases +=== + +enum Direction { + case north + case south + case east + case west +} + +--- + +source_file + class_declaration + body: + enum_class_body + enum_entry + name: simple_identifier "north" + enum_entry + name: simple_identifier "south" + enum_entry + name: simple_identifier "east" + enum_entry + name: simple_identifier "west" + declaration_kind: enum + name: type_identifier "Direction" + +--- + +top_level + body: unsupported_node "enum Direction {\n case north\n case south\n case east\n case west\n}" + +=== +Enum with associated values +=== + +enum Shape { + case circle(radius: Double) + case square(side: Double) +} + +--- + +source_file + class_declaration + body: + enum_class_body + enum_entry + data_contents: + enum_type_parameters + name: + user_type + type_identifier "Double" + simple_identifier "radius" + name: simple_identifier "circle" + enum_entry + data_contents: + enum_type_parameters + name: + user_type + type_identifier "Double" + simple_identifier "side" + name: simple_identifier "square" + declaration_kind: enum + name: type_identifier "Shape" + +--- + +top_level + body: unsupported_node "enum Shape {\n case circle(radius: Double)\n case square(side: Double)\n}" + +=== +Protocol declaration +=== + +protocol Drawable { + func draw() +} + +--- + +source_file + protocol_declaration + body: + protocol_body + protocol_function_declaration + name: simple_identifier "draw" + declaration_kind: protocol + name: type_identifier "Drawable" + +--- + +top_level + body: unsupported_node "protocol Drawable {\n func draw()\n}" + +=== +Extension +=== + +extension Int { + func squared() -> Int { return self * self } +} + +--- + +source_file + class_declaration + body: + class_body + function_declaration + body: + function_body + statements + control_transfer_statement + result: + multiplicative_expression + lhs: + self_expression + op: * + rhs: + self_expression + name: + simple_identifier "squared" + user_type + type_identifier "Int" + declaration_kind: extension + name: + user_type + type_identifier "Int" + +--- + +top_level + body: unsupported_node "extension Int {\n func squared() -> Int { return self * self }\n}" + +=== +Computed property +=== + +class Rect { + var w: Double + var h: Double + var area: Double { + return w * h + } +} + +--- + +source_file + class_declaration + body: + class_body + property_declaration + name: + pattern + bound_identifier: simple_identifier "w" + value_binding_pattern + mutability: var + type_annotation + name: + user_type + type_identifier "Double" + property_declaration + name: + pattern + bound_identifier: simple_identifier "h" + value_binding_pattern + mutability: var + type_annotation + name: + user_type + type_identifier "Double" + property_declaration + computed_value: + computed_property + statements + control_transfer_statement + result: + multiplicative_expression + lhs: simple_identifier "w" + op: * + rhs: simple_identifier "h" + name: + pattern + bound_identifier: simple_identifier "area" + value_binding_pattern + mutability: var + type_annotation + name: + user_type + type_identifier "Double" + declaration_kind: class + name: type_identifier "Rect" + +--- + +top_level + body: unsupported_node "class Rect {\n var w: Double\n var h: Double\n var area: Double {\n return w * h\n }\n}" + +=== +Property with getter and setter +=== + +class Box { + private var _v = 0 + var v: Int { + get { return _v } + set { _v = newValue } + } +} + +--- + +source_file + class_declaration + body: + class_body + property_declaration + name: + pattern + bound_identifier: simple_identifier "_v" + value: integer_literal "0" + modifiers + visibility_modifier + value_binding_pattern + mutability: var + property_declaration + computed_value: + computed_property + computed_getter + getter_specifier + statements + control_transfer_statement + result: simple_identifier "_v" + computed_setter + setter_specifier + statements + assignment + operator: = + result: simple_identifier "newValue" + target: + directly_assignable_expression + simple_identifier "_v" + name: + pattern + bound_identifier: simple_identifier "v" + value_binding_pattern + mutability: var + type_annotation + name: + user_type + type_identifier "Int" + declaration_kind: class + name: type_identifier "Box" + +--- + +top_level + body: unsupported_node "class Box {\n private var _v = 0\n var v: Int {\n get { return _v }\n set { _v = newValue }\n }\n}" diff --git a/unified/extractor/tests/corpus/swift/variables.txt b/unified/extractor/tests/corpus/swift/variables.txt new file mode 100644 index 00000000000..429aac05867 --- /dev/null +++ b/unified/extractor/tests/corpus/swift/variables.txt @@ -0,0 +1,187 @@ +=== +Let binding +=== + +let x = 1 + +--- + +source_file + property_declaration + name: + pattern + bound_identifier: simple_identifier "x" + value: integer_literal "1" + value_binding_pattern + mutability: let + +--- + +top_level + body: unsupported_node "let x = 1" + +=== +Var binding +=== + +var x = 1 + +--- + +source_file + property_declaration + name: + pattern + bound_identifier: simple_identifier "x" + value: integer_literal "1" + value_binding_pattern + mutability: var + +--- + +top_level + body: unsupported_node "var x = 1" + +=== +Let with type annotation +=== + +let x: Int = 1 + +--- + +source_file + property_declaration + name: + pattern + bound_identifier: simple_identifier "x" + value: integer_literal "1" + value_binding_pattern + mutability: let + type_annotation + name: + user_type + type_identifier "Int" + +--- + +top_level + body: unsupported_node "let x: Int = 1" + +=== +Var without initialiser +=== + +var x: Int + +--- + +source_file + property_declaration + name: + pattern + bound_identifier: simple_identifier "x" + value_binding_pattern + mutability: var + type_annotation + name: + user_type + type_identifier "Int" + +--- + +top_level + body: unsupported_node "var x: Int" + +=== +Tuple destructuring binding +=== + +let (a, b) = pair + +--- + +source_file + property_declaration + name: + pattern + pattern + simple_identifier "a" + pattern + simple_identifier "b" + value: simple_identifier "pair" + value_binding_pattern + mutability: let + +--- + +top_level + body: unsupported_node "let (a, b) = pair" + +=== +Multiple bindings on one line +=== + +let x = 1, y = 2 + +--- + +source_file + property_declaration + name: + pattern + bound_identifier: simple_identifier "x" + pattern + bound_identifier: simple_identifier "y" + value: + integer_literal "1" + integer_literal "2" + value_binding_pattern + mutability: let + +--- + +top_level + body: unsupported_node "let x = 1, y = 2" + +=== +Assignment +=== + +x = 1 + +--- + +source_file + assignment + operator: = + result: integer_literal "1" + target: + directly_assignable_expression + simple_identifier "x" + +--- + +top_level + body: unsupported_node "x = 1" + +=== +Compound assignment +=== + +x += 1 + +--- + +source_file + assignment + operator: += + result: integer_literal "1" + target: + directly_assignable_expression + simple_identifier "x" + +--- + +top_level + body: unsupported_node "x += 1"