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

Yul's grammar with smoke test using AST #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ license = "GPL-3.0"
readme = "README.md"
description = "Yultsur (or Yülçür) is a toolkit for Yul."
keywords = ["yul", "ethereum", "solidity"]

[dependencies]
pest = "^1.0"
pest_derive = "^1.0"
1 change: 1 addition & 0 deletions examples/empty_block.yul
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ }
5 changes: 5 additions & 0 deletions examples/power_function_signature.yul
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
function power() -> result:u256
{
}
}
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
extern crate pest;
#[macro_use]
extern crate pest_derive;

pub mod yul;
pub mod validator;
pub mod yul_parser;
83 changes: 83 additions & 0 deletions src/yul.pest
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
block = { soi? ~ "{" ~ (statement)* ~ "}" ~ eoi? }

statement = {
(block
| function_definition
| variable_declaration
| assignment
| if_statement
| for_loop
| expression
| switch
| break_statement
| continue_statement)
}

function_definition = { "function" ~ identifier ~ "(" ~ (typed_parameter_list)? ~ ")" ~ ("->" ~ typed_identifier_list)? ~ block }

variable_declaration = { "let" ~ typed_identifier_list ~ (":=" ~ expression)? }

assignment = { identifier_list ~ ":=" ~ expression }

expression = { function_call | identifier | literal }

if_statement = { "if" ~ expression ~ block }

switch = { "switch" ~ expression ~ ((case)+ ~ (default)? | default) }

case = { "case" ~ literal ~ block }

default = { "default" ~ block }

for_loop = { "for" ~ block ~ expression ~ block ~ block }

break_statement = { "break" }

continue_statement = { "continue" }

function_call = { identifier ~ "(" ~ (expression ~ ("," ~ expression)*)? ~ ")" }

identifier = @ { alpha ~ (alpha | digit)* }

identifier_list = { identifier ~ ("," ~ identifier)* }

type_name = { builtin_typename | identifier }

builtin_typename = { "bool" | "u8" | "u32" | "u64" | "u128" | "u256" | "s8" | "s32" | "s64" | "s128" | "s256" } // FIXME: this can probably be simplified

typed_parameter_list = { typed_identifier ~ ("," ~ typed_identifier)* }
typed_identifier_list = { typed_identifier ~ ("," ~ typed_identifier)* }
untyped_parameter_list = { identifier ~ ("," ~ identifier)* }
untyped_identifier_list = { identifier ~ ("," ~ identifier)* }

typed_identifier = { identifier ~ ":" ~ type_name }

literal = {
(number_literal
| string_literal
| hex_literal
| true_literal
| false_literal)
~ ":"
~ type_name
}

number_literal = { hex_number | decimal_number }

hex_literal = { "hex" ~ ("\"" ~ (('0'..'9' | 'a'..'f' | 'A'..'F'){2})* ~ "\"" | "\'" ~ (('0'..'9' | 'a'..'f' | 'A'..'F'){2})* ~ "\'") } // FIXME: can be refactored into "hex_byte"

string_literal = { "\"" ~ ((!("\\" | "\n" | "\r" | "\"") ~ any) | "\\" ~ (!( "\n" ) ~ any))* ~ "\"" }

true_literal = { "true" }

false_literal = { "false" }

hex_number = @ { "0x" ~ ('0'..'9' | 'a'..'f' | 'A'..'F')+ } // TODO: this may need to be flagged atomic to avoid something like "0x deadbeef"

decimal_number = @ { digit+ }

alpha = _ { 'a'..'z' | 'A'..'Z' }

digit = _ { '0'..'9' }

whitespace = _ { " " | "\t" | "\n" }
54 changes: 54 additions & 0 deletions src/yul_parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use yul::*;

use pest::Parser;
use pest::iterators::Pair;

#[derive(Parser)]
#[grammar = "yul.pest"]
struct BlockParser;

use std::fs::File;
use std::io::prelude::*;

fn file_to_string(path: &str) -> String {
let mut file = File::open(path).unwrap();
let mut content = String::new();
file.read_to_string(&mut content).unwrap();
content
}

impl Block {
fn from(pair: Pair<Rule>) -> Block {
let mut statements: Vec<Statement> = vec![];
for p in pair.into_inner() {
match p.as_rule() {
Rule::statement => {
//statements.push(Statement::from(p));
}
c => panic!("{:?}", c),
}
}
Block { statements }
}
}

pub fn parse_block(source: &str) -> Block {
let mut pairs = BlockParser::parse(Rule::block, &source).unwrap();
Block::from(pairs.next().unwrap())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn empty_block() {
let source = file_to_string("examples/empty_block.yul");
let block = parse_block(&source);
assert_eq!(
source,
block.to_string()
);
}

}