Version two of assert, now with parser

This commit is contained in:
elvis
2025-07-29 22:56:32 +02:00
parent 1a2ffe1b32
commit c28a8c9685
3 changed files with 240 additions and 134 deletions

View File

@ -5,7 +5,9 @@ use crate::rsprocess::structure::{RSset,
RSprocess,
RSenvironment,
RSsystem,
RSlabel,
RSreaction};
use crate::rsprocess::structure::assert;
use crate::rsprocess::translator::{Translator, IdType};
use crate::rsprocess::presets::Instructions;
use crate::rsprocess::presets;
@ -120,6 +122,7 @@ Reaction: RSreaction = {
RSreaction::from(r, i, p),
}
// -----------------------------------------------------------------------------
// ContextParser
// -----------------------------------------------------------------------------
@ -152,6 +155,7 @@ CTX_process: RSprocess = {
}
};
// -----------------------------------------------------------------------------
// EnvironmentParser
// -----------------------------------------------------------------------------
@ -165,28 +169,154 @@ Env_term: (IdType, RSprocess) = {
(translator.encode(identifier), k)
};
// -----------------------------------------------------------------------------
// AssertParser
// -----------------------------------------------------------------------------
// pub Assert: Box<RSassert> = {
// <f: Formula_Assert> => Box::new(f)
// };
pub Assert: Box<assert::RSassert> = {
"fn" "{" <f: AssertTree> "}" =>
Box::new(assert::RSassert{tree: f}),
};
AssertTree: assert::Tree = {
<t1: AssertTree2> ";" <t2: AssertTree> =>
assert::Tree::Concat(Box::new(t1), Box::new(t2)),
<t: AssertTree2> => t,
}
AssertTree2: assert::Tree = {
"if" <e: AssertExpression>
"then" "{" <t: AssertTree> "}" =>
assert::Tree::If(Box::new(e), Box::new(t)),
"if" <e: AssertExpression>
"then" "{" <t1: AssertTree> "}"
"else" "{" <t2: AssertTree> "}" =>
assert::Tree::IfElse(Box::new(e), Box::new(t1), Box::new(t2)),
"let" <v: AssertAssignmentVar> "=" <e: AssertExpression> =>
assert::Tree::Assignment(v, Box::new(e)),
"return" <e: AssertExpression> =>
assert::Tree::Return(Box::new(e)),
"for" <v: AssertVariable> "in" <r: AssertRange> "{" <t: AssertTree> "}" =>
assert::Tree::For(v, r, Box::new(t)),
}
AssertAssignmentVar: assert::AssignmentVariable = {
<v: AssertVariable> => assert::AssignmentVariable::Var(v),
<v: AssertVariable> "." <q: AssertQualifierRestricted> =>
assert::AssignmentVariable::QualifiedVar(v, q),
}
AssertVariable: assert::Variable = {
<v: Literal> => assert::Variable::from(v)
}
AssertExpression: assert::Expression = {
"(" <e: AssertExpression> ")" => e,
"True" => assert::Expression::True,
"False" => assert::Expression::False,
// If changing IntegerType in assert.rs, also change from Num to another
// similar parser with different return type
<i: Num> => assert::Expression::Integer(i),
<lab: AssertLabel> => assert::Expression::Label(Box::new(lab)),
<set: Set_of_entities> => assert::Expression::Set(set),
"'" <el: Literal> "'" => assert::Expression::Element(translator.encode(el)),
// strings
PATH => assert::Expression::String(<>.trim_end_matches("\"")
.trim_start_matches("\"")
.to_string()),
<unp: AssertUnaryPrefix> <e: AssertExpression> =>
assert::Expression::Unary(unp, Box::new(e)),
"(" <e: AssertExpression> ")" <uns: AssertUnarySuffix> =>
assert::Expression::Unary(uns, Box::new(e)),
"(" <e1: AssertExpression> <b: AssertBinary> <e2: AssertExpression> ")" =>
assert::Expression::Binary(b, Box::new(e1), Box::new(e2)),
<b: AssertBinaryPrefix>
"(" <e1: AssertExpression> "," <e2: AssertExpression> ")" =>
assert::Expression::Binary(b, Box::new(e1), Box::new(e2)),
}
AssertUnaryPrefix: assert::Unary = {
"not" => assert::Unary::Not,
"rand" => assert::Unary::Rand,
}
AssertUnarySuffix: assert::Unary = {
".empty" => assert::Unary::Empty,
".length" => assert::Unary::Length,
".tostr" => assert::Unary::ToStr,
"." <q: AssertQualifier> => assert::Unary::Qualifier(q),
}
AssertBinary: assert::Binary = {
"&&" => assert::Binary::And,
"||" => assert::Binary::Or,
"^^" => assert::Binary::Xor,
"<" => assert::Binary::Less,
"<=" => assert::Binary::LessEq,
">" => assert::Binary::More,
">=" => assert::Binary::MoreEq,
"==" => assert::Binary::Eq,
"!=" => assert::Binary::NotEq,
"+" => assert::Binary::Plus,
"-" => assert::Binary::Minus,
"*" => assert::Binary::Times,
"^" => assert::Binary::Exponential,
"/" => assert::Binary::Quotient,
"%" => assert::Binary::Reminder,
"::" => assert::Binary::Concat,
}
AssertBinaryPrefix: assert::Binary = {
"substr" => assert::Binary::SubStr,
"min" => assert::Binary::Min,
"max" => assert::Binary::Max,
"commonsubstr" => assert::Binary::CommonSubStr,
}
AssertQualifier: assert::Qualifier = {
"AvailableEntities" => assert::Qualifier::AvailableEntities,
"AllReactants" => assert::Qualifier::AllReactants,
"AllInhibitors" => assert::Qualifier::AllInhibitors,
<q: AssertQualifierRestricted> => assert::Qualifier::Restricted(q),
}
AssertQualifierRestricted: assert::QualifierRestricted = {
"Entities" => assert::QualifierRestricted::Entities,
"Context" => assert::QualifierRestricted::Context,
"Reactants" => assert::QualifierRestricted::Reactants,
"ReactantsAbsent" => assert::QualifierRestricted::ReactantsAbsent,
"Inhibitors" => assert::QualifierRestricted::Inhibitors,
"InhibitorsPresent" => assert::QualifierRestricted::InhibitorsPresent,
"Products" => assert::QualifierRestricted::Products,
}
AssertLabel: RSlabel = {
"["
"Entities" ":" <e: Set_of_entities> ","
"Context" ":" <c: Set_of_entities> ","
"Reactants" ":" <r: Set_of_entities> ","
"ReactantsAbsent" ":" <r_a: Set_of_entities> ","
"Inhibitors" ":" <i: Set_of_entities> ","
"InhibitorsPresent" ":" <i_p: Set_of_entities> ","
"Products" ":" <p: Set_of_entities> ","
"]" => RSlabel::create(e, c, r, r_a, i, i_p, p)
}
AssertRange: assert::Range = {
"{" <e: AssertExpression> "}" => assert::Range::IterateOverSet(Box::new(e)),
"{" <e1: AssertExpression> ".." <e2: AssertExpression> "}" =>
assert::Range::IterateInRange(Box::new(e1), Box::new(e2)),
}
// Formula_Assert: RSassert = {
// "-" <f: Formula_Assert> => RSassert::Not(Box::new(f)),
// "(" <f1: Formula_Assert> "^" <f2: Formula_Assert> ")" =>
// RSassert::Xor(Box::new(f1), Box::new(f2)),
// "(" <f: Separated<Formula_Assert, "\\/">> ")" => RSassert::Or(f),
// "(" <f: Separated<Formula_Assert, "/\\">> ")" => RSassert::And(f),
// <c: Set_of_entities> "inW" => RSassert::Sub(c, RSassertOp::InW),
// <c: Set_of_entities> "inR" => RSassert::Sub(c, RSassertOp::InR),
// <c: Set_of_entities> "inI" => RSassert::Sub(c, RSassertOp::InI),
// <c: Set_of_entities> "inP" => RSassert::Sub(c, RSassertOp::InP),
// "?" "inW" => RSassert::NonEmpty(RSassertOp::InW),
// "?" "inR" => RSassert::NonEmpty(RSassertOp::InR),
// "?" "inI" => RSassert::NonEmpty(RSassertOp::InI),
// "?" "inP" => RSassert::NonEmpty(RSassertOp::InP),
// };
// -----------------------------------------------------------------------------
// BHMLParser
@ -272,7 +402,9 @@ LiteralSeparatorNode: graph::NodeDisplayBase = {
LiteralSeparatorEdge: graph::EdgeDisplayBase = {
PATH =>
graph::EdgeDisplayBase::String {
string: <>.trim_end_matches("\"").trim_start_matches("\"").to_string()
string: <>.trim_end_matches("\"")
.trim_start_matches("\"")
.to_string()
}
};