Skip to content

allow fparser2 list classes to contain a single class #170

@rupertford

Description

@rupertford

fparser2 generates a list class whenever there is a Fortran rule that specifies a list. In the current implementation a list object is only created if there are two or more items in the list. If there is only one item then the item itself is returned, rather than a list containing the item.

As an example, consider the Fortran2003 rule for a calling a subroutine and its arguments ...

R1218 call-stmt is CALL procedure-designator [ ( [ actual-arg-spec-list ] ) ]
R1220 actual-arg-spec is [ keyword = ] actual-arg

If there are two arguments then we will get the following hierarchy

Call_Stmt
    Actual_Arg_Spec_List
        Actual_Arg
        Actual_Arg

but if there is only one we will get

Call_Stmt
    Actual_Arg

There is nothing wrong with this is terms of correctness. The problem comes when trying to traverse the parse tree. If you want to get access to all Arguments then you need to add in extra code to deal with the case when there is only one.

This is the situation for all generated lists in fparser2 and there are many of them (just search for *-list in the spec).

The proposed solution is to have a list object created when there are 1 or more items in the list. I don't think there is any fundamental reason why this can't be done in fparser2 and on face value it seems quite a simple change but it would nee to be investigated.

This would mean a change to the generated fparser2 parse tree which would affect anyone who has written code that relies on the current structure but I think it is worth it.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions