Browse Source

parser: introduce GetPositionalLength()

Signed-off-by: Alejandro Mery <amery@jpi.io>
pull/7/head
Alejandro Mery 1 year ago
parent
commit
8cc75da138
  1. 5
      parser/lexer_runes.go
  2. 38
      parser/text_position.go

5
parser/lexer_runes.go

@ -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)

38
parser/text_position.go

@ -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
}
Loading…
Cancel
Save