diff options
author | Adam Scarr <adam@vektah.net> | 2017-08-07 21:20:30 +1000 |
---|---|---|
committer | Adam Scarr <adam@vektah.net> | 2017-08-07 21:22:54 +1000 |
commit | a656dc0d78c5f51a16dc4c26936d337cdae5105c (patch) | |
tree | abd40ad9b8c6218d61c5dab230ef3712a0fef0b3 /state.go | |
parent | cc9d18219af9375ad89eaa8a23f1e0bcffa5734e (diff) |
AutoWS
Diffstat (limited to 'state.go')
-rw-r--r-- | state.go | 35 |
1 files changed, 30 insertions, 5 deletions
@@ -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"} } |