-
Notifications
You must be signed in to change notification settings - Fork 22
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
cmester0
wants to merge
14
commits into
main
Choose a base branch
from
AST-EBNF-documentation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add EBNF for AST to book #1066
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
64c9561
Add EBNF for AST to book
cmester0 9d69c95
Add some description to EBNF
cmester0 1d9da95
Merge branch 'main' into AST-EBNF-documentation
cmester0 8a0c8a9
Update ast_ebnf.md
cmester0 9692b39
Update ast_ebnf.md
cmester0 0bbe2b2
Add EBNF to summary of book
cmester0 5abc947
Update ast_ebnf.md
cmester0 0d12b3d
Update ast_ebnf.md
cmester0 29c4aac
Merge branch 'main' into AST-EBNF-documentation
cmester0 3031479
Update SUMMARY.md
cmester0 66c9f87
Update SUMMARY.md
cmester0 1edfe2f
Make it work for visualization
cmester0 bfbf8ba
Update ast_ebnf.md
cmester0 9ababbb
Backport paper EBNF
cmester0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,243 @@ | ||
We currently take inputs from the following AST (visualization can be done | ||
using <https://rr.red-dove.com/ui>). Literals are strings, numbers and | ||
booleans | ||
|
||
``` ebnf | ||
char ::= [a-zA-Z] | ||
string ::= char* | ||
digit ::= [0-9] | ||
uint ::= digit+ | ||
int ::= ("-")? uint | ||
float ::= int (".")? uint | ||
bool ::= "true" | "false" | ||
|
||
local_var ::= ident | ||
global_var ::= rust-path-identifier | ||
|
||
literal ::= | ||
| "\"" string "\"" | ||
| "'" char "'" | ||
| int | ||
| float | ||
| bool | ||
``` | ||
|
||
We support a number of simple types characters, strings, booleans and | ||
numbers. Number types for integers (8,16,32,64,128 bit or machine sized) | ||
and floats (16,32, or 64 bit). Composite types are tuples, fixed length | ||
lists (arrays), variable length lists (vectors/slices), ptr types, and | ||
function types. Lastly we have named types defined by items, e.g. enums | ||
and structs. | ||
|
||
``` ebnf | ||
ty ::= | ||
| "bool" | ||
| "char" | ||
| "u8" | "u16" | "u32" | "u64" | ||
| "u128" | "usize" | ||
| "i8" | "i16" | "i32" | "i64" | ||
| "i128" | "isize" | ||
| "f16" | "f32" | "f64" | ||
| "str" | ||
| (ty ",")* | ||
| "[" ty ";" int "]" | ||
| "[" ty "]" | ||
| "*const" ty | "*mut" ty | ||
| "*" expr | "*mut" expr | ||
| ident | ||
| (ty "->")* ty | ||
| dyn (goal)+ | ||
``` | ||
|
||
The patterns allowed reflect these types. Wildcard patterns, literal | ||
types, typed patterns, list patterns, record or tuple patterns. | ||
|
||
``` ebnf | ||
pat ::= | ||
| "_" | ||
| ident "{" (ident ":" pat ";")* "}" | ||
| ident "(" (pat ",")* ")" | ||
| (pat "|")* pat | ||
| "[" (pat ",")* "]" | ||
| "&" pat | ||
| literal | ||
| ("&")? ("mut")? ident ("@" pat)? | ||
``` | ||
|
||
The simple expressions are literals, local or global variables, type | ||
casts, assignments and lists. Control flow expressions, if statements, | ||
match statements, loops, return, break and continue. The rest is blocks, | ||
macro calls, lambda functions and borrowing. | ||
|
||
``` ebnf | ||
expr ::= | ||
| "if" expr "{" expr "}" ("else" "{" expr "}")? | ||
| "if" "let" pat (":" ty)? "=" expr "{" expr "}" ("else" "{" expr "}")? | ||
| expr "(" (expr ",")* ")" | ||
| literal | ||
| "[" (expr ",")* "]" | "[" expr ";" int "]" | ||
| ident "{" (ident ":"expr ";")* "}" | ||
| ident "{" (ident ":"expr ";")* ".." expr "}" | ||
| "match" expr guard "{" | ||
(("|" pat)* "=>" (expr "," | "{" expr "}"))* | ||
"}" | ||
| "let" pat (":" ty)? "=" expr ";" expr | ||
| "let" pat (":" ty)? "=" expr "else" "{" expr "}" ";" expr | ||
| modifiers "{" expr "}" | ||
| local_var | ||
| global_var | ||
| expr "as" ty | ||
| "loop" "{" expr "}"$~[\subref{fig:ebnf-iterators}]$ | ||
| "while" "(" expr ")" "{" expr "}"$~[\subref{fig:ebnf-iterators}]$ | ||
| "for" "(" pat "in" expr ")" "{" expr "}"$~[\subref{fig:ebnf-iterators}]$ | ||
| "for" "(" "let" ident "in" expr ".." expr ")" "{" expr "}"$~[\subref{fig:ebnf-iterators}]$ | ||
| "break" expr | ||
| "continue" | ||
| pat "=" expr | ||
| "return" expr | ||
| expr "?" | ||
| "&" ("mut")? expr | ||
| "&" expr "as" "&const _" | ||
| "&mut" expr "as" "&mut _" | ||
| "|" pat "|" expr | ||
``` | ||
|
||
The items supported are functions, type aliasing, enums, structs, trait | ||
definitions and implementations, and imports. | ||
|
||
``` ebnf | ||
item ::= | ||
| "const" ident "=" expr | ||
| "static" ident "=" expr | ||
| modifiers "fn" ident ("<" (generics ",")* ">")? "(" (pat ":" ty ",")* ")" (":" ty)? "{" expr "}" | ||
| "type" ident "=" ty | ||
| "enum" ident ("<" (generics ",")* ">")? "{" (ident ("(" (ty)* ")")? ",")* "}" | ||
| "struct" ident ("<" (generics ",")* ">")? "{" (ident ":" ty ",")* "}" | ||
| "trait" ident ("<" (generics ",")* ">")? "{" (trait_item)* "}" | ||
| "impl" ("<" (generics ",")* ">")? ident "for" ty "{" (impl_item)* "}" | ||
| "mod" ident "{" (item)* "}" | ||
| "use" path ";" | ||
``` | ||
|
||
The full AST description | ||
|
||
``` ebnf | ||
char ::= [a-zA-Z] | ||
string ::= char* | ||
digit ::= [0-9] | ||
uint ::= digit+ | ||
int ::= ("-")? uint | ||
float ::= int (".")? uint | ||
bool ::= "true" | "false" | ||
|
||
local_var ::= ident | ||
global_var ::= rust-path-identifier | ||
|
||
literal ::= | ||
| "\"" string "\"" | ||
| "'" char "'" | ||
| int | ||
| float [a] | ||
| bool | ||
|
||
generic_value ::= | ||
| "'" ident | ||
| ty | ||
| expr | ||
|
||
goal ::= | ||
| ident "<" (generic_value ",")* ">" | ||
|
||
ty ::= | ||
| "bool" | ||
| "char" | ||
| "u8" | "u16" | "u32" | "u64" | ||
| "u128" | "usize" | ||
| "i8" | "i16" | "i32" | "i64" | ||
| "i128" | "isize" | ||
| "f16" | "f32" | "f64" [a] | ||
| "str" | ||
| (ty ",")* | ||
| "[" ty ";" int "]" | ||
| "[" ty "]" | ||
| "*const" ty | "*mut" ty [b] | ||
| "*" expr | "*mut" expr [b] | ||
| ident | ||
| (ty "->")* ty | ||
| dyn (goal)+ [c] | ||
|
||
pat ::= | ||
| "_" | ||
| ident "{" (ident ":" pat ";")* "}" | ||
| ident "(" (pat ",")* ")" | ||
| (pat "|")* pat | ||
| "[" (pat ",")* "]" [d] | ||
| "&" pat | ||
| literal | ||
| ("&")? ("mut")? ident ("@" pat)? [e] | ||
|
||
modifiers ::= | ||
| "" | ||
| "unsafe" modifiers | ||
| "const" modifiers | ||
| "async" modifiers [b] | ||
|
||
guard ::= | ||
| "if" "let" pat (":" ty)? "=" expr | ||
|
||
expr ::= | ||
| "if" expr "{" expr "}" ("else" "{" expr "}")? | ||
| "if" "let" pat (":" ty)? "=" expr "{" expr "}" ("else" "{" expr "}")? | ||
| expr "(" (expr ",")* ")" | ||
| literal | ||
| "[" (expr ",")* "]" | "[" expr ";" int "]" | ||
| ident "{" (ident ":"expr ";")* "}" | ||
| ident "{" (ident ":"expr ";")* ".." expr "}" | ||
| "match" expr guard "{" | ||
(("|" pat)* "=>" (expr "," | "{" expr "}"))* | ||
"}" | ||
| "let" pat (":" ty)? "=" expr ";" expr | ||
| "let" pat (":" ty)? "=" expr "else" "{" expr "}" ";" expr | ||
| modifiers "{" expr "}" | ||
| local_var | ||
| global_var | ||
| expr "as" ty | ||
| "loop" "{" expr "}"$~[\subref{fig:ebnf-iterators}]$ | ||
| "while" "(" expr ")" "{" expr "}"$~[\subref{fig:ebnf-iterators}]$ | ||
| "for" "(" pat "in" expr ")" "{" expr "}"$~[\subref{fig:ebnf-iterators}]$ | ||
| "for" "(" "let" ident "in" expr ".." expr ")" "{" expr "}"$~[\subref{fig:ebnf-iterators}]$ | ||
| "break" expr | ||
| "continue" | ||
| pat "=" expr | ||
| "return" expr | ||
| expr "?" | ||
| "&" ("mut")? expr [e] | ||
| "&" expr "as" "&const _" [b] | ||
| "&mut" expr "as" "&mut _" | ||
| "|" pat "|" expr | ||
|
||
impl_item ::= | ||
| "type" ident "=" ty ";" | ||
| modifiers "fn" ident ("<" (generics ",")* ">")? "(" (pat ":" ty ",")* ")" (":" ty)? "{" expr "}" | ||
|
||
trait_item ::= | ||
| "type" ident ";" | ||
| modifiers "fn" ident ("<" (generics ",")* ">")? "(" (pat ":" ty ",")* ")" (":" ty)? ("{" expr "}" | ";") | ||
|
||
item ::= | ||
| "const" ident "=" expr | ||
| "static" ident "=" expr [b] | ||
| modifiers "fn" ident ("<" (generics ",")* ">")? "(" (pat ":" ty ",")* ")" (":" ty)? "{" expr "}" | ||
| "type" ident "=" ty | ||
| "enum" ident ("<" (generics ",")* ">")? "{" (ident ("(" (ty)* ")")? ",")* "}" | ||
| "struct" ident ("<" (generics ",")* ">")? "{" (ident ":" ty ",")* "}" | ||
| "trait" ident ("<" (generics ",")* ">")? "{" (trait_item)* "}" | ||
| "impl" ("<" (generics ",")* ">")? ident "for" ty "{" (impl_item)* "}" | ||
| "mod" ident "{" (item)* "}" | ||
| "use" path ";" | ||
``` | ||
(a) no support yet for raw pointers, async/await, static, extern, or union types | ||
(b) partial support for nested matching and range patterns | ||
(c) partial support for mutable borrows | ||
(d) most backends lack support for dynamic dispatch, floating point operations | ||
(e) some backends only handle specific forms of iterators |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The full AST description is really just a
cat
of all previousebnf
code blocks, right?I guess this is for visualization?
In that case, let's just create a EBNF file on the side or something like that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, wanted it to have the full description somewhere, but could just be a separate file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Throwing this onto https://rr.red-dove.com/ui gives me errors. Can you check what's wrong?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems it does not like escaping
"
as\"
. And comments/TODO formatting is incorrect.