<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/walk/walk.go
diff options
context:
space:
mode:
Diffstat (limited to 'walk/walk.go')
-rw-r--r--walk/walk.go135
1 files changed, 1 insertions, 134 deletions
diff --git a/walk/walk.go b/walk/walk.go
index 6e86877..1073c67 100644
--- a/walk/walk.go
+++ b/walk/walk.go
@@ -1,18 +1,14 @@
package walk
import (
- "fmt"
"strings"
"math"
"unicode/utf8"
- "bufio"
)
// int or string
type PathSegment interface {}
-func stringPathSegment(segment PathSegment) string {
- return fmt.Sprintf("%v", segment)
-}
+
type Path []PathSegment
func (path Path) ToWalkValues() []Value {
var values []Value
@@ -49,135 +45,6 @@ type WalkItem struct {
Path []Atom
}
-type JSONOutStructure int
-const (
- JSONOutRoot JSONOutStructure = iota
- JSONOutMap
- JSONOutArray
- JSONOutString
- JSONOutValueEnd
-)
-
-type JSONOut struct {
- structure []JSONOutStructure
- writer *bufio.Writer
-}
-
-func (out *JSONOut) indent(adjust int) {
- fmt.Fprint(out.writer, strings.Repeat("\t", len(out.structure) - 1 + adjust))
-}
-
-func (out *JSONOut) atomOut(key string, atom Atom) {
- state := out.structure[len(out.structure) - 1]
- switch state {
- case JSONOutRoot, JSONOutMap, JSONOutArray:
- switch atom.Typ {
- case AtomNull, AtomBool, AtomNumber:
- out.indent(0)
- if state == JSONOutMap {
- fmt.Fprintf(out.writer, "%q: ", key)
- }
- fmt.Fprint(out.writer, atom.String())
- out.structure = append(out.structure, JSONOutValueEnd)
- case AtomStringTerminal:
- out.indent(0)
- if state == JSONOutMap {
- fmt.Fprintf(out.writer, "%q: ", key)
- }
- fmt.Fprint(out.writer, "\"")
- out.structure = append(out.structure, JSONOutString)
- case AtomTerminal:
- switch ValueTerminal(atom.data) {
- case MapBegin:
- out.indent(0)
- if state == JSONOutMap {
- fmt.Fprintf(out.writer, "%q: ", key)
- }
- fmt.Fprint(out.writer, "{\n")
- out.structure = append(out.structure, JSONOutMap)
- case ArrayBegin:
- out.indent(0)
- if state == JSONOutMap {
- fmt.Fprintf(out.writer, "%q: ", key)
- }
- fmt.Fprint(out.writer, "[\n")
- out.structure = append(out.structure, JSONOutArray)
- case MapEnd:
- out.indent(-1)
- if state != JSONOutMap {
- panic("Map ended while not inside a map")
- }
- fmt.Fprint(out.writer, "}")
- out.structure[len(out.structure) - 1] = JSONOutValueEnd
- case ArrayEnd:
- out.indent(-1)
- if state != JSONOutArray {
- panic("Array ended while not inside a array")
- }
- fmt.Fprint(out.writer, "]")
- out.structure[len(out.structure) - 1] = JSONOutValueEnd
- default:
- panic("Invalid TerminalValue")
- }
- default:
- panic("Invalid AtomType in root value")
- }
- case JSONOutValueEnd:
- out.structure = out.structure[:len(out.structure) - 1]
- underState := out.structure[len(out.structure) - 1]
- if underState == JSONOutMap && atom.Typ == AtomTerminal && ValueTerminal(atom.data) == MapEnd {
- fmt.Fprint(out.writer, "\n")
- out.indent(-1)
- fmt.Fprint(out.writer, "}")
- out.structure[len(out.structure) - 1] = JSONOutValueEnd
- } else if underState == JSONOutArray && atom.Typ == AtomTerminal && ValueTerminal(atom.data) == ArrayEnd {
- fmt.Fprint(out.writer, "\n")
- out.indent(-1)
- fmt.Fprint(out.writer, "]")
- out.structure[len(out.structure) - 1] = JSONOutValueEnd
- } else if underState == JSONOutRoot {
- panic("Tried to output JSON after root value has concluded")
- } else {
- fmt.Fprint(out.writer, ",\n")
- out.atomOut(key, atom)
- }
- case JSONOutString:
- if atom.Typ == AtomStringTerminal {
- fmt.Fprint(out.writer, "\"")
- out.structure[len(out.structure) - 1] = JSONOutValueEnd
- } else {
- fmt.Fprint(out.writer, atom.String())
- }
- default:
- panic("Invalid JSONOutState")
- }
-}
-
-func (out *JSONOut) Print(path Path, values []Atom) {
- var segment PathSegment
- if len(path) > 0 {
- segment = path[len(path) - 1]
- }
- segmentString := stringPathSegment(segment)
- for _, atom := range values {
- out.atomOut(segmentString, atom)
- }
-}
-
-func (out *JSONOut) AssertDone() {
- out.writer.Flush()
- if len(out.structure) != 2 || out.structure[0] != JSONOutRoot || out.structure[1] != JSONOutValueEnd {
- panic("Program ended with incomplete JSON output")
- }
-}
-
-func NewJSONOut(writer *bufio.Writer) JSONOut {
- return JSONOut {
- structure: []JSONOutStructure{JSONOutRoot},
- writer: writer,
- }
-}
-
func ConcatData(first []Atom, second []Atom) []Atom {
res := make([]Atom, 0, len(first) + len(second))
res = append(res, first...)