Skip to content

Commit b1bf482

Browse files
authored
Merge pull request #257 from guillaumebrunerie/instantiation-expressions
feat: add instantiation expressions
2 parents e5fa28f + 58c8f46 commit b1bf482

File tree

9 files changed

+256777
-253373
lines changed

9 files changed

+256777
-253373
lines changed

common/corpus/expressions.txt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,54 @@ foo! += bar;
214214
(non_null_expression
215215
(parenthesized_expression (identifier)))
216216
(identifier))))
217+
218+
==================================
219+
Generic calls
220+
==================================
221+
222+
f<T>(x)
223+
224+
---
225+
226+
(program
227+
(expression_statement
228+
(call_expression
229+
(identifier)
230+
(type_arguments
231+
(type_identifier))
232+
(arguments
233+
(identifier)))))
234+
235+
==================================
236+
Instantiation expressions
237+
==================================
238+
239+
const makeHammerBox = makeBox<Hammer>;
240+
const makeStringBox = makeBox<string>;
241+
const ErrorMap = Map<string, Error>;
242+
243+
---
244+
245+
(program
246+
(lexical_declaration
247+
(variable_declarator
248+
(identifier)
249+
(instantiation_expression
250+
(identifier)
251+
(type_arguments
252+
(type_identifier)))))
253+
(lexical_declaration
254+
(variable_declarator
255+
(identifier)
256+
(instantiation_expression
257+
(identifier)
258+
(type_arguments
259+
(predefined_type)))))
260+
(lexical_declaration
261+
(variable_declarator
262+
(identifier)
263+
(instantiation_expression
264+
(identifier)
265+
(type_arguments
266+
(predefined_type)
267+
(type_identifier))))))

common/corpus/types.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,8 @@ class E extends funThatEvalsToClass()<
602602
Y,
603603
> {}
604604

605+
class F extends G, H<T> {}
606+
605607
---
606608

607609
(program
@@ -621,6 +623,15 @@ class E extends funThatEvalsToClass()<
621623
(extends_clause
622624
(call_expression (identifier) (arguments))
623625
(type_arguments (type_identifier) (type_identifier))))
626+
(class_body))
627+
(class_declaration
628+
(type_identifier)
629+
(class_heritage
630+
(extends_clause
631+
(identifier)
632+
(identifier)
633+
(type_arguments
634+
(type_identifier))))
624635
(class_body)))
625636

626637
=======================================
@@ -1599,3 +1610,24 @@ type X2 = (typeof Y)[keyof typeof Z];
15991610
(index_type_query
16001611
(type_query
16011612
(identifier))))))
1613+
1614+
=========================
1615+
Type of instantiation expressions
1616+
=========================
1617+
1618+
// This is a special construct, different from both "typeof (f<T>)" and
1619+
// "(typeof f)<T>" (which are both invalid)
1620+
type T = typeof f<T>;
1621+
1622+
---
1623+
1624+
(program
1625+
(comment)
1626+
(comment)
1627+
(type_alias_declaration
1628+
(type_identifier)
1629+
(type_query
1630+
(instantiation_expression
1631+
(identifier)
1632+
(type_arguments
1633+
(type_identifier))))))

common/define-grammar.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,16 @@ module.exports = function defineGrammar(dialect) {
1919
precedences: ($, previous) => previous.concat([
2020
[
2121
'call',
22+
'instantiation',
2223
'unary',
2324
'binary',
2425
$.await_expression,
2526
$.arrow_function,
2627
],
28+
[
29+
'extends',
30+
'instantiation',
31+
],
2732
[
2833
$.intersection_type,
2934
$.union_type,
@@ -53,6 +58,7 @@ module.exports = function defineGrammar(dialect) {
5358
[$._type_query_subscript_expression, $.subscript_expression],
5459
[$._type_query_subscript_expression, $.primary_expression],
5560
[$._type_query_call_expression, $.primary_expression],
61+
[$._type_query_instantiation_expression, $.primary_expression],
5662
[$.type_query, $.primary_expression],
5763
[$.override_modifier, $.primary_expression],
5864
[$.decorator_call_expression, $.decorator],
@@ -61,11 +67,11 @@ module.exports = function defineGrammar(dialect) {
6167
]),
6268

6369
conflicts: ($, previous) => previous.concat([
64-
[$.call_expression, $.binary_expression],
65-
[$.call_expression, $.binary_expression, $.unary_expression],
66-
[$.call_expression, $.binary_expression, $.update_expression],
70+
[$.call_expression, $.instantiation_expression, $.binary_expression],
71+
[$.call_expression, $.instantiation_expression, $.binary_expression, $.unary_expression],
72+
[$.call_expression, $.instantiation_expression, $.binary_expression, $.update_expression],
6773
[$.call_expression, $.binary_expression, $.type_assertion],
68-
[$.call_expression, $.binary_expression, $.await_expression],
74+
[$.call_expression, $.instantiation_expression, $.binary_expression, $.await_expression],
6975

7076
// This appears to be necessary to parse a parenthesized class expression
7177
[$.class],
@@ -208,6 +214,7 @@ module.exports = function defineGrammar(dialect) {
208214
const choices = [
209215
$.as_expression,
210216
$.satisfies_expression,
217+
$.instantiation_expression,
211218
$.internal_module,
212219
];
213220

@@ -447,6 +454,11 @@ module.exports = function defineGrammar(dialect) {
447454
$._type
448455
)),
449456

457+
instantiation_expression: $ => prec('instantiation', seq(
458+
$.expression,
459+
field('type_arguments', $.type_arguments),
460+
)),
461+
450462
class_heritage: $ => choice(
451463
seq($.extends_clause, optional($.implements_clause)),
452464
$.implements_clause
@@ -463,11 +475,13 @@ module.exports = function defineGrammar(dialect) {
463475

464476
extends_clause: $ => seq(
465477
'extends',
466-
commaSep1(seq(
478+
commaSep1($._extends_clause_single),
479+
),
480+
481+
_extends_clause_single: $ => prec('extends', seq(
467482
field('value', $.expression),
468483
field('type_arguments', optional($.type_arguments))
469-
)),
470-
),
484+
)),
471485

472486
implements_clause: $ => seq(
473487
'implements',
@@ -786,12 +800,22 @@ module.exports = function defineGrammar(dialect) {
786800
)),
787801
field('arguments', $.arguments)
788802
),
803+
_type_query_instantiation_expression: $ => seq(
804+
field('function', choice(
805+
$.import,
806+
$.identifier,
807+
alias($._type_query_member_expression, $.member_expression),
808+
alias($._type_query_subscript_expression, $.subscript_expression)
809+
)),
810+
field('type_arguments', $.type_arguments)
811+
),
789812
type_query: $ => prec.right(seq(
790813
'typeof',
791814
choice(
792815
alias($._type_query_subscript_expression, $.subscript_expression),
793816
alias($._type_query_member_expression, $.member_expression),
794817
alias($._type_query_call_expression, $.call_expression),
818+
alias($._type_query_instantiation_expression, $.instantiation_expression),
795819
$.identifier
796820
),
797821
)),

0 commit comments

Comments
 (0)