Skip to content

Commit 2734945

Browse files
Merge pull request #224 from tree-sitter/csharp-11-features
C# 11 features
2 parents 7bfc87d + 6685be0 commit 2734945

9 files changed

+456454
-449749
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ Comprehensive supports C# 1 through 10.0 with the following exceptions:
5555
- [x] Generic attributes
5656
- [x] Static abstract members in interfaces
5757
- [x] Newlines in string interpolations
58-
- [ ] List patterns
58+
- [x] List patterns
59+
- [x] Slice pattern
60+
- [x] Required members
5961
- [ ] Raw string literals
6062

6163
### References

corpus/expressions.txt

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,9 +1877,9 @@ var x = c is < '0' or >= 'A' and <= 'Z';
18771877
(equals_value_clause
18781878
(is_pattern_expression
18791879
(identifier)
1880-
(binary_pattern
1880+
(or_pattern
18811881
(relational_pattern (character_literal))
1882-
(binary_pattern
1882+
(and_pattern
18831883
(relational_pattern (character_literal))
18841884
(relational_pattern (character_literal)))))))))))
18851885

@@ -2019,7 +2019,7 @@ var c = o is int; //is_expression with type
20192019
(equals_value_clause
20202020
(is_pattern_expression
20212021
(identifier)
2022-
(binary_pattern
2022+
(or_pattern
20232023
(type_pattern (predefined_type))
20242024
(type_pattern (predefined_type))))))))) (comment)
20252025
(global_statement
@@ -2033,6 +2033,31 @@ var c = o is int; //is_expression with type
20332033
(identifier)
20342034
(predefined_type))))))) (comment))
20352035

2036+
=====================================
2037+
List patterns
2038+
=====================================
2039+
2040+
var a = p is [1,2,x,] and [] or [2,..];
2041+
2042+
---
2043+
2044+
(compilation_unit
2045+
(global_statement
2046+
(local_declaration_statement
2047+
(variable_declaration (implicit_type)
2048+
(variable_declarator (identifier)
2049+
(equals_value_clause
2050+
(is_pattern_expression (identifier)
2051+
(or_pattern
2052+
(and_pattern
2053+
(list_pattern
2054+
(constant_pattern (integer_literal))
2055+
(constant_pattern (integer_literal))
2056+
(constant_pattern (identifier)))
2057+
(list_pattern))
2058+
(list_pattern
2059+
(constant_pattern (integer_literal))
2060+
(slice_pattern))))))))))
20362061

20372062
=====================================
20382063
Conditional expression with member accesses

corpus/literals.txt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,50 @@ class A {
314314
(identifier)
315315
(equals_value_clause (string_literal (escape_sequence)))))))))
316316

317+
=======================================
318+
utf-8 string literals
319+
=======================================
320+
321+
var a = "lower"u8;
322+
var b = "upper"U8;
323+
var c = @"lower"u8;
324+
var d = @"upper"U8;
325+
---
326+
327+
(compilation_unit
328+
(global_statement
329+
(local_declaration_statement
330+
(variable_declaration
331+
(implicit_type)
332+
(variable_declarator
333+
(identifier)
334+
(equals_value_clause
335+
(string_literal))))))
336+
(global_statement
337+
(local_declaration_statement
338+
(variable_declaration
339+
(implicit_type)
340+
(variable_declarator
341+
(identifier)
342+
(equals_value_clause
343+
(string_literal))))))
344+
(global_statement
345+
(local_declaration_statement
346+
(variable_declaration
347+
(implicit_type)
348+
(variable_declarator
349+
(identifier)
350+
(equals_value_clause
351+
(verbatim_string_literal))))))
352+
(global_statement
353+
(local_declaration_statement
354+
(variable_declaration
355+
(implicit_type)
356+
(variable_declarator
357+
(identifier)
358+
(equals_value_clause
359+
(verbatim_string_literal)))))))
360+
317361
==================================================
318362
string literals containing newline
319363
==================================================

corpus/type-fields.txt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ class A {
242242
(variable_declarator (identifier)))))))
243243

244244
=======================================
245-
Native interger types
245+
Native integer types
246246
=======================================
247247

248248
class A {
@@ -264,3 +264,26 @@ class A {
264264
(variable_declaration
265265
(predefined_type)
266266
(variable_declarator (identifier)))))))
267+
268+
269+
=======================================
270+
Required fields
271+
=======================================
272+
273+
class A {
274+
public required int B;
275+
}
276+
277+
---
278+
279+
(compilation_unit
280+
(class_declaration
281+
(identifier)
282+
(declaration_list
283+
(field_declaration
284+
(modifier)
285+
(modifier)
286+
(variable_declaration
287+
(predefined_type)
288+
(variable_declarator
289+
(identifier)))))))

corpus/type-properties.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,27 @@ class Foo: IFoo {
194194
(explicit_interface_specifier (identifier))
195195
(identifier)
196196
(accessor_list (accessor_declaration))))))
197+
198+
199+
=======================================
200+
Required properties
201+
=======================================
202+
203+
class A {
204+
public required int B { get; set; }
205+
}
206+
207+
---
208+
209+
(compilation_unit
210+
(class_declaration
211+
(identifier)
212+
(declaration_list
213+
(property_declaration
214+
(modifier)
215+
(modifier)
216+
(predefined_type)
217+
(identifier)
218+
(accessor_list
219+
(accessor_declaration)
220+
(accessor_declaration))))))

grammar.js

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ module.exports = grammar({
7474
[$._contextual_keywords, $.type_parameter_constraint],
7575

7676
[$._type, $.array_creation_expression],
77+
[$._type, $.attribute],
7778
[$._type, $.stack_alloc_array_creation_expression],
7879
[$._type, $._nullable_base_type],
7980
[$._type, $._nullable_base_type, $.array_creation_expression],
@@ -258,6 +259,7 @@ module.exports = grammar({
258259
'protected',
259260
'public',
260261
'readonly',
262+
'required',
261263
prec(1, 'ref'), //make sure that 'ref' is treated as a modifier for local variable declarations instead of as a ref expression
262264
'sealed',
263265
'static',
@@ -924,12 +926,22 @@ module.exports = grammar({
924926
$.negated_pattern,
925927
$.parenthesized_pattern,
926928
$.relational_pattern,
927-
$.binary_pattern,
929+
$.or_pattern,
930+
$.and_pattern,
931+
$.list_pattern,
928932
$.type_pattern
929933
),
930934

931935
type_pattern: $ => $._type,
932936

937+
list_pattern: $ => seq(
938+
'[',
939+
optional(seq(commaSep1(choice($._pattern, $.slice_pattern)), optional(','))),
940+
']'
941+
),
942+
943+
slice_pattern: $ => '..',
944+
933945
parenthesized_pattern: $ => seq('(', $._pattern, ')'),
934946

935947
relational_pattern: $ => prec.left(choice(
@@ -941,18 +953,17 @@ module.exports = grammar({
941953

942954
negated_pattern: $ => seq('not', $._pattern),
943955

944-
binary_pattern: $ => choice(
945-
prec.left(PREC.AND, seq(
946-
field('left', $._pattern),
947-
field('operator', 'and'),
948-
field('right', $._pattern)
949-
)),
950-
prec.left(PREC.OR, seq(
951-
field('left', $._pattern),
952-
field('operator', 'or'),
953-
field('right', $._pattern)
954-
)),
955-
),
956+
and_pattern: $ => prec.left(PREC.AND, seq(
957+
field('left', $._pattern),
958+
field('operator', 'and'),
959+
field('right', $._pattern)
960+
)),
961+
962+
or_pattern: $ => prec.left(PREC.OR, seq(
963+
field('left', $._pattern),
964+
field('operator', 'or'),
965+
field('right', $._pattern)
966+
)),
956967

957968
//We may need to expand this list if more things can be evaluated at compile time
958969
constant_pattern: $ => choice(
@@ -1634,7 +1645,7 @@ module.exports = grammar({
16341645
$._string_literal_fragment,
16351646
$.escape_sequence
16361647
)),
1637-
'"'
1648+
choice('"', '"U8', '"u8')
16381649
),
16391650

16401651
_string_literal_fragment: $ => token.immediate(prec(1, /[^"\\\n]+/)),
@@ -1645,7 +1656,7 @@ module.exports = grammar({
16451656
/[^"]/,
16461657
'""',
16471658
)),
1648-
'"'
1659+
choice('"', '"U8', '"u8')
16491660
)),
16501661

16511662
// Comments

0 commit comments

Comments
 (0)