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], [$.attribute_definition_clause, $._attribute_reference],
[$.component_choice_list, $.discrete_choice], [$.component_choice_list, $.discrete_choice],
[$.component_choice_list, $.positional_array_aggregate], [$.component_choice_list, $.positional_array_aggregate],
[$.discriminant_association, $._parenthesized_expression],
], ],
inline: $ => [ inline: $ => [
$._name_not_function_call, $._name_not_function_call,
@ -413,10 +414,16 @@ module.exports = grammar({
field('subtype_mark', $._name_not_function_call), field('subtype_mark', $._name_not_function_call),
optional($._constraint), optional($._constraint),
), ),
discriminant_constraint: $ => seq( // RM 3.7.1 discriminant_constraint: $ => choice( // RM 3.7.1
'(', // If we have a single positional discriminant, it can be an
comma_separated_list_of($.discriminant_association), // 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 discriminant_association: $ => seq( // RM 3.7.1
optional(seq( optional(seq(

View File

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

View File

@ -2588,9 +2588,29 @@
"multiple": true, "multiple": true,
"required": true, "required": true,
"types": [ "types": [
{
"type": "case_expression",
"named": true
},
{
"type": "declare_expression",
"named": true
},
{ {
"type": "discriminant_association", "type": "discriminant_association",
"named": true "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 (handled_sequence_of_statements
(null_statement))))) (null_statement)))))
======================== ================================================================================
Record with discr Record with discr
======================== ================================================================================
procedure Proc is procedure Proc is
type Rec (Len : Natural) is null record; type Rec (Len : Natural) is null record;
R : Rec (0); R : Rec (0);
R2 : Rec (if N > 0 then 1 else 0);
begin begin
null; null;
end; end;
-------- --------------------------------------------------------------------------------
(compilation (compilation
(compilation_unit (compilation_unit
@ -540,7 +541,23 @@ end;
(identifier) (identifier)
(identifier) (identifier)
(discriminant_constraint (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 (expression
(term (term
(numeric_literal))))))) (numeric_literal)))))))