Allow if-expression for discriminant constraints

The rule (not visible in the RM grammar) is that
if we have a single positional discriminant, we
can omit the extra pair of parenthesis.
This commit is contained in:
Emmanuel Briot 2022-12-14 13:49:02 +01:00
parent 7fe1b1edb4
commit faa006e5fd
5 changed files with 32335 additions and 32212 deletions

View File

@ -107,6 +107,7 @@ module.exports = grammar({
[$.attribute_definition_clause, $._attribute_reference],
[$.component_choice_list, $.discrete_choice],
[$.component_choice_list, $.positional_array_aggregate],
[$.discriminant_association, $._parenthesized_expression],
],
inline: $ => [
$._name_not_function_call,
@ -413,10 +414,16 @@ module.exports = grammar({
field('subtype_mark', $._name_not_function_call),
optional($._constraint),
),
discriminant_constraint: $ => seq( // RM 3.7.1
'(',
comma_separated_list_of($.discriminant_association),
')',
discriminant_constraint: $ => choice( // RM 3.7.1
// If we have a single positional discriminant, it can be an
// if-expression without additional parenthesis "A : R (if cond then
// 1 else 0)" but otherwise extra parenthesis are needed.
$._parenthesized_expression, // not in ARM
seq(
'(',
comma_separated_list_of($.discriminant_association),
')',
),
),
discriminant_association: $ => seq( // RM 3.7.1
optional(seq(

View File

@ -1868,40 +1868,49 @@
]
},
"discriminant_constraint": {
"type": "SEQ",
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "("
"type": "SYMBOL",
"name": "_parenthesized_expression"
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "discriminant_association"
"type": "STRING",
"value": "("
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "discriminant_association"
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "discriminant_association"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "discriminant_association"
}
]
}
]
}
}
]
},
{
"type": "STRING",
"value": ")"
}
]
},
{
"type": "STRING",
"value": ")"
}
]
},
@ -15331,6 +15340,10 @@
[
"component_choice_list",
"positional_array_aggregate"
],
[
"discriminant_association",
"_parenthesized_expression"
]
],
"precedences": [],

View File

@ -2588,9 +2588,29 @@
"multiple": true,
"required": true,
"types": [
{
"type": "case_expression",
"named": true
},
{
"type": "declare_expression",
"named": true
},
{
"type": "discriminant_association",
"named": true
},
{
"type": "expression",
"named": true
},
{
"type": "if_expression",
"named": true
},
{
"type": "quantified_expression",
"named": true
}
]
}

64430
src/parser.c

File diff suppressed because it is too large Load Diff

View File

@ -508,18 +508,19 @@ end;
(handled_sequence_of_statements
(null_statement)))))
========================
================================================================================
Record with discr
========================
================================================================================
procedure Proc is
type Rec (Len : Natural) is null record;
R : Rec (0);
R2 : Rec (if N > 0 then 1 else 0);
begin
null;
end;
--------
--------------------------------------------------------------------------------
(compilation
(compilation_unit
@ -540,7 +541,23 @@ end;
(identifier)
(identifier)
(discriminant_constraint
(discriminant_association
(expression
(term
(numeric_literal)))))
(object_declaration
(identifier)
(identifier)
(discriminant_constraint
(if_expression
(expression
(term
(identifier))
(relational_operator)
(term
(numeric_literal)))
(expression
(term
(numeric_literal)))
(expression
(term
(numeric_literal)))))))