Skip to content

Instantly share code, notes, and snippets.

@sambacha
Created May 21, 2024 13:47
Show Gist options
  • Save sambacha/6a3c5c1c163da8f1631c65450f60dd99 to your computer and use it in GitHub Desktop.
Save sambacha/6a3c5c1c163da8f1631c65450f60dd99 to your computer and use it in GitHub Desktop.
@top Program { Statement* }
@tokens {
pragma { "pragma" }
import { "import" }
contract { "contract" }
interface { "interface" }
library { "library" }
function { "function" }
modifier { "modifier" }
event { "event" }
struct { "struct" }
enum { "enum" }
mapping { "mapping" }
address { "address" }
bool { "bool" }
string { "string" }
int { "int" }
uint { "uint" }
public { "public" }
private { "private" }
internal { "internal" }
external { "external" }
view { "view" }
pure { "pure" }
payable { "payable" }
returns { "returns" }
memory { "memory" }
storage { "storage" }
calldata { "calldata" }
if { "if" }
else { "else" }
while { "while" }
for { "for" }
return { "return" }
break { "break" }
continue { "continue" }
new { "new" }
delete { "delete" }
require { "require" }
assert { "assert" }
Operator {
"+" | "-" | "*" | "/" | "%" | "=" | "==" | "!=" | "<=" | ">=" | "<" | ">" |
"&&" | "||" | "!" | "++" | "--" | "+=" | "-=" | "*=" | "/=" | "%=" | "&" |
"|" | "^" | "<<" | ">>" | "~"
}
Number { std.digit+ }
Boolean { "true" | "false" }
String { "\"" [^"]* "\"" }
"{ }" { "{" | "}" }
"( )" { "(" | ")" }
"[ ]" { "[" | "]" }
";" { ";" }
"," { "," }
"." { "." }
Identifier { std.asciiLetter std.asciiLetterOrDigit* }
}
Statement {
VariableDeclaration | FunctionDeclaration | IfStatement | WhileStatement |
ForStatement | ReturnStatement | ExpressionStatement
}
Expression {
Literal | Identifier | BinaryExpression | FunctionCall | Assignment |
NewExpression
}
Literal {
Number | Boolean | String
}
BinaryExpression {
Expression Operator Expression
}
FunctionCall {
Identifier "(" ArgumentList? ")"
}
Assignment {
Identifier "=" Expression
}
NewExpression {
"new" Type "(" ArgumentList? ")"
}
FunctionDeclaration {
"function" Identifier "(" ParameterList? ")" VisibilitySpecifier*
ReturnParameters? Block
}
ParameterList {
(Type Identifier ("," Type Identifier)*)?
}
VisibilitySpecifier {
"public" | "private" | "internal" | "external"
}
ReturnParameters {
"returns" "(" Type ("," Type)* ")"
}
Block {
"{" Statement* "}"
}
VariableDeclaration {
Type Identifier "=" Expression ";"
}
IfStatement {
"if" "(" Expression ")" Block ("else" Block)?
}
WhileStatement {
"while" "(" Expression ")" Block
}
ForStatement {
"for" "(" (VariableDeclaration | Expression? ";") Expression? ";"
Expression? ")" Block
}
ReturnStatement {
"return" Expression? ";"
}
ExpressionStatement {
Expression ";"
}
Type {
ElementaryType | UserDefinedType
}
ElementaryType {
"uint" | "int" | "bool" | "address" | "string"
}
UserDefinedType {
Identifier
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment