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() { 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() {
let a = 5;
}
fn main() {
let a = 5;
let b = { 5 };
}
...@@ -26,5 +26,15 @@ pub fn parse(file_name: &str) { ...@@ -26,5 +26,15 @@ pub fn parse(file_name: &str) {
#[test] #[test]
fn syntax() { fn syntax() {
parse("examples/syntax.rs");
}
#[test]
fn syntax2() {
parse("examples/syntax2.rs");
}
#[test]
fn syntax3() {
parse("examples/syntax3.rs"); parse("examples/syntax3.rs");
} }
...@@ -14,6 +14,15 @@ match { ...@@ -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: () = { pub Function: () = {
"fn" Id Params ("->" Type)? Block => (), "fn" Id Params ("->" Type)? Block => (),
} }
...@@ -34,64 +43,90 @@ pub Type:() = { ...@@ -34,64 +43,90 @@ pub Type:() = {
} }
pub Block: () = { pub Block: () = {
"{" Stmt* "}" => (), "{" StmtSeq* "}" => (),
"{" StmtSeq* Stmt "}" => (),
}
pub StmtSeq: () = {
Stmt ";" => (),
StmtBlock => (),
}
pub StmtBlock: () = {
"while" Expr Block => (),
"if" Expr Block ("else" Block)? => (),
Block => (),
} }
pub Stmt: () = { pub Stmt: () = {
";" => (), ";" => (),
"let" "mut"? Id "=" Expr ";" => (), "let" "mut"? Id "=" Expr => (),
ExprNoBlock "=" Expr => (), ExprNoBlock "=" Expr => (),
"while" Expr Block => (),
"if" Expr Block ("else" Block)? => (),
ExprNoBlock => (), ExprNoBlock => (),
Block => (),
} }
pub Expr: () = { pub Expr: () = {
ExprBlock => (), ExprBlock => (),
ExprNoBlock => (), ExprNoBlock => (),
} }
pub ExprBlock: () = { pub ExprBlock: () = {
"if" Expr Block "else" Block => (), "if" ExprNoBlock Block "else" Block => (),
Block => (), Block => (),
} }
// Here is our ordinary expressions
pub ExprNoBlock: () = { pub ExprNoBlock: () = {
ExprNoBlock Cmp Expr1 => (), ExprNoBlock "||" Expr0 => (),
Expr1 => (), ExprNoBlock "&&" Expr0 => (),
Expr0 => (),
} }
pub Cmp: () = {
"<" => () // Comparison
pub Expr0: () = {
Expr0 "==" Expr1 => (),
Expr0 "!=" Expr1 => (),
Expr0 ">" Expr1 => (),
Expr0 "<" Expr1 => (),
Expr1 => (),
} }
// AddSub
pub Expr1: () = { pub Expr1: () = {
Expr1 AddSub Expr2 => (), Expr1 "+" Expr2 => (),
Expr1 "-" Expr2 => (),
Expr2 => (), Expr2 => (),
} }
pub AddSub: () = { // MulDiv
"+" => (),
"-" => (),
}
pub Expr2: () = { pub Expr2: () = {
Expr2 MulDiv Term => (), Expr2 "/" Expr3 => (),
Term => (), Expr2 "*" Expr3 => (),
Expr3 => (),
} }
pub MulDiv: () = { // Unary
"/" => (), pub Expr3: () = {
"*" => (), "*" Term => (),
"&" Term => (),
"&" "mut" Term => (),
"!" Term => (),
Term => (),
} }
pub Term: () = { pub Term: () = {
Id => (), Id => (),
Num => (), Num => (),
Id "(" CommaNoTrail<Expr> ")" => (),
"(" Expr ")" => (), "(" Expr ")" => (),
} }
pub Num: i32 = { pub Num: i32 = {
r"[0-9]+" => i32::from_str(<>).unwrap(), 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