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

Add EBNF for AST to book #1066

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
55 changes: 28 additions & 27 deletions book/src/contributing/ast_ebnf.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,17 @@ function types. Lastly we have named types defined by items, e.g. enums
and structs.

``` ebnf
impl ::= <!!TODO!!>
goal ::= <!!TODO!!>
generic_value ::=
| "'" ident
| ty
| expr

impl ::=
| "self"
| goal
| "dyn"
| <!TODO!>
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess we can remove this TODO altogether?
The category impl is a bit weird since it has no Rust syntax, but I think it's fine

goal ::= ident "<" (generic_value ",")* ">"

ty ::=
| "bool"
Expand Down Expand Up @@ -61,7 +70,7 @@ pat ::=
| ("[" (expr ",")* "]" | "[" expr ";" int "]")
| "&" pat
| literal
| <!!TODO(PBinding): ident ":=" path !!>
| ident "@" pat
cmester0 marked this conversation as resolved.
Show resolved Hide resolved
```

The simple expressions are literals, local or global variables, type
Expand All @@ -75,28 +84,21 @@ global_var ::= ident

modifiers ::= "" | "unsafe" modifiers | "const" modifiers

macro_name ::= string
macro_args ::= (.)*

lhs ::= pat
param ::= pat

monadic_binding ::= <!!TODO!!>
monad ::= <!!TODO!!>

expr ::=
| "if" expr "{" expr "}" ("else" "{" expr "}")?
| expr "(" (expr ",")* ")"
| literal
| "[" (expr ",")* "]" | "[" expr ";" int "]"
| ident "(" (expr ",")* ")" | ident "{" (ident ":"expr ";")* "}" | ident "{" (ident ":"expr ";")* ".." expr "}"
| "match" expr "{" (("|" pat)* "=>" (expr "," | "{" expr "}"))* "}"
| "let" pat (":" ty)? ":=" expr ";" expr | monadic_binding "<" monad ">" "(" "|" pat "|" expr "," expr ")"
| "let" pat (":" ty)? ":=" expr ";" expr
| modifiers "{" expr "}"
| local_var
| global_var
| expr "as" ty
| macro_name "!" "(" macro_args ")"
| loops_expr
| lhs "=" expr
| "return" expr
Expand All @@ -123,10 +125,8 @@ item ::=
| "type" ident "=" ty
| "enum" ident "=" "{" (ident ("(" (ty)* ")")? ",")* "}"
| "struct" ident "=" "{" (ident ":" ty ",")* "}"
| <!!TODO(macros): "public_nat_mod!()" | "bytes!()" | "public_bytes" | "array" | "unsigned_public_integer" !!>
| "trait" ident "{" (trait_item)* "}"
| "impl" ("<" (generics ",")* ">")? ident "for" ty "{" (impl_item)* "}"
| <!!TODO(Alias)!!>
| "use" path ";"
```

Expand All @@ -150,8 +150,17 @@ literal ::=

ident ::= string

impl ::= <!!TODO!!>
goal ::= <!!TODO!!>
generic_value ::=
| "'" ident
| ty
| expr

impl ::=
| "self"
| goal
| "dyn"
| <!TODO!>
goal ::= ident "<" (generic_value ",")* ">"

ty ::=
| "bool"
Expand All @@ -160,6 +169,7 @@ ty ::=
| "f16" | "f32" | "f64"
| "str"
| (ty ",")*
| ident "(" (ident ",")* ")"
| "[" ty ";" int "]"
| "[" ty "]"
| "*const" ty | "*mut" ty
Expand All @@ -168,7 +178,7 @@ ty ::=
| (ty "->")* ty
| impl "::" ident
| "impl" ty
| (goal)*
| "dyn" (goal)*

pat ::=
| "_"
Expand All @@ -178,35 +188,28 @@ pat ::=
| ("[" (expr ",")* "]" | "[" expr ";" int "]")
| "&" pat
| literal
| <!!TODO(PBinding): ident ":=" path !!>
| ident "@" pat

local_var ::= ident
global_var ::= ident

modifiers ::= "" | "unsafe" modifiers | "const" modifiers

macro_name ::= string
macro_args ::= (.)*

lhs ::= pat
param ::= pat

monadic_binding ::= <!!TODO!!>
monad ::= <!!TODO!!>

expr ::=
| "if" expr "{" expr "}" ("else" "{" expr "}")?
| expr "(" (expr ",")* ")"
| literal
| "[" (expr ",")* "]" | "[" expr ";" int "]"
| ident "(" (expr ",")* ")" | ident "{" (ident ":"expr ";")* "}" | ident "{" (ident ":"expr ";")* ".." expr "}"
| "match" expr "{" (("|" pat)* "=>" (expr "," | "{" expr "}"))* "}"
| "let" pat (":" ty)? ":=" expr ";" expr | monadic_binding "<" monad ">" "(" "|" pat "|" expr "," expr ")"
| "let" pat (":" ty)? ":=" expr ";" expr
| modifiers "{" expr "}"
| local_var
| global_var
| expr "as" ty
| macro_name "!" "(" macro_args ")"
| loops_expr
| lhs "=" expr
| "return" expr
Expand All @@ -228,9 +231,7 @@ item ::=
| "type" ident "=" ty
| "enum" ident "=" "{" (ident ("(" (ty)* ")")? ",")* "}"
| "struct" ident "=" "{" (ident ":" ty ",")* "}"
| <!!TODO(macros): "public_nat_mod!()" | "bytes!()" | "public_bytes" | "array" | "unsigned_public_integer" !!>
| "trait" ident "{" (trait_item)* "}"
| "impl" ("<" (generics ",")* ">")? ident "for" ty "{" (impl_item)* "}"
| <!!TODO(Alias)!!>
| "use" path ";"
```
Loading