tamer: asg::air: Extract template parsing into `tpl`

Same as the previous commit.  These commits have significantly reduced the
cognitive burden of working on this subsystem.

DEV-13708
main
Mike Gerwitz 2023-03-07 14:00:46 -05:00
parent 4fd8e9ea40
commit c1d04f1cf4
4 changed files with 322 additions and 259 deletions

View File

@ -37,11 +37,7 @@
use super::{graph::object::Pkg, Asg, AsgError, ObjectIndex};
use crate::{
asg::graph::object::Tpl,
diagnose::Annotate,
diagnostic_unreachable,
fmt::{DisplayWrapper, TtQuote},
parse::{prelude::*, util::SPair},
diagnose::Annotate, diagnostic_unreachable, parse::prelude::*,
sym::SymbolId,
};
use std::fmt::{Debug, Display};
@ -51,7 +47,9 @@ mod ir;
pub use ir::Air;
mod expr;
mod tpl;
use expr::AirExprAggregate;
use tpl::AirTplAggregate;
pub type IdentSym = SymbolId;
pub type DepSym = SymbolId;
@ -167,7 +165,7 @@ impl ParseState for AirAggregate {
) => Transition(PkgTpl(
oi_pkg,
expr,
AirTplAggregate::define_in_pkg(oi_pkg),
AirTplAggregate::new_in_pkg(oi_pkg),
))
.incomplete()
.with_lookahead(tok),
@ -314,130 +312,5 @@ impl AirAggregate {
}
}
/// Template parser and token aggregator.
///
/// A template consists of
///
/// - Metadata about the template,
/// including its parameters; and
/// - A collection of [`Air`] tokens representing the body of the
/// template that will be expanded into the application site when the
/// template is applied.
///
/// This contains an embedded [`AirExprAggregate`] parser for handling
/// expressions just the same as [`AirAggregate`] does with packages.
#[derive(Debug, PartialEq, Eq)]
pub enum AirTplAggregate {
/// Ready for a template,
/// defined as part of the given package.
///
/// This state also include the template header;
/// unlike NIR,
/// AIR has no restrictions on when template header tokens are
/// provided,
/// which simplifies AIR generation.
///
/// The expression parser [`AirExprAggregate`] is initialized at this
/// state to avoid re-allocating its stack for adjacent templates.
/// This will not save any allocations if,
/// after reaching a dead state,
/// the caller
/// (or parent parser)
/// chooses to deallocate us.
Ready(ObjectIndex<Pkg>, AirExprAggregate),
/// Aggregating tokens into a template.
BuildingTpl(
ObjectIndex<Pkg>,
ObjectIndex<Tpl>,
AirExprAggregate,
Option<SPair>,
),
}
impl Display for AirTplAggregate {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::Ready(_, _) => write!(f, "ready for template definition"),
Self::BuildingTpl(_, expr, _, None) => {
write!(f, "building anonymous template with {expr}")
}
Self::BuildingTpl(_, expr, _, Some(name)) => {
write!(
f,
"building named template {} with {expr}",
TtQuote::wrap(name)
)
}
}
}
}
impl ParseState for AirTplAggregate {
type Token = Air;
type Object = ();
type Error = AsgError;
type Context = Asg;
fn parse_token(
self,
tok: Self::Token,
asg: &mut Self::Context,
) -> TransitionResult<Self::Super> {
use ir::{AirBind::*, AirSubsets::*, AirTodo::*, AirTpl::*};
use AirTplAggregate::*;
match (self, tok.into()) {
(st, AirTodo(Todo(_))) => Transition(st).incomplete(),
(Ready(oi_pkg, expr), AirTpl(TplOpen(span))) => {
let oi_tpl = asg.create(Tpl::new(span));
Transition(BuildingTpl(oi_pkg, oi_tpl, expr, None)).incomplete()
}
(
BuildingTpl(oi_pkg, oi_tpl, expr, _),
AirBind(BindIdent(name)),
) => asg
.lookup_or_missing(name)
.bind_definition(asg, name, oi_tpl)
.map(|oi_ident| oi_pkg.defines(asg, oi_ident))
.map(|_| ())
.transition(BuildingTpl(oi_pkg, oi_tpl, expr, Some(name))),
(BuildingTpl(oi_pkg, oi_tpl, expr, _), AirTpl(TplClose(span))) => {
oi_tpl.close(asg, span);
Transition(Ready(oi_pkg, expr)).incomplete()
}
(BuildingTpl(..), AirPkg(_)) => {
todo!("template cannot define packages")
}
(BuildingTpl(..), tok) => todo!("BuildingTpl body: {tok:?}"),
(st @ Ready(..), AirTpl(TplClose(span))) => {
Transition(st).err(AsgError::UnbalancedTpl(span))
}
(
st @ Ready(..),
tok @ (AirPkg(..) | AirExpr(..) | AirBind(..) | AirIdent(..)),
) => Transition(st).dead(tok.into()),
}
}
fn is_accepting(&self, _: &Self::Context) -> bool {
matches!(self, Self::Ready(..))
}
}
impl AirTplAggregate {
fn define_in_pkg(oi_pkg: ObjectIndex<Pkg>) -> Self {
Self::Ready(oi_pkg, AirExprAggregate::new_in_pkg(oi_pkg))
}
}
#[cfg(test)]
mod test;

View File

@ -20,9 +20,9 @@
//! These are tested as if they are another API directly atop of the ASG,
//! since that is how they are used.
use super::{super::Ident, expr::test::collect_subexprs, *};
use super::{super::Ident, *};
use crate::{
asg::{Expr, ExprOp, IdentKind, Source},
asg::{IdentKind, Source},
parse::{ParseError, Parsed, Parser},
span::dummy::*,
};
@ -319,132 +319,6 @@ where
)
}
// A template is defined by the package containing it,
// like an expression.
#[test]
fn tpl_defining_pkg() {
let id_tpl = SPair("_tpl_".into(), S3);
let toks = vec![
Air::PkgOpen(S1),
// This also tests tpl as a transition away from the package header.
Air::TplOpen(S2),
Air::BindIdent(id_tpl),
Air::TplClose(S4),
Air::PkgClose(S5),
];
let mut sut = Sut::parse(toks.into_iter());
assert!(sut.all(|x| x.is_ok()));
let asg = sut.finalize().unwrap().into_context();
let tpl = asg.expect_ident_obj::<Tpl>(id_tpl);
assert_eq!(S2.merge(S4).unwrap(), tpl.span());
let oi_id_tpl = asg.lookup(id_tpl).unwrap();
assert_eq!(
S1.merge(S5),
oi_id_tpl.src_pkg(&asg).map(|pkg| pkg.resolve(&asg).span()),
);
}
#[test]
fn tpl_after_expr() {
let id_expr = SPair("expr".into(), S3);
let id_tpl = SPair("_tpl_".into(), S6);
#[rustfmt::skip]
let toks = vec![
Air::PkgOpen(S1),
// This expression is incidental to this test;
// it need only parse.
Air::ExprOpen(ExprOp::Sum, S2),
Air::BindIdent(id_expr),
Air::ExprClose(S4),
// Open after an expression.
Air::TplOpen(S5),
Air::BindIdent(id_tpl),
Air::TplClose(S7),
Air::PkgClose(S8),
];
let mut sut = Sut::parse(toks.into_iter());
assert!(sut.all(|x| x.is_ok()));
let asg = sut.finalize().unwrap().into_context();
let tpl = asg.expect_ident_obj::<Tpl>(id_tpl);
assert_eq!(S5.merge(S7).unwrap(), tpl.span());
}
// Templates within expressions are permitted by NIR at the time of writing
// (and so cannot be observed in system tests using TAME's source
// language),
// but it _is_ permitted by AIR,
// to simplify lowering, desugaring, and template expansion.
//
// This test is a rather important one,
// since it ensures that expression context is properly restored
// regardless of whether a template is encountered.
// This context includes the entire active expression stack.
#[test]
fn tpl_within_expr() {
let id_expr = SPair("expr".into(), S3);
let id_tpl = SPair("_tpl_".into(), S7);
#[rustfmt::skip]
let toks = vec![
Air::PkgOpen(S1),
Air::ExprOpen(ExprOp::Sum, S2),
Air::BindIdent(id_expr),
// Child expression before the template to ensure that the
// context is properly restored after template parsing.
Air::ExprOpen(ExprOp::Sum, S4),
Air::ExprClose(S5),
// Template _within_ an expression.
// This will not be present in the final expression,
// as if it were hoisted out.
Air::TplOpen(S6),
Air::BindIdent(id_tpl),
Air::TplClose(S8),
// Child expression _after_ the template for the same reason.
Air::ExprOpen(ExprOp::Sum, S9),
Air::ExprClose(S10),
Air::ExprClose(S11),
Air::PkgClose(S12),
];
let mut sut = Sut::parse(toks.into_iter());
assert!(sut.all(|x| x.is_ok()));
let asg = sut.finalize().unwrap().into_context();
// The inner template.
let tpl = asg.expect_ident_obj::<Tpl>(id_tpl);
assert_eq!(S6.merge(S8).unwrap(), tpl.span());
// The expression that was produced on the graph ought to be equivalent
// to the expression without the template being present at all
// (noting that the spans are of course not adjusted).
let oi_expr = asg.expect_ident_oi::<Expr>(id_expr);
let expr = oi_expr.resolve(&asg);
assert_eq!(S2.merge(S11).unwrap(), expr.span());
assert_eq!(
#[rustfmt::skip]
vec![
S4.merge(S5).unwrap(),
S9.merge(S10).unwrap(),
],
collect_subexprs(&asg, oi_expr)
.iter()
.map(|(_, expr)| expr.span())
.rev()
.collect::<Vec<_>>(),
);
}
pub fn asg_from_toks<I: IntoIterator<Item = Air>>(toks: I) -> Asg
where
I::IntoIter: Debug,

View File

@ -0,0 +1,163 @@
// ASG IR template parsing
//
// Copyright (C) 2014-2023 Ryan Specialty, LLC.
//
// This file is part of TAME.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//! AIR template parser.
//!
//! See the [parent module](super) for more information.
use super::{
super::{
graph::object::{Pkg, Tpl},
Asg, AsgError, ObjectIndex,
},
Air, AirExprAggregate,
};
use crate::{
fmt::{DisplayWrapper, TtQuote},
parse::prelude::*,
};
/// Template parser and token aggregator.
///
/// A template consists of
///
/// - Metadata about the template,
/// including its parameters; and
/// - A collection of [`Air`] tokens representing the body of the
/// template that will be expanded into the application site when the
/// template is applied.
///
/// This contains an embedded [`AirExprAggregate`] parser for handling
/// expressions just the same as [`super::AirAggregate`] does with
/// packages.
#[derive(Debug, PartialEq, Eq)]
pub enum AirTplAggregate {
/// Ready for a template,
/// defined as part of the given package.
///
/// This state also include the template header;
/// unlike NIR,
/// AIR has no restrictions on when template header tokens are
/// provided,
/// which simplifies AIR generation.
///
/// The expression parser [`AirExprAggregate`] is initialized at this
/// state to avoid re-allocating its stack for adjacent templates.
/// This will not save any allocations if,
/// after reaching a dead state,
/// the caller
/// (or parent parser)
/// chooses to deallocate us.
Ready(ObjectIndex<Pkg>, AirExprAggregate),
/// Aggregating tokens into a template.
BuildingTpl(
ObjectIndex<Pkg>,
ObjectIndex<Tpl>,
AirExprAggregate,
Option<SPair>,
),
}
impl Display for AirTplAggregate {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::Ready(_, _) => write!(f, "ready for template definition"),
Self::BuildingTpl(_, expr, _, None) => {
write!(f, "building anonymous template with {expr}")
}
Self::BuildingTpl(_, expr, _, Some(name)) => {
write!(
f,
"building named template {} with {expr}",
TtQuote::wrap(name)
)
}
}
}
}
impl ParseState for AirTplAggregate {
type Token = Air;
type Object = ();
type Error = AsgError;
type Context = Asg;
fn parse_token(
self,
tok: Self::Token,
asg: &mut Self::Context,
) -> TransitionResult<Self::Super> {
use super::ir::{AirBind::*, AirSubsets::*, AirTodo::*, AirTpl::*};
use AirTplAggregate::*;
match (self, tok.into()) {
(st, AirTodo(Todo(_))) => Transition(st).incomplete(),
(Ready(oi_pkg, expr), AirTpl(TplOpen(span))) => {
let oi_tpl = asg.create(Tpl::new(span));
Transition(BuildingTpl(oi_pkg, oi_tpl, expr, None)).incomplete()
}
(
BuildingTpl(oi_pkg, oi_tpl, expr, _),
AirBind(BindIdent(name)),
) => asg
.lookup_or_missing(name)
.bind_definition(asg, name, oi_tpl)
.map(|oi_ident| oi_pkg.defines(asg, oi_ident))
.map(|_| ())
.transition(BuildingTpl(oi_pkg, oi_tpl, expr, Some(name))),
(BuildingTpl(oi_pkg, oi_tpl, expr, _), AirTpl(TplClose(span))) => {
oi_tpl.close(asg, span);
Transition(Ready(oi_pkg, expr)).incomplete()
}
(BuildingTpl(..), AirPkg(_)) => {
todo!("template cannot define packages")
}
(BuildingTpl(..), tok) => todo!("BuildingTpl body: {tok:?}"),
(st @ Ready(..), AirTpl(TplClose(span))) => {
Transition(st).err(AsgError::UnbalancedTpl(span))
}
(
st @ Ready(..),
tok @ (AirPkg(..) | AirExpr(..) | AirBind(..) | AirIdent(..)),
) => Transition(st).dead(tok.into()),
}
}
fn is_accepting(&self, _: &Self::Context) -> bool {
matches!(self, Self::Ready(..))
}
}
impl AirTplAggregate {
pub(super) fn new_in_pkg(oi_pkg: ObjectIndex<Pkg>) -> Self {
Self::Ready(oi_pkg, AirExprAggregate::new_in_pkg(oi_pkg))
}
}
#[cfg(test)]
mod test;

View File

@ -0,0 +1,153 @@
// Tests for ASG IR template parsing
//
// Copyright (C) 2014-2023 Ryan Specialty, LLC.
//
// This file is part of TAME.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
use super::*;
use crate::asg::{
air::{expr::test::collect_subexprs, Air, AirAggregate},
Expr, ExprOp,
};
use crate::span::dummy::*;
type Sut = AirAggregate;
// A template is defined by the package containing it,
// like an expression.
#[test]
fn tpl_defining_pkg() {
let id_tpl = SPair("_tpl_".into(), S3);
let toks = vec![
Air::PkgOpen(S1),
// This also tests tpl as a transition away from the package header.
Air::TplOpen(S2),
Air::BindIdent(id_tpl),
Air::TplClose(S4),
Air::PkgClose(S5),
];
let mut sut = Sut::parse(toks.into_iter());
assert!(sut.all(|x| x.is_ok()));
let asg = sut.finalize().unwrap().into_context();
let tpl = asg.expect_ident_obj::<Tpl>(id_tpl);
assert_eq!(S2.merge(S4).unwrap(), tpl.span());
let oi_id_tpl = asg.lookup(id_tpl).unwrap();
assert_eq!(
S1.merge(S5),
oi_id_tpl.src_pkg(&asg).map(|pkg| pkg.resolve(&asg).span()),
);
}
#[test]
fn tpl_after_expr() {
let id_expr = SPair("expr".into(), S3);
let id_tpl = SPair("_tpl_".into(), S6);
#[rustfmt::skip]
let toks = vec![
Air::PkgOpen(S1),
// This expression is incidental to this test;
// it need only parse.
Air::ExprOpen(ExprOp::Sum, S2),
Air::BindIdent(id_expr),
Air::ExprClose(S4),
// Open after an expression.
Air::TplOpen(S5),
Air::BindIdent(id_tpl),
Air::TplClose(S7),
Air::PkgClose(S8),
];
let mut sut = Sut::parse(toks.into_iter());
assert!(sut.all(|x| x.is_ok()));
let asg = sut.finalize().unwrap().into_context();
let tpl = asg.expect_ident_obj::<Tpl>(id_tpl);
assert_eq!(S5.merge(S7).unwrap(), tpl.span());
}
// Templates within expressions are permitted by NIR at the time of writing
// (and so cannot be observed in system tests using TAME's source
// language),
// but it _is_ permitted by AIR,
// to simplify lowering, desugaring, and template expansion.
//
// This test is a rather important one,
// since it ensures that expression context is properly restored
// regardless of whether a template is encountered.
// This context includes the entire active expression stack.
#[test]
fn tpl_within_expr() {
let id_expr = SPair("expr".into(), S3);
let id_tpl = SPair("_tpl_".into(), S7);
#[rustfmt::skip]
let toks = vec![
Air::PkgOpen(S1),
Air::ExprOpen(ExprOp::Sum, S2),
Air::BindIdent(id_expr),
// Child expression before the template to ensure that the
// context is properly restored after template parsing.
Air::ExprOpen(ExprOp::Sum, S4),
Air::ExprClose(S5),
// Template _within_ an expression.
// This will not be present in the final expression,
// as if it were hoisted out.
Air::TplOpen(S6),
Air::BindIdent(id_tpl),
Air::TplClose(S8),
// Child expression _after_ the template for the same reason.
Air::ExprOpen(ExprOp::Sum, S9),
Air::ExprClose(S10),
Air::ExprClose(S11),
Air::PkgClose(S12),
];
let mut sut = Sut::parse(toks.into_iter());
assert!(sut.all(|x| x.is_ok()));
let asg = sut.finalize().unwrap().into_context();
// The inner template.
let tpl = asg.expect_ident_obj::<Tpl>(id_tpl);
assert_eq!(S6.merge(S8).unwrap(), tpl.span());
// The expression that was produced on the graph ought to be equivalent
// to the expression without the template being present at all
// (noting that the spans are of course not adjusted).
let oi_expr = asg.expect_ident_oi::<Expr>(id_expr);
let expr = oi_expr.resolve(&asg);
assert_eq!(S2.merge(S11).unwrap(), expr.span());
assert_eq!(
#[rustfmt::skip]
vec![
S4.merge(S5).unwrap(),
S9.merge(S10).unwrap(),
],
collect_subexprs(&asg, oi_expr)
.iter()
.map(|(_, expr)| expr.span())
.rev()
.collect::<Vec<_>>(),
);
}