Compare commits
2 Commits
8557ef9a7e
..
v0.2.2
| Author | SHA1 | Date | |
|---|---|---|---|
| 79fc27f965 | |||
| 11c5ce70a6 |
+14
-12
@@ -10,22 +10,24 @@ var (
|
||||
errInvalidToken = errors.New("invalid token")
|
||||
)
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
func newErrInvalidToken(t *token) *lexer.Error {
|
||||
return newError(t.pos, t.value, "", errInvalidToken)
|
||||
err := &lexer.Error{
|
||||
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 {
|
||||
err = newError(pos, content, "", err)
|
||||
err = &lexer.Error{
|
||||
Line: pos.Line,
|
||||
Column: pos.Column,
|
||||
Content: content,
|
||||
Err: err,
|
||||
}
|
||||
|
||||
dec.executeFinal()
|
||||
return err
|
||||
}
|
||||
|
||||
+5
-13
@@ -29,7 +29,7 @@ func (dec *decoder) executeFinal() {
|
||||
}
|
||||
}
|
||||
|
||||
func (dec *decoder) execute(typ parser.TokenType) error {
|
||||
func (dec *decoder) execute(typ parser.TokenType) {
|
||||
switch typ {
|
||||
case parser.TokenSectionEnd:
|
||||
name1, ok1 := dec.getValue(1, parser.TokenSectionName)
|
||||
@@ -48,8 +48,6 @@ func (dec *decoder) execute(typ parser.TokenType) error {
|
||||
dec.addField(key, value)
|
||||
dec.reset()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dec *decoder) addSection(key, id string, allowEmptyID bool) {
|
||||
@@ -138,25 +136,19 @@ func (dec *decoder) typeOK(typ parser.TokenType) bool {
|
||||
}
|
||||
|
||||
func (dec *decoder) OnToken(pos lexer.Position, typ parser.TokenType, value string) error {
|
||||
var err error
|
||||
t := &token{pos, typ, value}
|
||||
|
||||
switch {
|
||||
case typ == parser.TokenComment:
|
||||
// ignore comments
|
||||
return nil
|
||||
case dec.typeOK(typ):
|
||||
// acceptable token
|
||||
dec.queue = append(dec.queue, t)
|
||||
err = dec.execute(typ)
|
||||
dec.execute(typ)
|
||||
return nil
|
||||
default:
|
||||
// unacceptable
|
||||
err = newErrInvalidToken(t)
|
||||
return newErrInvalidToken(t)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
dec.executeFinal()
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
-72
@@ -1,72 +0,0 @@
|
||||
package ini
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"asciigoat.org/core"
|
||||
"asciigoat.org/core/reflection"
|
||||
"asciigoat.org/ini/parser"
|
||||
)
|
||||
|
||||
// Decoder ...
|
||||
type Decoder[T any] struct {
|
||||
io.Closer
|
||||
|
||||
out *reflection.Reflection[T]
|
||||
p *parser.Parser
|
||||
queue []*token
|
||||
}
|
||||
|
||||
// Decode ...
|
||||
func (dec *Decoder[T]) Decode(v *T) error {
|
||||
defer dec.Close()
|
||||
|
||||
r, err := reflection.New(v)
|
||||
switch e := err.(type) {
|
||||
case *reflection.InvalidUnmarshalError:
|
||||
// customize error
|
||||
e.Prefix = "ini"
|
||||
e.Method = "Decode"
|
||||
case nil:
|
||||
// good reflection. Go!
|
||||
dec.out = r
|
||||
err = dec.p.Run()
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// NewDecoder creates a Decoder using the provided [io.Reader]
|
||||
// as source
|
||||
func NewDecoder[T any](r io.Reader) *Decoder[T] {
|
||||
rc := core.NewReadCloser(r)
|
||||
switch {
|
||||
case rc == nil:
|
||||
return nil
|
||||
default:
|
||||
dec := &Decoder[T]{
|
||||
p: parser.NewParser(rc),
|
||||
Closer: rc,
|
||||
}
|
||||
dec.init()
|
||||
return dec
|
||||
}
|
||||
}
|
||||
|
||||
func (dec *Decoder[T]) init() {
|
||||
dec.p.OnToken = dec.parserOnToken
|
||||
dec.p.OnError = dec.parserOnError
|
||||
}
|
||||
|
||||
// NewDecoderBytes creates a Decoder using the provided bytes array
|
||||
// as source
|
||||
func NewDecoderBytes[T any](b []byte) *Decoder[T] {
|
||||
return NewDecoder[T](bytes.NewReader(b))
|
||||
}
|
||||
|
||||
// NewDecoderString creates a Decoder over a provided string of data
|
||||
func NewDecoderString[T any](s string) *Decoder[T] {
|
||||
return NewDecoder[T](strings.NewReader(s))
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package ini
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
|
||||
"asciigoat.org/core/lexer"
|
||||
)
|
||||
|
||||
var (
|
||||
errInvalidToken = errors.New("invalid token")
|
||||
)
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
func (*Decoder[T]) newErrInvalidToken(t *token) *lexer.Error {
|
||||
return newError(t.pos, t.value, "", errInvalidToken)
|
||||
}
|
||||
|
||||
// parserOnError is the callback for lexer errors
|
||||
func (dec *Decoder[T]) parserOnError(pos lexer.Position, content string, err error) error {
|
||||
log.Printf("%s: %s %s: %q: %v", "ini", pos, "error:", content, err)
|
||||
|
||||
dec.executeFinal()
|
||||
return newError(pos, content, "", err)
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
package ini
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"asciigoat.org/core/lexer"
|
||||
"asciigoat.org/ini/parser"
|
||||
)
|
||||
|
||||
type token struct {
|
||||
pos lexer.Position
|
||||
typ parser.TokenType
|
||||
value string
|
||||
}
|
||||
|
||||
func (t token) String() string {
|
||||
return fmt.Sprintf("%s %s: %q", t.pos, t.typ, t.value)
|
||||
}
|
||||
|
||||
// typeOK tells if a token of the specified type is acceptable
|
||||
// at this time.
|
||||
func (dec *Decoder[T]) typeOK(typ parser.TokenType) bool
|
||||
|
||||
// execute is called after each acceptable token is appended to the queue
|
||||
func (dec *Decoder[T]) execute() error
|
||||
|
||||
// executeFinal is called after an error
|
||||
func (dec *Decoder[T]) executeFinal()
|
||||
|
||||
// parserOnToken is the callback from the parser
|
||||
func (dec *Decoder[T]) parserOnToken(pos lexer.Position, typ parser.TokenType, value string) error {
|
||||
var err error
|
||||
|
||||
log.Printf("%s: %s %s %q", "ini", pos, typ, value)
|
||||
|
||||
t := &token{pos, typ, value}
|
||||
switch {
|
||||
case typ == parser.TokenComment:
|
||||
// ignore comments
|
||||
case dec.typeOK(typ):
|
||||
// acceptable token
|
||||
dec.queue = append(dec.queue, t)
|
||||
err = dec.execute()
|
||||
default:
|
||||
// unacceptable
|
||||
err = dec.newErrInvalidToken(t)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
dec.executeFinal()
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -2,8 +2,6 @@ module asciigoat.org/ini
|
||||
|
||||
go 1.19
|
||||
|
||||
replace asciigoat.org/core => ../core
|
||||
|
||||
require (
|
||||
asciigoat.org/core v0.3.7
|
||||
github.com/mgechev/revive v1.3.3
|
||||
@@ -12,16 +10,17 @@ require (
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.3.2 // indirect
|
||||
github.com/chavacava/garif v0.0.0-20230608123814-4bd63c2919ab // indirect
|
||||
github.com/chavacava/garif v0.1.0 // indirect
|
||||
github.com/fatih/color v1.15.0 // indirect
|
||||
github.com/fatih/structtag v1.2.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.19 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.9 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.15 // indirect
|
||||
github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517 // indirect
|
||||
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
||||
github.com/olekukonko/tablewriter v0.0.5 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/rivo/uniseg v0.4.4 // indirect
|
||||
golang.org/x/mod v0.12.0 // indirect
|
||||
golang.org/x/sys v0.11.0 // indirect
|
||||
)
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
asciigoat.org/core v0.3.7 h1:tMasdvZgsMJJMVsZVfXXB5lqq82pFiCsyEmOEmcmAfI=
|
||||
asciigoat.org/core v0.3.7/go.mod h1:tXj+JUutxRbcO40ZQRuUVaZ4rnYz1kAZ0nblisV8u74=
|
||||
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
|
||||
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/chavacava/garif v0.0.0-20230608123814-4bd63c2919ab h1:5JxePczlyGAtj6R1MUEFZ/UFud6FfsOejq7xLC2ZIb0=
|
||||
github.com/chavacava/garif v0.0.0-20230608123814-4bd63c2919ab/go.mod h1:XMyYCkEL58DF0oyW4qDjjnPWONs2HBqYKI+UIPD+Gww=
|
||||
github.com/chavacava/garif v0.1.0 h1:2JHa3hbYf5D9dsgseMKAmc/MZ109otzgNFk5s87H9Pc=
|
||||
github.com/chavacava/garif v0.1.0/go.mod h1:XMyYCkEL58DF0oyW4qDjjnPWONs2HBqYKI+UIPD+Gww=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
@@ -14,8 +16,9 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
|
||||
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
|
||||
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
||||
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
|
||||
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||
github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517 h1:zpIH83+oKzcpryru8ceC6BxnoG8TBrhgAvRg8obzup0=
|
||||
github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517/go.mod h1:KQ7+USdGKfpPjXk4Ga+5XxQM4Lm4e3gAogrreFAYpOg=
|
||||
github.com/mgechev/revive v1.3.3 h1:GUWzV3g185agbHN4ZdaQvR6zrLVYTUSA2ktvIinivK0=
|
||||
@@ -28,6 +31,9 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
|
||||
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
|
||||
Reference in New Issue
Block a user