Skip to content

Instantly share code, notes, and snippets.

@iambenkay
Last active June 1, 2023 12:01
Show Gist options
  • Save iambenkay/ef76f3162cc9da0d83c1a7f7854d0f49 to your computer and use it in GitHub Desktop.
Save iambenkay/ef76f3162cc9da0d83c1a7f7854d0f49 to your computer and use it in GitHub Desktop.
WASL Parsing Expression Grammar for Pest.rs
ws = _{ " " | "\t" }
wsln = _{ ws | "\n" }
COMMENT = _{ "/*" ~ (!"*/" ~ ANY)* ~ "*/" | "//" ~ (!"\n" ~ ANY)* ~ "\n"+ }
Program = _{ SOI ~ wsln* ~ (Stmt ~ wsln*)* ~ Stmt? ~ EOI }
Stmt = { AssignmentExpr | ForExpr }
Identifier = @{ ASCII_ALPHA ~ (ASCII_ALPHANUMERIC | "_")* }
Type = { Identifier }
Declaration = { "let" }
AssignmentExpr = { Declaration ~ ws+ ~ Identifier ~ ws* ~ "=" ~ ws* ~ Expr ~ ";" }
StandaloneExpr = { Expr ~ ";" }
NumberExpr = { Float | Integer }
Integer = { ASCII_DIGIT+ }
Expr = _{ ArithmeticExpr }
Float = { ASCII_DIGIT* ~ "." ~ ASCII_DIGIT+ }
ArithmeticExpr = { AddExpr }
ForExpr = {
"for" ~ ws* ~ "(" ~ ws* ~ InExpr
~ ws* ~ ")" ~ ws* ~ "{" ~ wsln* ~ (Stmt ~ wsln*)* ~ "}"
}
InExpr = { Declaration ~ ws+
~ Identifier ~ ws+ ~ "in" ~ ws+ ~ Identifier }
AddExpr = {
MulExpr ~ ws* ~ "+" ~ ws* ~ AddExpr
| MulExpr ~ ws* ~ "-" ~ ws* ~ AddExpr
| MulExpr
}
ArrayExpr = { "[" ~ wsln* ~ (ArithmeticExpr ~ ws* ~ "," ~ wsln*)*
~ (ArithmeticExpr ~ ws* ~ ","* ~ wsln*)* ~ "]" }
UnitExpr = {
"(" ~ ws* ~ ArithmeticExpr ~ ws* ~ ")"
| "+" ~ ws* ~ UnitExpr
| "-" ~ ws* ~ UnitExpr
| Identifier
| NumberExpr
| ArrayExpr
}
MulExpr = {
UnitExpr ~ ws* ~ "*" ~ ws* ~ MulExpr
| UnitExpr ~ ws* ~ "/" ~ ws* ~ MulExpr
| UnitExpr
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment