|
|
|
@ -7,6 +7,28 @@ use pest_derive::Parser;
|
|
|
|
|
#[grammar = "rpy.pest"]
|
|
|
|
|
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) {
|
|
|
|
|
let unparsed_file = fs::read_to_string(file_path).expect("cannot find file");
|
|
|
|
|
let file = RpyParser::parse(Rule::file, &unparsed_file)
|
|
|
|
@ -30,6 +52,12 @@ pub fn parse(file_path: &str) {
|
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
Rule::array => {
|
|
|
|
|
println!("Array:");
|
|
|
|
|
for element in token.into_inner() {
|
|
|
|
|
describe_token(element);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Rule::keyword => println!("keyword: {}", token.as_str()),
|
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
};
|
|
|
|
|