mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-26 21:39:07 +00:00
Fix help_can and help_parse
This commit is contained in:
parent
67bca80921
commit
78ceb8cdbf
5 changed files with 54 additions and 19 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -3813,12 +3813,14 @@ dependencies = [
|
||||||
"roc_can",
|
"roc_can",
|
||||||
"roc_derive",
|
"roc_derive",
|
||||||
"roc_load",
|
"roc_load",
|
||||||
|
"roc_module",
|
||||||
"roc_parse",
|
"roc_parse",
|
||||||
"roc_problem",
|
"roc_problem",
|
||||||
"roc_region",
|
"roc_region",
|
||||||
"roc_reporting",
|
"roc_reporting",
|
||||||
"roc_solve",
|
"roc_solve",
|
||||||
"roc_target",
|
"roc_target",
|
||||||
|
"roc_types",
|
||||||
"test_solve_helpers",
|
"test_solve_helpers",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,8 @@ roc_region = { path = "../compiler/region" }
|
||||||
roc_load = { path = "../compiler/load" }
|
roc_load = { path = "../compiler/load" }
|
||||||
roc_parse = { path = "../compiler/parse" }
|
roc_parse = { path = "../compiler/parse" }
|
||||||
roc_can = { path = "../compiler/can" }
|
roc_can = { path = "../compiler/can" }
|
||||||
|
roc_module = { path = "../compiler/module" }
|
||||||
|
roc_types = { path = "../compiler/types" }
|
||||||
roc_problem = { path = "../compiler/problem" }
|
roc_problem = { path = "../compiler/problem" }
|
||||||
roc_reporting = { path = "../reporting" }
|
roc_reporting = { path = "../reporting" }
|
||||||
roc_target = { path = "../compiler/roc_target" }
|
roc_target = { path = "../compiler/roc_target" }
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
use bumpalo::Bump;
|
use bumpalo::Bump;
|
||||||
use std::borrow::Cow;
|
|
||||||
|
|
||||||
/// The purpose of this function is to let us run tests like this:
|
/// The purpose of this function is to let us run tests like this:
|
||||||
///
|
///
|
||||||
|
/// ```rust,ignore
|
||||||
/// run_some_test(r#"
|
/// run_some_test(r#"
|
||||||
/// x = 1
|
/// x = 1
|
||||||
///
|
///
|
||||||
/// x
|
/// x
|
||||||
/// ")
|
/// "#)
|
||||||
///
|
///
|
||||||
/// ...without needing to call a macro like `indoc!` to deal with the fact that
|
/// ...without needing to call a macro like `indoc!` to deal with the fact that
|
||||||
/// multiline Rust string literals preserve all the indented spaces.
|
/// multiline Rust string literals preserve all the indented spaces.
|
||||||
|
|
|
@ -1,6 +1,31 @@
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
use crate::help_parse::ParseExpr;
|
use crate::help_parse::ParseExpr;
|
||||||
use bumpalo::Bump;
|
use bumpalo::Bump;
|
||||||
use roc_can::expr::Expr;
|
use roc_can::{
|
||||||
|
desugar,
|
||||||
|
env::Env,
|
||||||
|
expr::{canonicalize_expr, Expr, Output},
|
||||||
|
scope::Scope,
|
||||||
|
};
|
||||||
|
use roc_module::symbol::{IdentIds, Interns, ModuleId, ModuleIds, PackageModuleIds, Symbol};
|
||||||
|
use roc_problem::can::Problem;
|
||||||
|
use roc_region::all::{Loc, Region};
|
||||||
|
use roc_types::{
|
||||||
|
subs::{VarStore, Variable},
|
||||||
|
types::{AliasVar, Type},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct CanExprOut {
|
||||||
|
pub loc_expr: Loc<Expr>,
|
||||||
|
pub output: Output,
|
||||||
|
pub problems: Vec<Problem>,
|
||||||
|
pub home: ModuleId,
|
||||||
|
pub interns: Interns,
|
||||||
|
pub var_store: VarStore,
|
||||||
|
pub var: Variable,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct CanExpr {
|
pub struct CanExpr {
|
||||||
parse_expr: ParseExpr,
|
parse_expr: ParseExpr,
|
||||||
|
@ -14,20 +39,18 @@ impl Default for CanExpr {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CanExpr {
|
fn test_home() -> ModuleId {
|
||||||
pub fn can_expr<'a>(&'a self, input: &'a str) -> Result<Expr, CanExprProblem> {
|
ModuleIds::default().get_or_insert(&"Test".into())
|
||||||
match self.parse_expr.parse_expr(input) {
|
}
|
||||||
Ok(ast) => {
|
|
||||||
// todo canonicalize AST and return that result.
|
|
||||||
let loc_expr = roc_parse::test_helpers::parse_loc_with(arena, expr_str).unwrap_or_else(|e| {
|
|
||||||
panic!(
|
|
||||||
"can_expr_with() got a parse error when attempting to canonicalize:\n\n{expr_str:?} {e:?}"
|
|
||||||
)
|
|
||||||
});
|
|
||||||
|
|
||||||
|
impl CanExpr {
|
||||||
|
pub fn can_expr<'a>(&'a self, input: &'a str) -> CanExprOut {
|
||||||
|
match self.parse_expr.parse_loc_expr(input) {
|
||||||
|
Ok(loc_expr) => {
|
||||||
let mut var_store = VarStore::default();
|
let mut var_store = VarStore::default();
|
||||||
let var = var_store.fresh();
|
let var = var_store.fresh();
|
||||||
let qualified_module_ids = PackageModuleIds::default();
|
let qualified_module_ids = PackageModuleIds::default();
|
||||||
|
let home = test_home();
|
||||||
|
|
||||||
let mut scope = Scope::new(
|
let mut scope = Scope::new(
|
||||||
home,
|
home,
|
||||||
|
@ -38,8 +61,8 @@ impl CanExpr {
|
||||||
|
|
||||||
let dep_idents = IdentIds::exposed_builtins(0);
|
let dep_idents = IdentIds::exposed_builtins(0);
|
||||||
let mut env = Env::new(
|
let mut env = Env::new(
|
||||||
arena,
|
&self.arena(),
|
||||||
expr_str,
|
input,
|
||||||
home,
|
home,
|
||||||
Path::new("Test.roc"),
|
Path::new("Test.roc"),
|
||||||
&dep_idents,
|
&dep_idents,
|
||||||
|
@ -95,7 +118,7 @@ impl CanExpr {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(syntax_error) => {
|
Err(syntax_error) => {
|
||||||
// todo panic due to unexpected syntax error
|
panic!("Unexpected syntax error: {:?}", syntax_error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -103,4 +126,8 @@ impl CanExpr {
|
||||||
pub fn into_arena(self) -> Bump {
|
pub fn into_arena(self) -> Bump {
|
||||||
self.parse_expr.into_arena()
|
self.parse_expr.into_arena()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn arena(&self) -> &Bump {
|
||||||
|
&self.parse_expr.arena()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,4 +57,8 @@ impl ParseExpr {
|
||||||
pub fn into_arena(self) -> Bump {
|
pub fn into_arena(self) -> Bump {
|
||||||
self.arena
|
self.arena
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn arena(&self) -> &Bump {
|
||||||
|
&self.arena
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue