expr working, debugging def

This commit is contained in:
Anton-4 2021-10-02 19:47:46 +02:00
parent bc50280c0e
commit c6066cc629
8 changed files with 306 additions and 277 deletions

View file

@ -19,7 +19,7 @@ pub fn new_comma_mn(expr_id: ExprId, parent_id_opt: Option<MarkNodeId>) -> Marku
MarkupNode::Text {
content: nodes::COMMA.to_owned(),
ast_node_id: ASTNodeId::AExprId(expr_id),
syn_high_style: HighlightStyle::Blank,
syn_high_style: HighlightStyle::Comma,
attributes: Attributes::default(),
parent_id_opt,
newlines_at_end: 0,

View file

@ -6,6 +6,7 @@ use crate::colors::{self, from_hsb, RgbaTup};
#[derive(Hash, Eq, PartialEq, Copy, Clone, Debug, Deserialize, Serialize)]
pub enum HighlightStyle {
Operator, // =+-<>...
Comma,
String,
FunctionName,
Type,
@ -25,6 +26,7 @@ pub fn default_highlight_map() -> HashMap<HighlightStyle, RgbaTup> {
let mut highlight_map = HashMap::new();
[
(Operator, colors::WHITE),
(Comma, from_hsb(258, 50, 90)),
(String, from_hsb(346, 65, 97)),
(FunctionName, colors::WHITE),
(Type, colors::WHITE),

View file

@ -1,12 +1,119 @@
use crate::html::ToHtml;
use roc_ast::{ast_error::ASTResult, lang::{self, core::def::def_to_def2::def_to_def2}, mem_pool::pool::Pool};
use roc_code_markup::{markup::nodes::def2_to_markup, slow_pool::SlowPool};
use roc_module::symbol::{IdentIds, Interns, ModuleId, ModuleIds};
use roc_region::all::Region;
use bumpalo::{Bump, collections::String as BumpString};
use roc_types::subs::VarStore;
struct EqualSign;
use crate::html::mark_node_to_html;
impl<'a> ToHtml<'a> for EqualSign {
fn css_class(&self) -> Option<&'a str> {
Some("operator")
}
fn html_body(&self, buf: &mut bumpalo::collections::String<'a>) {
buf.push_str(" = ")
// html is written to buf
pub fn defs_to_html<'a>(buf: &mut BumpString<'a>, defs: Vec<roc_parse::ast::Def<'a>>, env_module_id: ModuleId, env_module_ids: &'a ModuleIds, interns: &Interns) {
let mut env_pool = Pool::with_capacity(1024);
let mut env_arena = Bump::new();
let mut var_store = VarStore::default();
let dep_idents = IdentIds::exposed_builtins(8);
let exposed_ident_ids = IdentIds::default();
let def_arena = Bump::new();
let mut env = lang::env::Env::new(
env_module_id,
&mut env_arena,
&mut env_pool,
&mut var_store,
dep_idents,
env_module_ids,
exposed_ident_ids,
);
let mut scope = lang::scope::Scope::new(env.home, env.pool, env.var_store);
let region = Region::new(0, 0, 0, 0);
for def in defs.iter() {
// TODO remove unwrap
write_def_to_bump_str_html(
&def_arena,
&mut env,
&mut scope,
region,
&def,
interns,
buf
).unwrap();
}
}
// html is written to buf
pub fn def_to_html<'a>(buf: &mut BumpString<'a>, def: roc_parse::ast::Def<'a>, env_module_id: ModuleId, env_module_ids: &'a ModuleIds, interns: &Interns) {
let mut env_pool = Pool::with_capacity(1024);
let mut env_arena = Bump::new();
let mut var_store = VarStore::default();
let dep_idents = IdentIds::exposed_builtins(8);
let exposed_ident_ids = IdentIds::default();
let def_arena = Bump::new();
let mut env = lang::env::Env::new(
env_module_id,
&mut env_arena,
&mut env_pool,
&mut var_store,
dep_idents,
env_module_ids,
exposed_ident_ids,
);
let mut scope = lang::scope::Scope::new(env.home, env.pool, env.var_store);
let region = Region::new(0, 0, 0, 0);
// TODO remove unwrap
write_def_to_bump_str_html(
&def_arena,
&mut env,
&mut scope,
region,
&def,
interns,
buf
).unwrap();
}
fn write_def_to_bump_str_html<'a, 'b>(
arena: &'a Bump,
env: &mut lang::env::Env<'a>,
scope: &mut lang::scope::Scope,
region: Region,
def: &'a roc_parse::ast::Def<'a>,
interns: &Interns,
buf: &mut BumpString<'b>
) -> ASTResult<()> {
let def2 = def_to_def2(arena, env, scope, def, region);
let mut def2_pool = Pool::with_capacity(1024);
let def2_id = def2_pool.add(def2);
let mut mark_node_pool = SlowPool::default();
let def2_markup_id = def2_to_markup(
env,
&def2_pool.get(def2_id),
def2_id,
&mut mark_node_pool,
interns,
)?;
let def2_markup_node = mark_node_pool.get(def2_markup_id);
mark_node_to_html(def2_markup_node, &mark_node_pool, buf);
Ok(())
}

View file

@ -1,173 +1,53 @@
use crate::html::ToHtml;
use crate::html::{mark_node_to_html};
use roc_ast::{ast_error::ASTResult, lang::{self, core::expr::expr_to_expr2::expr_to_expr2}, 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_module::symbol::{IdentIds, Interns, ModuleId, ModuleIds};
use roc_parse::ast::{Expr};
use roc_region::all::{Region};
use roc_code_markup::{markup::nodes::{MarkupNode}};
use bumpalo::{Bump, collections::String as BumpString};
use roc_types::subs::VarStore;
impl<'a> ToHtml<'a> for Expr<'a> {
fn css_class(&self) -> Option<&'a str> {
match self {
Expr::Float(_) | Expr::Num(_) | Expr::NonBase10Int { .. } => Some("num"),
Expr::Str(_) => Some("str"),
// Expr::Access(_, _) => {}
// Expr::AccessorFunction(_) => {}
// Expr::List { .. } => {}
// Expr::RecordUpdate { .. } => {}
// Expr::Record { .. } => {}
Expr::Var { .. } => None,
// Expr::Underscore(_) => {}
// Expr::GlobalTag(_) => {}
// Expr::PrivateTag(_) => {}
Expr::Closure(_, _) => None,
// html is written to buf
pub fn expr_to_html<'a>(buf: &mut BumpString<'a>, expr: Expr<'a>, env_module_id: ModuleId, env_module_ids: &'a ModuleIds, interns: &Interns) {
// Expr::Defs(_, _) => {}
// Expr::Backpassing(_, _, _) => {}
// Expr::Expect(_, _) => {}
// Expr::Apply(_, _, _) => {}
// Expr::BinOps(_, _) => {}
// Expr::UnaryOp(_, _) => {}
// Expr::If(_, _) => {}
// Expr::When(_, _) => {}
Expr::SpaceBefore(_, _) => None,
Expr::SpaceAfter(_, _) => None,
// Expr::ParensAround(_) => {}
// Expr::MalformedIdent(_, _) => {}
// Expr::MalformedClosure => {}
// Expr::PrecedenceConflict(_) => {}
_ => None,
}
}
fn html_body(&self, buf: &mut bumpalo::collections::String<'a>) {
match self {
Expr::Float(str) => {
buf.push_str(str);
}
Expr::Num(str) => {
buf.push_str(str);
}
Expr::NonBase10Int { string, .. } => {
buf.push_str(string);
}
Expr::Str(str_literal) => match str_literal {
StrLiteral::PlainLine(str) => {
buf.push('"');
buf.push_str(str);
buf.push('"');
}
StrLiteral::Line(line) => {
panic!("TODO str segments");
}
StrLiteral::Block(str) => {
panic!("TODO str segments");
}
},
// Expr::Access(_, _) => {}
// Expr::AccessorFunction(_) => {}
// Expr::List { .. } => {}
// Expr::RecordUpdate { .. } => {}
// Expr::Record { .. } => {}
Expr::Var { ident, module_name } => {
if !module_name.is_empty() {
buf.push_str(module_name);
buf.push('.');
}
buf.push_str(ident);
}
// Expr::Underscore(_) => {}
// Expr::GlobalTag(_) => {}
// Expr::PrivateTag(_) => {}
Expr::Closure(patterns, loc_sub_expr) => {
ClosureDash.html(buf);
let mut env_pool = Pool::with_capacity(1024);
let mut env_arena = Bump::new();
let mut patterns_iter = patterns.iter().peekable();
let mut var_store = VarStore::default();
let dep_idents = IdentIds::exposed_builtins(8);
let exposed_ident_ids = IdentIds::default();
while let Some(pattern) = patterns_iter.next() {
pattern.value.html(buf);
if let Some(_) = patterns_iter.peek() {
ParamComma.html(buf);
}
let mut env = lang::env::Env::new(
env_module_id,
&mut env_arena,
&mut env_pool,
&mut var_store,
dep_idents,
env_module_ids,
exposed_ident_ids,
);
let mut scope = lang::scope::Scope::new(env.home, env.pool, env.var_store);
let region = Region::new(0, 0, 0, 0);
// TODO remove unwrap
write_expr_to_bump_str_html(
&mut env,
&mut scope,
region,
&expr,
interns,
buf
).unwrap();
}
ClosureArrow.html(buf);
loc_sub_expr.html(buf);
}
Expr::Defs(defs, sub_expr) => {
for def_loc in defs.iter() {
unimplemented!();
//def_loc.html(buf);
}
sub_expr.html(buf)
}
// Expr::Backpassing(_, _, _) => {}
// Expr::Expect(_, _) => {}
// Expr::Apply(_, _, _) => {}
// Expr::BinOps(_, _) => {}
// Expr::UnaryOp(_, _) => {}
// Expr::If(_, _) => {}
// Expr::When(_, _) => {}
Expr::SpaceBefore(sub_expr, spaces) => {
for space in spaces.iter() {
space.html(buf);
}
sub_expr.html(buf);
}
Expr::SpaceAfter(sub_expr, spaces) => {
sub_expr.html(buf);
for space in spaces.iter() {
space.html(buf);
}
}
// Expr::ParensAround(_) => {}
// Expr::MalformedIdent(_, _) => {}
// Expr::MalformedClosure => {}
// Expr::PrecedenceConflict(_) => {}
_ => {}
}
}
}
struct ClosureDash;
impl<'a> ToHtml<'a> for ClosureDash {
fn css_class(&self) -> Option<&'a str> {
Some("closure-dash")
}
fn html_body(&self, buf: &mut bumpalo::collections::String<'a>) {
buf.push('\\')
}
}
struct ClosureArrow;
impl<'a> ToHtml<'a> for ClosureArrow {
fn css_class(&self) -> Option<&'a str> {
Some("closure-arrow")
}
fn html_body(&self, buf: &mut bumpalo::collections::String<'a>) {
buf.push_str(" ->")
}
}
struct ParamComma;
impl<'a> ToHtml<'a> for ParamComma {
fn css_class(&self) -> Option<&'a str> {
Some("param-comma")
}
fn html_body(&self, buf: &mut bumpalo::collections::String<'a>) {
buf.push_str(", ")
}
}
pub fn write_expr_to_bump_str_html<'a, 'b>(
fn write_expr_to_bump_str_html<'a, 'b>(
env: &mut lang::env::Env<'a>,
scope: &mut lang::scope::Scope,
region: Region,
expr: &'a Expr,
interns: &Interns,
bump_str: &mut bumpalo::collections::String<'b>
buf: &mut BumpString<'b>
) -> ASTResult<()> {
let (expr2, _) = expr_to_expr2(env, scope, expr, region);
@ -186,7 +66,7 @@ pub fn write_expr_to_bump_str_html<'a, 'b>(
let expr2_markup_node = mark_node_pool.get(expr2_markup_id);
expr2_markup_node.html(bump_str);
mark_node_to_html(expr2_markup_node, &mark_node_pool, buf);
Ok(())
}

View file

@ -48,26 +48,27 @@ where
// determine appropriate css class for MarkupNode
fn mark_node_html<'a>(mark_node: &MarkupNode, mark_node_pool: &SlowPool, buf: &mut bumpalo::collections::String<'a>) {
pub fn mark_node_to_html<'a>(mark_node: &MarkupNode, mark_node_pool: &SlowPool, buf: &mut BumpString<'a>) {
let additional_newlines:usize;
match mark_node {
MarkupNode::Nested { children_ids, newlines_at_end, .. } => {
for &child_id in children_ids {
mark_node_html(
mark_node_to_html(
mark_node_pool.get(child_id),
mark_node_pool,
buf
)
}
for _ in 0..*newlines_at_end {
buf.push('\n')
additional_newlines = *newlines_at_end;
}
}
MarkupNode::Text { syn_high_style, newlines_at_end, .. } => {
use HighlightStyle::*;
MarkupNode::Text { content, syn_high_style, newlines_at_end, .. } => {
use roc_code_markup::syntax_highlight::HighlightStyle::*;
let css_class = match syn_high_style {
Operator => "operator",
Comma => "comma",
String => "string",
FunctionName => "function_name",
Type => "type",
@ -80,27 +81,37 @@ fn mark_node_html<'a>(mark_node: &MarkupNode, mark_node_pool: &SlowPool, buf: &m
Provides => "provides",
Blank => "blank",
};
write_html_to_buf(content, css_class, buf);
additional_newlines = *newlines_at_end;
}
MarkupNode::Blank { syn_high_style, newlines_at_end, .. } => {
MarkupNode::Blank { newlines_at_end, .. } => {
let mut content_str = " ".to_string();
for _ in 0..*newlines_at_end {
content_str.push('\n');
}
write_html_to_buf(content_str, "blank", buf)
write_html_to_buf(&content_str, "blank", buf);
additional_newlines = *newlines_at_end;
}
}
for _ in 0..additional_newlines {
buf.push('\n')
}
}
fn write_html_to_buf<'a>(content: String, css_class: &'static str, buf: &mut BumpString<'a>) {
fn write_html_to_buf<'a>(content: &str, css_class: &'static str, buf: &mut BumpString<'a>) {
let opening_tag: String = ["<span class=\"syntax-", css_class, "\">"].concat();
buf.push_str(opening_tag.as_str());
buf.push_str(&content);
buf.push_str(content);
buf.push_str("</span>");
}

View file

@ -1,9 +1,8 @@
extern crate pulldown_cmark;
extern crate roc_load;
use bumpalo::{collections::String as BumpString, collections::Vec as BumpVec, Bump};
use expr::write_expr_to_bump_str_html;
use roc_ast::lang;
use roc_ast::mem_pool::pool::Pool;
use def::defs_to_html;
use expr::{expr_to_html};
use roc_builtins::std::StdLib;
use roc_can::builtins::builtin_defs_map;
use roc_can::scope::Scope;
@ -13,11 +12,9 @@ use roc_load::docs::{DocEntry, TypeAnnotation};
use roc_load::docs::{ModuleDocumentation, RecordField};
use roc_load::file::{LoadedModule, LoadingProblem};
use roc_module::symbol::{IdentIds, Interns, ModuleId, ModuleIds};
use roc_parse::ast::Expr;
use roc_parse::ident::{parse_ident, Ident};
use roc_parse::parser::{State, SyntaxError};
use roc_region::all::Region;
use roc_types::subs::VarStore;
use std::fs;
use std::path::{Path, PathBuf};
@ -130,42 +127,8 @@ pub fn generate_docs_html(filenames: Vec<PathBuf>, std_lib: StdLib, build_dir: &
println!("🎉 Docs generated in {}", build_dir.display());
}
// html is written to buf
fn expr_to_html<'a>(buf: &mut BumpString<'a>, expr: Expr<'a>, env_module_id: ModuleId, env_module_ids: &'a ModuleIds, interns: &Interns) {
let mut env_pool = Pool::with_capacity(1024);
let mut env_arena = Bump::new();
let mut var_store = VarStore::default();
let dep_idents = IdentIds::exposed_builtins(8);
let exposed_ident_ids = IdentIds::default();
let mut env = lang::env::Env::new(
env_module_id,
&mut env_arena,
&mut env_pool,
&mut var_store,
dep_idents,
env_module_ids,
exposed_ident_ids,
);
let mut scope = lang::scope::Scope::new(env.home, env.pool, env.var_store);
let region = Region::new(0, 0, 0, 0);
// TODO remove unwrap
write_expr_to_bump_str_html(
&mut env,
&mut scope,
region,
&expr,
interns,
buf
).unwrap();
}
// converts plain text code to highlighted html
pub fn syntax_highlight_code<'a>(
// converts plain-text code to highlighted html
pub fn syntax_highlight_expr<'a>(
arena: &'a Bump,
buf: &mut BumpString<'a>,
code_str: &'a str,
@ -184,11 +147,30 @@ pub fn syntax_highlight_code<'a>(
}
Err(fail) => Err(SyntaxError::Expr(fail)),
}
// roc_parse::test_helpers::parse_expr_with(&arena, trimmed_code_str).map(|expr| {
// expr.html(buf);
//
// buf.to_string()
// })
}
// converts plain-text code to highlighted html
pub fn syntax_highlight_top_level_defs<'a>(
arena: &'a Bump,
buf: &mut BumpString<'a>,
code_str: &'a str,
env_module_id: ModuleId,
env_module_ids: &'a ModuleIds,
interns: &Interns
) -> Result<String, SyntaxError<'a>> {
let trimmed_code_str = code_str.trim_end().trim();
match roc_parse::test_helpers::parse_defs_with(arena, trimmed_code_str) {
Ok(vec_loc_def) => {
let vec_def =
vec_loc_def.iter().map(|loc| loc.value).collect();
defs_to_html(buf, vec_def, env_module_id, env_module_ids, interns);
Ok(buf.to_string())
}
Err(err) => Err(err),
}
}
// TODO improve name, what is main content?
@ -997,7 +979,7 @@ fn markdown_to_html(
let code_block_arena = Bump::new();
let mut code_block_buf = BumpString::new_in(&code_block_arena);
match syntax_highlight_code(
match syntax_highlight_expr(
&code_block_arena,
&mut code_block_buf,
code_str,

View file

@ -7,7 +7,6 @@
--body-bg-color: #fdfdfd;
--border-color: #e9e9e9;
--faded-color: #4c4c4c;
--monospace-font;
--font-sans: -apple-system, BlinkMacSystemFont, Roboto, Helvetica, Arial, sans-serif;
--font-mono: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace;
--top-header-height: 67px;
@ -310,22 +309,25 @@ pre {
display: none !important;
}
.syntax-num {
color: #8080ff;
.syntax-number {
color: #60B7BF;
}
.syntax-string {
color:#F7577C;
}
.syntax-bracket {
color:#FF335F;
}
.syntax-closure-dash,
.syntax-closure-arrow,
.syntax-operator ,
.syntax-param-comma
.syntax-operator
{
color: #ff0000;
color: #ffffff;
}
.syntax-str {
color:#ffff00;
.syntax-comma {
color: #9573E6;
}
.syntax-comment {
color: #ff0000;
}

View file

@ -7,11 +7,11 @@ mod insert_doc_syntax_highlighting {
use roc_can::env::Env;
use roc_can::scope::Scope;
use roc_collections::all::MutMap;
use roc_docs::syntax_highlight_code;
use roc_docs::{syntax_highlight_expr, syntax_highlight_top_level_defs};
use roc_module::symbol::{IdentIds, Interns, ModuleIds};
use roc_types::subs::VarStore;
fn expect_html(code_str: &str, want: &str) {
fn expect_html(code_str: &str, want: &str, use_expr: bool) {
let code_block_arena = Bump::new();
@ -24,7 +24,24 @@ mod insert_doc_syntax_highlighting {
};
let mut code_block_buf = BumpString::new_in(&code_block_arena);
match syntax_highlight_code(
if use_expr {
match syntax_highlight_expr(
&code_block_arena,
&mut code_block_buf,
code_str,
mod_id,
&module_ids,
&interns
) {
Ok(highlighted_code_str) => {
assert_eq!(highlighted_code_str, want);
}
Err(syntax_error) => {
panic!("Unexpected parse failure when parsing this for rendering in docs:\n\n{}\n\nParse error was:\n\n{:?}\n\n", code_str, syntax_error)
}
};
} else {
match syntax_highlight_top_level_defs(
&code_block_arena,
&mut code_block_buf,
code_str,
@ -41,35 +58,63 @@ mod insert_doc_syntax_highlighting {
};
}
/*#[test]
fn simple_code_block() {
expect_html(
r#"
f = \x, y ->
b = 6
c
}
"string"
"#,
r#"
<div class="syntax-var">x</div> <div class="syntax-operator">:</div> <div class="syntax-type">Nat</div>
<div class="syntax-var">x</div> <div class="syntax-operator">=</div>
<div class="syntax-number">4</div>
fn expect_html_expr(code_str: &str, want: &str) {
expect_html(code_str, want, true)
}
<div class="syntax-number">2</div>
"#,
);
}*/
fn expect_html_def(code_str: &str, want: &str) {
expect_html(code_str, want, false)
}
#[test]
fn simple_code_block() {
expect_html(
r#"
2
"#,
r#"
<div class="syntax-number">2</div>
"#,
fn number_expr() {
expect_html_expr(
"2",
r#"<span class="syntax-number">2</span>"#,
);
}
#[test]
fn string_expr() {
expect_html_expr(
r#""abc""#,
r#"<span class="syntax-string">"abc"</span>"#,
);
}
#[test]
fn empty_list_expr() {
expect_html_expr(
r#"[]"#,
r#"<span class="syntax-bracket">[ </span><span class="syntax-bracket"> ]</span>"#,
);
}
#[test]
fn single_elt_list_expr() {
expect_html_expr(
r#"[ 0 ]"#,
r#"<span class="syntax-bracket">[ </span><span class="syntax-number">0</span><span class="syntax-bracket"> ]</span>"#,
);
}
#[test]
fn multi_elt_list_expr() {
expect_html_expr(
r#"[ "hello", "WoRlD" ]"#,
r#"<span class="syntax-bracket">[ </span><span class="syntax-string">"hello"</span><span class="syntax-comma">, </span><span class="syntax-string">"WoRlD"</span><span class="syntax-bracket"> ]</span>"#,
);
}
// TODO test record, nested records
#[test]
fn function_def() {
expect_html_def(
r#"main = "Hello, World!""#,
r#"TODO"#,
);
}
}