diff options
author | Silvan Jegen <s.jegen@gmail.com> | 2016-12-28 19:48:53 +0100 |
---|---|---|
committer | Silvan Jegen <s.jegen@gmail.com> | 2016-12-28 20:05:44 +0100 |
commit | e2fdd3b1b714b9fa896218c68fdfe57d906730ba (patch) | |
tree | b649bdb62664b43884bf51c093f8a602d29d31b4 /conf | |
parent | d700b7019e8fc380ee5ca454f3aa3edd36f30faf (diff) |
Implement Peek()
Diffstat (limited to 'conf')
-rw-r--r-- | conf/scanner.go | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/conf/scanner.go b/conf/scanner.go index 44301fe..74e66e8 100644 --- a/conf/scanner.go +++ b/conf/scanner.go @@ -33,6 +33,8 @@ type scanner struct { data []byte offset int curline int + + peeked *token } func newScanner(r io.Reader) *scanner { @@ -55,9 +57,9 @@ func getTokenType(s []byte) tokentype { switch s[0] { case '"', '\'': return Literal - case '[': + case '[', ']': return ListDelimiter - case '{': + case '{', '}': return ObjectDelimiter } @@ -68,6 +70,11 @@ 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 { @@ -108,6 +115,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 } |