From ce8beddc0e4b8a4f07170d762ae76800d47105fb Mon Sep 17 00:00:00 2001 From: Alex Pinkus Date: Thu, 13 Jan 2022 20:13:00 -0800 Subject: [PATCH] Add field names to a few more nodes In advance of reaching 0.1, adding a few more field names should make stability guarantees easier to satisfy. Also promotes `protocol_composition_type` to named which breaks semver, so bumping to 0.0.3. --- Cargo.toml | 2 +- README.md | 4 +- bindings/rust/lib.rs | 2 +- corpus/classes.txt | 63 ++++++++------ corpus/functions.txt | 9 +- grammar.ts | 131 ++++++++++++++++++++++------- package.json | 2 +- test-npm-package/index.js | 2 +- test-npm-package/package-lock.json | 2 +- 9 files changed, 148 insertions(+), 69 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3a6d7c3..6597931 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "experimental-tree-sitter-swift" description = "swift grammar for the tree-sitter parsing library" -version = "0.0.2" +version = "0.0.3" keywords = ["incremental", "parsing", "swift"] categories = ["parsing", "text-editors"] repository = "https://github.com/alex-pinkus/experimental-tree-sitter-swift" diff --git a/README.md b/README.md index db9c344..1ec6eef 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ To use this parser to parse Swift code, you'll want to depend on either the Rust To use the Rust crate, you'll add this to your `Cargo.toml`: ``` tree-sitter = "0.20.0" -experimental-tree-sitter-swift = "=0.0.2" +experimental-tree-sitter-swift = "=0.0.3" ``` Then you can use a `tree-sitter` parser with the language declared here: @@ -36,7 +36,7 @@ let tree = parser.parse(&my_source_code, None) To use this from NPM, you'll add similar dependencies to `package.json`: ``` "dependencies: { - "experimental-tree-sitter-swift": "0.0.2", + "experimental-tree-sitter-swift": "0.0.3", "tree-sitter": "^0.20.0" } ``` diff --git a/bindings/rust/lib.rs b/bindings/rust/lib.rs index 23ac56e..fe46c8c 100644 --- a/bindings/rust/lib.rs +++ b/bindings/rust/lib.rs @@ -61,7 +61,7 @@ mod tests { .ok_or_else(|| anyhow!("Unable to parse!"))?; assert_eq!( - "(source_file (assignment target: (directly_assignable_expression (simple_identifier)) result: (line_string_literal (line_str_text))))", + "(source_file (assignment target: (directly_assignable_expression (simple_identifier)) result: (line_string_literal text: (line_str_text))))", tree.root_node().to_sexp(), ); diff --git a/corpus/classes.txt b/corpus/classes.txt index 4b77908..95575a0 100755 --- a/corpus/classes.txt +++ b/corpus/classes.txt @@ -559,10 +559,11 @@ var propertyMap: NodePropertyMap & KeypathSearchable { (non_binding_pattern (simple_identifier))) (type_annotation - (user_type - (type_identifier)) - (user_type - (type_identifier))) + (protocol_composition_type + (user_type + (type_identifier)) + (user_type + (type_identifier)))) (computed_property (statements (control_transfer_statement @@ -731,10 +732,11 @@ class SomeClassWithAlias: NSObject { (class_body (typealias_declaration (type_identifier) - (user_type - (type_identifier)) - (user_type - (type_identifier)))))) + (protocol_composition_type + (user_type + (type_identifier)) + (user_type + (type_identifier))))))) ================================================================================ Special constructors @@ -1216,18 +1218,20 @@ let result: HasGeneric = HasGeneric(t: "Hello") (user_type (type_identifier) (type_arguments - (user_type - (type_identifier)) - (user_type - (type_identifier))))) + (protocol_composition_type + (user_type + (type_identifier)) + (user_type + (type_identifier)))))) (constructor_expression (user_type (type_identifier) (type_arguments - (user_type - (type_identifier)) - (user_type - (type_identifier)))) + (protocol_composition_type + (user_type + (type_identifier)) + (user_type + (type_identifier))))) (constructor_suffix (value_arguments (value_argument @@ -1266,10 +1270,11 @@ func iterate(h: S) where S : Sequence, S.Element == P1 & P2 { } (identifier (simple_identifier) (simple_identifier)) - (user_type - (type_identifier)) - (user_type - (type_identifier))))) + (protocol_composition_type + (user_type + (type_identifier)) + (user_type + (type_identifier)))))) (function_body))) ================================================================================ @@ -1289,10 +1294,11 @@ enum Foo { (enum_entry (simple_identifier) (enum_type_parameters - (user_type - (type_identifier)) - (user_type - (type_identifier))))))) + (protocol_composition_type + (user_type + (type_identifier)) + (user_type + (type_identifier)))))))) ================================================================================ Protocol composition in as expressions @@ -1316,10 +1322,11 @@ let result = greet("me") as P1 & P2 (line_string_literal (line_str_text)))))) (as_operator) - (user_type - (type_identifier)) - (user_type - (type_identifier))))) + (protocol_composition_type + (user_type + (type_identifier)) + (user_type + (type_identifier)))))) ================================================================================ Modify declarations diff --git a/corpus/functions.txt b/corpus/functions.txt index fba6e74..3a8fcb4 100755 --- a/corpus/functions.txt +++ b/corpus/functions.txt @@ -782,10 +782,11 @@ func multipleType() -> Foo & Bar { return Foo() } (line_string_literal))))) (function_declaration (simple_identifier) - (user_type - (type_identifier)) - (user_type - (type_identifier)) + (protocol_composition_type + (user_type + (type_identifier)) + (user_type + (type_identifier))) (function_body (statements (control_transfer_statement diff --git a/grammar.ts b/grammar.ts index ca21c3e..8b7fd89 100644 --- a/grammar.ts +++ b/grammar.ts @@ -307,7 +307,11 @@ module.exports = grammar({ ), line_string_literal: ($) => - seq('"', repeat(choice($._line_string_content, $._interpolation)), '"'), + seq( + '"', + repeat(choice(field("text", $._line_string_content), $._interpolation)), + '"' + ), _line_string_content: ($) => choice($.line_str_text, $.str_escaped_char), @@ -321,7 +325,9 @@ module.exports = grammar({ multi_line_string_literal: ($) => seq( '"""', - repeat(choice($._multi_line_string_content, $._interpolation)), + repeat( + choice(field("text", $._multi_line_string_content), $._interpolation) + ), '"""' ), @@ -329,12 +335,12 @@ module.exports = grammar({ seq( repeat( seq( - $.raw_str_part, - $.raw_str_interpolation, + field("text", $.raw_str_part), + field("interpolation", $.raw_str_interpolation), optional($.raw_str_continuing_indicator) ) ), - $.raw_str_end_part + field("text", $.raw_str_end_part) ), raw_str_interpolation: ($) => @@ -348,7 +354,13 @@ module.exports = grammar({ _interpolation: ($) => seq("\\(", $._interpolation_contents, ")"), _interpolation_contents: ($) => - sep1(alias($.value_argument, $.interpolated_expression), ","), + sep1( + field( + "interpolation", + alias($.value_argument, $.interpolated_expression) + ), + "," + ), _escaped_identifier: ($) => /\\[0\\tnr"'\n]/, @@ -367,7 +379,7 @@ module.exports = grammar({ _type: ($) => prec.right( PRECS.ty, - seq(optional($.type_modifiers), $._unannotated_type) + seq(optional($.type_modifiers), field("name", $._unannotated_type)) ), _unannotated_type: ($) => @@ -382,7 +394,7 @@ module.exports = grammar({ $.optional_type, $.metatype, $.opaque_type, - $._protocol_composition_type + $.protocol_composition_type ) ), @@ -454,7 +466,7 @@ module.exports = grammar({ opaque_type: ($) => seq("some", $.user_type), - _protocol_composition_type: ($) => + protocol_composition_type: ($) => prec.right( seq( $._unannotated_type, @@ -533,12 +545,15 @@ module.exports = grammar({ PRECS.range, seq( choice($._open_ended_range_operator, $._three_dot_operator), - prec.right(PRECS.range_suffix, $._expression) + prec.right(PRECS.range_suffix, field("end", $._expression)) ) ), open_end_range_expression: ($) => - prec.right(PRECS.range, seq($._expression, $._three_dot_operator)), + prec.right( + PRECS.range, + seq(field("start", $._expression), $._three_dot_operator) + ), prefix_expression: ($) => prec.left( @@ -585,70 +600,123 @@ module.exports = grammar({ multiplicative_expression: ($) => prec.left( PRECS.multiplication, - seq($._expression, $._multiplicative_operator, $._expression) + seq( + field("lhs", $._expression), + field("op", $._multiplicative_operator), + field("rhs", $._expression) + ) ), additive_expression: ($) => prec.left( PRECS.addition, - seq($._expression, $._additive_operator, $._expression) + seq( + field("lhs", $._expression), + field("op", $._additive_operator), + field("rhs", $._expression) + ) ), range_expression: ($) => prec.right( PRECS.range, seq( - $._expression, - choice($._open_ended_range_operator, $._three_dot_operator), - $._expression + field("start", $._expression), + field( + "op", + choice($._open_ended_range_operator, $._three_dot_operator) + ), + field("end", $._expression) ) ), infix_expression: ($) => prec.left( PRECS.infix_operations, - seq($._expression, $.custom_operator, $._expression) + seq( + field("lhs", $._expression), + field("op", $.custom_operator), + field("rhs", $._expression) + ) ), nil_coalescing_expression: ($) => prec.right( PRECS.nil_coalescing, - seq($._expression, $._nil_coalescing_operator, $._expression) + seq( + field("value", $._expression), + $._nil_coalescing_operator, + field("if_nil", $._expression) + ) ), check_expression: ($) => - prec.left(PRECS.check, seq($._expression, $._is_operator, $._type)), + prec.left( + PRECS.check, + seq( + field("target", $._expression), + field("op", $._is_operator), + field("type", $._type) + ) + ), comparison_expression: ($) => - prec.left(seq($._expression, $._comparison_operator, $._expression)), + prec.left( + seq( + field("lhs", $._expression), + field("op", $._comparison_operator), + field("rhs", $._expression) + ) + ), equality_expression: ($) => prec.left( PRECS.equality, - seq($._expression, $._equality_operator, $._expression) + seq( + field("lhs", $._expression), + field("op", $._equality_operator), + field("rhs", $._expression) + ) ), conjunction_expression: ($) => prec.left( PRECS.conjunction, - seq($._expression, $._conjunction_operator, $._expression) + seq( + field("lhs", $._expression), + field("op", $._conjunction_operator), + field("rhs", $._expression) + ) ), disjunction_expression: ($) => prec.left( PRECS.disjunction, - seq($._expression, $._disjunction_operator, $._expression) + seq( + field("lhs", $._expression), + field("op", $._disjunction_operator), + field("rhs", $._expression) + ) ), bitwise_operation: ($) => - prec.left(seq($._expression, $._bitwise_binary_operator, $._expression)), + prec.left( + seq( + field("lhs", $._expression), + field("op", $._bitwise_binary_operator), + field("rhs", $._expression) + ) + ), custom_operator: ($) => seq(CUSTOM_OPERATORS, optional("<")), // Suffixes navigation_suffix: ($) => - seq($._dot, choice($.simple_identifier, $.integer_literal)), + seq( + $._dot, + field("suffix", choice($.simple_identifier, $.integer_literal)) + ), call_suffix: ($) => prec( @@ -656,7 +724,7 @@ module.exports = grammar({ seq( choice( $.value_arguments, - sep1($.lambda_literal, seq($.simple_identifier, ":")) + sep1($.lambda_literal, seq(field("name", $.simple_identifier), ":")) ) ) ), @@ -856,8 +924,8 @@ module.exports = grammar({ PRECS.lambda, seq( "{", - prec(PRECS.expr, optional($.capture_list)), - optional(seq(optional($.lambda_function_type), "in")), + prec(PRECS.expr, optional(field("captures", $.capture_list))), + optional(seq(optional(field("type", $.lambda_function_type)), "in")), optional($.statements), "}" ) @@ -868,7 +936,7 @@ module.exports = grammar({ capture_list_item: ($) => choice( - $.self_expression, + field("name", $.self_expression), prec( PRECS.expr, seq( @@ -989,7 +1057,10 @@ module.exports = grammar({ catch_block: ($) => seq( $.catch_keyword, - optional(generate_pattern_matching_rule($, true, false)), + field( + "error", + optional(generate_pattern_matching_rule($, true, false)) + ), optional($.where_clause), $._block ), diff --git a/package.json b/package.json index f7aeba0..e873295 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "experimental-tree-sitter-swift", - "version": "0.0.2", + "version": "0.0.3", "description": "An experimental tree-sitter grammar for the Swift programming language.", "main": "bindings/node/index.js", "scripts": { diff --git a/test-npm-package/index.js b/test-npm-package/index.js index ef925f5..fe2f9d2 100644 --- a/test-npm-package/index.js +++ b/test-npm-package/index.js @@ -20,6 +20,6 @@ console.log(tree.rootNode.toString()); const assert = require("assert"); const smallTree = parser.parse(`_ = "Hello!"\n`); assert.equal( - `(source_file (assignment target: (directly_assignable_expression (simple_identifier)) result: (line_string_literal (line_str_text))))`, + `(source_file (assignment target: (directly_assignable_expression (simple_identifier)) result: (line_string_literal text: (line_str_text))))`, smallTree.rootNode.toString() ); diff --git a/test-npm-package/package-lock.json b/test-npm-package/package-lock.json index 2fc08b9..c2db099 100644 --- a/test-npm-package/package-lock.json +++ b/test-npm-package/package-lock.json @@ -14,7 +14,7 @@ } }, "..": { - "version": "0.0.2", + "version": "0.0.3", "hasInstallScript": true, "license": "MIT", "dependencies": {