diff options
author | Adam Scarr <adam@vektah.net> | 2017-08-06 23:32:10 +1000 |
---|---|---|
committer | Adam Scarr <adam@vektah.net> | 2017-08-06 23:32:10 +1000 |
commit | 666ea93dba377f267a2c8ecf97378a420db18383 (patch) | |
tree | 5efee0b6e4ccf44b854d9bb65e4e6fa5e7f86548 /state.go | |
parent | 9d7779e8ca5404f26abbd8cce0314d9cee967bba (diff) |
Eliminate a bunch of allocations
Diffstat (limited to 'state.go')
-rw-r--r-- | state.go | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/state.go b/state.go new file mode 100644 index 0000000..f8df3f1 --- /dev/null +++ b/state.go @@ -0,0 +1,45 @@ +package parsec + +import "fmt" + +type Error struct { + pos int + Expected string +} + +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 +} + +func (s *State) Advance(i int) { + s.Pos += i +} + +func (s *State) Get() string { + if s.Pos > len(s.Input) { + return "" + } + return s.Input[s.Pos:] +} + +func (s *State) ErrorHere(expected string) { + s.Error.pos = s.Pos + s.Error.Expected = expected +} + +func (s *State) ClearError() { + s.Error.Expected = "" +} + +func (s *State) Errored() bool { + return s.Error.Expected != "" +} + +func InputString(input string) *State { + return &State{Input: input} +} |