Skip to content
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

opIndex, opDollar and opSlice override on ParseTree #275

Merged
merged 1 commit into from
Mar 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions pegged/peg.d
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,24 @@ struct ParseTree
{
return cast(immutable)dup();
}

// Override opIndex operators
ref ParseTree opIndex(size_t index) {
return children[index];
}

ref ParseTree[] opIndex() {
return children;
}

size_t opDollar(size_t pos)()
{
return children.length;
}

ParseTree[] opSlice(size_t i, size_t j) {
return children[i..j];
}
}


Expand All @@ -365,6 +383,11 @@ unittest // ParseTree testing
ParseTree child = ParseTree("Child", true, ["abc", "", "def"], "input", 0, 1, null);
p.children = [child, child];

assert(p.children[0] == p[0], "override opIndex allows to write less verbose code to navigate the tree");
assert(p.children == p[] );
assert(p.children[0..1] == p[0..1] );
assert(p.children[0..$] == p[0..$] );

ParseTree q = p;
assert(p == q, "Copying creates equal trees.");

Expand Down