Skip to content

Commit 98ef311

Browse files
authored
Merge pull request #135 from resolritter/class_syntax
Support more of the class syntax
2 parents 7579fd0 + af8df99 commit 98ef311

File tree

9 files changed

+398916
-398311
lines changed

9 files changed

+398916
-398311
lines changed

common/corpus/declarations.txt

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,3 +877,122 @@ export interface I { }
877877
(export_statement (class_declaration (type_identifier) (class_body)))
878878
(export_statement (interface_declaration (type_identifier) (object_type)))
879879
(export_statement (interface_declaration (type_identifier) (object_type))))
880+
881+
=======================================
882+
Classes with generic parameters
883+
=======================================
884+
885+
class A<
886+
B,
887+
C,
888+
> {}
889+
890+
class D extends A<
891+
X,
892+
Y,
893+
> {}
894+
895+
---
896+
897+
(program
898+
(class_declaration
899+
(type_identifier)
900+
(type_parameters (type_parameter (type_identifier)) (type_parameter (type_identifier)))
901+
(class_body))
902+
(class_declaration
903+
(type_identifier)
904+
(class_heritage (extends_clause (generic_type
905+
(type_identifier)
906+
(type_arguments (type_identifier) (type_identifier)))))
907+
(class_body)))
908+
909+
=======================================
910+
Classes with extensions
911+
=======================================
912+
913+
class A extends B<C>(D) implements C {}
914+
export class A extends B<C>(D) implements C {}
915+
916+
class A extends B<C>(D)<E> implements C {}
917+
export class A extends B<C>(D)<E> implements C {}
918+
919+
export class A extends B<C, D> implements C {}
920+
921+
---
922+
923+
(program
924+
(class_declaration
925+
(type_identifier)
926+
(class_heritage
927+
(extends_clause
928+
(functional_extension
929+
(generic_type
930+
(type_identifier)
931+
(type_arguments
932+
(type_identifier)))
933+
(arguments
934+
(identifier))))
935+
(implements_clause
936+
(type_identifier)))
937+
(class_body))
938+
(export_statement
939+
(class_declaration
940+
(type_identifier)
941+
(class_heritage
942+
(extends_clause
943+
(functional_extension
944+
(generic_type
945+
(type_identifier)
946+
(type_arguments
947+
(type_identifier)))
948+
(arguments
949+
(identifier))))
950+
(implements_clause
951+
(type_identifier)))
952+
(class_body)))
953+
(class_declaration
954+
(type_identifier)
955+
(class_heritage
956+
(extends_clause
957+
(functional_extension
958+
(generic_type
959+
(type_identifier)
960+
(type_arguments
961+
(type_identifier)))
962+
(arguments
963+
(identifier))
964+
(type_arguments
965+
(type_identifier))))
966+
(implements_clause
967+
(type_identifier)))
968+
(class_body))
969+
(export_statement
970+
(class_declaration
971+
(type_identifier)
972+
(class_heritage
973+
(extends_clause
974+
(functional_extension
975+
(generic_type
976+
(type_identifier)
977+
(type_arguments
978+
(type_identifier)))
979+
(arguments
980+
(identifier))
981+
(type_arguments
982+
(type_identifier))))
983+
(implements_clause
984+
(type_identifier)))
985+
(class_body)))
986+
(export_statement
987+
(class_declaration
988+
(type_identifier)
989+
(class_heritage
990+
(extends_clause
991+
(generic_type
992+
(type_identifier)
993+
(type_arguments
994+
(type_identifier)
995+
(type_identifier))))
996+
(implements_clause
997+
(type_identifier)))
998+
(class_body))))

common/corpus/types.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ interface Enum extends Bar, Baz, funThatEvalsToInterface() {
498498
(extends_clause
499499
(type_identifier)
500500
(type_identifier)
501-
(call_expression (identifier) (arguments)))
501+
(functional_extension (type_identifier) (arguments)))
502502
(object_type
503503
(method_signature
504504
(accessibility_modifier)

common/define-grammar.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module.exports = function defineGrammar(dialect) {
3636
[$.mapped_type_clause, $.primary_expression],
3737
[$.accessibility_modifier, $.primary_expression],
3838
['unary_void', $.expression],
39-
['extends_type', $.primary_expression],
39+
[$._extends_type, $.extends_clause, $.primary_expression],
4040
['unary', 'assign'],
4141
['declaration', $.expression],
4242
[$.predefined_type, $.unary_expression],
@@ -255,7 +255,7 @@ module.exports = function defineGrammar(dialect) {
255255
previous,
256256
seq('export', 'type', $.export_clause),
257257
seq('export', '=', $.identifier, $._semicolon),
258-
seq('export', 'as', 'namespace', $.identifier, $._semicolon)
258+
seq('export', 'as', 'namespace', $.identifier, $._semicolon),
259259
),
260260

261261
non_null_expression: $ => prec.left('unary', seq(
@@ -458,17 +458,20 @@ module.exports = function defineGrammar(dialect) {
458458
field('body', $.object_type)
459459
),
460460

461-
extends_clause: $ => prec('extends_type', seq(
461+
_extends_type: $ => choice(
462+
$._type_identifier,
463+
$.nested_type_identifier,
464+
$.generic_type,
465+
),
466+
functional_extension: $ => seq($._extends_type, $.arguments, optional($.type_arguments)),
467+
468+
extends_clause: $ => seq(
462469
'extends',
463470
commaSep1(choice(
464-
prec('extends_type', choice(
465-
$._type_identifier,
466-
$.nested_type_identifier,
467-
$.generic_type
468-
)),
471+
choice($._extends_type, $.functional_extension),
469472
$.expression
470473
))
471-
)),
474+
),
472475

473476
enum_declaration: $ => seq(
474477
optional('const'),

0 commit comments

Comments
 (0)