Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions common/corpus/functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,28 @@ const lines = new Array<DiffLine>()
(identifier)
(new_expression
(identifier)
(type_arguments (type_identifier))
(call_type_arguments (type_identifier))
(arguments)))))

==================================
Function calls with optional chaining and type arguments
==================================

A?.<B>();
A<B[]>();

---

(program
(expression_statement
(call_expression
(identifier)
(type_arguments (type_identifier))
(call_type_arguments (type_identifier))
(arguments)))
(expression_statement
(call_expression
(identifier)
(call_type_arguments (array_type (type_identifier)))
(arguments))))

==================================
Expand Down
59 changes: 34 additions & 25 deletions common/define-grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const PREC = {
PLUS: 4,
REL: 5,
TIMES: 6,
TYPEOF: 7,
EXTENDS: 7,
NEG: 9,
INC: 10,
Expand All @@ -24,7 +23,8 @@ const PREC = {
MEMBER: 14,
TYPE_ASSERTION: 16,
TYPE_REFERENCE: 16,
CONSTRUCTOR_TYPE: 17
CONSTRUCTOR_TYPE: 17,
TYPEOF: 18
};

module.exports = function defineGrammar(dialect) {
Expand All @@ -39,11 +39,20 @@ module.exports = function defineGrammar(dialect) {
// work.
'||',
$._function_signature_automatic_semicolon,
$._call_type_arguments_closing_bracket
]),

conflicts: ($, previous) => previous.concat([
[$.call_expression, $.binary_expression],
[$.call_expression, $.binary_expression, $.unary_expression],
[$._expression, $.call_expression],
[$._expression, $.arrow_function, $.call_expression],
[$.arrow_function, $.call_expression, $._property_name],
[$._expression, $.arrow_function, $.call_expression, $._property_name],
[$.call_expression, $._property_name],
[$._expression, $.call_expression, $._property_name],
[$._expression, $.call_expression, $.generic_type],
[$._expression, $.call_expression, $.new_expression],
[$._expression, $.arrow_function, $.call_expression, $.new_expression],
[$.call_expression, $.new_expression],

[$.nested_type_identifier, $.nested_identifier],
[$.nested_type_identifier, $.member_expression],
Expand Down Expand Up @@ -105,26 +114,9 @@ module.exports = function defineGrammar(dialect) {
optional($._initializer)
),

call_expression: ($, previous) => choice(
prec(PREC.CALL, seq(
field('function', $._expression),
field('type_arguments', optional($.type_arguments)),
field('arguments', choice($.arguments, $.template_string))
)),
prec(PREC.MEMBER, seq(
field('function', $._primary_expression),
'?.',
field('type_arguments', optional($.type_arguments)),
field('arguments', $.arguments)
))
),
call_expression: $ => callExpression($, "function"),

new_expression: $ => prec.right(PREC.NEW, seq(
'new',
field('constructor', $._primary_expression),
field('type_arguments', optional($.type_arguments)),
field('arguments', optional($.arguments))
)),
new_expression: $ => seq('new', callExpression($, "constructor")),

_augmented_assignment_lhs: ($, previous) => choice(previous, $.non_null_expression),

Expand Down Expand Up @@ -659,8 +651,10 @@ module.exports = function defineGrammar(dialect) {
'void'
),

type_arguments: $ => seq(
'<', commaSep1($._type), optional(','), '>'
type_arguments: $ => typeArguments($),
call_type_arguments: $ => typeArguments(
$,
$._call_type_arguments_closing_bracket
),

object_type: $ => seq(
Expand Down Expand Up @@ -808,6 +802,21 @@ module.exports = function defineGrammar(dialect) {
});
}

function typeArguments($, closingBracket = '>') {
return seq(
'<', commaSep1($._type), optional(','), closingBracket
)
}

function callExpression ($, expressionName) {
return seq(
field(expressionName, $._primary_expression),
optional('?.'),
field('type_arguments', optional($.call_type_arguments)),
field('arguments', choice($.arguments, $.template_string))
)
}

function commaSep1 (rule) {
return sepBy1(',', rule);
}
Expand Down
20 changes: 18 additions & 2 deletions common/scanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ enum TokenType {
TEMPLATE_CHARS,
BINARY_OPERATORS,
FUNCTION_SIGNATURE_AUTOMATIC_SEMICOLON,
CALL_TYPE_ARGUMENTS_CLOSING_BRACKET
};

static void advance(TSLexer *lexer) { lexer->advance(lexer, false); }
Expand Down Expand Up @@ -150,7 +151,22 @@ static inline bool external_scanner_scan(void *payload, TSLexer *lexer, const bo
}

return true;
} else {
return false;
} else if (valid_symbols[CALL_TYPE_ARGUMENTS_CLOSING_BRACKET]) {
lexer->mark_end(lexer);

scan_whitespace_and_comments(lexer);
if (lexer->lookahead != '>') {
return false;
}

advance(lexer);
scan_whitespace_and_comments(lexer);
if (lexer->lookahead == '(') {
lexer->mark_end(lexer);
lexer->result_symbol = CALL_TYPE_ARGUMENTS_CLOSING_BRACKET;
return true;
}
}

return false;
}
Loading