WHITESPACE = _{ " " | "\t" | "\r" | "\n" }
COMMENT = _{ "(*" ~ (!"*)" ~ ANY)* ~ "*)" }
PartExtract = { Identifier ~ "[[" ~ Expression ~ "]]" }
Integer = @{ ("-")? ~ ASCII_DIGIT+ }
Real = @{ ("-")? ~ ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT+ }
Constant = { ("-")? ~ "Pi" }
NumericValue = { Real | Integer | Constant }
Identifier = @{ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")* }
String = @{ "\"" ~ (!"\"" ~ ANY)* ~ "\"" }
// Special functions
SpecialFunction = { "Sin" | "Cos" | "Tan" | "Plot" | "Prime" | "GroupBy" | "Print" }
FunctionCall = { (Identifier | SpecialFunction | AnonymousFunction) ~ "[" ~ (Expression ~ ("," ~ (Expression | List))*)? ~ "]" }
// Anonymous function syntax
Slot = @{ "#" ~ ASCII_DIGIT* }
AnonymousFunction = { Slot ~ (Operator ~ Term)? ~ "&" }
Operator = {
"/@" // keep first
| "==" | "!="
| "<=" | ">="
| ":="
| "+" | "-" | "*" | "/" | "^"
| "<" | ">"
| "=" | "@"
}
PostfixApplication = { Term ~ "//" ~ Identifier }
Expression = { PostfixApplication | Term ~ (Operator ~ Term)* }
Term = _{
PartExtract
| NumericValue
| Constant
| String
| List
| FunctionCall
| Identifier
| AnonymousFunction
| Slot
| Association
| "(" ~ Expression ~ ")"
}
List = { "{" ~ (Expression ~ ("," ~ Expression)*)? ~ "}" }
Pattern = { Identifier ~ "_" }
AssociationItem = { Expression ~ "->" ~ Expression }
Association = { "<|" ~ ( AssociationItem ~ ("," ~ AssociationItem)* )? ~ "|>" }
FunctionDefinition = { Identifier ~ "[" ~ Pattern ~ "]" ~ ":=" ~ Expression }
Statement = _{ FunctionDefinition | Expression }
Program = { SOI ~ Statement ~ (";" ~ Statement)* ~ EOI }