From 57b000aacceb248d3d47f70f8c9bd1c6b0445b3f Mon Sep 17 00:00:00 2001 From: Per Lindgren <per.lindgren@ltu.se> Date: Mon, 31 Aug 2020 18:01:48 +0200 Subject: [PATCH] a minimal example added --- .vscode/settings.json | 9 +++++-- Cargo.toml | 7 ++++- README.md | 2 +- src/grammar.lalrpop | 7 +++++ src/main.rs | 15 +++++++++++ src/minimal/main.rs | 36 +++++++++++++++++++++++++ src/minimal/parser.lalrpop | 55 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 127 insertions(+), 4 deletions(-) create mode 100644 src/grammar.lalrpop create mode 100644 src/main.rs create mode 100644 src/minimal/main.rs create mode 100644 src/minimal/parser.lalrpop diff --git a/.vscode/settings.json b/.vscode/settings.json index ecdb9be..ddaae1d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,10 @@ { "cSpell.ignoreWords": [ - "llvm" - ] + "intrinsics", + "lalrpop", + "lalrpop mod", + "llvm", + "mod" + ], + "rust-analyzer.inlayHints.enable": true } \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 73757b3..a4ab3bc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,9 +8,14 @@ build = "build.rs" # LALRPOP preprocessing [build-dependencies.lalrpop] version = "0.19.0" +features = ["lexer"] [dependencies] lalrpop-util = "0.19.0" regex = "1.3.9" -lalrpop = {version = "0.19.0", features = ["lexer"] } +# lalrpop = {version = "0.19.0", features = ["lexer"] } + +[[bin]] +name = "minimal" +path = "src/minimal/main.rs" diff --git a/README.md b/README.md index 9317dc3..ac70683 100644 --- a/README.md +++ b/README.md @@ -163,7 +163,7 @@ You will each be scheduled 30 minutes to present Your home exam to us, based on Implement for higher grades - Basic code generation. -- Pass `noalias` where possible allowing for better optimization (assuming your borrowchecker prevents aliasing). +- Pass `noalias` where possible allowing for better optimization (assuming your borrow checker prevents aliasing). - Other attributes, intrinsics, etc. that enables further LLVM optimizations. - Bindings to external code, (this could allow for ) diff --git a/src/grammar.lalrpop b/src/grammar.lalrpop new file mode 100644 index 0000000..31d2921 --- /dev/null +++ b/src/grammar.lalrpop @@ -0,0 +1,7 @@ +use std::str::FromStr; + +grammar; + +pub Id: String = { + r"([a-z]|[A-Z])([a-z]|[A-Z]|[0-9]|_)*" => String::from_str(<>).unwrap(), +}; diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..a319e91 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,15 @@ +use lalrpop_util::lalrpop_mod; + +lalrpop_mod!(pub grammar); + +use grammar::*; + +fn main() { + println!("hello world"); +} + +#[test] +fn hello() { + println!("Hello in test") +} + diff --git a/src/minimal/main.rs b/src/minimal/main.rs new file mode 100644 index 0000000..1f5731b --- /dev/null +++ b/src/minimal/main.rs @@ -0,0 +1,36 @@ +use lalrpop_util::lalrpop_mod; + +lalrpop_mod!(pub parser, "/minimal/parser.rs"); + +use parser::*; + +fn main() {} + +#[test] +fn parse_id() { + println!( + "{:?}", + IdParser::new().parse( + " + + //a1_a // + + /* + a1_a + */ + a1_a + " + ) + ); +} + +#[test] +fn parse_num() { + println!("{:?}", NumParser::new().parse("123")); +} + +#[test] +fn parse_num_or_id() { + println!("{:?}", NumOrIdParser::new().parse("123")); + println!("{:?}", NumOrIdParser::new().parse("a1_a")); +} diff --git a/src/minimal/parser.lalrpop b/src/minimal/parser.lalrpop new file mode 100644 index 0000000..1372d50 --- /dev/null +++ b/src/minimal/parser.lalrpop @@ -0,0 +1,55 @@ +use std::str::FromStr; + +grammar; + +match { + // The default whitespace skipping is disabled an `ignore pattern` is specified + r"\s*" => { }, + // Skip `// comments` + r"//[^\n\r]*" => { }, + // Skip `/* comments */` + r"/\*([^\*]*\*+[^\*/])*([^\*]*\*+|[^\*])*\*/" => { }, + _ +} + +pub NumOrId = { + Num => <>.to_string(), + Id, +} + +pub Num: usize = { + r"[0-9]+" => usize::from_str(<>).unwrap(), +}; + +pub Id: String = { + r"([a-z]|[A-Z])([a-z]|[A-Z]|[0-9]|_)*" => String::from_str(<>).unwrap(), +}; + +// lets try to break down the multi-line comment +// +// should allow patterns like +// /* +// ... +// */ +// and +// /* /* +// */ +// */ +// but reject patterns like +// /* /* +// */ +// and +// /* */ */ +// +// Rather tricky right... +// +// /\*([^\*]*\*+[^\*/])*([^\*]*\*+|[^\*])*\*/ +// the outer "/\* ... \*/" requires start and end "/*" ... "*/" +// so let's look closer at "([^\*]*\*+[^\*/])*([^\*]*\*+|[^\*])*" +// the trailing "*", just means any number of +// "[^\*]*\*+[^\*/])*([^\*]*\*+|[^\*]" +// +// "[^\*]*\*+[^\*/])*" +// "[^\*]*"" any number or Not("*"), e.g. 0123 +// "\*" one or many "*", e.g. *, or **** +// .... ehhh I gave up, regexp is haaaaaaarrrrrrrd, another day perhaps \ No newline at end of file -- GitLab