iowo/crates/pawarser/src/parser.rs

149 lines
4 KiB
Rust

use std::cell::Cell;
use enumset::{EnumSet, EnumSetType};
use self::{error::SyntaxError, event::Event, input::Input, marker::Marker};
mod error;
mod event;
mod input;
mod marker;
/// this is used to define some required SyntaxKinds like an EOF token or an error token
pub trait SyntaxElement
where
Self: EnumSetType + Into<rowan::SyntaxKind> + Clone,
{
/// EOF value. This will be used by the rest of the parser library to represent an EOF.
const EOF: Self;
/// Error value. This will be used as a placeholder for associated respective errors.
const ERROR: Self;
}
pub struct Parser<'src, 'toks, SyntaxKind: SyntaxElement, SyntaxErr: SyntaxError> {
input: Input<'src, 'toks, SyntaxKind>,
pos: usize,
events: Vec<Event<SyntaxKind, SyntaxErr>>,
step_limit: u32,
steps: Cell<u32>,
}
impl<'src, 'toks, SyntaxKind: SyntaxElement, SyntaxErr: SyntaxError>
Parser<'src, 'toks, SyntaxKind, SyntaxErr>
{
/// eat all meaningless tokens at the end of the file.
pub fn eat_succeeding_meaningless(&mut self) {
self.push_ev(Event::Eat {
count: self.input.meaningless_tail_len(),
});
}
/// Get token from current position of the parser.
pub fn current(&self) -> SyntaxKind {
self.step();
self.input.kind(self.pos)
}
pub fn start(&mut self, name: &str) -> Marker {
let pos = self.events.len();
self.push_ev(Event::tombstone());
Marker::new(pos, name)
}
/// Eat next token if it's of kind `kind` and return `true`.
/// Otherwise, `false`.
pub fn eat(&mut self, kind: SyntaxKind) -> bool {
if !self.at(kind) {
return false;
}
self.do_bump();
true
}
fn do_bump(&mut self) {
self.push_ev(Event::Eat {
count: self.input.preceding_meaningless(self.pos),
});
self.pos += 1;
}
/// Check if the token at the current parser position is of `kind`
pub fn at(&self, kind: SyntaxKind) -> bool {
self.nth_at(0, kind)
}
/// Check if the token that is `n` ahead is of `kind`
pub fn nth_at(&self, n: usize, kind: SyntaxKind) -> bool {
self.nth(n) == kind
}
pub fn nth(&self, n: usize) -> SyntaxKind {
self.step();
self.input.kind(self.pos + n)
}
fn push_ev(&mut self, event: Event<SyntaxKind, SyntaxErr>) {
self.events.push(event);
}
fn step(&self) {
let steps = self.steps.get();
assert!(steps <= self.step_limit, "the parser seems stuck.");
self.steps.set(steps + 1);
}
}
pub struct ParserBuilder<
'src,
'toks,
SyntaxKind: SyntaxElement,
// SyntaxErr: SyntaxError,
> {
raw_toks: &'toks Vec<(SyntaxKind, &'src str)>,
meaningless_token_kinds: EnumSet<SyntaxKind>,
step_limit: u32,
}
impl<'src, 'toks, SyntaxKind: SyntaxElement> ParserBuilder<'src, 'toks, SyntaxKind> {
pub fn new(raw_toks: &'toks Vec<(SyntaxKind, &'src str)>) -> Self {
Self {
raw_toks,
meaningless_token_kinds: EnumSet::new(),
step_limit: 4096,
}
}
/// Sets the parser step limit.
/// Defaults to 4096
pub fn step_limit(mut self, new: u32) -> Self {
self.step_limit = new;
self
}
pub fn add_meaningless(mut self, kind: SyntaxKind) -> Self {
self.meaningless_token_kinds.insert(kind);
self
}
pub fn add_meaningless_many(mut self, kind: Vec<SyntaxKind>) -> Self {
self.meaningless_token_kinds
.insert_all(kind.into_iter().collect());
self
}
pub fn build<SyntaxErr: SyntaxError>(self) -> Parser<'src, 'toks, SyntaxKind, SyntaxErr> {
let Self {
raw_toks,
meaningless_token_kinds,
step_limit,
} = self;
Parser {
input: Input::new(raw_toks, Some(meaningless_token_kinds)),
pos: 0,
events: Vec::new(),
step_limit,
steps: Cell::new(0),
}
}
}