pawarser(setup): continue working on the bare basics

This commit is contained in:
Schrottkatze 2024-10-17 09:54:09 +02:00
commit 21bcf62ea5
No known key found for this signature in database
5 changed files with 196 additions and 26 deletions

View file

@ -2,18 +2,25 @@ use std::cell::Cell;
use enumset::{EnumSet, EnumSetType};
use self::{error::SyntaxError, event::Event, input::Input};
use self::{error::SyntaxError, event::Event, input::Input, marker::Marker};
mod error;
mod event;
pub mod input;
mod input;
mod marker;
pub struct Parser<
'src,
'toks,
SyntaxKind: EnumSetType + Into<rowan::SyntaxKind>,
SyntaxErr: SyntaxError,
> {
/// 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>>,
@ -21,10 +28,76 @@ pub struct Parser<
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: EnumSetType + Into<rowan::SyntaxKind>,
SyntaxKind: SyntaxElement,
// SyntaxErr: SyntaxError,
> {
raw_toks: &'toks Vec<(SyntaxKind, &'src str)>,
@ -32,9 +105,7 @@ pub struct ParserBuilder<
step_limit: u32,
}
impl<'src, 'toks, SyntaxKind: EnumSetType + Into<rowan::SyntaxKind>>
ParserBuilder<'src, 'toks, SyntaxKind>
{
impl<'src, 'toks, SyntaxKind: SyntaxElement> ParserBuilder<'src, 'toks, SyntaxKind> {
pub fn new(raw_toks: &'toks Vec<(SyntaxKind, &'src str)>) -> Self {
Self {
raw_toks,