Skip to content

Commit

Permalink
fix special case of layout empty expr; tag 0.1.25
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Aug 2, 2023
1 parent f7906d6 commit e458a03
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cirru_parser"
version = "0.1.24"
version = "0.1.25"
authors = ["jiyinyiyong <[email protected]>"]
edition = "2021"
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defn fib (n)
"#,
);

let large_demo = "/Users/chen/repo/calcit-lang/editor/compact.cirru";
let large_demo = "/Users/chenyong/repo/calcit-lang/editor/compact.cirru";
// let large_demo = "/Users/chen/repo/calcit-lang/respo-calcit-workflow/js-out/program-ir.cirru";
// let large_demo = "/Users/chen/repo/calcit-lang/calcit_runner.rs/js-out/program-ir.cirru";
let content = fs::read_to_string(large_demo).unwrap();
Expand Down
38 changes: 38 additions & 0 deletions examples/list_match.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#[cfg(feature = "use-serde")]
use std::{fs, io};

#[cfg(feature = "use-serde")]
fn main() -> Result<(), io::Error> {
use cirru_parser::{format, Cirru, CirruWriterOptions};
// use std::collections::hash_map::DefaultHasher;
use cirru_parser::from_json_str;

let files = vec!["list-match"];

for file in files {
println!("testing file: {}", file);
let json_str = fs::read_to_string(format!("./tests/writer_data/{}.json", file))?;
let cirru_str = fs::read_to_string(format!("./tests/writer_cirru/{}.cirru", file))?;

let writer_options = CirruWriterOptions { use_inline: false };
match from_json_str(&json_str) {
Ok(tree) => {
if let Cirru::List(xs) = tree {
assert_eq!(cirru_str, format(&xs, writer_options).unwrap());
} else {
panic!("unexpected leaf here")
}
}
Err(e) => {
println!("{:?}", e);
panic!("failed to load edn data from json");
}
}
}
Ok(())
}

#[cfg(not(feature = "use-serde"))]
fn main() {
println!("this example requires feature `use-serde`");
}
4 changes: 2 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn build_exprs(tokens: &[CirruLexItem]) -> Result<Vec<Cirru>, String> {
loop {
let chunk = pull_token();

if chunk == None {
if chunk.is_none() {
return Ok(acc);
}
match chunk.unwrap() {
Expand All @@ -67,7 +67,7 @@ fn build_exprs(tokens: &[CirruLexItem]) -> Result<Vec<Cirru>, String> {
let mut pointer_stack: Vec<Vec<Cirru>> = Vec::with_capacity(16);
loop {
let cursor = pull_token();
if cursor == None {
if cursor.is_none() {
return Err(String::from("unexpected end of file"));
}
match cursor.unwrap() {
Expand Down
26 changes: 11 additions & 15 deletions src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ fn generate_tree(
let at_tail = idx != 0 && !in_tail && prev_kind == WriterNode::Leaf && idx == xs.len() - 1;

// println!("\nloop {:?} {:?}", prev_kind, kind);
// println!("cursor {:?}", cursor);
// println!("cursor {:?} {} {}", cursor, idx, insist_head);
// println!("{:?}", result);

let child: String = match cursor {
Expand All @@ -185,7 +185,13 @@ fn generate_tree(
} else if idx == 0 && insist_head {
generate_inline_expr(ys)
} else if kind == WriterNode::Leaf {
generate_empty_expr() // special since empty expr is treated as leaf
if idx == 0 {
let mut ret = render_newline(level);
ret.push_str(&generate_empty_expr());
ret
} else {
generate_empty_expr() // special since empty expr is treated as leaf
}
} else if kind == WriterNode::SimpleExpr {
if prev_kind == WriterNode::Leaf {
generate_inline_expr(ys)
Expand All @@ -195,13 +201,7 @@ fn generate_tree(
ret
} else {
let mut ret = render_newline(next_level);
ret.push_str(&generate_tree(
ys,
child_insist_head,
options,
next_level,
false,
)?);
ret.push_str(&generate_tree(ys, child_insist_head, options, next_level, false)?);
ret
}
} else if kind == WriterNode::Expr {
Expand All @@ -215,10 +215,7 @@ fn generate_tree(
}
} else if kind == WriterNode::BoxedExpr {
let content = generate_tree(ys, child_insist_head, options, next_level, false)?;
if prev_kind == WriterNode::Nil
|| prev_kind == WriterNode::Leaf
|| prev_kind == WriterNode::SimpleExpr
{
if prev_kind == WriterNode::Nil || prev_kind == WriterNode::Leaf || prev_kind == WriterNode::SimpleExpr {
content
} else {
let mut ret = render_newline(next_level);
Expand All @@ -231,8 +228,7 @@ fn generate_tree(
}
};

let bended = kind == WriterNode::Leaf
&& (prev_kind == WriterNode::BoxedExpr || prev_kind == WriterNode::Expr);
let bended = kind == WriterNode::Leaf && (prev_kind == WriterNode::BoxedExpr || prev_kind == WriterNode::Expr);

let chunk = if at_tail
|| (prev_kind == WriterNode::Leaf && kind == WriterNode::Leaf)
Expand Down
6 changes: 6 additions & 0 deletions tests/cirru/list-match.cirru
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

list-match (a b)
() $ c d
(e f)
g h
i j k
12 changes: 12 additions & 0 deletions tests/data/list-match.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
[
"list-match",
["a", "b"],
[[], ["c", "d"]],
[
["e", "f"],
["g", "h"],
["i", "j", "k"]
]
]
]
1 change: 1 addition & 0 deletions tests/parser_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ mod json_test {
"quote",
"spaces",
"unfolding",
"list-match",
];
for file in files {
println!("testing file: {}", file);
Expand Down
6 changes: 6 additions & 0 deletions tests/writer_cirru/list-match.cirru
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

list-match (a b)
() $ c d
(e f)
g h
i j k
12 changes: 12 additions & 0 deletions tests/writer_data/list-match.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
[
"list-match",
["a", "b"],
[[], ["c", "d"]],
[
["e", "f"],
["g", "h"],
["i", "j", "k"]
]
]
]
1 change: 1 addition & 0 deletions tests/writer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ mod json_write_test {
"quote",
"spaces",
"unfolding",
"list-match",
];
for file in files {
println!("testing file: {}", file);
Expand Down

0 comments on commit e458a03

Please sign in to comment.