asciigoat's core library
https://asciigoat.org/core
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
1.1 KiB
73 lines
1.1 KiB
1 year ago
|
package parser
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"io"
|
||
|
"strings"
|
||
|
|
||
|
"asciigoat.org/core/lexer"
|
||
|
)
|
||
|
|
||
|
// TextParser ...
|
||
|
type TextParser struct {
|
||
|
src *lexer.Reader
|
||
|
pos lexer.Position
|
||
|
}
|
||
|
|
||
|
// Position ...
|
||
|
func (p *TextParser) Position() lexer.Position {
|
||
|
return p.pos
|
||
|
}
|
||
|
|
||
|
// StepLine ...
|
||
|
func (p *TextParser) StepLine() {
|
||
|
p.src.Discard()
|
||
|
p.pos.StepLine()
|
||
|
}
|
||
|
|
||
|
// Step ...
|
||
|
func (p *TextParser) Step() {
|
||
|
s := p.src.Emit()
|
||
|
p.pos.StepN(len(s))
|
||
|
}
|
||
|
|
||
|
// Emit ...
|
||
|
func (p *TextParser) Emit() Token {
|
||
|
t := Token{
|
||
|
Value: p.src.Emit(),
|
||
|
Position: p.pos,
|
||
|
}
|
||
|
|
||
|
p.pos.StepN(t.Len())
|
||
|
return t
|
||
|
}
|
||
|
|
||
|
// Peek ...
|
||
|
func (p *TextParser) Peek() string {
|
||
|
return p.src.String()
|
||
|
}
|
||
|
|
||
|
// NewTextParser ...
|
||
|
func NewTextParser(r io.Reader) *TextParser {
|
||
|
if r == nil {
|
||
|
r = strings.NewReader("")
|
||
|
}
|
||
|
|
||
|
p := &TextParser{
|
||
|
src: lexer.NewReader(r),
|
||
|
}
|
||
|
p.pos.Reset()
|
||
|
|
||
|
return p
|
||
|
}
|
||
|
|
||
|
// NewTextParserBytes ...
|
||
|
func NewTextParserBytes(b []byte) *TextParser {
|
||
|
return NewTextParser(bytes.NewReader(b))
|
||
|
}
|
||
|
|
||
|
// NewTextParserString ...
|
||
|
func NewTextParserString(s string) *TextParser {
|
||
|
return NewTextParser(strings.NewReader(s))
|
||
|
}
|