initial parser signature
This commit is contained in:
parent
cc423a0bec
commit
b2e56594b0
3 changed files with 26 additions and 5 deletions
|
|
@ -1,3 +1,5 @@
|
||||||
|
use std::any::Any;
|
||||||
|
|
||||||
use enum_dispatch::enum_dispatch;
|
use enum_dispatch::enum_dispatch;
|
||||||
|
|
||||||
use super::{BoolExpr, ExprRef, NumExpr};
|
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
|
/// If the expression doesn't contain arguments (i.e., it is a leaf expression) then
|
||||||
/// the vector is empty.
|
/// the vector is empty.
|
||||||
fn args(&self) -> Vec<ExprRef<'_>>;
|
fn args(&self) -> Vec<ExprRef<'_>>;
|
||||||
|
|
||||||
|
/// [`std::any::Any`] trampoline for expressions
|
||||||
|
fn as_any(&self) -> &dyn Any;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Marker trait for numeric expressions
|
/// Marker trait for numeric expressions
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
use std::{env, fmt, fs};
|
use std::fmt;
|
||||||
|
|
||||||
use ariadne::{sources, Color, Label, Report, ReportKind};
|
|
||||||
use chumsky::prelude::*;
|
use chumsky::prelude::*;
|
||||||
|
|
||||||
pub type Span = SimpleSpan<usize>;
|
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)]
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
pub enum Token<'src> {
|
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
|
// A parser for numbers
|
||||||
let digits = text::digits(10).slice();
|
let digits = text::digits(10).slice();
|
||||||
|
|
||||||
|
|
@ -173,8 +174,6 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn simple_test() {
|
fn simple_test() {
|
||||||
use Token::*;
|
use Token::*;
|
||||||
type Output<'a> = Vec<(Token<'a>, Span)>;
|
|
||||||
type MyErr<'a> = extra::Err<Rich<'a, char, Span>>;
|
|
||||||
let cases = [
|
let cases = [
|
||||||
("true", vec![(Bool(true), Span::new(0, 4))]),
|
("true", vec![(Bool(true), Span::new(0, 4))]),
|
||||||
("false", vec![(Bool(false), Span::new(0, 5))]),
|
("false", vec![(Bool(false), Span::new(0, 5))]),
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue