// 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 . //! AIR template parser. //! //! See the [parent module](super) for more information. use super::{ super::{ graph::object::{Pkg, Tpl}, Asg, AsgError, ObjectIndex, }, expr::AirExprAggregateStoreDangling, ir::AirTemplatable, 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 [`AirTemplatable`] 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)] pub enum AirTplAggregate { /// Ready for a template, /// defined as part of the given package. /// /// This state also includes the template header; /// unlike NIR, /// AIR has no restrictions on when template header tokens are /// provided, /// which simplifies AIR generation. Ready(ObjectIndex), Toplevel( ObjectIndex, TplState, AirExprAggregateStoreDangling, ), /// Aggregating tokens into a template. TplExpr( ObjectIndex, TplState, AirExprAggregateStoreDangling, ), } 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::Toplevel(_, tpl, expr) | Self::TplExpr(_, tpl, expr) => { write!(f, "building {tpl} with {expr}") } } } } /// The current reachability status of the template. #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub enum TplState { /// Template is dangling and cannot be referenced by anything else. Dangling(ObjectIndex), /// Template is anonymous and is not reachable by an identifier, /// but is reachable in the current context. AnonymousReachable(ObjectIndex), /// Template is reachable via an identifier. /// /// This uses an [`SPair`] as evidence for that assertion rather than an /// [`ObjectIndex`] so that it provides useful output via [`Display`] /// in parser traces. Identified(ObjectIndex, SPair), } impl TplState { fn oi(&self) -> ObjectIndex { match self { TplState::Dangling(oi) | TplState::AnonymousReachable(oi) | TplState::Identified(oi, _) => *oi, } } fn identify(self, id: SPair) -> Self { Self::Identified(self.oi(), id) } } impl Display for TplState { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { TplState::Dangling(_) => write!(f, "anonymous dangling template"), TplState::AnonymousReachable(_) => { write!(f, "anonymous reachable template") } TplState::Identified(_, id) => { write!(f, "identified template {}", TtQuote::wrap(id)) } } } } impl ParseState for AirTplAggregate { type Token = AirTemplatable; type Object = (); type Error = AsgError; type Context = Asg; fn parse_token( self, tok: Self::Token, asg: &mut Self::Context, ) -> TransitionResult { use super::ir::{AirBind::*, AirTpl::*}; use AirTemplatable::*; use AirTplAggregate::*; match (self, tok) { (Ready(oi_pkg), AirTpl(TplStart(span))) => { let oi_tpl = asg.create(Tpl::new(span)); Transition(Toplevel( oi_pkg, TplState::Dangling(oi_tpl), AirExprAggregate::new_in(oi_tpl), )) .incomplete() } (Toplevel(..), AirTpl(TplStart(_span))) => todo!("nested tpl open"), (Toplevel(oi_pkg, tpl, expr), AirBind(BindIdent(id))) => asg .lookup_or_missing(id) .bind_definition(asg, id, tpl.oi()) .map(|oi_ident| oi_pkg.defines(asg, oi_ident)) .map(|_| ()) .transition(Toplevel(oi_pkg, tpl.identify(id), expr)), (Toplevel(..), AirBind(RefIdent(_))) => { todo!("tpl Toplevel RefIdent") } ( Toplevel(..), tok @ AirTpl(TplMetaStart(..) | TplMetaEnd(..) | TplApply(..)), ) => { todo!("Toplevel meta {tok:?}") } (Toplevel(..), tok @ AirTpl(TplLexeme(..))) => { todo!("err: Toplevel lexeme {tok:?} (must be within metavar)") } (Toplevel(oi_pkg, tpl, _expr_done), AirTpl(TplEnd(span))) => { tpl.oi().close(asg, span); Transition(Ready(oi_pkg)).incomplete() } (TplExpr(oi_pkg, tpl, expr), AirTpl(TplEnd(span))) => { // TODO: duplicated with AirAggregate match expr.is_accepting(asg) { true => { // TODO: this is duplicated with the above tpl.oi().close(asg, span); Transition(Ready(oi_pkg)).incomplete() } false => Transition(TplExpr(oi_pkg, tpl, expr)) .err(AsgError::InvalidTplEndContext(span)), } } (Toplevel(..) | TplExpr(..), AirTpl(TplEndRef(..))) => { todo!("TplEndRef") } ( Toplevel(oi_pkg, tpl, expr) | TplExpr(oi_pkg, tpl, expr), AirExpr(etok), ) => Self::delegate_expr(asg, oi_pkg, tpl, expr, etok), (TplExpr(oi_pkg, tpl, expr), AirBind(etok)) => { Self::delegate_expr(asg, oi_pkg, tpl, expr, etok) } (TplExpr(..), AirTpl(TplStart(_))) => { todo!("nested template (template-generated template)") } ( Ready(..) | TplExpr(..), tok @ AirTpl( TplMetaStart(..) | TplLexeme(..) | TplMetaEnd(..) | TplApply(..), ), ) => { todo!( "metasyntactic token in non-tpl-toplevel context: {tok:?}" ) } (st @ Ready(..), AirTpl(TplEnd(span) | TplEndRef(span))) => { Transition(st).err(AsgError::UnbalancedTpl(span)) } (st @ Ready(..), tok @ (AirExpr(..) | AirBind(..))) => { Transition(st).dead(tok) } } } fn is_accepting(&self, _: &Self::Context) -> bool { matches!(self, Self::Ready(..)) } } impl AirTplAggregate { pub(super) fn new_in_pkg(oi_pkg: ObjectIndex) -> Self { Self::Ready(oi_pkg) } /// Delegate to the expression parser [`AirExprAggregate`]. fn delegate_expr( asg: &mut ::Context, oi_pkg: ObjectIndex, tpl: TplState, expr: AirExprAggregateStoreDangling, etok: impl Into< as ParseState>::Token>, ) -> TransitionResult { let tok = etok.into(); expr.parse_token(tok, asg).branch_dead::( |expr, ()| { Transition(Self::Toplevel(oi_pkg, tpl, expr)).incomplete() }, |expr, result, ()| { result .map(ParseStatus::reflexivity) .transition(Self::TplExpr(oi_pkg, tpl, expr)) }, (), ) } } #[cfg(test)] mod test;