initial parser signature

This commit is contained in:
Anand Balakrishnan 2023-09-29 13:09:04 -07:00
parent cc423a0bec
commit b2e56594b0
No known key found for this signature in database
3 changed files with 26 additions and 5 deletions

View file

@ -1,3 +1,5 @@
use std::any::Any;
use enum_dispatch::enum_dispatch;
use super::{BoolExpr, ExprRef, NumExpr};
@ -14,6 +16,9 @@ pub trait Expr {
/// If the expression doesn't contain arguments (i.e., it is a leaf expression) then
/// the vector is empty.
fn args(&self) -> Vec<ExprRef<'_>>;
/// [`std::any::Any`] trampoline for expressions
fn as_any(&self) -> &dyn Any;
}
/// Marker trait for numeric expressions

View file

@ -1,9 +1,10 @@
use std::{env, fmt, fs};
use std::fmt;
use ariadne::{sources, Color, Label, Report, ReportKind};
use chumsky::prelude::*;
pub type Span = SimpleSpan<usize>;
pub type Output<'a> = Vec<(Token<'a>, Span)>;
pub type Error<'a> = extra::Err<Rich<'a, char, Span>>;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Token<'src> {
@ -76,7 +77,7 @@ impl<'src> fmt::Display for Token<'src> {
}
}
pub fn lexer<'src>() -> impl Parser<'src, &'src str, Vec<(Token<'src>, Span)>, extra::Err<Rich<'src, char, Span>>> {
pub fn lexer<'src>() -> impl Parser<'src, &'src str, Output<'src>, Error<'src>> {
// A parser for numbers
let digits = text::digits(10).slice();
@ -173,8 +174,6 @@ mod tests {
#[test]
fn simple_test() {
use Token::*;
type Output<'a> = Vec<(Token<'a>, Span)>;
type MyErr<'a> = extra::Err<Rich<'a, char, Span>>;
let cases = [
("true", vec![(Bool(true), Span::new(0, 4))]),
("false", vec![(Bool(false), Span::new(0, 5))]),

View file

@ -1 +1,18 @@
use std::{env, fmt, fs};
use ariadne::{sources, Color, Label, Report, ReportKind};
use chumsky::{input::SpannedInput, prelude::*};
use crate::lexer::{lexer, Span, Token};
pub type Spanned<T> = (T, Span);
// The type of the input that our parser operates on. The input is the `&[(Token, Span)]` token buffer generated by the
// lexer, wrapped in a `SpannedInput` which 'splits' it apart into its constituent parts, tokens and spans, for chumsky
// to understand.
type ParserInput<'tokens, 'src> = SpannedInput<Token<'src>, Span, &'tokens [(Token<'src>, Span)]>;
pub fn parser<'tokens, 'src: 'tokens>(
) -> impl Parser<'tokens, ParserInput<'tokens, 'src>, Spanned<Expr<'src>>, extra::Err<Rich<'tokens, Token<'src>, Span>>>
+ Clone {
}