Browse Source

parser.TextParser: AcceptNewLine() and AcceptRune()

Signed-off-by: Alejandro Mery <amery@jpi.io>
pull/7/head
Alejandro Mery 1 year ago
parent
commit
e34e8eda0a
  1. 26
      parser/text.go

26
parser/text.go

@ -74,3 +74,29 @@ func (p *TextParser) StepLine() {
func (p *TextParser) Position() lexer.Position { func (p *TextParser) Position() lexer.Position {
return p.pos 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
})
}

Loading…
Cancel
Save