1
0
Fork 0

Parse expectations

This commit is contained in:
Gregory Eremin 2015-02-16 20:36:23 +07:00
parent def30c216c
commit b6eceb5dcd
1 changed files with 52 additions and 5 deletions

View File

@ -1,19 +1,66 @@
package parser package parser
import ( import (
"fmt"
"strconv"
"strings"
"github.com/localhots/punk/lexer" "github.com/localhots/punk/lexer"
) )
type ( type (
ExpectationType int
Expectation struct {
Type ExpectationType
Key string
Index int64
}
Parser struct { Parser struct {
lex *lexer.Lexer exps []Expectation
selector string
} }
) )
func New(lex *lexer.Lexer, selector string) { const (
Object ExpectationType = iota
Array
)
func New(selector string) *Parser {
return &Parser{ return &Parser{
lex: lex, exps: parseSelector(selector),
selector: 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
}