diff --git a/src/lib.rs b/src/lib.rs index 3d1563f..8670d77 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,13 +12,21 @@ pub fn parse(file_path: &str) { 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 - - for record in file.into_inner() { - match record.as_rule() { + for line in file.into_inner() { + match line.as_rule() { Rule::line => { - for field in record.into_inner().map(|pairs| pairs.as_str()) { - println!("'{field}'"); + println!("Line:"); + for token in line.into_inner() { + match token.as_rule() { + Rule::token => { + println!("{}", token.as_str()); + }, + _ => { + println!("{}", token.as_str()); + } + } } + println!() }, Rule::EOI => (), _ => unreachable!(), diff --git a/src/rpy.pest b/src/rpy.pest index 2d56e45..5fdaf5d 100644 --- a/src/rpy.pest +++ b/src/rpy.pest @@ -4,14 +4,25 @@ WHITESPACE = _{ " " } // characters are anything but newlines char = { !NEWLINE ~ ANY } +// token definition +// http://pest.rs/book/grammars/syntax.html#atomic +inner = @{ char* } + +token = { string | keyword } + +// has to be atomic for no implicit separate (spaces) +keyword = @{ (!(WHITESPACE | NEWLINE) ~ ANY)+ } + +// strings cannot contain quotes +// TODO: escaped quotes +string_data = @{ (!"\"" ~ ANY)* } +string = ${ "\"" ~ string_data ~ "\"" } + // 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 } +line = { token+ } -file = { SOI ~ (line ~ ("\r\n" | "\n")*)* ~ "\n"* ~ EOI } \ No newline at end of file +file = { SOI ~ line ~ (NEWLINE+ ~ line)* ~ NEWLINE* ~ EOI } \ No newline at end of file