Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum grammar #159

Merged
merged 1 commit into from
Dec 1, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/items/enumerations.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
# Enumerations

> **<sup>Syntax</sup>**
> _Enumeration_ :
> &nbsp;&nbsp; `enum`
> [IDENTIFIER]&nbsp;
> [_Generics_]<sup>?</sup>
> [_WhereClause_]<sup>?</sup>
> `{` _EnumItems_<sup>?</sup> `}`
>
> _EnumItems_ :
> &nbsp;&nbsp; _EnumItem_ ( `,` _EnumItem_ )<sup>\*</sup> `,`<sup>?</sup>
>
> _EnumItem_ :
> &nbsp;&nbsp; _OuterAttribute_<sup>\*</sup>
> &nbsp;&nbsp; [IDENTIFIER]&nbsp;( _EnumItemTuple_ | _EnumItemStruct_
> | _EnumItemDiscriminant_ )<sup>?</sup>
>
> _EnumItemTuple_ :
> &nbsp;&nbsp; `(` [_TupleFields_]<sup>?</sup> `)`
>
> _EnumItemStruct_ :
> &nbsp;&nbsp; `{` [_StructFields_]<sup>?</sup> `}`
>
> _EnumItemDiscriminant_ :
> &nbsp;&nbsp; `=` [_Expression_]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL it can be an expression, not just a literal.


An _enumeration_ is a simultaneous definition of a nominal [enumerated type] as
well as a set of *constructors*, that can be used to create or pattern-match
values of the corresponding enumerated type.
Expand Down Expand Up @@ -67,3 +92,10 @@ let x = Foo::Baz as u32; // x is now 123u32

This only works as long as none of the variants have data attached. If it were
`Baz(i32)`, this is disallowed.

[IDENTIFIER]: identifiers.html
[_Generics_]: items.html#type-parameters
[_WhereClause_]: items.html#type-parameters
[_Expression_]: expressions.html
[_TupleFields_]: items/structs.html
[_StructFields_]: items/structs.html