7 Commits

Author SHA1 Message Date
amery d8af7821e4 Merge pull request 'parser: introduce NewError() and ErrPlusPosition()' (#8)
Reviewed-on: #8
2023-09-04 19:33:24 +02:00
amery 8f3e59ec36 parser: introduce ErrPlusPosition to apply a position offset to a lexer.Error
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-04 15:25:20 +00:00
amery d316031c44 basic: cleanup using parser.NewError()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-04 15:25:20 +00:00
amery c3883cbb0d parser: introduce NewError() to create lexer.Error using lexer.Position
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-04 15:16:43 +00:00
amery 314c004efd Merge pull request 'parser: introduce TextParser and refactor Parser' (#7)
Reviewed-on: #7
2023-09-04 16:17:04 +02:00
amery 30a86e170b parser: use GetPositionalLength() on TextParser.Discard() and TextParser.Emit()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-04 13:32:27 +00:00
amery 8cc75da138 parser: introduce GetPositionalLength()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-03 17:43:46 +00:00
8 changed files with 83 additions and 29 deletions
+3 -14
View File
@@ -4,6 +4,7 @@ import (
"errors" "errors"
"asciigoat.org/core/lexer" "asciigoat.org/core/lexer"
"asciigoat.org/ini/parser"
) )
var ( var (
@@ -11,23 +12,11 @@ var (
) )
func newErrInvalidToken(t *token) *lexer.Error { func newErrInvalidToken(t *token) *lexer.Error {
err := &lexer.Error{ return parser.NewError(t.pos, t.value, "", errInvalidToken)
Line: t.pos.Line,
Column: t.pos.Column,
Content: t.value,
Err: errInvalidToken,
}
return err
} }
func (dec *decoder) OnError(pos lexer.Position, content string, err error) error { func (dec *decoder) OnError(pos lexer.Position, content string, err error) error {
err = &lexer.Error{ err = parser.NewError(pos, content, "", err)
Line: pos.Line,
Column: pos.Column,
Content: content,
Err: err,
}
dec.executeFinal() dec.executeFinal()
return err return err
} }
+1 -1
View File
@@ -3,7 +3,7 @@ module asciigoat.org/ini
go 1.19 go 1.19
require ( require (
asciigoat.org/core v0.3.7 asciigoat.org/core v0.3.9
github.com/mgechev/revive v1.3.3 github.com/mgechev/revive v1.3.3
golang.org/x/tools v0.12.0 golang.org/x/tools v0.12.0
) )
+2 -2
View File
@@ -1,5 +1,5 @@
asciigoat.org/core v0.3.7 h1:tMasdvZgsMJJMVsZVfXXB5lqq82pFiCsyEmOEmcmAfI= asciigoat.org/core v0.3.9 h1:hgDDz4ecm3ZvehX++m8A/IzAt+B5oDPiRtxatzfUHPQ=
asciigoat.org/core v0.3.7/go.mod h1:tXj+JUutxRbcO40ZQRuUVaZ4rnYz1kAZ0nblisV8u74= asciigoat.org/core v0.3.9/go.mod h1:CAaHwyw8MpAq4a1MYtN2dxJrsK+hmIdW50OndaQZYPI=
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/chavacava/garif v0.1.0 h1:2JHa3hbYf5D9dsgseMKAmc/MZ109otzgNFk5s87H9Pc= github.com/chavacava/garif v0.1.0 h1:2JHa3hbYf5D9dsgseMKAmc/MZ109otzgNFk5s87H9Pc=
+27
View File
@@ -0,0 +1,27 @@
package parser
import (
"asciigoat.org/core/lexer"
)
// NewError creates a lexer.Error using a lexer.Position
func NewError(pos lexer.Position, content, hint string, err error) *lexer.Error {
return &lexer.Error{
Line: pos.Line,
Column: pos.Column,
Content: content,
Hint: hint,
Err: err,
}
}
// ErrPlusPosition returns a copy of the given [lexer.Error]
// offsetting the Line/Column information.
func ErrPlusPosition(pos lexer.Position, e *lexer.Error) *lexer.Error {
pos.Add(lexer.Position{
Line: e.Line,
Column: e.Column,
})
return NewError(pos, e.Content, e.Hint, e.Err)
}
+5
View File
@@ -43,6 +43,11 @@ var (
IsCommentStart = lexer.NewIsIn(RunesComment) 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 // IsSpaceNotNewLine indicates a rune is whitespace but not a new line
func IsSpaceNotNewLine(r rune) bool { func IsSpaceNotNewLine(r rune) bool {
return IsSpace(r) && !IsNewLine(r) return IsSpace(r) && !IsNewLine(r)
+1 -7
View File
@@ -31,13 +31,7 @@ func defaultOnToken(pos lexer.Position, typ TokenType, value string) error {
func defaultOnError(pos lexer.Position, content string, err error) error { func defaultOnError(pos lexer.Position, content string, err error) error {
log.Printf("%s:%v:%v: %q: %s", "error", pos.Line, pos.Column, content, err) log.Printf("%s:%v:%v: %q: %s", "error", pos.Line, pos.Column, content, err)
return lexer.Error{ return NewError(pos, content, "", err)
Line: pos.Line,
Column: pos.Column,
Content: content,
Err: err,
}
} }
func (p *Parser) setDefaults() { func (p *Parser) setDefaults() {
+6 -5
View File
@@ -39,9 +39,10 @@ func (p *TextParser) InitString(s string) {
// Discard shadows [lexer.Reader]'s, and takes in consideration // Discard shadows [lexer.Reader]'s, and takes in consideration
// new lines on the discarded data when moving the position // new lines on the discarded data when moving the position
func (*TextParser) Discard() { func (p *TextParser) Discard() {
// TODO: consider new lines s := p.Reader.Emit()
panic("not implemented") l := GetPositionalLength(s)
p.pos.Add(l)
} }
// Emit returns the accepted text, its position, and // Emit returns the accepted text, its position, and
@@ -49,8 +50,8 @@ func (*TextParser) Discard() {
func (p *TextParser) Emit() (lexer.Position, string) { func (p *TextParser) Emit() (lexer.Position, string) {
pos := p.pos pos := p.pos
s := p.Reader.Emit() s := p.Reader.Emit()
// TODO: consider new lines l := GetPositionalLength(s)
p.pos.StepN(len(s)) p.pos.Add(l)
return pos, s return pos, s
} }
+38
View File
@@ -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
}