1- /// Cirru uses nested Vecters and Strings as data structure
1+ ///| Cirru uses nested Vecters and Strings as data structure
22pub (all ) enum Cirru {
33 /// Leaf node, with a string
44 Leaf (String )
55 /// List node, with a list of children
66 List (Array [Cirru ])
77} derive (Eq )
88
9+ ///|
910pub impl Hash for Cirru with hash (self ) {
1011 let mut i = 0
1112 match self {
@@ -18,6 +19,7 @@ pub impl Hash for Cirru with hash(self) {
1819 i
1920}
2021
22+ ///|
2123pub impl Hash for Cirru with hash_combine (self , hasher ) {
2224 match self {
2325 Cirru ::Leaf (s ) => hasher .combine_string (s )
@@ -28,6 +30,7 @@ pub impl Hash for Cirru with hash_combine(self, hasher) {
2830 }
2931}
3032
33+ ///|
3134pub fn to_json (self : Cirru ) -> Json {
3235 match self {
3336 Cirru ::Leaf (s ) => Json ::String (s )
@@ -41,6 +44,7 @@ pub fn to_json(self : Cirru) -> Json {
4144 }
4245}
4346
47+ ///|
4448pub impl @json .FromJson for Cirru with from_json (json , path ) {
4549 match json {
4650 String (a ) => Cirru ::Leaf (a )
@@ -60,14 +64,17 @@ pub impl @json.FromJson for Cirru with from_json(json, path) {
6064 }
6165}
6266
67+ ///|
6368fn Cirru ::default () -> Cirru {
6469 Cirru ::List (Array ::new ())
6570}
6671
72+ ///|
6773pub fn output (self : Cirru , logger : Logger ) -> Unit {
6874 logger .write_string (self .to_string ())
6975}
7076
77+ ///|
7178pub fn to_string (self : Cirru ) -> String {
7279 match self {
7380 Cirru ::Leaf (s ) =>
@@ -90,6 +97,7 @@ pub fn to_string(self : Cirru) -> String {
9097 }
9198}
9299
100+ ///|
93101pub fn compare (self : Cirru , other : Cirru ) -> Int {
94102 match (self , other ) {
95103 (Cirru ::Leaf (a ), Cirru ::Leaf (b )) => a .compare (b )
@@ -110,37 +118,23 @@ pub fn compare(self : Cirru, other : Cirru) -> Int {
110118 }
111119}
112120
113- pub fn debug_write (self : Cirru , buffer : Buffer ) -> Unit {
114- match self {
115- Cirru ::Leaf (s ) => buffer .write_string (s )
116- Cirru ::List (xs ) => {
117- buffer .write_char ('(' )
118- for i = 0 ; i < xs .length (); i = i + 1 {
119- let x = xs [i ]
120- buffer .write_string (x .to_string ())
121- if i < xs .length () - 1 {
122- buffer .write_char (' ' )
123- }
124- }
125- buffer .write_char (')' )
126- }
127- }
128- }
129-
121+ ///|
130122pub fn length (self : Cirru ) -> Int {
131123 match self {
132124 Leaf (s ) => s .length ()
133125 List (xs ) => xs .length ()
134126 }
135127}
136128
129+ ///|
137130pub fn is_empty (self : Cirru ) -> Bool {
138131 match self {
139132 Leaf (s ) => s .length () == 0
140133 List (xs ) => xs .length () == 0
141134 }
142135}
143136
137+ ///|
144138pub fn is_nested (self : Cirru ) -> Bool {
145139 match self {
146140 Leaf (_ ) => false
@@ -156,13 +150,15 @@ pub fn is_nested(self : Cirru) -> Bool {
156150 }
157151}
158152
153+ ///|
159154pub fn is_comment (self : Cirru ) -> Bool {
160155 match self {
161156 Leaf (s ) => s [0 ] == ';'
162157 _ => false
163158 }
164159}
165160
161+ ///|
166162enum CirruLexState {
167163 Space
168164 Token
@@ -171,17 +167,20 @@ enum CirruLexState {
171167 Str
172168} derive (Show )
173169
170+ ///|
174171enum CirruLexItem {
175172 Open
176173 Close
177174 Indent (Int )
178175 Str (String )
179176}
180177
178+ ///|
181179fn output (self : CirruLexItem , logger : Logger ) -> Unit {
182180 logger .write_string (self .to_string ())
183181}
184182
183+ ///|
185184fn to_string (self : CirruLexItem ) -> String {
186185 match self {
187186 CirruLexItem ::Open => "("
@@ -191,6 +190,7 @@ fn to_string(self : CirruLexItem) -> String {
191190 }
192191}
193192
193+ ///|
194194fn CirruLexItem ::is_normal_str (tok : String ) -> Bool {
195195 let size = tok .length ()
196196 if size == 0 {
@@ -227,6 +227,7 @@ fn CirruLexItem::is_normal_str(tok : String) -> Bool {
227227 return true
228228}
229229
230+ ///|
230231fn escape_cirru_leaf (s : String ) -> String {
231232 if is_normal_str (s ) {
232233 return "\" \{ s } \" "
0 commit comments