Merge branch 'pr-amery-textparser' into next-amery
This commit is contained in:
@@ -43,6 +43,11 @@ var (
|
||||
IsCommentStart = lexer.NewIsIn(RunesComment)
|
||||
)
|
||||
|
||||
// IsAny accepts any rune
|
||||
func IsAny(_ rune) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// IsSpaceNotNewLine indicates a rune is whitespace but not a new line
|
||||
func IsSpaceNotNewLine(r rune) bool {
|
||||
return IsSpace(r) && !IsNewLine(r)
|
||||
|
||||
+6
-5
@@ -39,9 +39,10 @@ func (p *TextParser) InitString(s string) {
|
||||
|
||||
// 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")
|
||||
func (p *TextParser) Discard() {
|
||||
s := p.Reader.Emit()
|
||||
l := GetPositionalLength(s)
|
||||
p.pos.Add(l)
|
||||
}
|
||||
|
||||
// Emit returns the accepted text, its position, and
|
||||
@@ -49,8 +50,8 @@ func (*TextParser) Discard() {
|
||||
func (p *TextParser) Emit() (lexer.Position, string) {
|
||||
pos := p.pos
|
||||
s := p.Reader.Emit()
|
||||
// TODO: consider new lines
|
||||
p.pos.StepN(len(s))
|
||||
l := GetPositionalLength(s)
|
||||
p.pos.Add(l)
|
||||
|
||||
return pos, s
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"asciigoat.org/core/lexer"
|
||||
)
|
||||
|
||||
type positionLengthParser struct {
|
||||
TextParser
|
||||
|
||||
lexer.Position
|
||||
}
|
||||
|
||||
func (p *positionLengthParser) lexStart() (lexer.StateFn, error) {
|
||||
for {
|
||||
switch {
|
||||
case p.AcceptNewLine():
|
||||
p.Position.StepLine()
|
||||
case p.Accept(IsAny):
|
||||
p.Position.StepN(1)
|
||||
default:
|
||||
return nil, io.EOF
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GetPositionalLength calculates the [lexer.Position] at
|
||||
// the end of a text.
|
||||
func GetPositionalLength(s string) lexer.Position {
|
||||
var p positionLengthParser
|
||||
if s == "" {
|
||||
p.InitString(s)
|
||||
|
||||
_ = lexer.Run(p.lexStart)
|
||||
}
|
||||
return p.Position
|
||||
}
|
||||
Reference in New Issue
Block a user