Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| da009aae4f |
+14
-12
@@ -10,22 +10,24 @@ var (
|
|||||||
errInvalidToken = errors.New("invalid token")
|
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 {
|
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 {
|
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()
|
dec.executeFinal()
|
||||||
return err
|
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 {
|
switch typ {
|
||||||
case parser.TokenSectionEnd:
|
case parser.TokenSectionEnd:
|
||||||
name1, ok1 := dec.getValue(1, parser.TokenSectionName)
|
name1, ok1 := dec.getValue(1, parser.TokenSectionName)
|
||||||
@@ -48,8 +48,6 @@ func (dec *decoder) execute(typ parser.TokenType) error {
|
|||||||
dec.addField(key, value)
|
dec.addField(key, value)
|
||||||
dec.reset()
|
dec.reset()
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dec *decoder) addSection(key, id string, allowEmptyID bool) {
|
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 {
|
func (dec *decoder) OnToken(pos lexer.Position, typ parser.TokenType, value string) error {
|
||||||
var err error
|
|
||||||
t := &token{pos, typ, value}
|
t := &token{pos, typ, value}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case typ == parser.TokenComment:
|
case typ == parser.TokenComment:
|
||||||
// ignore comments
|
// ignore comments
|
||||||
|
return nil
|
||||||
case dec.typeOK(typ):
|
case dec.typeOK(typ):
|
||||||
// acceptable token
|
// acceptable token
|
||||||
dec.queue = append(dec.queue, t)
|
dec.queue = append(dec.queue, t)
|
||||||
err = dec.execute(typ)
|
dec.execute(typ)
|
||||||
|
return nil
|
||||||
default:
|
default:
|
||||||
// unacceptable
|
// unacceptable
|
||||||
err = newErrInvalidToken(t)
|
return newErrInvalidToken(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
dec.executeFinal()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-3
@@ -14,9 +14,8 @@ import (
|
|||||||
type Decoder[T any] struct {
|
type Decoder[T any] struct {
|
||||||
io.Closer
|
io.Closer
|
||||||
|
|
||||||
out *reflection.Reflection[T]
|
out *reflection.Reflection[T]
|
||||||
p *parser.Parser
|
p *parser.Parser
|
||||||
queue []*token
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode ...
|
// Decode ...
|
||||||
|
|||||||
+2
-17
@@ -1,33 +1,18 @@
|
|||||||
package ini
|
package ini
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"asciigoat.org/core/lexer"
|
"asciigoat.org/core/lexer"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
func (*Decoder[T]) parserOnError(pos lexer.Position, content string, err error) error {
|
||||||
errInvalidToken = errors.New("invalid token")
|
log.Printf("%s: %s %s: %q: %v", "ini", pos, "error:", content, err)
|
||||||
)
|
|
||||||
|
|
||||||
func newError(pos lexer.Position, content, hint string, err error) *lexer.Error {
|
|
||||||
return &lexer.Error{
|
return &lexer.Error{
|
||||||
Line: pos.Line,
|
Line: pos.Line,
|
||||||
Column: pos.Column,
|
Column: pos.Column,
|
||||||
Content: content,
|
Content: content,
|
||||||
Hint: hint,
|
|
||||||
Err: err,
|
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
-43
@@ -1,56 +1,14 @@
|
|||||||
package ini
|
package ini
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"asciigoat.org/core/lexer"
|
"asciigoat.org/core/lexer"
|
||||||
"asciigoat.org/ini/parser"
|
"asciigoat.org/ini/parser"
|
||||||
)
|
)
|
||||||
|
|
||||||
type token struct {
|
func (*Decoder[T]) parserOnToken(pos lexer.Position, typ parser.TokenType, value string) error {
|
||||||
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)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user