tamer: xir::tree: Use parse::Parser for parse
All tree module parsing functions now make use of parse::Parser. This module will eventually be hoisted from tree. DEV-11268main
parent
b30d7dc84e
commit
69acba3ec0
|
@ -38,7 +38,7 @@
|
|||
//!# let token_stream: std::vec::IntoIter<Token> = vec![].into_iter();
|
||||
//! // Consume a single token at a time, yielding either an incomplete state
|
||||
//! // or the next parsed object.
|
||||
//! let parser = token_stream.scan(Stack::default(), parse);
|
||||
//! let parser = parse(token_stream);
|
||||
//! ```
|
||||
//!
|
||||
//! `parser_from` Or `parse`?
|
||||
|
@ -178,11 +178,11 @@ mod parse;
|
|||
|
||||
use self::{
|
||||
attr::AttrParserState,
|
||||
parse::{ParseResult, ParseStateResult},
|
||||
parse::{ParseResult, ParseState, ParseStateResult, ParsedResult},
|
||||
};
|
||||
|
||||
use super::{QName, Token, TokenResultStream, TokenStream};
|
||||
use crate::{span::Span, sym::SymbolId, xir::tree::parse::ParseState};
|
||||
use crate::{span::Span, sym::SymbolId};
|
||||
use std::{error::Error, fmt::Display, mem::take};
|
||||
|
||||
pub use attr::{Attr, AttrList};
|
||||
|
@ -840,8 +840,7 @@ pub enum Object {
|
|||
Attr(Attr),
|
||||
}
|
||||
|
||||
/// Wrap [`Stack::parse_token`] result in [`Some`],
|
||||
/// suitable for use with [`Iterator::scan`].
|
||||
/// Produce a streaming parser for the given [`TokenStream`].
|
||||
///
|
||||
/// If you do not require a single-step [`Iterator::next`] and simply want
|
||||
/// the next parsed object,
|
||||
|
@ -859,10 +858,12 @@ pub enum Object {
|
|||
///
|
||||
///# let token_stream: std::vec::IntoIter<Token> = vec![].into_iter();
|
||||
/// // The above is equivalent to:
|
||||
/// let parser = token_stream.scan(Stack::default(), parse);
|
||||
/// let parser = parse(token_stream);
|
||||
/// ```
|
||||
pub fn parse(state: &mut Stack, tok: Token) -> Option<Result<Parsed>> {
|
||||
Some(Stack::parse_token(state, tok).map(Into::into))
|
||||
pub fn parse(
|
||||
toks: impl TokenStream,
|
||||
) -> impl Iterator<Item = ParsedResult<Stack>> {
|
||||
Stack::parse(toks)
|
||||
}
|
||||
|
||||
/// Produce a lazy parser from a given [`TokenStream`],
|
||||
|
@ -892,7 +893,7 @@ pub fn parse(state: &mut Stack, tok: Token) -> Option<Result<Parsed>> {
|
|||
pub fn parser_from(
|
||||
toks: impl TokenStream,
|
||||
) -> impl Iterator<Item = ParseResult<Stack, Tree>> {
|
||||
Stack::parser(toks).filter_map(|parsed| match parsed {
|
||||
Stack::parse(toks).filter_map(|parsed| match parsed {
|
||||
Ok(Parsed::Object(Object::Tree(tree))) => Some(Ok(tree)),
|
||||
Ok(Parsed::Incomplete) => None,
|
||||
Err(x) => Some(Err(x)),
|
||||
|
@ -930,7 +931,7 @@ pub fn attr_parser_from<'a>(
|
|||
) -> impl Iterator<Item = Result<Attr>> + 'a {
|
||||
use parse::Parsed;
|
||||
|
||||
AttrParserState::parser(toks).filter_map(|parsed| match parsed {
|
||||
AttrParserState::parse(toks).filter_map(|parsed| match parsed {
|
||||
Ok(Parsed::Object(attr)) => Some(Ok(attr)),
|
||||
Ok(Parsed::Incomplete) => None,
|
||||
Err(x) => Some(Err(x.into())),
|
||||
|
|
|
@ -62,7 +62,7 @@ pub trait ParseState: Default {
|
|||
///
|
||||
/// Whether this method is helpful or provides any clarity depends on
|
||||
/// the context and the types that are able to be inferred.
|
||||
fn parser<I: TokenStream>(toks: I) -> Parser<Self, I> {
|
||||
fn parse<I: TokenStream>(toks: I) -> Parser<Self, I> {
|
||||
Parser::from(toks)
|
||||
}
|
||||
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
use super::*;
|
||||
use crate::convert::ExpectInto;
|
||||
use crate::sym::GlobalSymbolIntern;
|
||||
use crate::xir::tree::parse::ParseError;
|
||||
|
||||
lazy_static! {
|
||||
static ref S: Span =
|
||||
|
@ -94,7 +95,7 @@ fn empty_element_self_close_from_toks() {
|
|||
span: (*S, *S2),
|
||||
};
|
||||
|
||||
let mut sut = toks.scan(Default::default(), parse);
|
||||
let mut sut = Stack::parse(toks);
|
||||
|
||||
assert_eq!(sut.next(), Some(Ok(Parsed::Incomplete)));
|
||||
assert_eq!(
|
||||
|
@ -120,7 +121,7 @@ fn empty_element_balanced_close_from_toks() {
|
|||
span: (*S, *S2),
|
||||
};
|
||||
|
||||
let mut sut = toks.scan(Default::default(), parse);
|
||||
let mut sut = Stack::parse(toks);
|
||||
|
||||
assert_eq!(sut.next(), Some(Ok(Parsed::Incomplete)));
|
||||
assert_eq!(
|
||||
|
@ -143,15 +144,15 @@ fn empty_element_unbalanced_close_from_toks() {
|
|||
]
|
||||
.into_iter();
|
||||
|
||||
let mut sut = toks.scan(Default::default(), parse);
|
||||
let mut sut = Stack::parse(toks);
|
||||
|
||||
assert_eq!(sut.next(), Some(Ok(Parsed::Incomplete)));
|
||||
assert_eq!(
|
||||
sut.next(),
|
||||
Some(Err(StackError::UnbalancedTag {
|
||||
Some(Err(ParseError::StateError(StackError::UnbalancedTag {
|
||||
open: (open_name, *S),
|
||||
close: (close_name, *S2),
|
||||
}))
|
||||
})))
|
||||
);
|
||||
|
||||
// TODO: We need to figure out how to best implement recovery before
|
||||
|
@ -186,7 +187,7 @@ fn empty_element_with_attrs_from_toks() {
|
|||
span: (*S, *S2),
|
||||
};
|
||||
|
||||
let mut sut = toks.scan(Default::default(), parse);
|
||||
let mut sut = Stack::parse(toks);
|
||||
|
||||
assert_eq!(sut.next(), Some(Ok(Parsed::Incomplete))); // Open
|
||||
assert_eq!(sut.next(), Some(Ok(Parsed::Incomplete))); // AttrName
|
||||
|
@ -235,7 +236,7 @@ fn child_element_after_attrs() {
|
|||
span: (*S, *S3),
|
||||
};
|
||||
|
||||
let mut sut = toks.scan(Default::default(), parse);
|
||||
let mut sut = Stack::parse(toks);
|
||||
|
||||
assert_eq!(sut.next(), Some(Ok(Parsed::Incomplete))); // Open
|
||||
assert_eq!(sut.next(), Some(Ok(Parsed::Incomplete))); // AttrName
|
||||
|
|
Loading…
Reference in New Issue