From 28cf11a1d90d9fd6ca620de9ea906e368c7d13bc Mon Sep 17 00:00:00 2001 From: Anton-4 <17049058+Anton-4@users.noreply.github.com> Date: Wed, 29 Sep 2021 20:05:03 +0200 Subject: [PATCH] started on docs to markup to html --- Cargo.lock | 2 ++ ast/src/canonicalization/canonicalize.rs | 8 ++--- ast/src/lang/core/def/def.rs | 6 ++-- ast/src/lang/core/def/def_to_def2.rs | 8 ++--- ast/src/lang/core/expr/expr_to_expr2.rs | 24 ++++++------- ast/src/lang/core/expr/mod.rs | 2 +- ast/src/lang/core/str.rs | 4 +-- code_markup/src/markup/nodes.rs | 6 ++-- docs/Cargo.toml | 2 ++ docs/src/def.rs | 43 +++--------------------- docs/src/expr.rs | 40 ++++++++++++++++++++-- 11 files changed, 75 insertions(+), 70 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3bc8f34f5d..e3520f0753 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3582,8 +3582,10 @@ dependencies = [ "maplit", "pretty_assertions 0.5.1", "pulldown-cmark", + "roc_ast", "roc_builtins", "roc_can", + "roc_code_markup", "roc_collections", "roc_fmt", "roc_load", diff --git a/ast/src/canonicalization/canonicalize.rs b/ast/src/canonicalization/canonicalize.rs index 211e22cf97..f6a8d00b5c 100644 --- a/ast/src/canonicalization/canonicalize.rs +++ b/ast/src/canonicalization/canonicalize.rs @@ -9,7 +9,7 @@ use crate::{ def::def::References, expr::{ expr2::{Expr2, ExprId, WhenBranch}, - expr_to_expr2::to_expr2, + expr_to_expr2::expr_to_expr2, output::Output, record_field::RecordField, }, @@ -147,7 +147,7 @@ fn canonicalize_field<'a>( // Both a label and a value, e.g. `{ name: "blah" }` RequiredValue(label, _, loc_expr) => { let field_var = env.var_store.fresh(); - let (loc_can_expr, output) = to_expr2(env, scope, &loc_expr.value, loc_expr.region); + let (loc_can_expr, output) = expr_to_expr2(env, scope, &loc_expr.value, loc_expr.region); Ok(CanonicalField::LabelAndValue { label: label.value, @@ -211,7 +211,7 @@ pub(crate) fn canonicalize_when_branch<'a>( } let (value, mut branch_output) = - to_expr2(env, &mut scope, &branch.value.value, branch.value.region); + expr_to_expr2(env, &mut scope, &branch.value.value, branch.value.region); let value_id = env.pool.add(value); env.set_region(value_id, branch.value.region); @@ -219,7 +219,7 @@ pub(crate) fn canonicalize_when_branch<'a>( None => None, Some(loc_expr) => { let (can_guard, guard_branch_output) = - to_expr2(env, &mut scope, &loc_expr.value, loc_expr.region); + expr_to_expr2(env, &mut scope, &loc_expr.value, loc_expr.region); let expr_id = env.pool.add(can_guard); env.set_region(expr_id, loc_expr.region); diff --git a/ast/src/lang/core/def/def.rs b/ast/src/lang/core/def/def.rs index c8654b3d73..9584175493 100644 --- a/ast/src/lang/core/def/def.rs +++ b/ast/src/lang/core/def/def.rs @@ -27,7 +27,7 @@ use ven_graph::{strongly_connected_components, topological_sort_into_groups}; use crate::{ lang::{ core::{ - expr::{expr2::Expr2, expr_to_expr2::to_expr2, output::Output}, + expr::{expr2::Expr2, expr_to_expr2::expr_to_expr2, output::Output}, fun_def::FunctionDef, pattern::{self, symbols_from_pattern, to_pattern_id, Pattern2, PatternId}, types::{to_annotation2, Alias, Annotation2, Signature, Type2, TypeId}, @@ -472,7 +472,7 @@ fn canonicalize_pending_def<'a>( }; let (loc_can_expr, can_output) = - to_expr2(env, scope, &loc_expr.value, loc_expr.region); + expr_to_expr2(env, scope, &loc_expr.value, loc_expr.region); output.references.union_mut(can_output.references.clone()); @@ -639,7 +639,7 @@ fn canonicalize_pending_def<'a>( env.closure_name_symbol = Some(*defined_symbol); }; - let (loc_can_expr, can_output) = to_expr2(env, scope, &loc_expr.value, loc_expr.region); + let (loc_can_expr, can_output) = expr_to_expr2(env, scope, &loc_expr.value, loc_expr.region); output.references.union_mut(can_output.references.clone()); diff --git a/ast/src/lang/core/def/def_to_def2.rs b/ast/src/lang/core/def/def_to_def2.rs index 05cf2747a6..ff659b4310 100644 --- a/ast/src/lang/core/def/def_to_def2.rs +++ b/ast/src/lang/core/def/def_to_def2.rs @@ -20,11 +20,11 @@ pub fn defs_to_defs2<'a>( ) -> Vec { parsed_defs .iter() - .map(|loc| to_def2_from_def(arena, env, scope, &loc.value, region)) + .map(|loc| def_to_def2(arena, env, scope, &loc.value, region)) .collect() } -pub fn to_def2_from_def<'a>( +pub fn def_to_def2<'a>( arena: &'a Bump, env: &mut Env<'a>, scope: &mut Scope, @@ -34,8 +34,8 @@ pub fn to_def2_from_def<'a>( use roc_parse::ast::Def::*; match parsed_def { - SpaceBefore(inner_def, _) => to_def2_from_def(arena, env, scope, inner_def, region), - SpaceAfter(inner_def, _) => to_def2_from_def(arena, env, scope, inner_def, region), + SpaceBefore(inner_def, _) => def_to_def2(arena, env, scope, inner_def, region), + SpaceAfter(inner_def, _) => def_to_def2(arena, env, scope, inner_def, region), Body(&loc_pattern, &loc_expr) => { // TODO loc_pattern use identifier let expr2 = loc_expr_to_expr2(arena, loc_expr, env, scope, region).0; diff --git a/ast/src/lang/core/expr/expr_to_expr2.rs b/ast/src/lang/core/expr/expr_to_expr2.rs index febad6b13a..9e96fac7e5 100644 --- a/ast/src/lang/core/expr/expr_to_expr2.rs +++ b/ast/src/lang/core/expr/expr_to_expr2.rs @@ -36,12 +36,12 @@ pub fn loc_expr_to_expr2<'a>( ) -> (Expr2, Output) { let desugared_loc_expr = desugar_expr(arena, arena.alloc(loc_expr)); - to_expr2(env, scope, arena.alloc(desugared_loc_expr.value), region) + expr_to_expr2(env, scope, arena.alloc(desugared_loc_expr.value), region) } const ZERO: Region = Region::zero(); -pub fn to_expr2<'a>( +pub fn expr_to_expr2<'a>( env: &mut Env<'a>, scope: &mut Scope, parse_expr: &'a roc_parse::ast::Expr<'a>, @@ -139,7 +139,7 @@ pub fn to_expr2<'a>( let elems: PoolVec = PoolVec::with_capacity(items.len() as u32, env.pool); for (node_id, item) in elems.iter_node_ids().zip(items.iter()) { - let (expr, sub_output) = to_expr2(env, scope, &item.value, item.region); + let (expr, sub_output) = expr_to_expr2(env, scope, &item.value, item.region); output_ref.union(sub_output); @@ -188,7 +188,7 @@ pub fn to_expr2<'a>( final_comments: _, } => { let (can_update, update_out) = - to_expr2(env, scope, &loc_update.value, loc_update.region); + expr_to_expr2(env, scope, &loc_update.value, loc_update.region); if let Expr2::Var(symbol) = &can_update { match canonicalize_fields(env, scope, fields) { @@ -309,10 +309,10 @@ pub fn to_expr2<'a>( let mut output = Output::default(); for (condition, then_branch) in branches.iter() { - let (cond, cond_output) = to_expr2(env, scope, &condition.value, condition.region); + let (cond, cond_output) = expr_to_expr2(env, scope, &condition.value, condition.region); let (then_expr, then_output) = - to_expr2(env, scope, &then_branch.value, then_branch.region); + expr_to_expr2(env, scope, &then_branch.value, then_branch.region); output.references.union_mut(cond_output.references); output.references.union_mut(then_output.references); @@ -321,7 +321,7 @@ pub fn to_expr2<'a>( } let (else_expr, else_output) = - to_expr2(env, scope, &final_else.value, final_else.region); + expr_to_expr2(env, scope, &final_else.value, final_else.region); output.references.union_mut(else_output.references); @@ -338,7 +338,7 @@ pub fn to_expr2<'a>( When(loc_cond, branches) => { // Infer the condition expression's type. let cond_var = env.var_store.fresh(); - let (can_cond, mut output) = to_expr2(env, scope, &loc_cond.value, loc_cond.region); + let (can_cond, mut output) = expr_to_expr2(env, scope, &loc_cond.value, loc_cond.region); // the condition can never be a tail-call output.tail_call = None; @@ -411,7 +411,7 @@ pub fn to_expr2<'a>( } let (body_expr, new_output) = - to_expr2(env, &mut scope, &loc_body_expr.value, loc_body_expr.region); + expr_to_expr2(env, &mut scope, &loc_body_expr.value, loc_body_expr.region); let mut captured_symbols: MutSet = new_output.references.lookups.iter().copied().collect(); @@ -501,7 +501,7 @@ pub fn to_expr2<'a>( let fn_region = loc_fn.region; // Canonicalize the function expression and its arguments - let (fn_expr, mut output) = to_expr2(env, scope, &loc_fn.value, fn_region); + let (fn_expr, mut output) = expr_to_expr2(env, scope, &loc_fn.value, fn_region); // The function's return type let args = PoolVec::with_capacity(loc_args.len() as u32, env.pool); @@ -592,7 +592,7 @@ pub fn to_expr2<'a>( // The def as a whole is a tail call iff its return expression is a tail call. // Use its output as a starting point because its tail_call already has the right answer! - let (ret_expr, mut output) = to_expr2(env, &mut scope, &loc_ret.value, loc_ret.region); + let (ret_expr, mut output) = expr_to_expr2(env, &mut scope, &loc_ret.value, loc_ret.region); output .introduced_variables @@ -704,7 +704,7 @@ pub fn to_expr_id<'a>( parse_expr: &'a roc_parse::ast::Expr<'a>, region: Region, ) -> (ExprId, Output) { - let (expr, output) = to_expr2(env, scope, parse_expr, region); + let (expr, output) = expr_to_expr2(env, scope, parse_expr, region); (env.add(expr, region), output) } diff --git a/ast/src/lang/core/expr/mod.rs b/ast/src/lang/core/expr/mod.rs index 32d768c4c9..bb27a4ad9b 100644 --- a/ast/src/lang/core/expr/mod.rs +++ b/ast/src/lang/core/expr/mod.rs @@ -1,6 +1,6 @@ pub mod expr2; pub mod expr2_to_string; -pub(crate) mod expr_to_expr2; +pub mod expr_to_expr2; mod introduced_vars; pub(crate) mod output; pub mod record_field; diff --git a/ast/src/lang/core/str.rs b/ast/src/lang/core/str.rs index 53b0a999cf..7a939aac33 100644 --- a/ast/src/lang/core/str.rs +++ b/ast/src/lang/core/str.rs @@ -3,7 +3,7 @@ use roc_parse::ast::StrLiteral; use crate::{ ast_error::{ASTResult, UnexpectedASTNode}, - lang::{core::expr::expr_to_expr2::to_expr2, env::Env, scope::Scope}, + lang::{core::expr::expr_to_expr2::expr_to_expr2, env::Env, scope::Scope}, mem_pool::{pool::Pool, pool_str::PoolStr, pool_vec::PoolVec}, }; @@ -94,7 +94,7 @@ fn flatten_str_lines<'a>( } let (loc_expr, new_output) = - to_expr2(env, scope, loc_expr.value, loc_expr.region); + expr_to_expr2(env, scope, loc_expr.value, loc_expr.region); output.union(new_output); diff --git a/code_markup/src/markup/nodes.rs b/code_markup/src/markup/nodes.rs index cea08f288b..385536f7c8 100644 --- a/code_markup/src/markup/nodes.rs +++ b/code_markup/src/markup/nodes.rs @@ -809,11 +809,11 @@ pub fn ast_to_mark_nodes<'a, 'b>( for &def_id in ast.def_ids.iter() { let def2 = env.pool.get(def_id); - let expr2_markup_id = def2_to_markup(arena, env, def2, def_id, mark_node_pool, interns)?; + let def2_markup_id = def2_to_markup(arena, env, def2, def_id, mark_node_pool, interns)?; - set_parent_for_all(expr2_markup_id, mark_node_pool); + set_parent_for_all(def2_markup_id, mark_node_pool); - all_mark_node_ids.push(expr2_markup_id); + all_mark_node_ids.push(def2_markup_id); } Ok(all_mark_node_ids) diff --git a/docs/Cargo.toml b/docs/Cargo.toml index c0f7416516..c311423548 100644 --- a/docs/Cargo.toml +++ b/docs/Cargo.toml @@ -9,9 +9,11 @@ edition = "2018" [dependencies] pulldown-cmark = { version = "0.8", default-features = false } +roc_ast = { path = "../ast" } roc_load = { path = "../compiler/load" } roc_builtins = { path = "../compiler/builtins" } roc_can = { path = "../compiler/can" } +roc_code_markup = { path = "../code_markup"} roc_fmt = { path = "../compiler/fmt" } roc_module = { path = "../compiler/module" } roc_region = { path = "../compiler/region" } diff --git a/docs/src/def.rs b/docs/src/def.rs index 6b9a0cee11..4660d06572 100644 --- a/docs/src/def.rs +++ b/docs/src/def.rs @@ -1,47 +1,12 @@ use crate::html::ToHtml; -use roc_parse::ast::Def; +use roc_code_markup::{markup::nodes::{MarkupNode}}; -impl<'a> ToHtml<'a> for Def<'a> { +impl<'a> ToHtml<'a> for MarkupNode { fn css_class(&self) -> Option<&'a str> { - match self { - // Def::Annotation(_, _) => {} - // Def::Alias { .. } => {} - Def::Body(_, _) => None, - // Def::AnnotatedBody { .. } => {} - // Def::Expect(_) => {} - Def::SpaceBefore(_, _) => None, - Def::SpaceAfter(_, _) => None, - // Def::NotYetImplemented(_) => {} - _ => None, - } + Some("operator") } - fn html_body(&self, buf: &mut bumpalo::collections::String<'a>) { - match self { - // Def::Annotation(_, _) => {} - // Def::Alias { .. } => {} - Def::Body(pattern, expr) => { - pattern.html(buf); - EqualSign.html(buf); - expr.html(buf); - } - // Def::AnnotatedBody { .. } => {} - // Def::Expect(_) => {} - Def::SpaceBefore(sub_def, spaces) => { - for space in spaces.iter() { - space.html(buf); - } - sub_def.html(buf); - } - Def::SpaceAfter(sub_def, spaces) => { - sub_def.html(buf); - for space in spaces.iter() { - space.html(buf); - } - } - // Def::NotYetImplemented(_) => {} - _ => {} - } + buf.push_str("MarkupNode") } } diff --git a/docs/src/expr.rs b/docs/src/expr.rs index bcb7832cb9..a37c70d834 100644 --- a/docs/src/expr.rs +++ b/docs/src/expr.rs @@ -1,6 +1,10 @@ use crate::html::ToHtml; -use roc_parse::ast::{CommentOrNewline, Expr, StrLiteral, StrSegment}; -use roc_region::all::Located; +use bumpalo::Bump; +use roc_ast::{ast_error::ASTResult, lang::{core::expr::expr_to_expr2::expr_to_expr2, env::Env, scope::Scope}, mem_pool::pool::Pool}; +use roc_code_markup::{markup::nodes::expr2_to_markup, slow_pool::SlowPool}; +use roc_module::symbol::Interns; +use roc_parse::ast::{Expr, StrLiteral}; +use roc_region::all::{Located, Region}; impl<'a> ToHtml<'a> for Expr<'a> { fn css_class(&self) -> Option<&'a str> { @@ -155,3 +159,35 @@ impl<'a> ToHtml<'a> for ParamComma { buf.push_str(", ") } } + +fn write_expr_to_bump_str_html<'a>( + arena: &mut Bump, + env: &mut Env<'a>, + scope: &mut Scope, + region: Region, + expr: &'a Expr, + interns: &Interns, + bump_str: &mut bumpalo::collections::String<'a> +) -> ASTResult<()> { + let (expr2, _) = expr_to_expr2(env, scope, expr, region); + + let mut expr2_pool = Pool::with_capacity(1024); + let expr2_id = expr2_pool.add(expr2); + + let mut mark_node_pool = SlowPool::default(); + + let expr2_markup_id = expr2_to_markup( + arena, + env, + &expr2, + expr2_id, + &mut mark_node_pool, + interns, + )?; + + let expr2_markup_node = mark_node_pool.get(expr2_markup_id); + + expr2_markup_node.html(bump_str); + + Ok(()) +}