From b6eceb5dcd357082f5d91b74617138a5ad3fc416 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Mon, 16 Feb 2015 20:36:23 +0700 Subject: [PATCH] Parse expectations --- parser/parser.go | 57 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 5 deletions(-) diff --git a/parser/parser.go b/parser/parser.go index ceca450..a57a99d 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -1,19 +1,66 @@ package parser import ( + "fmt" + "strconv" + "strings" + "github.com/localhots/punk/lexer" ) type ( + ExpectationType int + Expectation struct { + Type ExpectationType + Key string + Index int64 + } Parser struct { - lex *lexer.Lexer - selector string + exps []Expectation } ) -func New(lex *lexer.Lexer, selector string) { +const ( + Object ExpectationType = iota + Array +) + +func New(selector string) *Parser { return &Parser{ - lex: lex, - selector: selector, + exps: parseSelector(selector), } } + +func (p *Parser) Parse(b []byte) { + lex := lexer.New(string(b)) + go lex.Run() + + for { + if item, ok := lex.NextItem(); ok { + fmt.Println(item) + } else { + break + } + } +} + +func parseSelector(sel string) []Expectation { + exps := []Expectation{} + parts := strings.Split(sel[1:], "/") + for _, part := range parts { + if len(part) > 2 && part[:1] == "[" && part[len(part)-1:] == "]" { + index, _ := strconv.ParseInt(part[1:len(part)-1], 10, 64) + exps = append(exps, Expectation{ + Type: Array, + Index: index, + }) + } else { + exps = append(exps, Expectation{ + Type: Object, + Key: part, + }) + } + } + + return exps +}