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