openquest/openquest-lib/src/syntax/legacy/v1_0_0/parser.rs

107 lines
3.4 KiB
Rust

// Standard Uses
// Crate Uses
use crate::inter_repr::Unit;
// External Uses
use anyhow::{Result};
use pest_derive::Parser;
use pest::iterators::Pair;
use pest::Parser;
#[derive(Parser)]
#[grammar = "syntax/legacy/v1_0_0/quest_shell_v1.pest"]
pub struct QuestV1Parser;
#[allow(unused)]
pub fn parse_into_unit(content: &str) -> Result<Vec<Unit>> {
let pairs = QuestV1Parser::parse(Rule::quest, content)?;
let mut units = vec![];
for pair in pairs { units.push(parse_inner(pair).unwrap()) }
Ok(units)
}
#[allow(unused)]
pub fn parse_inner(pairs: Pair<Rule>) -> Result<Unit> {
match pairs.as_rule() {
Rule::quest => {
let mut name: Option<String> = None;
let mut states: Vec<Unit> = vec![];
for pair in pairs.into_inner() {
match pair.as_rule() {
Rule::quest_name => name = Some(pair.as_str().to_owned()),
Rule::state => states.push(parse_inner(pair).unwrap()),
_ => {}
}
};
Ok(Unit::Quest{ name: name.unwrap(), states })
},
Rule::state => {
let mut name: Option<String> = None;
let mut whens: Vec<Unit> = vec![];
let mut functions: Vec<Unit> = vec![];
for pair in pairs.into_inner() {
match pair.as_rule() {
Rule::state_name => name = Some(pair.as_str().to_owned()),
Rule::function => functions.push(parse_inner(pair).unwrap()),
Rule::when_condition => whens.push(parse_inner(pair).unwrap()),
_ => {}
}
};
Ok(Unit::State { name: name.unwrap(), whens, functions })
},
Rule::when_condition => {
let mut condition = None;
let mut with= "".to_string();
let mut body: Option<Box<Unit>> = None;
for pair in pairs.into_inner() {
match pair.as_rule() {
Rule::when_trigger => condition = Some(pair.as_str().to_owned()),
Rule::lua_expression_scope => body = {
Some(Box::from(Unit::LuaScope(pair.as_str().to_owned())))
},
_ => {}
}
}
Ok(Unit::When {
condition: condition.unwrap(), with, body: body.unwrap()
})
}
Rule::when_with_expression => todo!(),
Rule::when_with_condition => todo!(),
Rule::function => {
let mut name: Option<String> = None;
let mut arguments: String = "".to_string();
let mut body: Option<Box<Unit>> = None;
for pair in pairs.into_inner() {
match pair.as_rule() {
Rule::function_name => name = Some(pair.as_str().to_string()),
Rule::function_arguments => arguments = pair.as_str().to_string(),
Rule::lua_expression_scope => body = {
Some(Box::from(Unit::LuaScope(pair.as_str().to_string())))
},
_ => {}
}
};
Ok(Unit::Function{
name: name.unwrap(), arguments, body: body.unwrap() }
)
},
Rule::COMMENT => todo!(),
(r) => panic!("Possible unimplemented rule reached: {:?}", r)
}
}