diff options
author | Adam Scarr <adam@vektah.net> | 2017-08-10 21:04:14 +1000 |
---|---|---|
committer | Adam Scarr <adam@vektah.net> | 2017-08-10 21:08:18 +1000 |
commit | af542eff9e1e51561a9efa37685ee07b1d01b53e (patch) | |
tree | c700bdca58b4f9bbeb89ba1b102b2a779c88f7f3 /state.go | |
parent | b64fcfaa6115eb4a5f65c12d37f95d842136ae35 (diff) |
Add parse logging
Diffstat (limited to 'state.go')
-rw-r--r-- | state.go | 14 |
1 files changed, 14 insertions, 0 deletions
@@ -25,6 +25,8 @@ type State struct { Input string // An offset into the string, pointing to the current tip Pos int + // Do not backtrack past this point + Cut int // Error is a secondary return channel from parsers, but used so heavily // in backtracking that it has been inlined to avoid allocations. Error Error @@ -88,6 +90,18 @@ func (s *State) Get() string { return s.Input[s.Pos:] } +// Preview of the the next x characters +func (s *State) Preview(x int) string { + if s.Pos > len(s.Input) { + return "" + } + if len(s.Input)-s.Pos >= x { + return s.Input[s.Pos : s.Pos+x] + } + + return s.Input[s.Pos:] +} + // ErrorHere raises an error at the current position. func (s *State) ErrorHere(expected string) { s.Error.pos = s.Pos |