mirror of
https://forge.katzen.cafe/katzen-cafe/iowo.git
synced 2024-11-21 21:24:41 +01:00
pawarser(setup): basic parser stuff and types around it. also, a builder.
This commit is contained in:
parent
a3ab844ba7
commit
ec2ff5778b
|
@ -1,9 +1,15 @@
|
||||||
#![feature(iter_collect_into)]
|
#![feature(iter_collect_into)]
|
||||||
pub mod parser {
|
pub mod parser {
|
||||||
|
use std::cell::Cell;
|
||||||
|
|
||||||
|
use enumset::{EnumSet, EnumSetType};
|
||||||
|
|
||||||
|
use self::{error::SyntaxError, event::Event, input::Input};
|
||||||
|
|
||||||
pub mod input {
|
pub mod input {
|
||||||
use enumset::{EnumSet, EnumSetType};
|
use enumset::{EnumSet, EnumSetType};
|
||||||
|
|
||||||
struct Input<'src, 'toks, SyntaxKind: EnumSetType + Into<rowan::SyntaxKind>> {
|
pub struct Input<'src, 'toks, SyntaxKind: EnumSetType + Into<rowan::SyntaxKind>> {
|
||||||
raw: &'toks Vec<(SyntaxKind, &'src str)>,
|
raw: &'toks Vec<(SyntaxKind, &'src str)>,
|
||||||
// enumset of meaningless tokens
|
// enumset of meaningless tokens
|
||||||
semantically_meaningless: EnumSet<SyntaxKind>,
|
semantically_meaningless: EnumSet<SyntaxKind>,
|
||||||
|
@ -36,4 +42,125 @@ pub mod parser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
mod event {
|
||||||
|
use enumset::EnumSetType;
|
||||||
|
|
||||||
|
use super::error::SyntaxError;
|
||||||
|
|
||||||
|
pub enum Event<SyntaxKind: EnumSetType + Into<rowan::SyntaxKind>, SyntaxErr: SyntaxError> {
|
||||||
|
Start {
|
||||||
|
kind: NodeKind<SyntaxKind, SyntaxErr>,
|
||||||
|
forward_parent: Option<usize>,
|
||||||
|
},
|
||||||
|
Finish,
|
||||||
|
Eat {
|
||||||
|
count: usize,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<SyntaxKind: EnumSetType + Into<rowan::SyntaxKind>, SyntaxErr: SyntaxError>
|
||||||
|
Event<SyntaxKind, SyntaxErr>
|
||||||
|
{
|
||||||
|
pub fn tombstone() -> Self {
|
||||||
|
Self::Start {
|
||||||
|
kind: NodeKind::Tombstone,
|
||||||
|
forward_parent: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum NodeKind<SyntaxKind: EnumSetType + Into<rowan::SyntaxKind>, SyntaxErr: SyntaxError> {
|
||||||
|
Tombstone,
|
||||||
|
Syntax(SyntaxKind),
|
||||||
|
Error(SyntaxErr),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<SyntaxKind: EnumSetType + Into<rowan::SyntaxKind>, SyntaxErr: SyntaxError>
|
||||||
|
NodeKind<SyntaxKind, SyntaxErr>
|
||||||
|
{
|
||||||
|
pub fn is_tombstone(&self) -> bool {
|
||||||
|
matches!(self, Self::Tombstone)
|
||||||
|
}
|
||||||
|
pub fn is_syntax(&self) -> bool {
|
||||||
|
matches!(self, Self::Syntax(_))
|
||||||
|
}
|
||||||
|
pub fn is_error(&self) -> bool {
|
||||||
|
matches!(self, Self::Error(_))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mod error {
|
||||||
|
/// A marker trait... for now!
|
||||||
|
// TODO: constrain that conversion to `NodeKind::Error` is enforced to be possible
|
||||||
|
pub trait SyntaxError {}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Parser<
|
||||||
|
'src,
|
||||||
|
'toks,
|
||||||
|
SyntaxKind: EnumSetType + Into<rowan::SyntaxKind>,
|
||||||
|
SyntaxErr: SyntaxError,
|
||||||
|
> {
|
||||||
|
input: Input<'src, 'toks, SyntaxKind>,
|
||||||
|
pos: usize,
|
||||||
|
events: Vec<Event<SyntaxKind, SyntaxErr>>,
|
||||||
|
step_limit: u32,
|
||||||
|
steps: Cell<u32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ParserBuilder<
|
||||||
|
'src,
|
||||||
|
'toks,
|
||||||
|
SyntaxKind: EnumSetType + Into<rowan::SyntaxKind>,
|
||||||
|
// SyntaxErr: SyntaxError,
|
||||||
|
> {
|
||||||
|
raw_toks: &'toks Vec<(SyntaxKind, &'src str)>,
|
||||||
|
meaningless_token_kinds: EnumSet<SyntaxKind>,
|
||||||
|
step_limit: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'src, 'toks, SyntaxKind: EnumSetType + Into<rowan::SyntaxKind>>
|
||||||
|
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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue