diff --git a/src/rsprocess/grammar.lalrpop b/src/rsprocess/grammar.lalrpop index ae77337..928631b 100644 --- a/src/rsprocess/grammar.lalrpop +++ b/src/rsprocess/grammar.lalrpop @@ -1,17 +1,20 @@ use std::str::FromStr; use lalrpop_util::ParseError; -use crate::rsprocess::{RSprocess, RSenvironment, RSassert, RSassertOp, RSBHML}; +use crate::rsprocess::{RSset, RSprocess, RSenvironment, RSassert, RSassertOp, RSBHML}; grammar; +// matches words (letter followed by numbers, letters or _) Literal = { r"[[:alpha:]][[:word:]]*" }; +// all numbers are i64 Num: i64 = { r"[0-9]+" =>? i64::from_str(<>) .map_err(|_| ParseError::User { error: "Number is too big" }) }; -Comma: Vec = { +// macro for matching sequence of patterns with C as separator +Separeted: Vec = { C)+> => match e { None => v, Some(e) => { @@ -21,40 +24,44 @@ Comma: Vec = { } }; -pub Set: Vec<&'input str> = { +// ----- SetParser ----- +pub Set: RSset<'input> = { => s }; +// ----- ContextParser ----- pub Context: Box> = { "[" "]" => Box::new(RSprocess::Nill), "[" "]" => Box::new(t), - "[" > "]" => Box::new(RSprocess::NondeterministicChoice{children: t}) + "[" > "]" => Box::new(RSprocess::NondeterministicChoice{children: t}) }; CTX_process: RSprocess<'input> = { "." => RSprocess::EntitySet{entities: c, next_process: Box::new(k)}, "(" ")" => k, - "(" > ")" => RSprocess::Summation{children: k}, + "(" > ")" => RSprocess::Summation{children: k}, "<" ">" "." => RSprocess::WaitEntity{repeat: n, repeated_process: Box::new(k1), next_process: Box::new(k)}, "nil" => RSprocess::Nill, => RSprocess::ConstantIdentifier{identifier: identifier} }; -Set_of_entities: Vec<&'input str> = { - "{" "}" => vec![], - "{" "}" => vec![t], - "{" > "}" => {t.sort(); t} +Set_of_entities: RSset<'input> = { + "{" "}" => RSset::from(vec![]), + "{" "}" => RSset::from(vec![t]), + "{" > "}" => RSset::from(t) }; +// ----- EnvironmentParser ----- pub Environment: Box> = { "[" "]" => Box::new(RSenvironment::new()), - "[" > "]" => Box::new(RSenvironment::from(t)) + "[" > "]" => Box::new(RSenvironment::from(t)) }; Env_term: (&'input str, Box>) = { "=" => (identifier, Box::new(k)) }; +// ----- AssertParser ----- pub Assert: Box> = { => Box::new(f) }; @@ -62,8 +69,8 @@ pub Assert: Box> = { Formula_Assert: RSassert<'input> = { "-" => RSassert::Not(Box::new(f)), "(" "^" ")" => RSassert::Xor(Box::new(f1), Box::new(f2)), - "(" > ")" => RSassert::Or(f), - "(" > ")" => RSassert::And(f), + "(" > ")" => RSassert::Or(f), + "(" > ")" => RSassert::And(f), "inW" => RSassert::Sub(c, RSassertOp::InW), "inR" => RSassert::Sub(c, RSassertOp::InR), "inI" => RSassert::Sub(c, RSassertOp::InI), @@ -74,6 +81,7 @@ Formula_Assert: RSassert<'input> = { "?" "inP" => RSassert::NonEmpty(RSassertOp::InP), }; +// ----- BHMLParser ----- pub BHML: Box> = { => Box::new(g) }; @@ -81,8 +89,8 @@ pub BHML: Box> = { Formula_BHML: RSBHML<'input> = { "true" => RSBHML::True, "false" => RSBHML::False, - "(" > ")" => RSBHML::Or(g), - "(" > ")" => RSBHML::And(g), + "(" > ")" => RSBHML::Or(g), + "(" > ")" => RSBHML::And(g), "<" ">" => RSBHML::Diamond(Box::new(f), Box::new(g)), "[" "]" => RSBHML::Box(Box::new(f), Box::new(g)), }; diff --git a/src/rsprocess/mod.rs b/src/rsprocess/mod.rs index e51ece5..5348efa 100644 --- a/src/rsprocess/mod.rs +++ b/src/rsprocess/mod.rs @@ -1,11 +1,40 @@ use std::collections::BTreeMap; +#[derive(Clone, Debug)] +#[allow(dead_code)] +pub struct RSset<'a> { + identifiers: Vec<&'a str> +} + +impl<'a, const N: usize> From<[&'a str; N]> for RSset<'a> { + fn from(mut arr: [&'a str; N]) -> Self { + arr.sort(); + RSset{identifiers: arr.to_vec()} + } +} + +impl<'a> From<&[&'a str]> for RSset<'a> { + fn from(arr: &[&'a str]) -> Self { + let mut tmp = arr.to_vec(); + tmp.sort(); + RSset{identifiers: tmp} + } +} + +impl<'a> From> for RSset<'a> { + fn from(mut arr: Vec<&'a str>) -> Self { + arr.sort(); + RSset{identifiers: arr} + } +} + + #[derive(Clone, Debug)] #[allow(dead_code)] pub enum RSprocess<'a> { Nill, ConstantIdentifier{identifier: &'a str}, - EntitySet{entities: Vec<&'a str>, next_process: Box>}, + EntitySet{entities: RSset<'a>, next_process: Box>}, WaitEntity{repeat: i64, repeated_process: Box>, next_process: Box>}, NondeterministicChoice{children: Vec>}, Summation{children: Vec>} @@ -58,7 +87,7 @@ pub enum RSassert<'a> { Xor(Box>, Box>), Or(Vec>), And(Vec>), - Sub(Vec<&'a str>, RSassertOp), + Sub(RSset<'a>, RSassertOp), NonEmpty(RSassertOp) }