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

feat(remap): diagnostic error messages #6023

Merged
merged 21 commits into from
Jan 18, 2021
Merged
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
12 changes: 12 additions & 0 deletions Cargo.lock

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

14 changes: 9 additions & 5 deletions lib/remap-cli/src/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{repl, Error};
use remap::{state, Object, Program, Runtime, Value};
use remap::{state, Formatter, Object, Program, Runtime, Value};
use std::collections::BTreeMap;
use std::fs::File;
use std::io::{self, Read};
Expand Down Expand Up @@ -51,7 +51,7 @@ fn run(opts: &Opts) -> Result<(), Error> {
repl(objects)
} else {
for mut object in objects {
let result = execute(&mut object, &program).map(|v| {
let result = execute(&mut object, program.clone()).map(|v| {
if opts.print_object {
object.to_string()
} else {
Expand Down Expand Up @@ -79,12 +79,16 @@ fn repl(object: Vec<Value>) -> Result<(), Error> {
Err(Error::ReplFeature)
}

fn execute(object: &mut impl Object, program: &str) -> Result<Value, Error> {
fn execute(object: &mut impl Object, source: String) -> Result<Value, Error> {
let state = state::Program::default();
let mut runtime = Runtime::new(state);
let program = Program::new(program, &remap_functions::all(), None, true)?;
let (program, _) = Program::new(source.clone(), &remap_functions::all(), None, true).map_err(
|diagnostics| Error::Parse(Formatter::new(&source, diagnostics).colored().to_string()),
)?;

runtime.execute(object, &program).map_err(Into::into)
runtime
.run(object, &program)
.map_err(|err| Error::Runtime(err.to_string()))
}

fn read_program(source: Option<&str>, file: Option<&PathBuf>) -> Result<String, Error> {
Expand Down
7 changes: 5 additions & 2 deletions lib/remap-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ pub enum Error {
#[error("io error")]
Io(#[from] std::io::Error),

#[error("remap error: {0}")]
Remap(#[from] remap::RemapError),
#[error("parse error")]
Parse(String),

#[error("runtime error")]
Runtime(String),

#[error("json error")]
Json(#[from] serde_json::Error),
Expand Down
31 changes: 24 additions & 7 deletions lib/remap-cli/src/repl.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::Error;
use prettytable::{format, Cell, Row, Table};
use regex::Regex;
use remap::{state, Object, Program, Runtime, Value};
use remap::{state, Formatter, Object, Program, Runtime, Value};
use remap_functions::all as funcs;
use rustyline::completion::Completer;
use rustyline::error::ReadlineError;
Expand All @@ -27,6 +27,7 @@ pub(crate) fn run(mut objects: Vec<Value>) -> Result<(), Error> {
let mut index = 0;
let func_docs_regex = Regex::new(r"^help\sdocs\s(\w{1,})$").unwrap();

let mut compiler_state = state::Compiler::default();
let mut rt = Runtime::new(state::Program::default());
let mut rl = Editor::<Repl>::new();
rl.set_helper(Some(Repl::new()));
Expand Down Expand Up @@ -113,7 +114,12 @@ pub(crate) fn run(mut objects: Vec<Value>) -> Result<(), Error> {
_ => line,
};

let value = resolve(objects.get_mut(index), &mut rt, command);
let value = resolve(
objects.get_mut(index),
&mut rt,
command,
&mut compiler_state,
);
println!("{}\n", value);
}
Err(ReadlineError::Interrupted) => break,
Expand All @@ -128,18 +134,29 @@ pub(crate) fn run(mut objects: Vec<Value>) -> Result<(), Error> {
Ok(())
}

fn resolve(object: Option<&mut impl Object>, runtime: &mut Runtime, program: &str) -> String {
fn resolve(
object: Option<&mut impl Object>,
runtime: &mut Runtime,
program: &str,
state: &mut state::Compiler,
) -> String {
let object = match object {
None => return Value::Null.to_string(),
Some(object) => object,
};

let program = match Program::new(program, &remap_functions::all(), None, true) {
Ok(program) => program,
Err(err) => return err.to_string(),
let program = match Program::new_with_state(
program.to_owned(),
&remap_functions::all(),
None,
true,
state,
) {
Ok((program, _)) => program,
Err(diagnostics) => return Formatter::new(program, diagnostics).colored().to_string(),
};

match runtime.execute(object, &program) {
match runtime.run(object, &program) {
Ok(value) => value.to_string(),
Err(err) => err.to_string(),
}
Expand Down
2 changes: 2 additions & 0 deletions lib/remap-lang/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ license = "MPL-2.0"
bitflags = "1"
bytes = "0.5.6"
chrono = "0.4"
codespan-reporting = "0.11"
dyn-clone = "1"
paste = "1"
pest = "2"
pest_derive = "2"
regex = "1"
serde = "1"
thiserror = "1"
termcolor = "1"

[dev-dependencies]
criterion = "0.3"
Loading