From a656dc0d78c5f51a16dc4c26936d337cdae5105c Mon Sep 17 00:00:00 2001 From: Adam Scarr Date: Mon, 7 Aug 2017 21:20:30 +1000 Subject: AutoWS --- state.go | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) (limited to 'state.go') diff --git a/state.go b/state.go index e7a5fe4..99e6021 100644 --- a/state.go +++ b/state.go @@ -1,6 +1,10 @@ package goparsify -import "fmt" +import ( + "fmt" + "strings" + "unicode/utf8" +) type Error struct { pos int @@ -11,15 +15,36 @@ func (e Error) Pos() int { return e.pos } func (e Error) Error() string { return fmt.Sprintf("offset %d: Expected %s", e.pos, e.Expected) } type State struct { - Input string - Pos int - Error Error + Input string + Pos int + Error Error + WSChars string + NoAutoWS bool } func (s *State) Advance(i int) { s.Pos += i } +// AutoWS consumes all whitespace +func (s *State) AutoWS() { + if s.NoAutoWS { + return + } + s.WS() +} + +func (s *State) WS() { + for s.Pos < len(s.Input) { + r, w := utf8.DecodeRuneInString(s.Input[s.Pos:]) + if !strings.ContainsRune(s.WSChars, r) { + return + } + s.Pos += w + + } +} + func (s *State) Get() string { if s.Pos > len(s.Input) { return "" @@ -41,5 +66,5 @@ func (s *State) Errored() bool { } func InputString(input string) *State { - return &State{Input: input} + return &State{Input: input, WSChars: "\t\n\v\f\r \x85\xA0"} } -- cgit v1.2.3