tamer: xir::tree::parser_from: Use parse::Parser

This nearly completely integrates the new Parser with xir::tree, but does
not yet compose AttrParseState.  I also need to determine what to do with
`parse()` and, further, make `parser_from` generic as part of mod parse.

If we take a moment to reflect on all of the changes, this struggle has been
a roundabout way of converting tree's parser into parse::Parser; providing
a trait for Stack (as ParseState); beginning parser decomposition; and
moving some common logic into Parser.  The composition of parsers is the
final piece to be realized.

This could have been a lot less work if I really understood exactly what I
wanted to do up front, but as was mentioned in previous commits, I was
really confusing myself trying to maintain API BC in ways that I should not
have for XmloReader.  More on that will be coming soon as well.

DEV-11268
main
Mike Gerwitz 2021-12-13 16:57:04 -05:00
parent 6e9d139373
commit b30d7dc84e
1 changed files with 14 additions and 15 deletions

View File

@ -176,7 +176,10 @@
mod attr;
mod parse;
use self::{attr::AttrParserState, parse::ParseStateResult};
use self::{
attr::AttrParserState,
parse::{ParseResult, ParseStateResult},
};
use super::{QName, Token, TokenResultStream, TokenStream};
use crate::{span::Span, sym::SymbolId, xir::tree::parse::ParseState};
@ -888,21 +891,17 @@ pub fn parse(state: &mut Stack, tok: Token) -> Option<Result<Parsed>> {
/// ```
pub fn parser_from(
toks: impl TokenStream,
) -> impl Iterator<Item = Result<Tree>> {
toks.scan(Stack::default(), parse)
.filter_map(|parsed| match parsed {
Ok(Parsed::Object(Object::Tree(tree))) => Some(Ok(tree)),
Ok(Parsed::Incomplete) => None,
Err(x) => Some(Err(x)),
) -> impl Iterator<Item = ParseResult<Stack, Tree>> {
Stack::parser(toks).filter_map(|parsed| match parsed {
Ok(Parsed::Object(Object::Tree(tree))) => Some(Ok(tree)),
Ok(Parsed::Incomplete) => None,
Err(x) => Some(Err(x)),
// These make no sense in this context and should never occur.
Ok(x @ Parsed::Object(Object::Attr(_))) => {
unreachable!(
"unexpected yield by XIRT (Tree expected): {:?}",
x
)
}
})
// These make no sense in this context and should never occur.
Ok(x @ Parsed::Object(Object::Attr(_))) => {
unreachable!("unexpected yield by XIRT (Tree expected): {:?}", x)
}
})
}
/// Produce a lazy attribute parser from a given [`TokenStream`],