|
|
@ -8,7 +8,7 @@
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
use crate::parse::query::parse_query;
|
|
|
|
use crate::parse::query::parse_query;
|
|
|
|
use crate::parse::{ExtractSpan, ImperativeProgram, ImperativeStmt, Pair, Pairs, Rule, SourceSpan};
|
|
|
|
use crate::parse::{ExtractSpan, ImperativeProgram, ImperativeStmt, Pair, Rule, SourceSpan};
|
|
|
|
use crate::{DataValue, FixedRule, ValidityTs};
|
|
|
|
use crate::{DataValue, FixedRule, ValidityTs};
|
|
|
|
use either::{Left, Right};
|
|
|
|
use either::{Left, Right};
|
|
|
|
use itertools::Itertools;
|
|
|
|
use itertools::Itertools;
|
|
|
@ -18,23 +18,24 @@ use std::collections::BTreeMap;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use thiserror::Error;
|
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
|
|
|
|
pub(crate) fn parse_imperative(
|
|
|
|
pub(crate) fn parse_imperative_block(
|
|
|
|
src: Pairs<'_>,
|
|
|
|
src: Pair<'_>,
|
|
|
|
param_pool: &BTreeMap<String, DataValue>,
|
|
|
|
param_pool: &BTreeMap<String, DataValue>,
|
|
|
|
fixed_rules: &BTreeMap<String, Arc<Box<dyn FixedRule>>>,
|
|
|
|
fixed_rules: &BTreeMap<String, Arc<Box<dyn FixedRule>>>,
|
|
|
|
cur_vld: ValidityTs,
|
|
|
|
cur_vld: ValidityTs,
|
|
|
|
) -> Result<ImperativeProgram> {
|
|
|
|
) -> Result<ImperativeProgram> {
|
|
|
|
let mut collected = vec![];
|
|
|
|
let mut collected = vec![];
|
|
|
|
|
|
|
|
|
|
|
|
for pair in src {
|
|
|
|
for pair in src.into_inner() {
|
|
|
|
if pair.as_rule() != Rule::EOI {
|
|
|
|
if pair.as_rule() == Rule::EOI {
|
|
|
|
collected.push(parse_imperative_stmt(
|
|
|
|
break;
|
|
|
|
pair,
|
|
|
|
|
|
|
|
param_pool,
|
|
|
|
|
|
|
|
fixed_rules,
|
|
|
|
|
|
|
|
cur_vld,
|
|
|
|
|
|
|
|
)?);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
collected.push(parse_imperative_stmt(
|
|
|
|
|
|
|
|
pair,
|
|
|
|
|
|
|
|
param_pool,
|
|
|
|
|
|
|
|
fixed_rules,
|
|
|
|
|
|
|
|
cur_vld,
|
|
|
|
|
|
|
|
)?);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Ok(collected)
|
|
|
|
Ok(collected)
|
|
|
@ -78,7 +79,7 @@ fn parse_imperative_stmt(
|
|
|
|
match pair.into_inner().next() {
|
|
|
|
match pair.into_inner().next() {
|
|
|
|
None => ImperativeStmt::ReturnNil,
|
|
|
|
None => ImperativeStmt::ReturnNil,
|
|
|
|
Some(p) => match p.as_rule() {
|
|
|
|
Some(p) => match p.as_rule() {
|
|
|
|
Rule::ident => {
|
|
|
|
Rule::ident | Rule::underscore_ident => {
|
|
|
|
let rel = SmartString::from(p.as_str());
|
|
|
|
let rel = SmartString::from(p.as_str());
|
|
|
|
ImperativeStmt::ReturnTemp { rel }
|
|
|
|
ImperativeStmt::ReturnTemp { rel }
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -95,7 +96,7 @@ fn parse_imperative_stmt(
|
|
|
|
let mut inner = pair.into_inner();
|
|
|
|
let mut inner = pair.into_inner();
|
|
|
|
let condition = inner.next().unwrap();
|
|
|
|
let condition = inner.next().unwrap();
|
|
|
|
let cond = match condition.as_rule() {
|
|
|
|
let cond = match condition.as_rule() {
|
|
|
|
Rule::ident => Left(SmartString::from(condition.as_str())),
|
|
|
|
Rule::underscore_ident => Left(SmartString::from(condition.as_str())),
|
|
|
|
Rule::query_script_inner => Right(parse_query(
|
|
|
|
Rule::query_script_inner => Right(parse_query(
|
|
|
|
condition.into_inner(),
|
|
|
|
condition.into_inner(),
|
|
|
|
param_pool,
|
|
|
|
param_pool,
|
|
|
@ -134,7 +135,7 @@ fn parse_imperative_stmt(
|
|
|
|
nxt = inner.next().unwrap();
|
|
|
|
nxt = inner.next().unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let cond = match nxt.as_rule() {
|
|
|
|
let cond = match nxt.as_rule() {
|
|
|
|
Rule::ident => Left(SmartString::from(nxt.as_str())),
|
|
|
|
Rule::underscore_ident => Left(SmartString::from(nxt.as_str())),
|
|
|
|
Rule::query_script_inner => Right(parse_query(
|
|
|
|
Rule::query_script_inner => Right(parse_query(
|
|
|
|
nxt.into_inner(),
|
|
|
|
nxt.into_inner(),
|
|
|
|
param_pool,
|
|
|
|
param_pool,
|
|
|
@ -143,8 +144,8 @@ fn parse_imperative_stmt(
|
|
|
|
)?),
|
|
|
|
)?),
|
|
|
|
_ => unreachable!(),
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
let body = parse_imperative(
|
|
|
|
let body = parse_imperative_block(
|
|
|
|
inner.next().unwrap().into_inner(),
|
|
|
|
inner.next().unwrap(),
|
|
|
|
param_pool,
|
|
|
|
param_pool,
|
|
|
|
fixed_rules,
|
|
|
|
fixed_rules,
|
|
|
|
cur_vld,
|
|
|
|
cur_vld,
|
|
|
@ -165,14 +166,14 @@ fn parse_imperative_stmt(
|
|
|
|
mark = Some(SmartString::from(nxt.as_str()));
|
|
|
|
mark = Some(SmartString::from(nxt.as_str()));
|
|
|
|
nxt = inner.next().unwrap();
|
|
|
|
nxt = inner.next().unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let body = parse_imperative(
|
|
|
|
let body = parse_imperative_block(
|
|
|
|
inner.next().unwrap().into_inner(),
|
|
|
|
inner.next().unwrap(),
|
|
|
|
param_pool,
|
|
|
|
param_pool,
|
|
|
|
fixed_rules,
|
|
|
|
fixed_rules,
|
|
|
|
cur_vld,
|
|
|
|
cur_vld,
|
|
|
|
)?;
|
|
|
|
)?;
|
|
|
|
let cond = match nxt.as_rule() {
|
|
|
|
let cond = match nxt.as_rule() {
|
|
|
|
Rule::ident => Left(SmartString::from(nxt.as_str())),
|
|
|
|
Rule::underscore_ident => Left(SmartString::from(nxt.as_str())),
|
|
|
|
Rule::query_script_inner => Right(parse_query(
|
|
|
|
Rule::query_script_inner => Right(parse_query(
|
|
|
|
nxt.into_inner(),
|
|
|
|
nxt.into_inner(),
|
|
|
|
param_pool,
|
|
|
|
param_pool,
|
|
|
@ -210,6 +211,15 @@ fn parse_imperative_stmt(
|
|
|
|
temp: SmartString::from(name),
|
|
|
|
temp: SmartString::from(name),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Rule::debug_stmt => {
|
|
|
|
|
|
|
|
// let span = pair.extract_span();
|
|
|
|
|
|
|
|
let name_p = pair.into_inner().next().unwrap();
|
|
|
|
|
|
|
|
let name = name_p.as_str();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ImperativeStmt::TempDebug {
|
|
|
|
|
|
|
|
temp: SmartString::from(name),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
Rule::query_script_inner => {
|
|
|
|
Rule::query_script_inner => {
|
|
|
|
let prog = parse_query(pair.into_inner(), param_pool, fixed_rules, cur_vld)?;
|
|
|
|
let prog = parse_query(pair.into_inner(), param_pool, fixed_rules, cur_vld)?;
|
|
|
|
ImperativeStmt::Program { prog }
|
|
|
|
ImperativeStmt::Program { prog }
|
|
|
@ -219,6 +229,6 @@ fn parse_imperative_stmt(
|
|
|
|
let prog = parse_query(pair.into_inner(), param_pool, fixed_rules, cur_vld)?;
|
|
|
|
let prog = parse_query(pair.into_inner(), param_pool, fixed_rules, cur_vld)?;
|
|
|
|
ImperativeStmt::IgnoreErrorProgram { prog }
|
|
|
|
ImperativeStmt::IgnoreErrorProgram { prog }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
r => unreachable!("{r:?}"),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|