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

a minimal example added

parent ad296c84
No related branches found
No related tags found
No related merge requests found
{ {
"cSpell.ignoreWords": [ "cSpell.ignoreWords": [
"llvm" "intrinsics",
] "lalrpop",
"lalrpop mod",
"llvm",
"mod"
],
"rust-analyzer.inlayHints.enable": true
} }
\ No newline at end of file
...@@ -8,9 +8,14 @@ build = "build.rs" # LALRPOP preprocessing ...@@ -8,9 +8,14 @@ build = "build.rs" # LALRPOP preprocessing
[build-dependencies.lalrpop] [build-dependencies.lalrpop]
version = "0.19.0" version = "0.19.0"
features = ["lexer"]
[dependencies] [dependencies]
lalrpop-util = "0.19.0" lalrpop-util = "0.19.0"
regex = "1.3.9" 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"
use std::str::FromStr;
grammar;
pub Id: String = {
r"([a-z]|[A-Z])([a-z]|[A-Z]|[0-9]|_)*" => String::from_str(<>).unwrap(),
};
use lalrpop_util::lalrpop_mod;
lalrpop_mod!(pub grammar);
use grammar::*;
fn main() {
println!("hello world");
}
#[test]
fn hello() {
println!("Hello in test")
}
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"));
}
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment