Reduced token size
This commit is contained in:
@ -37,11 +37,11 @@ func (expr *Expression) AddChild(child *Expression) {
|
||||
}
|
||||
|
||||
// Count counts how often the given token appears in the expression.
|
||||
func (expr *Expression) Count(kind token.Kind, name string) int {
|
||||
func (expr *Expression) Count(buffer []byte, kind token.Kind, name string) int {
|
||||
count := 0
|
||||
|
||||
expr.EachLeaf(func(leaf *Expression) error {
|
||||
if leaf.Token.Kind == kind && leaf.Token.Text() == name {
|
||||
if leaf.Token.Kind == kind && leaf.Token.Text(buffer) == name {
|
||||
count++
|
||||
}
|
||||
|
||||
@ -112,25 +112,33 @@ func (expr *Expression) LastChild() *Expression {
|
||||
}
|
||||
|
||||
// String generates a textual representation of the expression.
|
||||
func (expr *Expression) String() string {
|
||||
func (expr *Expression) String(data []byte) string {
|
||||
builder := strings.Builder{}
|
||||
expr.write(&builder)
|
||||
expr.write(&builder, data)
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
// write generates a textual representation of the expression.
|
||||
func (expr *Expression) write(builder *strings.Builder) {
|
||||
func (expr *Expression) write(builder *strings.Builder, data []byte) {
|
||||
if expr.IsLeaf() {
|
||||
builder.WriteString(expr.Token.Text())
|
||||
builder.WriteString(expr.Token.Text(data))
|
||||
return
|
||||
}
|
||||
|
||||
builder.WriteByte('(')
|
||||
builder.WriteString(expr.Token.Text())
|
||||
|
||||
switch expr.Token.Kind {
|
||||
case token.Call:
|
||||
builder.WriteString("λ")
|
||||
case token.Array:
|
||||
builder.WriteString("@")
|
||||
default:
|
||||
builder.WriteString(expr.Token.Text(data))
|
||||
}
|
||||
|
||||
for _, child := range expr.Children {
|
||||
builder.WriteByte(' ')
|
||||
child.write(builder)
|
||||
child.write(builder, data)
|
||||
}
|
||||
|
||||
builder.WriteByte(')')
|
||||
|
Reference in New Issue
Block a user