Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,7 +1,7 @@

[package]
name = "cirru_parser"
version = "0.2.4"
version = "0.2.5"
authors = ["jiyinyiyong <jiyinyiyong@gmail.com>"]
edition = "2024"
license = "MIT"
Expand Down
18 changes: 15 additions & 3 deletions src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ fn get_node_kind(cursor: &Cirru) -> WriterNode {
}
}

fn should_insist_nested_head(ys: &[Cirru], idx: usize, prev_kind: WriterNode) -> bool {
if prev_kind == WriterNode::BoxedExpr || prev_kind == WriterNode::Expr {
return true;
}

idx > 1 && matches!(ys.first(), Some(Cirru::List(head)) if head.len() > 1)
}

fn generate_tree(
xs: &[Cirru],
insist_head: bool,
Expand All @@ -196,7 +204,6 @@ fn generate_tree(
for (idx, cursor) in xs.iter().enumerate() {
let kind = get_node_kind(cursor);
let next_level = level + 1;
let child_insist_head = (prev_kind == WriterNode::BoxedExpr) || (prev_kind == WriterNode::Expr);
let at_tail = idx != 0 && !in_tail && prev_kind == WriterNode::Leaf && idx == xs.len() - 1;

// println!("\nloop {:?} {:?}", prev_kind, kind);
Expand All @@ -206,6 +213,7 @@ fn generate_tree(
let child: String = match cursor {
Cirru::Leaf(s) => generate_leaf(s),
Cirru::List(ys) => {
let child_insist_head = should_insist_nested_head(ys, idx, prev_kind);
if at_tail {
if ys.is_empty() {
String::from("$")
Expand Down Expand Up @@ -234,8 +242,12 @@ fn generate_tree(
generate_empty_expr() // special since empty expr is treated as leaf
}
} else if kind == WriterNode::SimpleExpr {
if prev_kind == WriterNode::Leaf {
if prev_kind == WriterNode::Leaf && (idx == 1 || level > base_level || xs.len().saturating_sub(idx) <= 2) {
generate_inline_expr(ys)
} else if prev_kind == WriterNode::Leaf {
let mut ret = render_newline(next_level);
ret.push_str(&generate_tree(ys, child_insist_head, options, next_level, false)?);
ret
} else if options.use_inline && prev_kind == WriterNode::SimpleExpr {
let mut ret = String::from(" ");
ret.push_str(&generate_inline_expr(ys));
Expand Down Expand Up @@ -273,7 +285,7 @@ fn generate_tree(

let chunk = if at_tail
|| (prev_kind == WriterNode::Leaf && kind == WriterNode::Leaf)
|| (prev_kind == WriterNode::Leaf && kind == WriterNode::SimpleExpr)
|| (prev_kind == WriterNode::Leaf && kind == WriterNode::SimpleExpr && !child.starts_with('\n'))
|| prev_kind == WriterNode::SimpleExpr && kind == WriterNode::Leaf
{
let mut ret = String::from(" ");
Expand Down
6 changes: 6 additions & 0 deletions tests/cirru/match.cirru
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

match x
:dyn 1
(:dyn x) 2
(:dyn x y) 3
(:dyn x y z) 4
10 changes: 10 additions & 0 deletions tests/data/match.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
[
"match",
"x",
[":dyn", "1"],
[[":dyn", "x"], "2"],
[[":dyn", "x", "y"], "3"],
[[":dyn", "x", "y", "z"], "4"]
]
]
1 change: 1 addition & 0 deletions tests/parser_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ mod json_test {
"indent-twice",
"indent",
"let",
"match",
"line",
"paren-indent",
"paren-indent2", // same result as parent-indent
Expand Down
5 changes: 5 additions & 0 deletions tests/writer_cirru/let.cirru
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

let
a 1
b 2
+ a b
6 changes: 6 additions & 0 deletions tests/writer_cirru/match.cirru
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

match x
:dyn 1
(:dyn x) 2
(:dyn x y) 3
(:dyn x y z) 4
10 changes: 10 additions & 0 deletions tests/writer_data/let.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
[
"let",
[
["a", "1"],
["b", "2"]
],
["+", "a", "b"]
]
]
10 changes: 10 additions & 0 deletions tests/writer_data/match.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
[
"match",
"x",
[":dyn", "1"],
[[":dyn", "x"], "2"],
[[":dyn", "x", "y"], "3"],
[[":dyn", "x", "y", "z"], "4"]
]
]
49 changes: 49 additions & 0 deletions tests/writer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ mod json_write_test {
"html",
"indent",
"inline-let",
"let",
"match",
// "inline-mode",
"inline-simple",
"line",
Expand Down Expand Up @@ -178,6 +180,53 @@ fn test_writer_options_from_bool() -> Result<(), String> {
Ok(())
}

#[test]
fn format_let_with_nested_second_element() -> Result<(), String> {
use cirru_parser::{Cirru, CirruWriterOptions, format};

let xs = vec![Cirru::List(vec![
Cirru::leaf("let"),
Cirru::List(vec![
Cirru::List(vec![Cirru::leaf("a"), Cirru::leaf("1")]),
Cirru::List(vec![Cirru::leaf("b"), Cirru::leaf("2")]),
]),
Cirru::List(vec![Cirru::leaf("+"), Cirru::leaf("a"), Cirru::leaf("b")]),
])];

let rendered = format(&xs, CirruWriterOptions::from(false))?;

assert_eq!("\nlet\n a 1\n b 2\n + a b\n", rendered);
Ok(())
}

#[test]
fn format_match_without_bending_later_clauses() -> Result<(), String> {
use cirru_parser::{Cirru, CirruWriterOptions, format};

let xs = vec![Cirru::List(vec![
Cirru::leaf("match"),
Cirru::leaf("x"),
Cirru::List(vec![Cirru::leaf(":dyn"), Cirru::leaf("1")]),
Cirru::List(vec![Cirru::List(vec![Cirru::leaf(":dyn"), Cirru::leaf("x")]), Cirru::leaf("2")]),
Cirru::List(vec![
Cirru::List(vec![Cirru::leaf(":dyn"), Cirru::leaf("x"), Cirru::leaf("y")]),
Cirru::leaf("3"),
]),
Cirru::List(vec![
Cirru::List(vec![Cirru::leaf(":dyn"), Cirru::leaf("x"), Cirru::leaf("y"), Cirru::leaf("z")]),
Cirru::leaf("4"),
]),
])];

let rendered = format(&xs, CirruWriterOptions::from(false))?;

assert_eq!(
"\nmatch x\n :dyn 1\n (:dyn x) 2\n (:dyn x y) 3\n (:dyn x y z) 4\n",
rendered
);
Ok(())
}

#[cfg(feature = "serde-json")]
#[test]
fn test_dollar_sign_spacing() -> Result<(), String> {
Expand Down
Loading