use crate::artifact::tla_trace::{TlaState, TlaTrace};
use crate::Error;
pub(crate) fn parse(counterexample: &str) -> Result<TlaTrace, Error> {
let lines = counterexample.lines();
let mut state_index = 0;
let mut state = None;
let mut trace = TlaTrace::new();
for line in lines {
if line.starts_with("(*") || line.starts_with("\\*") {
continue;
}
if line.starts_with("=====") {
return Ok(trace);
}
let next_state = format!("State{} ==", state_index);
let inv_violation = "InvariantViolation";
if line.starts_with(&next_state) || line.starts_with(&inv_violation) {
if state_index > 0 {
let state = state
.take()
.expect("[modelator] a trace state should have been started");
trace.add(state);
}
let mut tla_state = TlaState::new();
let line = line
.trim_start_matches(&next_state)
.trim_start_matches(&inv_violation);
tla_state.push_str(line);
tla_state.push('\n');
state = Some(tla_state);
state_index += 1;
continue;
}
if let Some(state) = state.as_mut() {
state.push_str(line);
state.push('\n');
}
}
Err(Error::InvalidApalacheCounterexample(
counterexample.to_owned(),
))
}