This commit is contained in:
parent
7a8cb43e9f
commit
f357285045
2 changed files with 40 additions and 17 deletions
|
@ -25,18 +25,6 @@ func (expr *Expression) AddChild(child *Expression) {
|
|||
child.Parent = expr
|
||||
}
|
||||
|
||||
// Reset resets all values to the default.
|
||||
func (expr *Expression) Reset() {
|
||||
expr.Parent = nil
|
||||
|
||||
if expr.Children != nil {
|
||||
expr.Children = expr.Children[:0]
|
||||
}
|
||||
|
||||
expr.Token.Reset()
|
||||
expr.precedence = 0
|
||||
}
|
||||
|
||||
// EachLeaf iterates through all leaves in the tree.
|
||||
func (expr *Expression) EachLeaf(call func(*Expression) error) error {
|
||||
if expr.IsLeaf() {
|
||||
|
@ -54,15 +42,15 @@ func (expr *Expression) EachLeaf(call func(*Expression) error) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// RemoveChild removes a child from the expression.
|
||||
func (expr *Expression) RemoveChild(child *Expression) {
|
||||
// Index returns the position of the child or `-1` if it's not a child of this expression.
|
||||
func (expr *Expression) Index(child *Expression) int {
|
||||
for i, c := range expr.Children {
|
||||
if c == child {
|
||||
expr.Children = append(expr.Children[:i], expr.Children[i+1:]...)
|
||||
child.Parent = nil
|
||||
return
|
||||
return i
|
||||
}
|
||||
}
|
||||
|
||||
return -1
|
||||
}
|
||||
|
||||
// InsertAbove replaces this expression in its parent's children with the given new parent,
|
||||
|
@ -87,6 +75,30 @@ func (expr *Expression) LastChild() *Expression {
|
|||
return expr.Children[len(expr.Children)-1]
|
||||
}
|
||||
|
||||
// RemoveChild removes a child from the expression.
|
||||
func (expr *Expression) RemoveChild(child *Expression) {
|
||||
for i, c := range expr.Children {
|
||||
if c == child {
|
||||
expr.Children = append(expr.Children[:i], expr.Children[i+1:]...)
|
||||
child.Parent = nil
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Reset resets all values to the default.
|
||||
func (expr *Expression) Reset() {
|
||||
expr.Parent = nil
|
||||
|
||||
if expr.Children != nil {
|
||||
expr.Children = expr.Children[:0]
|
||||
}
|
||||
|
||||
expr.Token.Reset()
|
||||
expr.Source = nil
|
||||
expr.precedence = 0
|
||||
}
|
||||
|
||||
// String generates a textual representation of the expression.
|
||||
func (expr *Expression) String(source []byte) string {
|
||||
builder := strings.Builder{}
|
||||
|
|
|
@ -45,6 +45,17 @@ func TestNilGroup(t *testing.T) {
|
|||
assert.Nil(t, expr)
|
||||
}
|
||||
|
||||
func TestIndex(t *testing.T) {
|
||||
src := []byte("1+2")
|
||||
tokens := token.Tokenize(src)
|
||||
expr := expression.Parse(tokens)
|
||||
left := expr.Children[0]
|
||||
right := expr.Children[1]
|
||||
assert.Equal(t, expr.Index(left), 0)
|
||||
assert.Equal(t, expr.Index(right), 1)
|
||||
assert.Equal(t, expr.Index(expr), -1)
|
||||
}
|
||||
|
||||
func TestRemoveChild(t *testing.T) {
|
||||
src := []byte("(1+2-3*4)+(5*6-7+8)")
|
||||
tokens := token.Tokenize(src)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue