parser: introduce TextParser and refactor Parser #7

Merged
amery merged 5 commits from pr-amery-textparser into main 2023-09-04 16:17:05 +02:00
Showing only changes of commit e34e8eda0a - Show all commits
+26
View File
@@ -74,3 +74,29 @@ func (p *TextParser) StepLine() {
func (p *TextParser) Position() lexer.Position {
return p.pos
}
// AcceptNewLine checks if next is a new line.
// It accepts "\n", "\n\r", "\r" and "\r\n".
func (p *TextParser) AcceptNewLine() bool {
r1, _, err := p.ReadRune()
switch {
case err != nil:
return false
case r1 == '\n':
p.AcceptRune('\r')
return true
case r1 == '\r':
p.AcceptRune('\n')
return true
default:
p.UnreadRune()
return false
}
}
// AcceptRune checks if next is the specified rune
func (p *TextParser) AcceptRune(r rune) bool {
return p.Accept(func(r2 rune) bool {
return r == r2
})
}