test expr parsing

This commit is contained in:
Alain Emilia Anna Zscheile 2024-07-23 13:05:08 +02:00
parent 57803002fc
commit 1ee85e4523

View file

@ -12,7 +12,7 @@ use yz_string_utils::StrLexerBase;
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Link(pub u16, pub Box<[u16]>); pub struct Link(pub u16, pub Box<[u16]>);
#[derive(Clone, Debug)] #[derive(Clone, Debug, PartialEq)]
pub enum Expr<L> { pub enum Expr<L> {
Const(f64), Const(f64),
Link(L), Link(L),
@ -56,7 +56,7 @@ impl<L: Clone + Eq + Ord> Expr<L> {
/// A system of PDEs and monitors (outputs) /// A system of PDEs and monitors (outputs)
/// ///
/// LHS is always the first derivative to time (it is not possible to refer to time derivatives on RHS) /// LHS is always the first derivative to time (it is not possible to refer to time derivatives on RHS)
#[derive(Clone, Debug)] #[derive(Clone, Debug, PartialEq)]
pub struct System { pub struct System {
pub vs: Vec<Expr<Link>>, pub vs: Vec<Expr<Link>>,
@ -97,7 +97,7 @@ impl System {
} }
} }
#[derive(Clone, Debug, thiserror::Error)] #[derive(Clone, Debug, PartialEq, thiserror::Error)]
pub enum ParseError { pub enum ParseError {
#[error("unexpected end of expression")] #[error("unexpected end of expression")]
EmptyExpr, EmptyExpr,
@ -151,6 +151,9 @@ impl ParseLink for Link {
} else { } else {
let mut new_slb = (*slb).clone(); let mut new_slb = (*slb).clone();
let var = new_slb.consume_select(|i| i.is_ascii_digit()); let var = new_slb.consume_select(|i| i.is_ascii_digit());
if var.is_empty() {
return Ok(None);
}
let var = var.parse::<u16>()?; let var = var.parse::<u16>()?;
if !new_slb.inp.starts_with('_') { if !new_slb.inp.starts_with('_') {
return Ok(None); return Ok(None);
@ -254,7 +257,7 @@ fn parse_expr_final<L: Clone + ParseLink>(slb: &mut StrLexerBase) -> Result<Expr
``` ```
%vs %vs
# u_t + 0.11 u_x = 0 # u_t + 0.11 u_x = 0
0; -0.11 0_0_ 0; prod(-0.11, 0_0_)
%mons %mons
# monitor u @ 1 + sin(t) # monitor u @ 1 + sin(t)
@ -349,3 +352,31 @@ pub fn parse_system(s: &str) -> Result<System, ((usize, usize), ParseError)> {
Ok(ret) Ok(ret)
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn example0_fail() {
assert_eq!(parse_system("
%vs
# u_t + 0.11 u_x = 0
0; -0.11 0_0_
"), Err(((3, 8), ParseError::InvalidTrail)));
}
#[test]
fn example0() {
println!("{:?}", parse_system("
%vs
# u_t + 0.11 u_x = 0
0; prod(-0.11, 0_0_)
%mons
# monitor u @ 1 + sin(t)
0_; sum(1.0, sin(.))
").unwrap());
}
}