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

passes simple test

parent eabfc31b
No related branches found
No related tags found
No related merge requests found
fn main() {
let a = {
let b = 1;
b
};
}
use std::fs::File;
use std::io::prelude::*;
use lalrpop_util::lalrpop_mod; use lalrpop_util::lalrpop_mod;
lalrpop_mod!(pub parser, "/ast/parser.rs"); lalrpop_mod!(pub parser, "/ast/parser.rs");
...@@ -7,13 +10,32 @@ use parser::*; ...@@ -7,13 +10,32 @@ use parser::*;
pub mod ast; pub mod ast;
use ast::*; use ast::*;
fn main() { fn main() {}
pub fn read(file_name: &str) -> std::io::Result<String> {
let mut file = File::open(file_name)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Ok(contents)
}
// pub fn parse(file_name: &str) -> Program {
// let p = read(file_name).expect("File not found");
// ProgramParser::new().parse(&p).unwrap()
// }
#[test]
fn t1() {
let s = " let s = "
{ {
let ; let a = {
let b = 1 + 1;
; ;
if a { }
b
}
} }
"; ";
StmtsParser::new().parse(s).unwrap();
println!("{:?}", BlockExpressionParser::new().parse(s));
} }
...@@ -4,40 +4,68 @@ use crate::ast::*; ...@@ -4,40 +4,68 @@ use crate::ast::*;
grammar; grammar;
pub ExpressionWithoutBlock: () = { match {
LiteralExpression => () // The default whitespace skipping is disabled an `ignore pattern` is specified
r"\s*" => { },
// Skip `// comments`
r"//[^\n\r]*" => { },
// Skip `/* comments */`
r"/\*([^\*]*\*+[^\*/])*([^\*]*\*+|[^\*])*\*/" => { },
_
} }
pub LiteralExpression = { //pub Expr: ()
Num pub Expr: () = {
Id ExprStmt => (),
ExprNoBlock => (),
} }
ExpressionWithBlock: () = { pub ExprNoBlock: () = {
BlockExpression => () Expr AddSub Factor => (),
Factor => (),
} }
pub BlockExpression: ()= { pub AddSub: () = {
"{" Statements? "}" => () "+" => (),
"-" => (),
} }
pub Statements: () = { pub Factor: () = {
Statement+ => (), Factor MulDiv Term => (),
Statement+ ExpressionWithoutBlock => (), Term => (),
ExpressionWithBlock => ()
} }
pub Statement: () = { pub MulDiv: () = {
";" => (), "/" => (),
"let" ";" => (), "*" => (),
ExpressionStatement => () }
pub Term: () = {
Id => (),
Num => (),
}
pub ExprStmt: () = {
"if" Expr Stmts "else" Stmts => (),
Stmts => (),
}
pub Stmts: () = {
"{" Stmt* "}" => (),
} }
pub ExpressionStatement: () = { pub Stmt: () = {
ExpressionWithoutBlock ";" => (), ";" => (),
ExpressionWithBlock ";"? => () "let" Id "=" Expr => (),
"if" Expr Stmts ("else" Stmts)? => (),
ExprNoBlock => (),
Stmts => (),
} }
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