From 3bd45dc49a35b82dcc4ae93796c3e152d799bc0b Mon Sep 17 00:00:00 2001 From: Charlie Stanton Date: Sun, 19 Feb 2023 09:27:55 +0000 Subject: Move JSON serialising, deserialising and walking code into a separate package --- main/parse.go | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) (limited to 'main/parse.go') diff --git a/main/parse.go b/main/parse.go index 5466a02..73c7913 100644 --- a/main/parse.go +++ b/main/parse.go @@ -4,6 +4,7 @@ import ( "strings" "strconv" "fmt" + "main/walk" ) type parser struct { @@ -130,9 +131,9 @@ func (p *parser) parseFilter(minPower int) Filter { return lhs } -func (p *parser) parseLiterals() (items []WalkItem) { - var path Path - var value WalkValue +func (p *parser) parseLiterals() (items []walk.WalkItem) { + var path walk.Path + var value walk.WalkValue loop: for { token := p.next() switch token.typ { @@ -141,17 +142,17 @@ func (p *parser) parseLiterals() (items []WalkItem) { break loop case TokenComma: case TokenNullLiteral: - value = ValueNull{} + value = walk.ValueNull{} case TokenTrueLiteral: - value = ValueBool(true) + value = walk.ValueBool(true) case TokenFalseLiteral: - value = ValueBool(false) + value = walk.ValueBool(false) case TokenNumberLiteral: numberLiteral, err := strconv.ParseFloat(token.val, 64) if err != nil { panic("Error parsing number literal to float64") } - value = ValueNumber(numberLiteral) + value = walk.ValueNumber(numberLiteral) case TokenDoubleQuote: stringToken := p.next() if stringToken.typ != TokenStringLiteral { @@ -167,29 +168,29 @@ func (p *parser) parseLiterals() (items []WalkItem) { if path != nil { panic("Expected value after path:") } - path = Path{stringLiteral} + path = walk.Path{stringLiteral} } else { p.rewind(colon) - value = ValueString(stringLiteral) + value = walk.ValueString(stringLiteral) } case TokenTerminalLiteral: switch token.val { case "{": - value = MapBegin + value = walk.MapBegin case "}": - value = MapEnd + value = walk.MapEnd case "[": - value = ArrayBegin + value = walk.ArrayBegin case "]": - value = ArrayEnd + value = walk.ArrayEnd default: panic("Invalid terminal token") } } if value != nil { - items = append(items, WalkItem { - path: path, - value: value, + items = append(items, walk.WalkItem { + Path: path, + Value: value, }) path = nil value = nil -- cgit v1.2.3