summaryrefslogtreecommitdiff
path: root/state.go
diff options
context:
space:
mode:
authorAdam Scarr <adam@vektah.net>2017-08-07 21:20:30 +1000
committerAdam Scarr <adam@vektah.net>2017-08-07 21:22:54 +1000
commita656dc0d78c5f51a16dc4c26936d337cdae5105c (patch)
treeabd40ad9b8c6218d61c5dab230ef3712a0fef0b3 /state.go
parentcc9d18219af9375ad89eaa8a23f1e0bcffa5734e (diff)
AutoWS
Diffstat (limited to 'state.go')
-rw-r--r--state.go35
1 files changed, 30 insertions, 5 deletions
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"}
}