From 3edf777c6804dcff30d2b25b234de6c05a35902d Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sun, 4 Jul 2021 00:40:22 +0100 Subject: [PATCH] lexer: add Lexer.AtLeast() to gather input data from the Feeder Signed-off-by: Alejandro Mery --- lexer/lexer.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lexer/lexer.go b/lexer/lexer.go index 8033769..e9bad1c 100644 --- a/lexer/lexer.go +++ b/lexer/lexer.go @@ -16,6 +16,8 @@ type Lexer interface { Position() TokenPosition // base for the next token Tokens() <-chan Token // tokens output + AtLeast(n int) ([]rune, error) + NewLine() Step(n int) @@ -51,6 +53,21 @@ func (lex *lexer) Run() { } } +func (lex *lexer) AtLeast(n int) ([]rune, error) { + min := lex.cursor + if n > 0 { + min += n + } + + s, err := lex.in.AtLeast(min) + if len(s) > lex.cursor { + s = s[lex.cursor:] + } else { + s = nil + } + return s, err +} + func (lex *lexer) Position() TokenPosition { return lex.pos }