Skip to content
Snippets Groups Projects
Commit 262e02bf authored by Per Lindgren's avatar Per Lindgren
Browse files

syntax tests pass

parent 4596e47a
No related branches found
No related tags found
No related merge requests found
fn main() {
let a = false;
let a = {
let b = 1 + 1;
// extra `;` are allowed
if a {} // no trialing `;`
b // return value
};
while false {
// do something here
}
let _b = if a < 5 { 1 } else { 2 };
}
fn main() {}
fn main() {
let a = 5;
}
fn main() {
let a = 5;
let b = { 5 };
}
......@@ -26,5 +26,15 @@ pub fn parse(file_name: &str) {
#[test]
fn syntax() {
parse("examples/syntax.rs");
}
#[test]
fn syntax2() {
parse("examples/syntax2.rs");
}
#[test]
fn syntax3() {
parse("examples/syntax3.rs");
}
......@@ -14,6 +14,15 @@ match {
_
}
// A comma separated sequence without trailing comma
CommaNoTrail<T>: Vec<T> = {
<v:(<T> ",")*> <e:T> => {
let mut v = v;
v.push(e);
v
}
}
pub Function: () = {
"fn" Id Params ("->" Type)? Block => (),
}
......@@ -34,64 +43,90 @@ pub Type:() = {
}
pub Block: () = {
"{" Stmt* "}" => (),
"{" StmtSeq* "}" => (),
"{" StmtSeq* Stmt "}" => (),
}
pub StmtSeq: () = {
Stmt ";" => (),
StmtBlock => (),
}
pub StmtBlock: () = {
"while" Expr Block => (),
"if" Expr Block ("else" Block)? => (),
Block => (),
}
pub Stmt: () = {
";" => (),
"let" "mut"? Id "=" Expr ";" => (),
"let" "mut"? Id "=" Expr => (),
ExprNoBlock "=" Expr => (),
"while" Expr Block => (),
"if" Expr Block ("else" Block)? => (),
ExprNoBlock => (),
Block => (),
}
pub Expr: () = {
ExprBlock => (),
ExprNoBlock => (),
}
pub ExprBlock: () = {
"if" Expr Block "else" Block => (),
"if" ExprNoBlock Block "else" Block => (),
Block => (),
}
// Here is our ordinary expressions
pub ExprNoBlock: () = {
ExprNoBlock Cmp Expr1 => (),
Expr1 => (),
ExprNoBlock "||" Expr0 => (),
ExprNoBlock "&&" Expr0 => (),
Expr0 => (),
}
pub Cmp: () = {
"<" => ()
// Comparison
pub Expr0: () = {
Expr0 "==" Expr1 => (),
Expr0 "!=" Expr1 => (),
Expr0 ">" Expr1 => (),
Expr0 "<" Expr1 => (),
Expr1 => (),
}
// AddSub
pub Expr1: () = {
Expr1 AddSub Expr2 => (),
Expr1 "+" Expr2 => (),
Expr1 "-" Expr2 => (),
Expr2 => (),
}
pub AddSub: () = {
"+" => (),
"-" => (),
}
// MulDiv
pub Expr2: () = {
Expr2 MulDiv Term => (),
Term => (),
Expr2 "/" Expr3 => (),
Expr2 "*" Expr3 => (),
Expr3 => (),
}
pub MulDiv: () = {
"/" => (),
"*" => (),
// Unary
pub Expr3: () = {
"*" Term => (),
"&" Term => (),
"&" "mut" Term => (),
"!" Term => (),
Term => (),
}
pub Term: () = {
Id => (),
Num => (),
Id "(" CommaNoTrail<Expr> ")" => (),
"(" Expr ")" => (),
}
pub Num: i32 = {
r"[0-9]+" => i32::from_str(<>).unwrap(),
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment