1+ ///|
12enum WriterNode {
23 Nil
34 Leaf
@@ -6,18 +7,23 @@ enum WriterNode {
67 Expr
78} derive (Eq )
89
10+ ///|
911let char_close : Char = ')'
1012
13+ ///|
1114let char_open : Char = '('
1215
16+ ///|
1317let allowed_chars : String = "$-:<>[]{}*=+.,\\ /!?~_@#&%^|;'"
1418
19+ ///|
1520fn is_a_digit (c : Char ) -> Bool {
1621 let n = c .to_int ()
1722 // ascii table https://tool.oschina.net/commons?type=4
1823 n >= 48 && n <= 57
1924}
2025
26+ ///|
2127fn is_a_letter (c : Char ) -> Bool {
2228 let n = c .to_int ()
2329 if n >= 65 && n <= 90 {
@@ -29,6 +35,7 @@ fn is_a_letter(c : Char) -> Bool {
2935 false
3036}
3137
38+ ///|
3239fn is_simple_expr (ys : Array [Cirru ]) -> Bool {
3340 for y in ys {
3441 match y {
@@ -39,6 +46,7 @@ fn is_simple_expr(ys : Array[Cirru]) -> Bool {
3946 true
4047}
4148
49+ ///|
4250fn is_boxed (ys : Array [Cirru ]) -> Bool {
4351 for y in ys {
4452 match y {
@@ -49,17 +57,20 @@ fn is_boxed(ys : Array[Cirru]) -> Bool {
4957 true
5058}
5159
60+ ///|
5261fn is_simple_char (x : Char ) -> Bool {
5362 is_a_letter (x ) || is_a_digit (x )
5463}
5564
65+ ///|
5666fn is_char_allowed (x : Char ) -> Bool {
5767 if is_simple_char (x ) {
5868 return true
5969 }
6070 allowed_chars .contains_char (x )
6171}
6272
73+ ///|
6374fn generate_leaf (s : String ) -> String {
6475 let mut all_allowed = true
6576 for x in s {
@@ -89,10 +100,12 @@ fn generate_leaf(s : String) -> String {
89100 }
90101}
91102
103+ ///|
92104fn generate_empty_expr () -> String {
93105 "()"
94106}
95107
108+ ///|
96109fn generate_inline_expr (xs : Array [Cirru ]) -> String {
97110 let mut result = char_open .to_string ()
98111 for idx , x in xs {
@@ -109,7 +122,7 @@ fn generate_inline_expr(xs : Array[Cirru]) -> String {
109122 result
110123}
111124
112- /// by 2 spaces
125+ ///| by 2 spaces
113126fn push_spaces (buf : String , n : Int ) -> String {
114127 let mut ret = buf
115128 // for _ in 0..n {
@@ -121,19 +134,20 @@ fn push_spaces(buf : String, n : Int) -> String {
121134 return ret
122135}
123136
137+ ///|
124138fn render_newline (n : Int ) -> String {
125139 let mut ret = ""
126140 ret = ret + "\n "
127141 ret = push_spaces (ret , n )
128142 ret
129143}
130144
131- /// options for writer, `use_inline` for more compact format.
145+ ///| options for writer, `use_inline` for more compact format.
132146pub (all ) struct CirruWriterOptions {
133147 use_inline : Bool
134148}
135149
136- /// kind for writer nodes
150+ ///| kind for writer nodes
137151fn get_node_kind (self : Cirru ) -> WriterNode {
138152 match self {
139153 Cirru ::Leaf (_ ) => WriterNode ::Leaf
@@ -150,14 +164,17 @@ fn get_node_kind(self : Cirru) -> WriterNode {
150164 }
151165}
152166
167+ ///|
153168pub (all ) type! FormatCirruError String
154169
170+ ///|
155171pub fn to_string (self : FormatCirruError ) -> String {
156172 match self {
157173 FormatCirruError (s ) => s
158174 }
159175}
160176
177+ ///|
161178fn generate_tree (
162179 xs : Array [Cirru ],
163180 insist_head : Bool ,
@@ -291,6 +308,7 @@ fn generate_tree(
291308 result
292309}
293310
311+ ///|
294312fn generate_statements (
295313 ys : Array [Cirru ],
296314 options : CirruWriterOptions
@@ -309,7 +327,7 @@ fn generate_statements(
309327 zs
310328}
311329
312- /// format Cirru code, use options to control `use_inline` option
330+ ///| format Cirru code, use options to control `use_inline` option
313331pub fn format (
314332 xs : Array [Cirru ],
315333 options : CirruWriterOptions
0 commit comments