Add deep Copy() for parser types

Change-Id: I94bca46d6d30da683cacb0f6ea1fdf26a5a46bc8
This commit is contained in:
Dan Willemsen 2015-07-06 12:29:57 -07:00
parent 91dc34a563
commit 38b0630a2a

View file

@ -507,6 +507,13 @@ type Expression struct {
Pos scanner.Position
}
func (e *Expression) Copy() *Expression {
ret := *e
ret.Args[0] = e.Args[0].Copy()
ret.Args[1] = e.Args[1].Copy()
return &ret
}
func (e *Expression) String() string {
return fmt.Sprintf("(%s %c %s)@%d:%s", e.Args[0].String(), e.Operator, e.Args[1].String(),
e.Pos.Offset, e.Pos)
@ -563,6 +570,15 @@ type Module struct {
RbracePos scanner.Position
}
func (m *Module) Copy() *Module {
ret := *m
ret.Properties = make([]*Property, len(m.Properties))
for i := range m.Properties {
ret.Properties[i] = m.Properties[i].Copy()
}
return &ret
}
func (m *Module) String() string {
propertyStrings := make([]string, len(m.Properties))
for i, property := range m.Properties {
@ -582,6 +598,12 @@ type Property struct {
Pos scanner.Position
}
func (p *Property) Copy() *Property {
ret := *p
ret.Value = p.Value.Copy()
return &ret
}
func (p *Property) String() string {
return fmt.Sprintf("%s@%d:%s: %s", p.Name, p.Pos.Offset, p.Pos, p.Value)
}
@ -607,6 +629,26 @@ type Value struct {
EndPos scanner.Position
}
func (p Value) Copy() Value {
ret := p
if p.MapValue != nil {
ret.MapValue = make([]*Property, len(p.MapValue))
for i := range p.MapValue {
ret.MapValue[i] = p.MapValue[i].Copy()
}
}
if p.ListValue != nil {
ret.ListValue = make([]Value, len(p.ListValue))
for i := range p.ListValue {
ret.ListValue[i] = p.ListValue[i].Copy()
}
}
if p.Expression != nil {
ret.Expression = p.Expression.Copy()
}
return ret
}
func (p Value) String() string {
var s string
if p.Variable != "" {