2021-05-22 17:20:22 +03:00
|
|
|
//! A "Parser" structure for token trees. We use this when parsing a declarative
|
|
|
|
//! macro definition into a list of patterns and templates.
|
2019-09-30 11:58:53 +03:00
|
|
|
|
2021-02-28 13:06:17 +08:00
|
|
|
use syntax::SyntaxKind;
|
internal: replace TreeSink with a data structure
The general theme of this is to make parser a better independent
library.
The specific thing we do here is replacing callback based TreeSink with
a data structure. That is, rather than calling user-provided tree
construction methods, the parser now spits out a very bare-bones tree,
effectively a log of a DFS traversal.
This makes the parser usable without any *specifc* tree sink, and allows
us to, eg, move tests into this crate.
Now, it's also true that this is a distinction without a difference, as
the old and the new interface are equivalent in expressiveness. Still,
this new thing seems somewhat simpler. But yeah, I admit I don't have a
suuper strong motivation here, just a hunch that this is better.
2021-12-19 17:36:23 +03:00
|
|
|
use tt::buffer::TokenBuffer;
|
2021-02-28 13:06:17 +08:00
|
|
|
|
2022-01-02 02:39:14 +01:00
|
|
|
use crate::{to_parser_input::to_parser_input, ExpandError, ExpandResult};
|
|
|
|
|
2019-09-17 02:54:22 +03:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub(crate) struct TtIter<'a> {
|
|
|
|
pub(crate) inner: std::slice::Iter<'a, tt::TokenTree>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> TtIter<'a> {
|
|
|
|
pub(crate) fn new(subtree: &'a tt::Subtree) -> TtIter<'a> {
|
|
|
|
TtIter { inner: subtree.token_trees.iter() }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn expect_char(&mut self, char: char) -> Result<(), ()> {
|
|
|
|
match self.next() {
|
2022-01-02 02:39:14 +01:00
|
|
|
Some(&tt::TokenTree::Leaf(tt::Leaf::Punct(tt::Punct { char: c, .. }))) if c == char => {
|
2019-09-17 02:54:22 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
_ => Err(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-03 03:08:31 +02:00
|
|
|
pub(crate) fn expect_any_char(&mut self, chars: &[char]) -> Result<(), ()> {
|
|
|
|
match self.next() {
|
|
|
|
Some(tt::TokenTree::Leaf(tt::Leaf::Punct(tt::Punct { char: c, .. })))
|
|
|
|
if chars.contains(c) =>
|
|
|
|
{
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
_ => Err(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-17 02:54:22 +03:00
|
|
|
pub(crate) fn expect_subtree(&mut self) -> Result<&'a tt::Subtree, ()> {
|
|
|
|
match self.next() {
|
|
|
|
Some(tt::TokenTree::Subtree(it)) => Ok(it),
|
|
|
|
_ => Err(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn expect_leaf(&mut self) -> Result<&'a tt::Leaf, ()> {
|
|
|
|
match self.next() {
|
|
|
|
Some(tt::TokenTree::Leaf(it)) => Ok(it),
|
|
|
|
_ => Err(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn expect_ident(&mut self) -> Result<&'a tt::Ident, ()> {
|
2021-03-20 17:43:51 -06:00
|
|
|
match self.expect_leaf()? {
|
|
|
|
tt::Leaf::Ident(it) if it.text != "_" => Ok(it),
|
|
|
|
_ => Err(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn expect_ident_or_underscore(&mut self) -> Result<&'a tt::Ident, ()> {
|
2019-09-17 02:54:22 +03:00
|
|
|
match self.expect_leaf()? {
|
|
|
|
tt::Leaf::Ident(it) => Ok(it),
|
|
|
|
_ => Err(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 03:24:17 +08:00
|
|
|
pub(crate) fn expect_literal(&mut self) -> Result<&'a tt::Leaf, ()> {
|
|
|
|
let it = self.expect_leaf()?;
|
|
|
|
match it {
|
|
|
|
tt::Leaf::Literal(_) => Ok(it),
|
|
|
|
tt::Leaf::Ident(ident) if ident.text == "true" || ident.text == "false" => Ok(it),
|
2019-09-17 02:54:22 +03:00
|
|
|
_ => Err(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn expect_punct(&mut self) -> Result<&'a tt::Punct, ()> {
|
|
|
|
match self.expect_leaf()? {
|
|
|
|
tt::Leaf::Punct(it) => Ok(it),
|
|
|
|
_ => Err(()),
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 22:29:55 +08:00
|
|
|
|
2021-02-28 13:06:17 +08:00
|
|
|
pub(crate) fn expect_fragment(
|
2021-12-27 15:22:44 +03:00
|
|
|
&mut self,
|
|
|
|
entry_point: parser::PrefixEntryPoint,
|
|
|
|
) -> ExpandResult<Option<tt::TokenTree>> {
|
|
|
|
let buffer = TokenBuffer::from_tokens(self.inner.as_slice());
|
|
|
|
let parser_input = to_parser_input(&buffer);
|
|
|
|
let tree_traversal = entry_point.parse(&parser_input);
|
|
|
|
|
|
|
|
let mut cursor = buffer.begin();
|
|
|
|
let mut error = false;
|
|
|
|
for step in tree_traversal.iter() {
|
|
|
|
match step {
|
|
|
|
parser::Step::Token { kind, mut n_input_tokens } => {
|
|
|
|
if kind == SyntaxKind::LIFETIME_IDENT {
|
|
|
|
n_input_tokens = 2;
|
|
|
|
}
|
|
|
|
for _ in 0..n_input_tokens {
|
|
|
|
cursor = cursor.bump_subtree();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parser::Step::Enter { .. } | parser::Step::Exit => (),
|
|
|
|
parser::Step::Error { .. } => error = true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 16:22:41 +03:00
|
|
|
let err = if error || !cursor.is_root() {
|
2022-02-03 17:25:24 +01:00
|
|
|
Some(ExpandError::BindingError(format!("expected {entry_point:?}").into()))
|
2021-12-27 15:22:44 +03:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut curr = buffer.begin();
|
|
|
|
let mut res = vec![];
|
|
|
|
|
internal: replace TreeSink with a data structure
The general theme of this is to make parser a better independent
library.
The specific thing we do here is replacing callback based TreeSink with
a data structure. That is, rather than calling user-provided tree
construction methods, the parser now spits out a very bare-bones tree,
effectively a log of a DFS traversal.
This makes the parser usable without any *specifc* tree sink, and allows
us to, eg, move tests into this crate.
Now, it's also true that this is a distinction without a difference, as
the old and the new interface are equivalent in expressiveness. Still,
this new thing seems somewhat simpler. But yeah, I admit I don't have a
suuper strong motivation here, just a hunch that this is better.
2021-12-19 17:36:23 +03:00
|
|
|
if cursor.is_root() {
|
|
|
|
while curr != cursor {
|
2021-02-28 13:06:17 +08:00
|
|
|
if let Some(token) = curr.token_tree() {
|
|
|
|
res.push(token);
|
|
|
|
}
|
|
|
|
curr = curr.bump();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.inner = self.inner.as_slice()[res.len()..].iter();
|
|
|
|
let res = match res.len() {
|
|
|
|
1 => Some(res[0].cloned()),
|
|
|
|
0 => None,
|
|
|
|
_ => Some(tt::TokenTree::Subtree(tt::Subtree {
|
|
|
|
delimiter: None,
|
|
|
|
token_trees: res.into_iter().map(|it| it.cloned()).collect(),
|
|
|
|
})),
|
|
|
|
};
|
|
|
|
ExpandResult { value: res, err }
|
|
|
|
}
|
|
|
|
|
2020-03-04 22:29:55 +08:00
|
|
|
pub(crate) fn peek_n(&self, n: usize) -> Option<&tt::TokenTree> {
|
|
|
|
self.inner.as_slice().get(n)
|
|
|
|
}
|
2019-09-17 02:54:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for TtIter<'a> {
|
|
|
|
type Item = &'a tt::TokenTree;
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
self.inner.next()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
self.inner.size_hint()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> std::iter::ExactSizeIterator for TtIter<'a> {}
|