From 0120cf0cc65de103ef072787f69c2d28401cd6cf Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Mon, 28 Aug 2023 23:58:43 +0000 Subject: [PATCH] lexer: introduce Lexer wrapper for io.RuneScanner Signed-off-by: Alejandro Mery --- lexer/lexer.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/lexer/lexer.go b/lexer/lexer.go index 2ee64ef..39b0c8b 100644 --- a/lexer/lexer.go +++ b/lexer/lexer.go @@ -3,6 +3,35 @@ package lexer import "io" +// Lexer adds Accept and AcceptAll support to a +// [io.RuneScanner] +type Lexer struct { + io.RuneScanner +} + +// Accept consumes a rune from the source if it meets the condition. +// it returns true if the condition was met and false if it wasn't. +func (p Lexer) Accept(cond func(rune) bool) bool { + return Accept(p, cond) +} + +// AcceptAll consumes runes from the source as long as they meet the +// condition. it returns true if the condition was met for at least one rune, +// and false if it wasn't. +func (p Lexer) AcceptAll(cond func(rune) bool) bool { + return AcceptAll(p, cond) +} + +// NewLexer extends a [io.RuneScanner] with Accept()/AcceptAll() +// functionality +func NewLexer(src io.RuneScanner) *Lexer { + if src == nil { + return nil + } + + return &Lexer{src} +} + // StateFn is a State Function of the parser type StateFn func() (StateFn, error)