tree-sitter-topaz 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/grammar.js CHANGED
@@ -1,3 +1,10 @@
1
+ // Canonical keyword_registry! in Topaz 5.20 token.rs; all other words are contextual.
2
+ const KEYWORDS = [
3
+ "let", "mut", "const", "type", "function", "return", "if", "else",
4
+ "match", "case", "for", "in", "by", "while", "break", "continue",
5
+ "defer", "concurrent", "true", "false", "null",
6
+ ];
7
+
1
8
  const PREC = {
2
9
  assignment: 1,
3
10
  lambda: 2,
@@ -20,6 +27,8 @@ module.exports = grammar({
20
27
 
21
28
  word: ($) => $.identifier,
22
29
 
30
+ inline: ($) => [$._identifier],
31
+
23
32
  externals: ($) => [
24
33
  $._nominal_record_construction,
25
34
  $._record_lbrace,
@@ -49,6 +58,8 @@ module.exports = grammar({
49
58
  [$.array_element, $.type_identifier],
50
59
  [$._expression, $.argument],
51
60
  [$._expression, $.binding_pattern],
61
+ // Range endpoints can start with calls; keep the constructor-pattern branch.
62
+ [$._expression, $.constructor_pattern],
52
63
  [$._top_level_item, $._statement],
53
64
  [$.refutable_let_declaration, $._pattern],
54
65
  [$.refutable_let_declaration, $._primary_pattern],
@@ -106,7 +117,7 @@ module.exports = grammar({
106
117
  field("path", $.module_path),
107
118
  optional(
108
119
  choice(
109
- seq("as", field("alias", $.identifier)),
120
+ seq("as", field("alias", $._identifier)),
110
121
  field("selections", $.import_list),
111
122
  ),
112
123
  ),
@@ -116,7 +127,7 @@ module.exports = grammar({
116
127
 
117
128
  module_path: ($) =>
118
129
  prec.right(
119
- seq($.identifier, repeat(seq(".", field("segment", $.identifier)))),
130
+ seq($._identifier, repeat(seq(".", field("segment", $._identifier)))),
120
131
  ),
121
132
 
122
133
  import_list: ($) =>
@@ -125,8 +136,8 @@ module.exports = grammar({
125
136
  optional(
126
137
  commaSep1(
127
138
  seq(
128
- field("name", $.identifier),
129
- optional(seq("as", field("alias", $.identifier))),
139
+ field("name", $._identifier),
140
+ optional(seq("as", field("alias", $._identifier))),
130
141
  ),
131
142
  ),
132
143
  ),
@@ -162,9 +173,9 @@ module.exports = grammar({
162
173
  seq(
163
174
  "export",
164
175
  "function",
165
- field("name", $.identifier),
176
+ field("name", $._identifier),
166
177
  "as",
167
- field("alias", $.identifier),
178
+ field("alias", $._identifier),
168
179
  $.parameter_list,
169
180
  optional($.return_type),
170
181
  $.block,
@@ -344,7 +355,7 @@ module.exports = grammar({
344
355
  seq(
345
356
  optional("export"),
346
357
  "function",
347
- field("name", $.identifier),
358
+ field("name", $._identifier),
348
359
  "(",
349
360
  "self",
350
361
  optional(seq(",", commaSep1($.parameter))),
@@ -367,7 +378,7 @@ module.exports = grammar({
367
378
  protocol_method_signature: ($) =>
368
379
  seq(
369
380
  "function",
370
- field("name", $.identifier),
381
+ field("name", $._identifier),
371
382
  optional($.function_type_parameters),
372
383
  $.parameter_list,
373
384
  optional($.return_type),
@@ -394,7 +405,7 @@ module.exports = grammar({
394
405
  protocol_impl_method: ($) =>
395
406
  seq(
396
407
  "function",
397
- field("name", $.identifier),
408
+ field("name", $._identifier),
398
409
  optional($.function_type_parameters),
399
410
  $.parameter_list,
400
411
  optional($.return_type),
@@ -404,7 +415,7 @@ module.exports = grammar({
404
415
  function_declaration: ($) =>
405
416
  seq(
406
417
  "function",
407
- field("name", $.identifier),
418
+ field("name", $._identifier),
408
419
  optional($.function_type_parameters),
409
420
  $.parameter_list,
410
421
  optional($.return_type),
@@ -449,8 +460,7 @@ module.exports = grammar({
449
460
  ),
450
461
  ),
451
462
 
452
- parameter_name: ($) =>
453
- choice($.identifier, alias("self", $.identifier)),
463
+ parameter_name: ($) => $._identifier,
454
464
 
455
465
  return_type: ($) => seq("->", field("type", $._type)),
456
466
 
@@ -559,7 +569,7 @@ module.exports = grammar({
559
569
  const_declaration: ($) =>
560
570
  seq(
561
571
  "const",
562
- field("name", $.identifier),
572
+ field("name", $._identifier),
563
573
  optional(seq(":", field("type", $._type))),
564
574
  "=",
565
575
  field("value", $._expression),
@@ -571,7 +581,7 @@ module.exports = grammar({
571
581
  2,
572
582
  seq(
573
583
  "using",
574
- field("name", $.identifier),
584
+ field("name", $._identifier),
575
585
  "=",
576
586
  field("value", $._expression),
577
587
  field("body", $.block),
@@ -614,7 +624,7 @@ module.exports = grammar({
614
624
  optional(";"),
615
625
  ),
616
626
 
617
- loop_label: ($) => seq("'", field("name", $.identifier)),
627
+ loop_label: ($) => seq("'", field("name", $._identifier)),
618
628
 
619
629
  assignment_statement: ($) =>
620
630
  prec.right(
@@ -773,7 +783,7 @@ module.exports = grammar({
773
783
 
774
784
  concurrent_arm: ($) =>
775
785
  seq(
776
- field("name", $.identifier),
786
+ field("name", $._identifier),
777
787
  ":",
778
788
  field("value", $._expression),
779
789
  optional(choice(",", ";")),
@@ -787,6 +797,7 @@ module.exports = grammar({
787
797
  "parameters",
788
798
  choice(
789
799
  $.identifier,
800
+ $.contextual_identifier,
790
801
  $.legacy_keyword_identifier,
791
802
  seq(
792
803
  "(",
@@ -803,10 +814,7 @@ module.exports = grammar({
803
814
 
804
815
  lambda_parameter: ($) =>
805
816
  seq(
806
- field(
807
- "name",
808
- choice($.identifier, $.legacy_keyword_identifier),
809
- ),
817
+ field("name", $._identifier),
810
818
  optional(seq(":", field("type", $._type))),
811
819
  ),
812
820
 
@@ -848,11 +856,15 @@ module.exports = grammar({
848
856
  argument: ($) =>
849
857
  choice(
850
858
  field("value", $.record_construction),
859
+ $.forbidden_keyword_argument,
851
860
  seq("...", field("value", $._expression)),
852
- seq(field("name", $.identifier), ":", field("value", $._expression)),
861
+ seq(field("name", $._identifier), ":", field("value", $._expression)),
853
862
  field("value", $._expression),
854
863
  ),
855
864
 
865
+ forbidden_keyword_argument: ($) =>
866
+ seq(field("name", choice(...KEYWORDS)), ":", field("value", $._expression)),
867
+
856
868
  member_expression: ($) =>
857
869
  prec.left(
858
870
  PREC.postfix,
@@ -865,6 +877,9 @@ module.exports = grammar({
865
877
 
866
878
  pipeline_placeholder: (_) => "_",
867
879
 
880
+ _identifier: ($) =>
881
+ choice($.identifier, $.contextual_identifier, $.legacy_keyword_identifier),
882
+
868
883
  contextual_identifier: ($) =>
869
884
  prec(-1, alias(choice("map", "set"), $.identifier)),
870
885
 
@@ -872,7 +887,12 @@ module.exports = grammar({
872
887
  prec(
873
888
  -2,
874
889
  alias(
875
- choice("import", "export", "use", "loop", "record", "self"),
890
+ choice(
891
+ "import", "export", "use", "loop", "record", "self",
892
+ "enum", "impl", "newtype", "protocol", "using",
893
+ "derives", "Eq", "Order", "Show", "JSON", "as", "timeout",
894
+ "int", "float", "string", "bool",
895
+ ),
876
896
  $.identifier,
877
897
  ),
878
898
  ),
@@ -904,6 +924,12 @@ module.exports = grammar({
904
924
  "value",
905
925
  choice(
906
926
  $.identifier,
927
+ $.contextual_identifier,
928
+ $.legacy_keyword_identifier,
929
+ $.member_expression,
930
+ $.index_expression,
931
+ $.call_expression,
932
+ $.record_update,
907
933
  $.parenthesized_expression,
908
934
  ),
909
935
  ),
@@ -994,11 +1020,13 @@ module.exports = grammar({
994
1020
  prec.right(
995
1021
  PREC.unary,
996
1022
  seq(
997
- field("operator", choice("+", "-", "!", "~")),
1023
+ field("operator", choice("+", "-", "!", $.forbidden_reserved_operator)),
998
1024
  field("argument", $._expression),
999
1025
  ),
1000
1026
  ),
1001
1027
 
1028
+ forbidden_reserved_operator: (_) => "~",
1029
+
1002
1030
  binary_expression: ($) =>
1003
1031
  choice(
1004
1032
  ...[
@@ -1099,28 +1127,36 @@ module.exports = grammar({
1099
1127
  $.legacy_keyword_identifier,
1100
1128
  ),
1101
1129
  type_pattern: ($) =>
1102
- seq(field("name", $.identifier), ":", field("type", $._type)),
1130
+ seq(field("name", $._identifier), ":", field("type", $._type)),
1103
1131
  constructor_pattern: ($) =>
1104
1132
  seq(
1105
- field("name", $.identifier),
1106
- "(",
1133
+ field("name", $._identifier),
1134
+ // Share the adjacent call token while also allowing spaced constructors.
1135
+ choice("(", token.immediate("(")),
1107
1136
  optional(commaSep1($._pattern)),
1108
1137
  optional(","),
1109
1138
  ")",
1110
1139
  ),
1111
1140
  range_pattern: ($) =>
1112
1141
  seq($._const_expression, field("operator", choice("..", "..<")), $._const_expression),
1142
+ // One optional rest marker, with ordinary patterns on either side.
1113
1143
  list_pattern: ($) =>
1114
1144
  seq(
1115
1145
  "[",
1116
1146
  optional(
1117
- choice(
1118
- $.list_rest_pattern,
1119
- seq(
1120
- commaSep1($._pattern),
1121
- optional(seq(",", $.list_rest_pattern)),
1122
- optional(","),
1147
+ seq(
1148
+ choice(
1149
+ seq($.list_rest_pattern, repeat(seq(",", $._pattern))),
1150
+ seq(
1151
+ commaSep1($._pattern),
1152
+ optional(seq(
1153
+ ",",
1154
+ $.list_rest_pattern,
1155
+ repeat(seq(",", $._pattern)),
1156
+ )),
1157
+ ),
1123
1158
  ),
1159
+ optional(","),
1124
1160
  ),
1125
1161
  ),
1126
1162
  "]",
@@ -1139,7 +1175,7 @@ module.exports = grammar({
1139
1175
  record_pattern_field: ($) =>
1140
1176
  choice(
1141
1177
  seq(field("name", $.field_identifier), ":", field("pattern", $._pattern)),
1142
- field("name", $.identifier),
1178
+ field("name", $._identifier),
1143
1179
  ),
1144
1180
 
1145
1181
  tagged_template: ($) => $._tagged_template,
@@ -1160,32 +1196,13 @@ module.exports = grammar({
1160
1196
  /[_\p{L}\u{1F300}-\u{1FAFF}\u{2600}-\u{27BF}][_\p{L}\p{N}\u{1F300}-\u{1FAFF}\u{2600}-\u{27BF}\u{FE0F}\u{200D}]*/u,
1161
1197
  ),
1162
1198
  ),
1163
- type_identifier: ($) => alias($.identifier, $.type_identifier),
1199
+ type_identifier: ($) => alias($._identifier, $.type_identifier),
1164
1200
  field_identifier: ($) =>
1165
1201
  choice(
1166
1202
  alias($.identifier, $.field_identifier),
1203
+ alias($.contextual_identifier, $.field_identifier),
1167
1204
  alias($.legacy_keyword_identifier, $.field_identifier),
1168
- "let",
1169
- "mut",
1170
- "const",
1171
- "type",
1172
- "function",
1173
- "return",
1174
- "if",
1175
- "else",
1176
- "match",
1177
- "case",
1178
- "for",
1179
- "in",
1180
- "by",
1181
- "while",
1182
- "break",
1183
- "continue",
1184
- "defer",
1185
- "concurrent",
1186
- "true",
1187
- "false",
1188
- "null",
1205
+ ...KEYWORDS,
1189
1206
  ),
1190
1207
 
1191
1208
  line_comment: (_) => token(seq("//", /[^\n\r]*/)),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tree-sitter-topaz",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "license": "Apache-2.0",
5
5
  "main": "bindings/node",
6
6
  "types": "bindings/node",
@@ -20,7 +20,8 @@
20
20
  "test": "tree-sitter test",
21
21
  "test:queries": "node scripts/check-queries.mjs",
22
22
  "parse:acceptance": "node scripts/parse-acceptance.mjs",
23
- "prepack": "tree-sitter generate"
23
+ "prepack": "tree-sitter generate",
24
+ "test:acceptance-extractor": "node --test scripts/rust-topaz-sources.test.mjs"
24
25
  },
25
26
  "dependencies": {
26
27
  "node-addon-api": "^8.5.0",
@@ -52,6 +52,11 @@
52
52
  (parameter
53
53
  name: (parameter_name (identifier) @variable.parameter))
54
54
  (lambda_parameter name: (identifier) @variable.parameter)
55
+ (parameter
56
+ name: (parameter_name
57
+ [(contextual_identifier) (legacy_keyword_identifier)] @variable.parameter))
58
+ (lambda_parameter
59
+ name: [(contextual_identifier) (legacy_keyword_identifier)] @variable.parameter)
55
60
  (field_identifier) @property
56
61
  (identifier) @variable
57
62