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.0"
version = "0.2.3"
authors = ["jiyinyiyong <jiyinyiyong@gmail.com>"]
edition = "2024"
license = "MIT"
Expand Down
130 changes: 65 additions & 65 deletions examples/test_escape_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,77 @@
use cirru_parser::{parse, print_error};

fn main() {
println!("🔍 Testing escape_debug in error messages\n");
println!("{}\n", "═".repeat(70));
println!("🔍 Testing escape_debug in error messages\n");
println!("{}\n", "═".repeat(70));

// Test 1: Newline in string
println!("Test 1: Newline character in string");
let code1 = "defn test\n print \"hello\nworld\"";
println!("Code: {code1:?}\n");
if let Err(e) = parse(code1) {
print_error(&e, Some(code1));
}
println!("\n{}\n", "─".repeat(70));
// Test 1: Newline in string
println!("Test 1: Newline character in string");
let code1 = "defn test\n print \"hello\nworld\"";
println!("Code: {code1:?}\n");
if let Err(e) = parse(code1) {
print_error(&e, Some(code1));
}
println!("\n{}\n", "─".repeat(70));

// Test 2: Tab indentation error
println!("Test 2: Tab character causing indentation issue");
let code2 = "defn test\n\tabc"; // Tab character
println!("Code: {code2:?}\n");
if let Err(e) = parse(code2) {
print_error(&e, Some(code2));
}
println!("\n{}\n", "─".repeat(70));
// Test 2: Tab indentation error
println!("Test 2: Tab character causing indentation issue");
let code2 = "defn test\n\tabc"; // Tab character
println!("Code: {code2:?}\n");
if let Err(e) = parse(code2) {
print_error(&e, Some(code2));
}
println!("\n{}\n", "─".repeat(70));

// Test 3: Multiple spaces (odd indentation)
println!("Test 3: Odd number of spaces (visible with escape_debug)");
let code3 = "defn test\n abc\n def"; // 3 spaces
println!("Code: {code3:?}\n");
if let Err(e) = parse(code3) {
print_error(&e, Some(code3));
}
println!("\n{}\n", "─".repeat(70));
// Test 3: Multiple spaces (odd indentation)
println!("Test 3: Odd number of spaces (visible with escape_debug)");
let code3 = "defn test\n abc\n def"; // 3 spaces
println!("Code: {code3:?}\n");
if let Err(e) = parse(code3) {
print_error(&e, Some(code3));
}
println!("\n{}\n", "─".repeat(70));

// Test 4: Mixed whitespace
println!("Test 4: Carriage return in string");
let code4 = "print \"hello\rworld\"";
println!("Code: {code4:?}\n");
match parse(code4) {
Ok(_) => println!("✅ Parsed successfully (\\r is valid in identifier)"),
Err(e) => print_error(&e, Some(code4)),
}
println!("\n{}\n", "─".repeat(70));
// Test 4: Mixed whitespace
println!("Test 4: Carriage return in string");
let code4 = "print \"hello\rworld\"";
println!("Code: {code4:?}\n");
match parse(code4) {
Ok(_) => println!("✅ Parsed successfully (\\r is valid in identifier)"),
Err(e) => print_error(&e, Some(code4)),
}
println!("\n{}\n", "─".repeat(70));

// Test 5: Long line with special chars
println!("Test 5: Long line with special characters and error");
let code5 = "defn process-long-text (data)\n let result \"This is a very long string with \\t tabs and \\n newlines \\x error\"";
println!("Code: {code5:?}\n");
if let Err(e) = parse(code5) {
print_error(&e, Some(code5));
}
println!("\n{}\n", "─".repeat(70));
// Test 5: Long line with special chars
println!("Test 5: Long line with special characters and error");
let code5 = "defn process-long-text (data)\n let result \"This is a very long string with \\t tabs and \\n newlines \\x error\"";
println!("Code: {code5:?}\n");
if let Err(e) = parse(code5) {
print_error(&e, Some(code5));
}
println!("\n{}\n", "─".repeat(70));

// Test 6: Unicode with newline
println!("Test 6: Unicode escape with newline nearby");
let code6 = "print \"emoji \\u{1F600}\ntext\"";
println!("Code: {code6:?}\n");
if let Err(e) = parse(code6) {
print_error(&e, Some(code6));
}
println!("\n{}\n", "─".repeat(70));
// Test 6: Unicode with newline
println!("Test 6: Unicode escape with newline nearby");
let code6 = "print \"emoji \\u{1F600}\ntext\"";
println!("Code: {code6:?}\n");
if let Err(e) = parse(code6) {
print_error(&e, Some(code6));
}
println!("\n{}\n", "─".repeat(70));

// Test 7: Indentation with spaces visible
println!("Test 7: Clear space visualization");
let code7 = "defn main\n line1\n line2\n line3"; // 5 spaces on line3
println!("Code with explicit spaces: 'defn main\\n line1\\n line2\\n line3'\n");
if let Err(e) = parse(code7) {
print_error(&e, Some(code7));
}
// Test 7: Indentation with spaces visible
println!("Test 7: Clear space visualization");
let code7 = "defn main\n line1\n line2\n line3"; // 5 spaces on line3
println!("Code with explicit spaces: 'defn main\\n line1\\n line2\\n line3'\n");
if let Err(e) = parse(code7) {
print_error(&e, Some(code7));
}

println!("\n{}\n", "═".repeat(70));
println!("✨ Now you can clearly see:");
println!(" • \\n for newlines");
println!(" • \\t for tabs");
println!(" • Spaces are preserved and visible");
println!(" • Special escape sequences are shown");
println!(" • Context window is ~60 chars (increased from 40)");
println!("\n{}\n", "═".repeat(70));
println!("✨ Now you can clearly see:");
println!(" • \\n for newlines");
println!(" • \\t for tabs");
println!(" • Spaces are preserved and visible");
println!(" • Special escape sequences are shown");
println!(" • Context window is ~60 chars (increased from 40)");
}
1 change: 0 additions & 1 deletion src/primes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ pub fn escape_cirru_leaf(s: &str) -> String {
'\t' => chunk.push_str("\\t"),
'\"' => chunk.push_str("\\\""),
'\\' => chunk.push_str("\\\\"),
'\'' => chunk.push_str("\\'"),
_ => chunk.push(c),
}
}
Expand Down
32 changes: 27 additions & 5 deletions src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ fn generate_leaf(s: &str) -> String {
'\t' => ret.push_str("\\t"),
'\"' => ret.push_str("\\\""),
'\\' => ret.push_str("\\\\"),
'\'' => ret.push_str("\\'"),
_ => ret.push(c),
}
}
Expand Down Expand Up @@ -128,13 +127,27 @@ fn render_newline(n: usize) -> String {

fn generate_statement_one_liner(xs: &[Cirru]) -> String {
let mut ret = String::new();
let len = xs.len();
for (idx, cursor) in xs.iter().enumerate() {
if idx > 0 {
ret.push(' ');
}
let at_tail = idx == len - 1 && idx > 0;
match cursor {
Cirru::Leaf(s) => ret.push_str(&generate_leaf(s)),
Cirru::List(ys) => ret.push_str(&generate_inline_expr(ys)),
Cirru::List(ys) => {
if at_tail {
// Use $ syntax for tail expressions
if ys.is_empty() {
ret.push('$');
} else {
ret.push_str("$ ");
ret.push_str(&generate_statement_one_liner(ys));
}
} else {
ret.push_str(&generate_inline_expr(ys));
}
}
}
}
ret
Expand Down Expand Up @@ -197,9 +210,18 @@ fn generate_tree(
if ys.is_empty() {
String::from("$")
} else {
let mut ret = String::from("$ ");
ret.push_str(&generate_tree(ys, false, options, level, at_tail)?);
ret
let content = generate_tree(ys, false, options, level, at_tail)?;
if content.starts_with('\n') {
// If content starts with newline, don't add space after $
let mut ret = String::from("$");
ret.push_str(&content);
ret
} else {
// Otherwise, add space after $
let mut ret = String::from("$ ");
ret.push_str(&content);
ret
}
}
} else if idx == 0 && insist_head {
generate_inline_expr(ys)
Expand Down
54 changes: 53 additions & 1 deletion tests/oneliner_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ fn test_format_expr_one_liner() -> Result<(), String> {
]);

let one_liner = format_expr_one_liner(&tree)?;
assert_eq!(one_liner, "defn main () (println \"Hello, world!\")");
// Tail expression should use $ syntax
assert_eq!(one_liner, "defn main () $ println \"Hello, world!\"");

let parsed = parse_expr_one_liner(&one_liner)?;
assert_eq!(parsed, tree);
Expand Down Expand Up @@ -38,3 +39,54 @@ fn test_complex_nesting() -> Result<(), String> {

Ok(())
}

#[test]
fn test_tail_expression() -> Result<(), String> {
// Test tail expression with $ syntax
let tree = Cirru::List(vec![
Cirru::Leaf("defn".into()),
Cirru::Leaf("main".into()),
Cirru::List(vec![]),
Cirru::List(vec![Cirru::Leaf("println".into()), Cirru::Leaf("Hello".into())]),
]);

let one_liner = format_expr_one_liner(&tree)?;
assert_eq!(one_liner, "defn main () $ println Hello");

let parsed = parse_expr_one_liner(&one_liner)?;
assert_eq!(parsed, tree);

Ok(())
}

#[test]
fn test_nested_tail_expression() -> Result<(), String> {
// Test nested tail expression
let tree = Cirru::List(vec![
Cirru::Leaf("if".into()),
Cirru::Leaf("condition".into()),
Cirru::List(vec![Cirru::Leaf("do".into()), Cirru::List(vec![Cirru::Leaf("action".into())])]),
]);

let one_liner = format_expr_one_liner(&tree)?;
assert_eq!(one_liner, "if condition $ do $ action");

let parsed = parse_expr_one_liner(&one_liner)?;
assert_eq!(parsed, tree);

Ok(())
}

#[test]
fn test_empty_tail_expression() -> Result<(), String> {
// Test empty list as tail expression
let tree = Cirru::List(vec![Cirru::Leaf("a".into()), Cirru::Leaf("b".into()), Cirru::List(vec![])]);

let one_liner = format_expr_one_liner(&tree)?;
assert_eq!(one_liner, "a b $");

let parsed = parse_expr_one_liner(&one_liner)?;
assert_eq!(parsed, tree);

Ok(())
}
10 changes: 2 additions & 8 deletions tests/parser_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,14 @@ mod json_test {
fn parse_demo() {
assert_eq!(parse("a").map(Cirru::List), Ok(Cirru::List(vec!(vec!["a"].into()))));

assert_eq!(
parse("a b c").map(Cirru::List),
Ok(Cirru::List(vec!(vec!["a", "b", "c"].into())))
);
assert_eq!(parse("a b c").map(Cirru::List), Ok(Cirru::List(vec!(vec!["a", "b", "c"].into()))));

assert_eq!(
parse("a\nb").map(Cirru::List),
Ok(Cirru::List(vec!(vec!["a"].into(), vec!["b"].into())))
);

assert_eq!(
parse("a\rb").map(Cirru::List),
Ok(Cirru::List(vec!(vec!["a\rb"].into())))
);
assert_eq!(parse("a\rb").map(Cirru::List), Ok(Cirru::List(vec!(vec!["a\rb"].into()))));

assert_eq!(
parse("a (b) c").map(Cirru::List),
Expand Down
2 changes: 1 addition & 1 deletion tests/writer_cirru/cond.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cond
(bool? x)
str x
(symbol? x)
str "\"\'" x
str "\"'" x
(map? x) "\"a map"
(set? x) "\"a set"
true $ str
4 changes: 4 additions & 0 deletions tests/writer_cirru/tag-match.cirru
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

tag-match self $
:plugin node cursor state
d! cursor $ assoc state :show? false
10 changes: 10 additions & 0 deletions tests/writer_data/tag-match.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
[
"tag-match",
"self",
[
[":plugin", "node", "cursor", "state"],
["d!", "cursor", ["assoc", "state", ":show?", "false"]]
]
]
]
Loading