Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPWN RWRT lexer-parser rewrite #270

Open
wants to merge 5 commits into
base: RWRT
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
.DS_Store
.DS_Store
.vscode
36 changes: 36 additions & 0 deletions Cargo.lock

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

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ ansi_term = "0.12.1"
regex = "1.5.6"
thiserror = "1.0.31"
ahash = "0.7.6"
serde = { version = "1.0.138", features = ["derive"] }
serde = { version = "1.0.138", features = ["derive", "rc"] }
bincode = "1.3.3"
lz4-compression = "0.7.0"
yazi = "0.1.4"
yazi = "0.1.4"
lasso = "0.6.0"
base64 = "0.13.0"
erased-serde = "0.3"
4 changes: 2 additions & 2 deletions src/compiler/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod compiler;
mod error;
// pub mod compiler;
// pub mod error;
37 changes: 33 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
use std::fmt::Display;

// use crate::compiler::error::CompilerError;
use crate::interpreter::error::RuntimeError;
use crate::parser::error::SyntaxError;
use ariadne::Color;

pub const ERROR_S: f64 = 0.4;
Expand All @@ -11,6 +16,23 @@ pub struct RainbowColorGenerator {
shift: f64,
}

#[derive(Debug)]
pub enum Error {
Syntax(SyntaxError),
Runtime(RuntimeError),
// Compiler(CompilerError),
}

impl Display for self::Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
todo!()
}
}

impl std::error::Error for self::Error {}

pub type Result<T> = std::result::Result<T, self::Error>;

impl RainbowColorGenerator {
pub fn new(h: f64, s: f64, v: f64, shift: f64) -> Self {
Self { h, s, v, shift }
Expand Down Expand Up @@ -76,12 +98,14 @@ macro_rules! error_maker {
)*

) => {
use std::path::PathBuf;
use $crate::error::*;
use ariadne::{Report, ReportKind, Label, Source, Fmt};
#[allow(unused_imports)]
use $crate::Globals;
// #[allow(unused_imports)]
// use $crate::Globals;

$(
#[derive(Debug)]
pub enum $err_type {
$(
$variant {
Expand All @@ -93,7 +117,7 @@ macro_rules! error_maker {
}

impl $err_type {
pub fn raise(self, source: $crate::sources::SpwnSource $(, $globals: &Globals)?) {
pub fn raise(self, code: String, source: Option<PathBuf> /*$(, $globals: &Globals)?*/) -> String {
let mut label_colors = RainbowColorGenerator::new(120.0, ERROR_S, ERROR_V, 45.0);
let mut item_colors = RainbowColorGenerator::new(0.0, ERROR_S, ERROR_V, 15.0);

Expand All @@ -113,6 +137,7 @@ macro_rules! error_maker {
)*
};

// epic
let mut report = Report::build(ReportKind::Error, area.name(), area.span.0)
.with_message(message.to_string() + "\n");

Expand All @@ -128,10 +153,14 @@ macro_rules! error_maker {
report = report.with_note(m)
}

let ret = vec![];

report
.finish()
.eprint((source.name(), Source::from(source.contents())))
.write((source.name(), Source::from(code)), &mut ret)
.unwrap();

std::str::from_utf8(&ret).unwrap().to_string()
}
}
)*
Expand Down
30 changes: 17 additions & 13 deletions src/interpreter/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,19 @@ error_maker! {
area: CodeArea,
},

#[
Message = "Pattern mismatch", Area = area, Note = None,
Labels = [
area => "This {} is not {}": @(v.value.get_type().to_str()), @(pat.value.to_str(globals));
v.def_area => "This is of type {}": @(v.value.get_type().to_str());
pat.def_area => "Pattern defined as {} here": @(pat.value.to_str(globals));
]
]
PatternMismatch {
v: StoredValue,
pat: StoredValue,
area: CodeArea,
},
// #[
// Message = "Pattern mismatch", Area = area, Note = None,
// Labels = [
// area => "This {} is not {}": @(v.value.get_type().to_str()), @(pat.value.to_str(globals));
// v.def_area => "This is of type {}": @(v.value.get_type().to_str());
// pat.def_area => "Pattern defined as {} here": @(pat.value.to_str(globals));
// ]
// ]
// PatternMismatch {
// v: StoredValue,
// pat: StoredValue,
// area: CodeArea,
// },

#[
Message = "Argument not satisfied", Area = call_area, Note = None,
Expand All @@ -116,12 +116,16 @@ error_maker! {
call_area => "Argument not provided here";
]
]

ArgumentNotSatisfied {
arg_name: String,
call_area: CodeArea,
arg_area: CodeArea,
},

// what if the errors just carry the file too
// and rest is spans

#[
Message = "Too many arguments!", Area = call_area, Note = None,
Labels = [
Expand Down
2 changes: 1 addition & 1 deletion src/interpreter/from_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::value::Value;
// can't return a `RuntimeError` cause can't get an `area` here
// instead return a tuple of arguments to be formatted into error at a different place that
// has an `area`
type Error = (String, &'static str);
pub type Error = (String, &'static str);

pub trait FromValue: Clone {
fn from_value(val: Value) -> Result<Self, Error>;
Expand Down
Loading