swiftlang/swift-book

Grammar missing optional parens for type identifiers in type inheritance clause

Opened this issue · 0 comments

Location

https://github.com/apple/swift-book/blob/main/TSPL.docc/ReferenceManual/Types.md?plain=1#L1279

Description

The grammar does not currently allow for parenthesized type identifiers in type inheritance clauses.

Current grammar:

type-inheritance-clause → : type-inheritance-list
type-inheritance-list → attributes? type-identifier | attributes? type-identifier , type-inheritance-list

Program that isn't derivable with current grammar:

protocol DefaultItem {}

protocol Item: (DefaultItem) {}

This applies to type inheritance clauses in general.
See an example with class inheritance:

class Class {}

class OtherClass: (Class) {}

Correction

Restructure the grammar to allow for parenthesized type identifiers in type inheritance classes.
Possible Solution:

type-inheritance-clause → : type-inheritance-list
type-inheritance-list → attributes? type-identifier | attributes? ( type-identifier ) | attributes? type-identifier , type-inheritance-list | attributes? ( type-identifier ), type-inheritance-list

This is just one solution but there are other, possibly cleaner, options.
Can also consider changing the productions for type to allow:
type -> type-identifier
along with
type -> (type-identifier)
This is cleaner but would allow any type-identifier to be parenthesized which may cause unintended side effects in other areas of the language.