From 8cc75da1383530eaa2334c65b05588c2be05be59 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sun, 3 Sep 2023 17:41:51 +0000 Subject: [PATCH 1/2] parser: introduce GetPositionalLength() Signed-off-by: Alejandro Mery --- parser/lexer_runes.go | 5 +++++ parser/text_position.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 parser/text_position.go diff --git a/parser/lexer_runes.go b/parser/lexer_runes.go index 950f84c..1d8d080 100644 --- a/parser/lexer_runes.go +++ b/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) diff --git a/parser/text_position.go b/parser/text_position.go new file mode 100644 index 0000000..d01c132 --- /dev/null +++ b/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 +} From baf2a97379ec24197fc83ddc6ddbd8b2ce285180 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sun, 3 Sep 2023 17:43:10 +0000 Subject: [PATCH 2/2] parser: use GetPositionalLength() on TextParser.Discard() and TextParser.Emit() Signed-off-by: Alejandro Mery --- parser/text.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/parser/text.go b/parser/text.go index 84454eb..b1bf0ae 100644 --- a/parser/text.go +++ b/parser/text.go @@ -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 }