parser: introduce generic-ish TextParser
Signed-off-by: Alejandro Mery <amery@jpi.io>
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"asciigoat.org/core/lexer"
|
||||
)
|
||||
|
||||
// TextParser is a generic text parser.
|
||||
type TextParser struct {
|
||||
*lexer.Reader
|
||||
pos lexer.Position
|
||||
}
|
||||
|
||||
// Init initializes the [TextParser] with a non-nil [io.Reader].
|
||||
func (p *TextParser) Init(r io.Reader) {
|
||||
switch {
|
||||
case p == nil || r == nil:
|
||||
panic("invalid call")
|
||||
case p.Reader != nil:
|
||||
panic("parser already initialized")
|
||||
default:
|
||||
p.Reader = lexer.NewReader(r)
|
||||
p.pos.Reset()
|
||||
}
|
||||
}
|
||||
|
||||
// InitBytes initializes the [TextParser] with a byte array
|
||||
func (p *TextParser) InitBytes(b []byte) {
|
||||
p.Init(bytes.NewReader(b))
|
||||
}
|
||||
|
||||
// InitString initializes the [TextParser] with a byte array
|
||||
func (p *TextParser) InitString(s string) {
|
||||
p.Init(strings.NewReader(s))
|
||||
}
|
||||
|
||||
// Discard shadows [lexer.Reader]'s, and takes in consideration
|
||||
// new lines on the discarded data when moving the position
|
||||
func (*TextParser) Discard() {
|
||||
// TODO: consider new lines
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
// Emit returns the accepted text, its position, and
|
||||
// moves the cursor position accordingly
|
||||
func (p *TextParser) Emit() (lexer.Position, string) {
|
||||
pos := p.pos
|
||||
s := p.Reader.Emit()
|
||||
// TODO: consider new lines
|
||||
p.pos.StepN(len(s))
|
||||
|
||||
return pos, s
|
||||
}
|
||||
|
||||
// Step discards what's been accepted and increments the
|
||||
// position assuming they all increment the column counter
|
||||
func (p *TextParser) Step() {
|
||||
s := p.Reader.Emit()
|
||||
p.pos.StepN(len(s))
|
||||
}
|
||||
|
||||
// StepLine discards what's been accepted and moves then
|
||||
// position to the beginning of the next line
|
||||
func (p *TextParser) StepLine() {
|
||||
p.Reader.Discard()
|
||||
p.pos.StepLine()
|
||||
}
|
||||
|
||||
// Position returns the position of the first character
|
||||
// of the accepted text
|
||||
func (p *TextParser) Position() lexer.Position {
|
||||
return p.pos
|
||||
}
|
||||
Reference in New Issue
Block a user