tamer: parse::lower::ParsedObject: Include Token type parameter

The token type was previously hard-coded to `UnknownToken`, since the use
case was the beginning of the lowering pipeline at the start of the program,
where there was no token type because the first parser (`XirReader`,
currently) is responsible for producing the first token type.

But when we're lowering from the graph (so, the other side of the lowering
pipeline), we _do_ have token types to deal with.

This also emphasizes the inappropriate coupling of `<XirReader as
Iterator>::Item` with `ParsedResult`; I'd like to follow the same approach
that I'm about to introduce with `tamec`, so see a future commit.

DEV-13708
main
Mike Gerwitz 2023-02-21 13:17:32 -05:00
parent a68930589e
commit 963688f889
4 changed files with 26 additions and 17 deletions

View File

@ -92,8 +92,9 @@ fn src_reader<'a>(
fn copy_xml_to<'e, W: io::Write + 'e>(
mut fout: W,
escaper: &'e DefaultEscaper,
) -> impl FnMut(&tamer::parse::ParsedResult<ParsedObject<XirToken, XirError>>) + 'e
{
) -> impl FnMut(
&tamer::parse::ParsedResult<ParsedObject<UnknownToken, XirToken, XirError>>,
) + 'e {
use tamer::{parse::Parsed, xir::writer::XmlWriter};
let mut xmlwriter = Default::default();
@ -157,7 +158,7 @@ fn compile<R: Reporter>(
let asg = DefaultAsg::with_capacity(1024, 2048);
let (_, asg) = Lower::<
ParsedObject<XirToken, XirError>,
ParsedObject<UnknownToken, XirToken, XirError>,
XirToXirf<64, RefinedText>,
_,
>::lower::<_, UnrecoverableError>(src, |toks| {

View File

@ -112,7 +112,7 @@ fn load_xmlo<P: AsRef<Path>, S: Escaper>(
// TODO: This entire block is a WIP and will be incrementally
// abstracted away.
let (mut asg, mut state) = Lower::<
ParsedObject<XirToken, XirError>,
ParsedObject<UnknownToken, XirToken, XirError>,
XirToXirf<4, Text>,
_,
>::lower(src, |toks| {

View File

@ -22,7 +22,6 @@
use super::{
state::ClosedParseState, FinalizeError, FinalizedParser, NoContext, Object,
ParseError, ParseState, Parsed, Parser, Token, TransitionResult,
UnknownToken,
};
use crate::diagnose::Diagnostic;
use std::{fmt::Display, iter, marker::PhantomData};
@ -255,22 +254,27 @@ pub type WidenedParsedResult<S, E> =
/// but as a type for lowering operations.
/// This is useful when a parser does not make use of [`ParseState`] but
/// still wishes to participate in a lowering pipeline.
/// The type of [`Token`] will always be [`UnknownToken`],
/// so this is only useful at the head of such a pipeline.
#[derive(Debug)]
pub struct ParsedObject<O: Object, E: Diagnostic + PartialEq> {
_phantom: PhantomData<(O, E)>,
pub struct ParsedObject<T: Token, O: Object, E: Diagnostic + PartialEq> {
_phantom: PhantomData<(T, O, E)>,
}
impl<O: Object, E: Diagnostic + PartialEq> PartialEq for ParsedObject<O, E> {
impl<T: Token, O: Object, E: Diagnostic + PartialEq> PartialEq
for ParsedObject<T, O, E>
{
fn eq(&self, _other: &Self) -> bool {
true
}
}
impl<O: Object, E: Diagnostic + PartialEq> Eq for ParsedObject<O, E> {}
impl<T: Token, O: Object, E: Diagnostic + PartialEq> Eq
for ParsedObject<T, O, E>
{
}
impl<O: Object, E: Diagnostic + PartialEq> Default for ParsedObject<O, E> {
impl<T: Token, O: Object, E: Diagnostic + PartialEq> Default
for ParsedObject<T, O, E>
{
fn default() -> Self {
Self {
_phantom: Default::default(),
@ -278,14 +282,18 @@ impl<O: Object, E: Diagnostic + PartialEq> Default for ParsedObject<O, E> {
}
}
impl<O: Object, E: Diagnostic + PartialEq> Display for ParsedObject<O, E> {
impl<T: Token, O: Object, E: Diagnostic + PartialEq> Display
for ParsedObject<T, O, E>
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "<generic data>")
}
}
impl<O: Object, E: Diagnostic + PartialEq> ParseState for ParsedObject<O, E> {
type Token = UnknownToken;
impl<T: Token, O: Object, E: Diagnostic + PartialEq> ParseState
for ParsedObject<T, O, E>
{
type Token = T;
type Object = O;
type Error = E;

View File

@ -26,7 +26,7 @@ use super::{
Token,
};
use crate::{
parse::{ParseError, Parsed, ParsedObject, ParsedResult},
parse::{ParseError, Parsed, ParsedObject, ParsedResult, UnknownToken},
span::Context,
sym::{st::raw::WS_EMPTY, GlobalSymbolInternBytes},
};
@ -559,7 +559,7 @@ where
B: BufRead,
S: Escaper,
{
type Item = ParsedResult<ParsedObject<Token, Error>>;
type Item = ParsedResult<ParsedObject<UnknownToken, Token, Error>>;
/// Produce the next XIR [`Token`] from the input.
///