<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/walk/walk.go
blob: 3fba62f2ce691692f9f31b40212c0a5af5df401b (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package walk

import (
	"fmt"
	"strings"
)

type PathSegment interface {}

type OutputList interface {
	outputList()
}

type OutputValueList []Value
func (_ OutputValueList) outputList() {}

type OutputRuneList []rune
func (_ OutputRuneList) outputList() {}

// Can be passed to .eat on a state
type Edible interface {
	edible()
}

type RuneEdible rune
func (_ RuneEdible) edible() {}

type Terminal int
const (
	ArrayEnd Terminal = iota
	MapEnd
	StringEnd
)
func (_ Terminal) edible() {}

// Simple value
// Has a literal
type Scalar interface {
	Value
	scalar()
}

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

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

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

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

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

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()
}
func (_ ArrayValue) edible() {}

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()
}
func (_ MapValue) edible() {}

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()
}