diff options
author | Silvan Jegen <s.jegen@gmail.com> | 2017-01-18 18:44:12 +0100 |
---|---|---|
committer | Silvan Jegen <s.jegen@gmail.com> | 2017-01-18 18:44:12 +0100 |
commit | 7acce76accb4d30e9968248bc4062dcac22fb9e3 (patch) | |
tree | 75abc84fb659340044239a4114d3fa48afc08445 /conf/scanner.go | |
parent | 76e2175159ac2ff4b26d2606f4d8f1d7a896c5cc (diff) | |
parent | ca96c7fd5c43b99873a67eb239176bd7a9e762f0 (diff) |
Merge branch 'writeownscanner'
This is not working correctly yet.
Diffstat (limited to 'conf/scanner.go')
-rw-r--r-- | conf/scanner.go | 49 |
1 files changed, 41 insertions, 8 deletions
diff --git a/conf/scanner.go b/conf/scanner.go index 54546aa..975a8e4 100644 --- a/conf/scanner.go +++ b/conf/scanner.go @@ -14,15 +14,19 @@ type tokentype int const ( Name tokentype = iota Literal - DeliOpen - DeliClose + ListOpen + ListClose + ObjectOpen + ObjectClose + EmptyList + EmptyObject Nothing IfStatement ) var tokentypestrings []string = []string{Name: "Name", Literal: "Literal", - DeliOpen: "Opening delimiter", DeliClose: "Closing delimiter", - Nothing: "Nothing", IfStatement: "If statement"} + ListOpen: "List Open", ListClose: "List Close", ObjectOpen: "Object Open", + ObjectClose: "Object Close", Nothing: "Nothing", IfStatement: "If statement"} func (tt tokentype) String() string { return tokentypestrings[tt] @@ -39,6 +43,8 @@ type scanner struct { data []byte offset int curline int + + peeked *token } func newScanner(r io.Reader) *scanner { @@ -58,18 +64,36 @@ func newScanner(r io.Reader) *scanner { } func getTokenType(s []byte) tokentype { - return Nothing + switch s[0] { + case '"', '\'': + return Literal + case '[': + return ListOpen + case ']': + return ListClose + case '{': + return ObjectOpen + case '}': + return ObjectClose + } + + return Name } func (s *scanner) Scan() (token, error) { processed := 0 tokenstarted := false oldline := s.curline + if s.peeked != nil { + ret := *s.peeked + s.peeked = nil + return ret, nil + } for { r, rlen := utf8.DecodeRune(s.data[s.offset+processed:]) if r == utf8.RuneError { if rlen == 1 { - return token{}, fmt.Errorf("found invalid UTF8 at offset %d, before: %s", s.offset, s.data[s.offset]) + return token{}, fmt.Errorf("found invalid UTF8 at offset %d, before: %s", s.offset, string(s.data[s.offset])) } else if rlen == 0 { return token{}, io.EOF } @@ -105,6 +129,15 @@ func (s *scanner) Scan() (token, error) { return ret, nil } -func (s *scanner) peek() (tokentype, string, error) { - return Nothing, "", nil +func (s *scanner) Peek() (token, error) { + if s.peeked != nil { + return *s.peeked, nil + } + + tok, err := s.Scan() + if err != nil { + return token{}, err + } + s.peeked = &tok + return tok, nil } |