mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-26 13:29:12 +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_derive",
|
||||
"roc_load",
|
||||
"roc_module",
|
||||
"roc_parse",
|
||||
"roc_problem",
|
||||
"roc_region",
|
||||
"roc_reporting",
|
||||
"roc_solve",
|
||||
"roc_target",
|
||||
"roc_types",
|
||||
"test_solve_helpers",
|
||||
]
|
||||
|
||||
|
|
|
@ -16,6 +16,8 @@ roc_region = { path = "../compiler/region" }
|
|||
roc_load = { path = "../compiler/load" }
|
||||
roc_parse = { path = "../compiler/parse" }
|
||||
roc_can = { path = "../compiler/can" }
|
||||
roc_module = { path = "../compiler/module" }
|
||||
roc_types = { path = "../compiler/types" }
|
||||
roc_problem = { path = "../compiler/problem" }
|
||||
roc_reporting = { path = "../reporting" }
|
||||
roc_target = { path = "../compiler/roc_target" }
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
use bumpalo::Bump;
|
||||
use std::borrow::Cow;
|
||||
|
||||
/// The purpose of this function is to let us run tests like this:
|
||||
///
|
||||
/// run_some_test(r#"
|
||||
/// x = 1
|
||||
/// ```rust,ignore
|
||||
/// run_some_test(r#"
|
||||
/// x = 1
|
||||
///
|
||||
/// x
|
||||
/// ")
|
||||
/// x
|
||||
/// "#)
|
||||
///
|
||||
/// ...without needing to call a macro like `indoc!` to deal with the fact that
|
||||
/// multiline Rust string literals preserve all the indented spaces.
|
||||
|
|
|
@ -1,6 +1,31 @@
|
|||
use std::path::Path;
|
||||
|
||||
use crate::help_parse::ParseExpr;
|
||||
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 {
|
||||
parse_expr: ParseExpr,
|
||||
|
@ -14,20 +39,18 @@ impl Default for CanExpr {
|
|||
}
|
||||
}
|
||||
|
||||
impl CanExpr {
|
||||
pub fn can_expr<'a>(&'a self, input: &'a str) -> Result<Expr, CanExprProblem> {
|
||||
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:?}"
|
||||
)
|
||||
});
|
||||
fn test_home() -> ModuleId {
|
||||
ModuleIds::default().get_or_insert(&"Test".into())
|
||||
}
|
||||
|
||||
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 var = var_store.fresh();
|
||||
let qualified_module_ids = PackageModuleIds::default();
|
||||
let home = test_home();
|
||||
|
||||
let mut scope = Scope::new(
|
||||
home,
|
||||
|
@ -38,8 +61,8 @@ impl CanExpr {
|
|||
|
||||
let dep_idents = IdentIds::exposed_builtins(0);
|
||||
let mut env = Env::new(
|
||||
arena,
|
||||
expr_str,
|
||||
&self.arena(),
|
||||
input,
|
||||
home,
|
||||
Path::new("Test.roc"),
|
||||
&dep_idents,
|
||||
|
@ -95,7 +118,7 @@ impl CanExpr {
|
|||
}
|
||||
}
|
||||
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 {
|
||||
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 {
|
||||
self.arena
|
||||
}
|
||||
|
||||
pub fn arena(&self) -> &Bump {
|
||||
&self.arena
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue