Struct debris_parser::parser::Parser
source · pub struct Parser<'a> {
pub input: &'a str,
tokens: Lexer<'a, TokenKind>,
peeked_tokens: VecDeque<Token>,
current: Token,
pub st: SyntaxTree,
pub stack: Vec<(NodeKind, Vec<NodeChild>, usize)>,
}
Fields§
§input: &'a str
§tokens: Lexer<'a, TokenKind>
§peeked_tokens: VecDeque<Token>
§current: Token
§st: SyntaxTree
§stack: Vec<(NodeKind, Vec<NodeChild>, usize)>
Implementations§
source§impl<'a> Parser<'a>
impl<'a> Parser<'a>
pub fn new(input: &'a str) -> Self
sourcefn skip(&mut self)
fn skip(&mut self)
Skips the current token and goes to the next.
The skipped token MUST NOT be discarded.
It must always be possible to reconstruct the input string from the Ast
.
fn nth_next(&mut self, n: usize) -> Token
fn nth_non_whitespace(&mut self, index: &mut usize) -> Token
sourcefn _next_token(&mut self) -> Token
fn _next_token(&mut self) -> Token
Should only be used by nth_next
and skip
fn current_stripped(&mut self) -> Token
sourcefn expect(&mut self, kind: TokenKind) -> ParseResult<Token>
fn expect(&mut self, kind: TokenKind) -> ParseResult<Token>
Consumes all consecutive whitespace and
expects that the current token has given kind
.
Errors if that is not the case.
sourcefn consume(&mut self, kind: TokenKind) -> ParseResult<Token>
fn consume(&mut self, kind: TokenKind) -> ParseResult<Token>
Consume all consecutive whitespace and then consumes the next token
if it equals kind
.
Returns whether the first non-whitespace token was consumed.
Panics
panics if the passed kind
is a whitespace
sourcefn consume_first_of(&mut self, options: &[TokenKind]) -> ParseResult<Token>
fn consume_first_of(&mut self, options: &[TokenKind]) -> ParseResult<Token>
Consumes the first matching token of options
Returns Err
if no option matches
fn consume_first_with( &mut self, predicate: impl Fn(TokenKind) -> bool ) -> Option<Token>
sourcefn consume_whitespace(&mut self)
fn consume_whitespace(&mut self)
Consumes a single whitespace token or does nothing
fn replace_stack(&mut self, kind: NodeKind)
pub fn begin_with(&mut self, kind: NodeKind, children_for_flat: usize) -> usize
sourcefn end_inline(&mut self)
fn end_inline(&mut self)
Discards the current stack kind and appends the elements to the previous node
sourcefn end_to_new(&mut self, node_kind: NodeKind, children_for_flat: usize)
fn end_to_new(&mut self, node_kind: NodeKind, children_for_flat: usize)
Creates a new node of kind node_kind
and ends the current node as the first child into it
fn insert_full_node( &mut self, children_for_flat: usize, kind: NodeKind, children: Box<[NodeChild]> )
sourcefn recover<const N: usize>(
&mut self,
to_stack_idx: usize,
safety_tokens: [TokenKind; N]
) -> ParseResult<()>
fn recover<const N: usize>( &mut self, to_stack_idx: usize, safety_tokens: [TokenKind; N] ) -> ParseResult<()>
Tries to advance the parser to a known safe state, so that parsing can continue
to_node
is the node in the parsers stack that should be closed on success.
safety_tokens
is a list of tokens that, when encountered, allow successful recovery.