use crate::parser::error::{SyntaxErr, SyntaxErrKind};
use crate::parser::marker::{CompletedMarker, Marker, UnknownMarker};
use crate::parser::Parser;
use crate::syntax::child::ChildKind;
use crate::syntax::green::GreenNode;
use crate::syntax::meta::Layout;
use crate::syntax::node_kind::NodeKind;
use crate::tokens::{Keyword, TokenKind};
macro_rules! match_next_token {
($parser:expr, $($body:tt)*) => {
match_next_token!(@inner $parser, [[ $($body)* ]], [[ $($body)* ]])
};
(@inner $parser:expr, [[ $($($pattern:pat_param),+ => $action:expr),+ $(,)? ]], [[ $($($pattern_expr:expr),+ => $_action_expr:expr),+ $(,)? ]]) => {
match $parser.peek_token() {
$($($pattern)|+ => $action),+,
_ => {
$parser.expect_tokens_recover([$($($pattern_expr),+),+]);
Default::default()
}
}
};
}
pub enum LookaheadError {
Eof,
MaxIndexReached,
TokenKindNotFound,
}
pub(crate) struct StallGuard {
last_token_index: Option<usize>,
}
impl StallGuard {
pub(crate) fn new() -> StallGuard {
StallGuard {
last_token_index: None,
}
}
pub(crate) fn should_continue(&mut self, parser: &mut Parser) -> bool {
let current = parser.token_index();
let Some(last) = self.last_token_index else {
self.last_token_index = Some(current);
return true;
};
self.last_token_index = Some(current);
current > last
}
}
pub(crate) const fn choice_options(layout: &Layout) -> &[NodeKind] {
match layout {
Layout::Choice(choice) => choice.options,
_ => panic!("Not a layout choice"),
}
}
impl Parser {
pub(crate) fn push_err(&mut self, kind: SyntaxErrKind) {
self.builder.push_err(kind);
}
pub(crate) fn check_node_is_allowed(&mut self, marker: &CompletedMarker, allowed: &[NodeKind]) {
if !allowed.contains(&marker.kind()) {
self.push_err(SyntaxErrKind::Unexpected(ChildKind::Node(marker.kind())));
}
}
pub(crate) fn skip(&mut self) {
if let Some((token, err)) = self.token_stream.next() {
self.builder.push(token, err);
}
}
pub(crate) fn skip_n(&mut self, n: usize) {
for _ in 0..n {
self.skip();
if self.peek_token().is_eof() {
break;
}
}
}
pub(crate) fn token_index(&self) -> usize {
self.builder.current_token_index()
}
pub(crate) fn expect_token(&mut self, kind: TokenKind) {
if let Some((token, err)) = self.token_stream.next_if(|token| token.kind() == kind) {
self.builder.push(token, err);
return;
}
self.expect_tokens_recover([kind]);
}
pub(crate) fn expect_tokens<const N: usize>(&mut self, kinds: [TokenKind; N]) {
for kind in kinds {
self.expect_token(kind)
}
}
pub(crate) fn expect_one_of_tokens<const N: usize>(
&mut self,
kinds: [TokenKind; N],
) -> Option<TokenKind> {
for kind in kinds {
if self.opt_token(kind) {
return Some(kind);
}
}
self.expect_tokens_recover(kinds);
None
}
pub(crate) fn peek_token(&self) -> TokenKind {
self.token_stream
.peek(0)
.map(|tok| tok.kind())
.unwrap_or(TokenKind::Eof)
}
pub(crate) fn peek_nth_token(&self, n: usize) -> TokenKind {
self.token_stream
.peek(n)
.map(|tok| tok.kind())
.unwrap_or(TokenKind::Eof)
}
pub(crate) fn next_is(&self, kind: TokenKind) -> bool {
self.peek_token() == kind
}
pub(crate) fn next_is_one_of<const N: usize>(&self, kinds: [TokenKind; N]) -> bool {
kinds.contains(&self.peek_token())
}
pub(crate) fn next_nth_is(&self, kind: TokenKind, n: usize) -> bool {
self.peek_nth_token(n) == kind
}
pub(crate) fn expect_kw(&mut self, kind: Keyword) {
self.expect_token(TokenKind::Keyword(kind))
}
pub(crate) fn opt_token(&mut self, kind: TokenKind) -> bool {
if let Some((token, err)) = self.token_stream.next_if(|token| token.kind() == kind) {
self.builder.push(token, err);
true
} else {
false
}
}
pub(crate) fn opt_tokens<const N: usize>(
&mut self,
kinds: [TokenKind; N],
) -> Option<TokenKind> {
if let Some((token, err)) = self
.token_stream
.next_if(|token| kinds.contains(&token.kind()))
{
let kind = token.kind();
self.builder.push(token, err);
Some(kind)
} else {
None
}
}
pub(crate) fn start_node(&mut self, kind: NodeKind) -> Marker {
let marker = self.builder.start_node(kind);
self.recovery.push(kind);
marker
}
pub(crate) fn node(
&mut self,
kind: NodeKind,
builder: impl FnOnce(&mut Parser),
) -> CompletedMarker {
let marker = self.start_node(kind);
builder(self);
marker.complete(self)
}
pub(crate) fn start_unknown(&mut self) -> UnknownMarker {
self.builder.start_unknown()
}
pub(crate) fn end(self) -> (GreenNode, Vec<SyntaxErr>) {
self.builder.end()
}
pub(crate) fn lookahead_max_token_index<const N: usize>(
&mut self,
maximum_index: usize,
kinds: [TokenKind; N],
) -> Result<(TokenKind, usize), (LookaheadError, usize)> {
self.lookahead_max_token_index_skip_n(maximum_index, 0, kinds)
}
pub(crate) fn skip_into_node(&mut self, node: NodeKind) -> CompletedMarker {
self.node(node, Parser::skip)
}
pub(crate) fn lookahead_skip_n<const N: usize>(
&mut self,
skip_n: usize,
kinds: [TokenKind; N],
) -> Result<(TokenKind, usize), (LookaheadError, usize)> {
self.lookahead_max_token_index_skip_n(usize::MAX, skip_n, kinds)
}
pub(crate) fn lookahead_max_token_index_skip_n<const N: usize>(
&mut self,
maximum_index: usize,
skip_n: usize,
kinds: [TokenKind; N],
) -> Result<(TokenKind, usize), (LookaheadError, usize)> {
let mut depth = 0;
let mut curr_token_index = self.token_index() + skip_n;
while curr_token_index <= maximum_index && depth >= 0 {
match self.peek_nth_token(curr_token_index - self.token_index()) {
TokenKind::LeftPar | TokenKind::LeftSquare => depth += 1,
tok @ (TokenKind::RightPar | TokenKind::RightSquare) => {
if depth == 0 && kinds.contains(&tok) {
return Ok((tok, curr_token_index));
}
depth -= 1;
}
TokenKind::Eof => return Err((LookaheadError::Eof, curr_token_index)),
tok => {
if depth == 0 && kinds.contains(&tok) {
return Ok((tok, curr_token_index));
}
}
}
curr_token_index += 1;
}
if curr_token_index > maximum_index {
Err((LookaheadError::MaxIndexReached, curr_token_index))
} else {
Err((LookaheadError::TokenKindNotFound, curr_token_index))
}
}
}