Skip to content

Commit

Permalink
[WIP] Implement first expression parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Zollerboy1 committed May 5, 2024
1 parent ea29b29 commit 4170cc5
Show file tree
Hide file tree
Showing 20 changed files with 1,312 additions and 328 deletions.
137 changes: 136 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ rust-version = "1.79.0"

[workspace.dependencies]
ariadne = "0.4.1"
chumsky = { version = "1.0.0-alpha.7", features = ["nightly", "sync"] }
derive_more = "0.99.17"
juice-core = { version = "0.1.0", path = "core" }
juice-driver = { version = "0.1.0", path = "driver" }
Expand Down
1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ rust-version = { workspace = true }

[dependencies]
ariadne = { workspace = true }
chumsky = { workspace = true }
in_definite = "1.0.0"
33 changes: 32 additions & 1 deletion core/src/diag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{
borrow::Cow,
fmt::{Display, Formatter, Result as FmtResult},
marker::PhantomData,
sync::Arc,
sync::{Arc, Mutex, PoisonError},
};

use ariadne::{Color, Fmt as _, ReportKind};
Expand All @@ -11,6 +11,37 @@ mod private {
pub trait Sealed {}
}

#[derive(Clone, Default)]
pub struct ColorGenerator {
colors: Arc<Mutex<(ariadne::ColorGenerator, Vec<Color>)>>,
current: usize,
}

impl ColorGenerator {
pub fn new() -> Self {
Self::default()
}

pub fn from_generator(generator: ariadne::ColorGenerator) -> Self {
Self {
colors: Arc::new(Mutex::new((generator, Vec::new()))),
current: 0,
}
}

pub fn next(&mut self) -> Color {

Check warning on line 32 in core/src/diag.rs

View workflow job for this annotation

GitHub Actions / Run clippy

method `next` can be confused for the standard trait method `std::iter::Iterator::next`
let mut colors = self.colors.lock().unwrap_or_else(PoisonError::into_inner);
while self.current >= colors.1.len() {
let color = colors.0.next();
colors.1.push(color);
}

let color = colors.1[self.current];
self.current += 1;
color
}
}

pub trait ColorExt {
fn error_color() -> Self;
fn warning_color() -> Self;
Expand Down
1 change: 1 addition & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
mod char_ext;
pub mod diag;
mod option_ext;
pub mod parser_ext;
mod peekable_chars;

use std::ops::{ControlFlow, FromResidual, Try};
Expand Down
41 changes: 41 additions & 0 deletions core/src/parser_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use chumsky::{
combinator::{FoldlWith, FoldrWith, MapWith},
extra::ParserExtra,
input::{Input, MapExtra},
IterParser, Parser,
};

pub trait ParserExt<'a, I: Input<'a>, O, E: ParserExtra<'a, I>>: Parser<'a, I, O, E> + Sized {
fn foldl_with_span<B: IterParser<'a, I, OB, E>, OB>(
self,
other: B,
f: impl Fn(O, OB, I::Span) -> O + Clone,
) -> FoldlWith<impl Fn(O, OB, &mut MapExtra<'a, '_, I, E>) -> O + Clone, Self, B, OB, E> {
self.foldl_with(other, move |o, ob, extra| f(o, ob, extra.span()))
}

fn map_with_span<U>(
self,
f: impl Fn(O, I::Span) -> U + Clone,
) -> MapWith<Self, O, impl Fn(O, &mut MapExtra<'a, '_, I, E>) -> U + Clone> {
self.map_with(move |o, extra| f(o, extra.span()))
}

fn with_span(self) -> MapWith<Self, O, impl Fn(O, &mut MapExtra<'a, '_, I, E>) -> (O, I::Span) + Clone> {
self.map_with(move |o, extra| (o, extra.span()))
}
}

impl<'a, I: Input<'a>, O, E: ParserExtra<'a, I>, P: Parser<'a, I, O, E>> ParserExt<'a, I, O, E> for P {}

pub trait IterParserExt<'a, I: Input<'a>, O, E: ParserExtra<'a, I>>: IterParser<'a, I, O, E> + Sized {
fn foldr_with_span<B: Parser<'a, I, OA, E>, OA>(
self,
other: B,
f: impl Fn(O, OA, I::Span) -> OA + Clone,
) -> FoldrWith<impl Fn(O, OA, &mut MapExtra<'a, '_, I, E>) -> OA + Clone, Self, B, O, E> {
self.foldr_with(other, move |o, oa, extra| f(o, oa, extra.span()))
}
}

impl<'a, I: Input<'a>, O, E: ParserExtra<'a, I>, P: IterParser<'a, I, O, E>> IterParserExt<'a, I, O, E> for P {}
2 changes: 2 additions & 0 deletions frontend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ edition = "2021"

[dependencies]
ariadne = { workspace = true }
chumsky = { workspace = true }
derive-where = "1.2.7"
derive_more = { workspace = true }
juice-core = { workspace = true }
juice-macros = { workspace = true }
lasso = "0.7.2"
num-bigint = "0.4.4"
thousands = "0.2.0"
Loading

0 comments on commit 4170cc5

Please sign in to comment.