From 46acbce60e84706b24101619d7b4105e125c882b Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Fri, 19 May 2023 17:54:25 -0700 Subject: [PATCH] Add comment parsing --- .vscode/settings.json | 8 +++++++- demo/demo.csv | 5 ----- demo/demo.rpy | 9 +++++++++ demo/src/main.rs | 2 +- src/csv.pest | 9 --------- src/lib.rs | 20 ++++++-------------- src/rpy.pest | 17 +++++++++++++++++ 7 files changed, 40 insertions(+), 30 deletions(-) delete mode 100644 demo/demo.csv create mode 100644 demo/demo.rpy delete mode 100644 src/csv.pest create mode 100644 src/rpy.pest diff --git a/.vscode/settings.json b/.vscode/settings.json index c69d7d3..5a95d22 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,11 @@ { "rust-analyzer.linkedProjects": [ "./demo/Cargo.toml" - ] + ], + "files.exclude": { + "**/*.rpyc": true, + "**/*.rpa": true, + "**/*.rpymc": true, + "**/cache/": true + } } \ No newline at end of file diff --git a/demo/demo.csv b/demo/demo.csv deleted file mode 100644 index 20a98c9..0000000 --- a/demo/demo.csv +++ /dev/null @@ -1,5 +0,0 @@ -65279,1179403647,1463895090 -3.1415927,2.7182817,1.618034 --40,-273.15 -13,42 -65537 diff --git a/demo/demo.rpy b/demo/demo.rpy new file mode 100644 index 0000000..d986d46 --- /dev/null +++ b/demo/demo.rpy @@ -0,0 +1,9 @@ +show black amogus # this is a comment +# this is a full line comment +what the heck +"this is a string with a # comment" +"this is a string over +multiple lines" +this is cool # comment + +huh \ No newline at end of file diff --git a/demo/src/main.rs b/demo/src/main.rs index 79ef2ea..4e2d100 100644 --- a/demo/src/main.rs +++ b/demo/src/main.rs @@ -1,5 +1,5 @@ use renrs; fn main() { - renrs::parse("demo.csv"); + renrs::parse("demo.rpy"); } diff --git a/src/csv.pest b/src/csv.pest deleted file mode 100644 index 8b94fc7..0000000 --- a/src/csv.pest +++ /dev/null @@ -1,9 +0,0 @@ -// + indicates one or more times -field = { (ASCII_DIGIT | "." | "-")+ } -// ~ indicates directly followed by -// * indicates zero or more times (optional) -record = { field ~ ("," ~ field)* } -// SOI - start of input -// END - end of input -// There may be trailing newlines at the end -file = { SOI ~ (record ~ ("\r\n" | "\n"))* ~ "\n"* ~ EOI } diff --git a/src/lib.rs b/src/lib.rs index 6b32541..3d1563f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,32 +4,24 @@ use pest::Parser; use pest_derive::Parser; #[derive(Parser)] -#[grammar = "csv.pest"] -struct CSVParser; +#[grammar = "rpy.pest"] +struct RpyParser; pub fn parse(file_path: &str) { let unparsed_file = fs::read_to_string(file_path).expect("cannot find file"); - let file = CSVParser::parse(Rule::file, &unparsed_file) + let file = RpyParser::parse(Rule::file, &unparsed_file) .expect("unsuccessful parse") // unwrap the parse result .next().unwrap(); // get and unwrap the `file` rule; never fails - let mut field_sum = 0.0; - let mut record_count: u64 = 0; - for record in file.into_inner() { match record.as_rule() { - Rule::record => { - record_count += 1; - - for field in record.into_inner() { - field_sum += field.as_str().parse::().unwrap(); + Rule::line => { + for field in record.into_inner().map(|pairs| pairs.as_str()) { + println!("'{field}'"); } }, Rule::EOI => (), _ => unreachable!(), } } - - println!("Sum of fields: {field_sum}"); - println!("Number of records: {record_count}"); } diff --git a/src/rpy.pest b/src/rpy.pest new file mode 100644 index 0000000..2d56e45 --- /dev/null +++ b/src/rpy.pest @@ -0,0 +1,17 @@ +// underscores mark are silent rules, are ignored +WHITESPACE = _{ " " } + +// characters are anything but newlines +char = { !NEWLINE ~ ANY } + +// comments are a # followed by +// any number of non-newline characters +COMMENT = _{ "#" ~ char* } + +// statements are comprised of at least one character +statement = { char+ } + +// lines are comprised of a statement +line = { statement } + +file = { SOI ~ (line ~ ("\r\n" | "\n")*)* ~ "\n"* ~ EOI } \ No newline at end of file