Skip to content

Commit dc2a85b

Browse files
Merge pull request #251 from veelo/dup
Dupping and comparing mutable/immutable parse trees.
2 parents d091b9f + f4f42e0 commit dc2a85b

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

pegged/peg.d

+19-5
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,23 @@ struct ParseTree
334334
return "Success";
335335
}
336336
337-
ParseTree dup() @property
337+
ParseTree dup() const @property
338338
{
339-
ParseTree result = this;
340-
result.matches = result.matches.dup;
341-
result.children = map!(p => p.dup)(result.children).array();
339+
ParseTree result;
340+
result.name = name;
341+
result.successful = successful;
342+
result.matches = matches.dup;
343+
result.input = input;
344+
result.begin = begin;
345+
result.end = end;
346+
result.children = map!(p => p.dup)(children).array();
342347
return result;
343348
}
349+
350+
immutable(ParseTree) idup() const @property
351+
{
352+
return cast(immutable)dup();
353+
}
344354
}
345355
346356
@@ -372,6 +382,10 @@ unittest // ParseTree testing
372382
p.children = null;
373383
assert(q.children != p.children);
374384
385+
immutable iq = p.idup;
386+
q = iq.dup;
387+
assert(p == q, "Dupping to/from immutable creates equal trees.");
388+
375389
q.children = [p,p];
376390
assert(p != q, "Tree with different children are not equal.");
377391
@@ -387,7 +401,7 @@ unittest // ParseTree testing
387401
388402
/// To compare two trees for content (not bothering with node names)
389403
/// That's useful to compare the results from two different grammars.
390-
bool softCompare(ParseTree p1, ParseTree p2)
404+
bool softCompare(const ParseTree p1, const ParseTree p2)
391405
{
392406
return p1.successful == p2.successful
393407
&& p1.matches == p2.matches

0 commit comments

Comments
 (0)