-
Notifications
You must be signed in to change notification settings - Fork 2
add one-liner parser APIs and clean warnings #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,7 +83,7 @@ fn is_char_allowed(x : Char) -> Bool { | |
| fn generate_leaf(s : String) -> String { | ||
| let mut all_allowed = true | ||
| for x in s { | ||
| if not(is_char_allowed(x)) { | ||
| if !is_char_allowed(x) { | ||
| all_allowed = false | ||
| break | ||
| } | ||
|
|
@@ -131,6 +131,33 @@ fn generate_inline_expr(xs : Array[Cirru]) -> String { | |
| result | ||
| } | ||
|
|
||
| ///| | ||
| fn generate_statement_one_liner(xs : Array[Cirru]) -> String { | ||
| let mut ret = "" | ||
| let size = xs.length() | ||
| for idx, cursor in xs { | ||
| if idx > 0 { | ||
| ret += " " | ||
| } | ||
| let at_tail = idx > 0 && idx == size - 1 | ||
| match cursor { | ||
| Leaf(s) => ret += generate_leaf(s) | ||
| List(ys) => | ||
| if at_tail { | ||
|
Comment on lines
+142
to
+146
|
||
| if ys.is_empty() { | ||
| ret += "$" | ||
| } else { | ||
| ret += "$ " | ||
| ret += generate_statement_one_liner(ys) | ||
| } | ||
| } else { | ||
| ret += generate_inline_expr(ys) | ||
| } | ||
| } | ||
| } | ||
| ret | ||
| } | ||
|
|
||
| ///| | ||
| /// by 2 spaces | ||
| fn push_spaces(buf : String, n : Int) -> String { | ||
|
|
@@ -170,7 +197,7 @@ fn Cirru::get_node_kind(self : Cirru) -> WriterNode { | |
| ///| | ||
| pub(all) suberror FormatCirruError { | ||
| FormatCirruError(String) | ||
| } derive(Show) | ||
| } derive(Debug) | ||
|
|
||
| ///| | ||
| fn generate_tree( | ||
|
|
@@ -188,7 +215,7 @@ fn generate_tree( | |
| let next_level = level + 1 | ||
| let child_insist_head = prev_kind == BoxedExpr || prev_kind == Expr | ||
| let at_tail = idx != 0 && | ||
| not(in_tail) && | ||
| !in_tail && | ||
| prev_kind == Leaf && | ||
| idx == xs.length() - 1 | ||
|
|
||
|
|
@@ -343,3 +370,15 @@ pub fn Cirru::format( | |
| ) -> String raise FormatCirruError { | ||
| generate_statements(xs, use_inline~) | ||
| } | ||
|
|
||
| ///| | ||
| /// format a single Cirru expression as a single line | ||
| pub fn Cirru::format_expr_one_liner( | ||
| expr : Cirru, | ||
| ) -> String raise FormatCirruError { | ||
| match expr { | ||
| Leaf(_) => | ||
| raise FormatCirruError("format_expr_one_liner expects an expr (list)") | ||
| List(xs) => generate_statement_one_liner(xs) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
generate_statement_one_linerreturns an empty string whenxsis empty, soCirru::format_expr_one_liner(List([]))would format to""instead of the valid empty expr representation"()". Add an explicit empty-case (e.g., returngenerate_empty_expr()/"()") and consider adding a regression test for formatting/parsing an empty expression.