From 24c34a66ccc61a3301f3e3c25b9e384c552d1f48 Mon Sep 17 00:00:00 2001 From: Kamil Jakubus Date: Fri, 27 Sep 2024 11:31:20 +0200 Subject: [PATCH] parser: add generic type and expression Fixes #84 --- grammar.js | 17 ++++++++++++++++- test/corpus/basic.txt | 14 +++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/grammar.js b/grammar.js index acd857c..6950b86 100644 --- a/grammar.js +++ b/grammar.js @@ -243,8 +243,23 @@ module.exports = grammar({ ), class_type: ($) => seq(kw("CLASS"), choice($.identifier, $.qualified_name)), + generic_parameter: ($) => seq($.identifier, $.type_tuning), + generic_expression: ($) => + seq( + "<", + _list(choice($.identifier, $.qualified_name, $.generic_parameter), ","), + ">" + ), + generic_type: ($) => + seq(choice($.identifier, $.qualified_name), $.generic_expression), _type: ($) => - choice($.primitive_type, $.identifier, $.qualified_name, $.class_type), + choice( + $.primitive_type, + $.identifier, + $.qualified_name, + $.class_type, + $.generic_type + ), when_expression: ($) => seq(kw("WHEN"), $._expression), assignment: ($) => diff --git a/test/corpus/basic.txt b/test/corpus/basic.txt index 5661736..bfe7180 100644 --- a/test/corpus/basic.txt +++ b/test/corpus/basic.txt @@ -2164,6 +2164,9 @@ VAR INT x = a + b , y = a - b, z = x - y. /* Indeterminate integer array initialized using expressions */ VAR INT[ ] x = [funct( ), a + b]. +/* Generic type */ +var List ListOfAnts. + -------------------------------------------------------------------------------- (source_code @@ -2472,7 +2475,16 @@ VAR INT[ ] x = [funct( ), a + b]. (arguments)) (additive_expression (identifier) - (identifier))))))) + (identifier)))))) + (comment) + (var_statement + (type_tuning + (generic_type + (identifier) + (generic_expression + (identifier)))) + (variable + (identifier)))) ================================================================================ RUN statement