<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/walk/walk.go
blob: 289d9ee9ae733e5d5d23162d86610d5e3ad770e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package walk

import (
	"fmt"
	"strings"
)

type Value interface {
	value()
	Debug() string
}

type NullValue struct{}
func (_ NullValue) value() {}
func (_ NullValue) Debug() string {
	return "null"
}

type BoolValue bool
func (_ BoolValue) value() {}
func (b BoolValue) Debug() string {
	if b {
		return "true"
	}
	return "false"
}

type NumberValue float64
func (_ NumberValue) value() {}
func (n NumberValue) Debug() string {
	return fmt.Sprintf("%v", float64(n))
}

type RuneValue rune
func (_ RuneValue) value() {}
func (r RuneValue) Debug() string {
	return string(r)
}

type StringValue string
func (_ StringValue) value() {}
func (s StringValue) Debug() string {
	return fmt.Sprintf("%q", string(s))
}

type ArrayElement struct {
	Index int
	Value Value
}

type ArrayValue []ArrayElement
func (_ ArrayValue) value() {}
func (array ArrayValue) Debug() string {
	builder := strings.Builder{}
	builder.WriteRune('[')
	var sep string
	for _, element := range array {
		builder.WriteString(sep)
		builder.WriteString(fmt.Sprintf("%v", element.Index))
		builder.WriteString(": ")
		builder.WriteString(element.Value.Debug())
		sep = ", "
	}
	builder.WriteRune(']')
	return builder.String()
}

type MapElement struct {
	Key string
	Value Value
}

type MapValue []MapElement
func (_ MapValue) value() {}
func (m MapValue) Debug() string {
	builder := strings.Builder{}
	builder.WriteRune('{')
	var sep string
	for _, element := range m {
		builder.WriteString(sep)
		builder.WriteString(fmt.Sprintf("%q", element.Key))
		builder.WriteString(": ")
		builder.WriteString(element.Value.Debug())
		sep = ", "
	}
	builder.WriteRune('}')
	return builder.String()
}

type WalkItem struct {
	Value Value
	Start, PrevStart, End, NextEnd bool
}

func (item WalkItem) Debug() string {
	builder := strings.Builder{}
	if item.Start {
		builder.WriteRune('a')
	}
	if item.PrevStart {
		builder.WriteRune('A')
	}
	if item.NextEnd {
		builder.WriteRune('Z')
	}
	if item.End {
		builder.WriteRune('z')
	}
	builder.WriteString(item.Value.Debug())
	return builder.String()
}