From 5e5c9f5bc6aaf1f6f44c4fdf81993aeeed2c931e Mon Sep 17 00:00:00 2001 From: Emmanuel Briot Date: Fri, 21 Oct 2022 17:12:31 +0200 Subject: [PATCH] Support for record types --- corpus/records.txt | 332 +++++++++++++++++++++++++++++++++++++++++++++ grammar.js | 103 +++++++++++--- 2 files changed, 416 insertions(+), 19 deletions(-) create mode 100644 corpus/records.txt diff --git a/corpus/records.txt b/corpus/records.txt new file mode 100644 index 0000000..b0c5b34 --- /dev/null +++ b/corpus/records.txt @@ -0,0 +1,332 @@ +================================================================================ +null record +================================================================================ + +package P is + type R is null record; +end; + +-------------------------------------------------------------------------------- + +(compilation + (compilation_unit + (package_specification + (name + (identifier)) + (type_declaration + (full_type_declaration + (identifier) + (type_definition + (record_type_definition + (record_definition)))))))) + +================================================================================ +records +================================================================================ + +package P is + type R2 is record + A : Integer; + -- B : Integer range 0 .. 2; + -- null; + end record; + + for R2 use record + A at 0 range 0 .. 31; + end record; +end; + +-------------------------------------------------------------------------------- + +(compilation + (compilation_unit + (package_specification + (name + (identifier)) + (type_declaration + (full_type_declaration + (identifier) + (type_definition + (record_type_definition + (record_definition + (component_list + (component_item + (component_declaration + (defining_identifier_list + (identifier)) + (component_definition + (subtype_indication + (name + (identifier))))))) + (comment) + (comment)))))) + (aspect_clause + (record_representation_clause + (name + (identifier)) + (component_clause + (name + (identifier)) + (expression + (relation + (simple_expression + (term + (factor + (primary + (numeric_literal))))))) + (simple_expression + (term + (factor + (primary + (numeric_literal))))) + (simple_expression + (term + (factor + (primary + (numeric_literal))))))))))) + +================================================================================ +Multiple fields on one line +================================================================================ + +package P is + type R is record + A, B : Integer; + end record; +end; + +-------------------------------------------------------------------------------- + +(compilation + (compilation_unit + (package_specification + (name + (identifier)) + (type_declaration + (full_type_declaration + (identifier) + (type_definition + (record_type_definition + (record_definition + (component_list + (component_item + (component_declaration + (defining_identifier_list + (identifier) + (identifier)) + (component_definition + (subtype_indication + (name + (identifier))))))))))))))) + +================================================================================ +Discriminated +================================================================================ + +package P is + type R (A : Integer; B : Integer) is record + F : Float; + end record; +end; + +-------------------------------------------------------------------------------- + +(compilation + (compilation_unit + (package_specification + (name + (identifier)) + (type_declaration + (full_type_declaration + (identifier) + (known_discriminant_part + (discriminant_specification_list + (discriminant_specification + (defining_identifier_list + (identifier)) + (name + (identifier))) + (discriminant_specification + (defining_identifier_list + (identifier)) + (name + (identifier))))) + (type_definition + (record_type_definition + (record_definition + (component_list + (component_item + (component_declaration + (defining_identifier_list + (identifier)) + (component_definition + (subtype_indication + (name + (identifier))))))))))))))) + +================================================================================ +tagged +================================================================================ + +package P is + type T is abstract tagged limited null record; + type T2 is new T with record + F : Integer; + end record; +end; + +-------------------------------------------------------------------------------- + +(compilation + (compilation_unit + (package_specification + (name + (identifier)) + (type_declaration + (full_type_declaration + (identifier) + (type_definition + (record_type_definition + (record_definition))))) + (type_declaration + (full_type_declaration + (identifier) + (type_definition + (derived_type_definition + (subtype_indication + (name + (identifier))) + (record_extension_part + (record_definition + (component_list + (component_item + (component_declaration + (defining_identifier_list + (identifier)) + (component_definition + (subtype_indication + (name + (identifier)))))))))))))))) + +================================================================================ +Variant +================================================================================ + +package P is + type R (A : Integer) is record + case A is + when 0 | 1 .. 2 => + B : Integer; + when others => + null; + end case; + end record; +end; + +-------------------------------------------------------------------------------- + + (compilation + (compilation_unit + (package_specification + (name + (identifier)) + (type_declaration + (full_type_declaration + (identifier) + (known_discriminant_part + (discriminant_specification_list + (discriminant_specification + (defining_identifier_list + (identifier)) + (name + (identifier))))) + (type_definition + (record_type_definition + (record_definition + (component_list + (variant_part + (identifier) + (variant_list + (variant + (discrete_choice_list + (discrete_choice + (expression + (relation + (simple_expression + (term + (factor + (primary + (numeric_literal)))))))) + (discrete_choice + (range_g + (simple_expression + (term + (factor + (primary + (numeric_literal))))) + (simple_expression + (term + (factor + (primary + (numeric_literal)))))))) + (component_list + (component_item + (component_declaration + (defining_identifier_list + (identifier)) + (component_definition + (subtype_indication + (name + (identifier)))))))) + (variant + (discrete_choice_list + (discrete_choice + (subtype_indication + (name + (identifier))))) + (component_list))))))))))))) + + +================================================================================ +interface +================================================================================ + +package P is + type R is interface; + type R2 is interface and Intf1; + type R3 is new Root and R with null record; +end; + +-------------------------------------------------------------------------------- + + (compilation + (compilation_unit + (package_specification + (name + (identifier)) + (type_declaration + (full_type_declaration + (identifier) + (type_definition + (interface_type_definition)))) + (type_declaration + (full_type_declaration + (identifier) + (type_definition + (interface_type_definition + (interface_list + (name + (identifier))))))) + (type_declaration + (full_type_declaration + (identifier) + (type_definition + (derived_type_definition + (subtype_indication + (name + (identifier))) + (interface_list + (name + (identifier))) + (record_extension_part + (record_definition))))))))) diff --git a/grammar.js b/grammar.js index 22fa79a..54658cb 100644 --- a/grammar.js +++ b/grammar.js @@ -62,7 +62,11 @@ module.exports = grammar({ [$.generic_package_declaration, $._package_declaration], [$.attribute_definition_clause, $.attribute_reference], - [$.record_extension_part, $.derived_type_definition], + + // 'type' identifier 'is' 'new' subtype_indication . 'with' + // which could be either a record_extension_part or + // an aspect_specification. + [$.derived_type_definition], // 'for' name 'use' '(' name . '=>' ... // The name could either be from a primary or a subtype_indication. @@ -71,6 +75,7 @@ module.exports = grammar({ // 'for' name 'use' '(' 'for' identifier 'in' name . 'use' [$.iterator_specification, $.subtype_indication], + ], rules: { @@ -575,7 +580,7 @@ module.exports = grammar({ seq( reservedWord('type'), $.identifier, -// optional($.known_discriminant_part), + optional($.known_discriminant_part), reservedWord('is'), $.type_definition, optional($.aspect_specification), @@ -584,15 +589,34 @@ module.exports = grammar({ // $.task_type_declaration, // $.protected_type_declaration, ), + known_discriminant_part: $ => seq( + '(', + $.discriminant_specification_list, + ')', + ), + discriminant_specification_list: $ => + list_of(';', $.discriminant_specification), + discriminant_specification: $ => seq( + $.defining_identifier_list, + ':', + choice( + seq( + optional($.null_exclusion), + $.name, + ), + $.access_definition, + ), + optional($.assign_value), + ), type_definition: $ => choice( $.enumeration_type_definition, $.integer_type_definition, $.real_type_definition, $.array_type_definition, -// $.record_type_definition, + $.record_type_definition, // $.access_type_definition, $.derived_type_definition, -// $.interface_type_definition, + $.interface_type_definition, ), array_type_definition: $ => choice( $.unconstrained_array_definition, @@ -683,21 +707,44 @@ module.exports = grammar({ '..', $.simple_expression, ), - derived_type_definition: $ => prec.left(seq( + derived_type_definition: $ => seq( optional(reservedWord('abstract')), optional(reservedWord('limited')), reservedWord('new'), $.subtype_indication, optional(seq( -// optional(seq( -// reservedWord('and'), -// $.interface_list, -// )), + optional(seq( + reservedWord('and'), + $.interface_list, + )), $.record_extension_part, )), - )), + ), + interface_type_definition: $ => seq( + optional(choice( + reservedWord('limited'), + reservedWord('task'), + reservedWord('protected'), + reservedWord('synchronized'), + )), + reservedWord('interface'), + optional(seq( + reservedWord('and'), + $.interface_list, + )), + ), + interface_list: $ => + list_of(reservedWord('and'), $.name), record_extension_part: $ => seq( - reservedWord('with'), + reservedWord('with'), // record_extension_part in Ada grammar + $.record_definition, + ), + record_type_definition: $ => seq( + optional(seq( + optional(reservedWord('abstract')), + reservedWord('tagged'), + )), + optional(reservedWord('limited')), $.record_definition, ), record_definition: $ => choice( @@ -715,13 +762,16 @@ module.exports = grammar({ ), component_list: $ => choice( repeat1($.component_item), -// seq( -// optional($.component_item), -// $.variant_part, -// ), - reservedWord('null'), + seq( + optional($.component_item), + $.variant_part, + ), + seq( + reservedWord('null'), + reservedWord(';'), + ), ), - component_item: $ => seq( + component_item: $ => choice( $.component_declaration, $.aspect_clause, ), @@ -740,8 +790,6 @@ module.exports = grammar({ // $.access_definition, ), ), - - abstract_subprogram_declaration: $ => seq( optional($.overriding_indicator), $.subprogram_specification, @@ -1134,6 +1182,7 @@ module.exports = grammar({ reservedWord('end'), reservedWord('record'), optional($.name), + ';', )), renaming_declaration: $ => choice( // $.object_renaming_declaration, @@ -1194,5 +1243,21 @@ module.exports = grammar({ optional($.aspect_specification), ';', ), + variant_part: $ => seq( + reservedWord('case'), + $._direct_name, + reservedWord('is'), + $.variant_list, + reservedWord('end'), + reservedWord('case'), + ';', + ), + variant_list: $ => repeat1($.variant), + variant: $ => seq( + reservedWord('when'), + $.discrete_choice_list, + '=>', + $.component_list, + ), } });