@@ -21,6 +21,11 @@ pub const INPUT_CHARSET: &str = "0123456789()[],'/*abcdefgh@:$%{}IJKLMNOPQRSTUVW
21
21
pub struct Tree < ' a > {
22
22
/// The name `x`
23
23
pub name : & ' a str ,
24
+ /// Position one past the last character of the node's name. If it has
25
+ /// children, the position of the '(' or '{'.
26
+ pub children_pos : usize ,
27
+ /// The type of parentheses surrounding the node's children.
28
+ pub parens : Parens ,
24
29
/// The comma-separated contents of the `(...)`, if any
25
30
pub args : Vec < Tree < ' a > > ,
26
31
}
@@ -38,11 +43,17 @@ impl PartialEq for Tree<'_> {
38
43
}
39
44
}
40
45
impl Eq for Tree < ' _ > { }
41
- // or_b(pk(A),pk(B))
42
- //
43
- // A = musig(musig(B,C),D,E)
44
- // or_b()
45
- // pk(A), pk(B)
46
+
47
+ /// The type of parentheses surrounding a node's children.
48
+ #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
49
+ pub enum Parens {
50
+ /// Node has no children.
51
+ None ,
52
+ /// Round parentheses: `(` and `)`.
53
+ Round ,
54
+ /// Curly braces: `{` and `}`.
55
+ Curly ,
56
+ }
46
57
47
58
/// Whether to treat `{` and `}` as deliminators when parsing an expression.
48
59
#[ derive( Copy , Clone , PartialEq , Eq ) ]
@@ -166,31 +177,45 @@ impl<'a> Tree<'a> {
166
177
// Now, knowing it is sane and well-formed, we can easily parse it backward,
167
178
// which will yield a post-order right-to-left iterator of its nodes.
168
179
let mut stack = Vec :: with_capacity ( max_depth) ;
169
- let mut children = None ;
180
+ let mut children_parens : Option < ( Vec < _ > , usize , Parens ) > = None ;
170
181
let mut node_name_end = s. len ( ) ;
171
182
let mut tapleaf_depth = 0 ;
172
183
for ( pos, ch) in s. bytes ( ) . enumerate ( ) . rev ( ) {
173
184
if ch == cparen {
174
185
stack. push ( vec ! [ ] ) ;
175
186
node_name_end = pos;
176
187
} else if tapleaf_depth == 0 && ch == b',' {
188
+ let ( mut args, children_pos, parens) =
189
+ children_parens
190
+ . take ( )
191
+ . unwrap_or ( ( vec ! [ ] , node_name_end, Parens :: None ) ) ;
192
+ args. reverse ( ) ;
193
+
177
194
let top = stack. last_mut ( ) . unwrap ( ) ;
178
- let mut new_tree = Tree {
179
- name : & s[ pos + 1 ..node_name_end] ,
180
- args : children. take ( ) . unwrap_or ( vec ! [ ] ) ,
181
- } ;
182
- new_tree. args . reverse ( ) ;
195
+ let new_tree =
196
+ Tree { name : & s[ pos + 1 ..node_name_end] , children_pos, parens, args } ;
183
197
top. push ( new_tree) ;
184
198
node_name_end = pos;
185
199
} else if ch == oparen {
200
+ let ( mut args, children_pos, parens) =
201
+ children_parens
202
+ . take ( )
203
+ . unwrap_or ( ( vec ! [ ] , node_name_end, Parens :: None ) ) ;
204
+ args. reverse ( ) ;
205
+
186
206
let mut top = stack. pop ( ) . unwrap ( ) ;
187
- let mut new_tree = Tree {
188
- name : & s[ pos + 1 ..node_name_end] ,
189
- args : children. take ( ) . unwrap_or ( vec ! [ ] ) ,
190
- } ;
191
- new_tree. args . reverse ( ) ;
207
+ let new_tree =
208
+ Tree { name : & s[ pos + 1 ..node_name_end] , children_pos, parens, args } ;
192
209
top. push ( new_tree) ;
193
- children = Some ( top) ;
210
+ children_parens = Some ( (
211
+ top,
212
+ pos,
213
+ match ch {
214
+ b'(' => Parens :: Round ,
215
+ b'{' => Parens :: Curly ,
216
+ _ => unreachable ! ( ) ,
217
+ } ,
218
+ ) ) ;
194
219
node_name_end = pos;
195
220
} else if delim == Delimiter :: Taproot && ch == b'(' {
196
221
tapleaf_depth += 1 ;
@@ -200,9 +225,12 @@ impl<'a> Tree<'a> {
200
225
}
201
226
202
227
assert_eq ! ( stack. len( ) , 0 ) ;
203
- let mut children = children. take ( ) . unwrap_or ( vec ! [ ] ) ;
204
- children. reverse ( ) ;
205
- Ok ( Tree { name : & s[ ..node_name_end] , args : children } )
228
+ let ( mut args, children_pos, parens) =
229
+ children_parens
230
+ . take ( )
231
+ . unwrap_or ( ( vec ! [ ] , node_name_end, Parens :: None ) ) ;
232
+ args. reverse ( ) ;
233
+ Ok ( Tree { name : & s[ ..node_name_end] , children_pos, parens, args } )
206
234
}
207
235
208
236
/// Parses a tree from a string
@@ -300,9 +328,19 @@ mod tests {
300
328
use super :: * ;
301
329
302
330
/// Test functions to manually build trees
303
- fn leaf ( name : & str ) -> Tree { Tree { name, args : vec ! [ ] } }
331
+ fn leaf ( name : & str ) -> Tree {
332
+ Tree { name, parens : Parens :: None , children_pos : name. len ( ) , args : vec ! [ ] }
333
+ }
334
+
335
+ fn paren_node < ' a > ( name : & ' a str , mut args : Vec < Tree < ' a > > ) -> Tree < ' a > {
336
+ let mut offset = name. len ( ) + 1 ; // +1 for open paren
337
+ for arg in & mut args {
338
+ arg. children_pos += offset;
339
+ offset += arg. name . len ( ) + 1 ; // +1 for comma
340
+ }
304
341
305
- fn paren_node < ' a > ( name : & ' a str , args : Vec < Tree < ' a > > ) -> Tree < ' a > { Tree { name, args } }
342
+ Tree { name, parens : Parens :: Round , children_pos : name. len ( ) , args }
343
+ }
306
344
307
345
#[ test]
308
346
fn test_parse_num ( ) {
0 commit comments