Add comment parsing

main
Elnu 2 years ago
parent 359c46d776
commit 46acbce60e

@ -1,5 +1,11 @@
{
"rust-analyzer.linkedProjects": [
"./demo/Cargo.toml"
]
],
"files.exclude": {
"**/*.rpyc": true,
"**/*.rpa": true,
"**/*.rpymc": true,
"**/cache/": true
}
}

@ -1,5 +0,0 @@
65279,1179403647,1463895090
3.1415927,2.7182817,1.618034
-40,-273.15
13,42
65537
1 65279,1179403647,1463895090
2 3.1415927,2.7182817,1.618034
3 -40,-273.15
4 13,42
5 65537

@ -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

@ -1,5 +1,5 @@
use renrs;
fn main() {
renrs::parse("demo.csv");
renrs::parse("demo.rpy");
}

@ -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 }

@ -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::<f64>().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}");
}

@ -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 }
Loading…
Cancel
Save