Implement naive arrays, better debugging

main
Elnu 2 years ago
parent a518097ff0
commit 3794a1d607

@ -8,5 +8,6 @@ multiple lines"
'this is a single quote string' 'this is a single quote string'
'this also has escaped \'quotes\'' 'this also has escaped \'quotes\''
this is cool # comment this is cool # comment
[ "this", "is", "an", "array" ]
huh huh

@ -7,6 +7,28 @@ use pest_derive::Parser;
#[grammar = "rpy.pest"] #[grammar = "rpy.pest"]
struct RpyParser; struct RpyParser;
fn describe_token(pair: pest::iterators::Pair<Rule>) {
let token = pair.as_rule();
match token {
Rule::token => {},
_ => panic!("Not a token!"),
};
let contents = pair.into_inner().next().unwrap();
let contents_rule = contents.as_rule();
let str = match contents_rule {
Rule::string => {
let data = contents.into_inner().next().unwrap();
match data.as_rule() {
Rule::single_quote_string_data => data.as_str().replace("\\'", "'"),
Rule::double_quote_string_data => data.as_str().replace("\\\"", "\""),
_ => unreachable!(),
}
},
_ => contents.into_inner().as_str().to_owned(),
};
println!("{:?}: {}", contents_rule, str);
}
pub fn parse(file_path: &str) { pub fn parse(file_path: &str) {
let unparsed_file = fs::read_to_string(file_path).expect("cannot find file"); let unparsed_file = fs::read_to_string(file_path).expect("cannot find file");
let file = RpyParser::parse(Rule::file, &unparsed_file) let file = RpyParser::parse(Rule::file, &unparsed_file)
@ -30,6 +52,12 @@ pub fn parse(file_path: &str) {
_ => unreachable!(), _ => unreachable!(),
}); });
}, },
Rule::array => {
println!("Array:");
for element in token.into_inner() {
describe_token(element);
}
}
Rule::keyword => println!("keyword: {}", token.as_str()), Rule::keyword => println!("keyword: {}", token.as_str()),
_ => unreachable!(), _ => unreachable!(),
}; };

@ -8,11 +8,13 @@ char = { !NEWLINE ~ ANY }
// http://pest.rs/book/grammars/syntax.html#atomic // http://pest.rs/book/grammars/syntax.html#atomic
inner = @{ char* } inner = @{ char* }
token = { string | keyword } token = { string | array | keyword }
// KEYWORDS
// has to be atomic for no implicit separate (spaces) // has to be atomic for no implicit separate (spaces)
keyword = @{ (!(WHITESPACE | NEWLINE) ~ ANY)+ } keyword = @{ (!(WHITESPACE | NEWLINE) ~ ANY)+ }
// STRING
single_quote_string_data = @{ ( single_quote_string_data = @{ (
"\\'" // Escaped single quotes "\\'" // Escaped single quotes
| (!"'" ~ ANY) | (!"'" ~ ANY)
@ -26,6 +28,9 @@ string = ${
| ("\"" ~ double_quote_string_data ~ "\"") | ("\"" ~ double_quote_string_data ~ "\"")
} }
// ARRAY
array = { "[" ~ token ~ ("," ~ token)* ~ "]"}
// comments are a # followed by // comments are a # followed by
// any number of non-newline characters // any number of non-newline characters
COMMENT = _{ "#" ~ char* } COMMENT = _{ "#" ~ char* }

Loading…
Cancel
Save