@@ -0,0 +1,2 @@
|
||||
// Package basic provides a basic representation of dosini-style documents
|
||||
package basic
|
||||
@@ -0,0 +1,73 @@
|
||||
package basic
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"io/fs"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"asciigoat.org/core/lexer"
|
||||
"asciigoat.org/ini/parser"
|
||||
)
|
||||
|
||||
type token struct {
|
||||
pos lexer.Position
|
||||
typ parser.TokenType
|
||||
value string
|
||||
}
|
||||
|
||||
type decoder struct {
|
||||
p *parser.Parser
|
||||
out *Document
|
||||
|
||||
queue []token
|
||||
curSection *Section
|
||||
curField *Field
|
||||
}
|
||||
|
||||
func (dec *decoder) OnToken(pos lexer.Position, typ parser.TokenType, value string) error {
|
||||
tok := token{pos, typ, value}
|
||||
|
||||
log.Printf("%s[%v]: %s %s: %q", "decoder", len(dec.queue), pos, typ, value)
|
||||
|
||||
dec.queue = append(dec.queue, tok)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (*decoder) OnError(pos lexer.Position, hint string, err error) error {
|
||||
log.Printf("%s: %s %s: %s: %v", "decoder", pos, "error", hint, err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Decode ...
|
||||
func Decode(r io.Reader) (*Document, error) {
|
||||
var out Document
|
||||
|
||||
if r == nil {
|
||||
return nil, fs.ErrNotExist
|
||||
}
|
||||
|
||||
// parser
|
||||
p := parser.NewParser(r)
|
||||
// decoder
|
||||
dec := decoder{p: p, out: &out}
|
||||
// glue
|
||||
p.OnToken = dec.OnToken
|
||||
p.OnError = dec.OnError
|
||||
|
||||
// Go!
|
||||
err := p.Run()
|
||||
return &out, err
|
||||
}
|
||||
|
||||
// DecodeBytes ...
|
||||
func DecodeBytes(b []byte) (*Document, error) {
|
||||
return Decode(bytes.NewReader(b))
|
||||
}
|
||||
|
||||
// DecodeString ...
|
||||
func DecodeString(s string) (*Document, error) {
|
||||
return Decode(strings.NewReader(s))
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package basic
|
||||
|
||||
// Document ...
|
||||
type Document struct {
|
||||
Global []Field
|
||||
|
||||
Sections []Section
|
||||
}
|
||||
|
||||
// Section ...
|
||||
type Section struct {
|
||||
Name string
|
||||
Key string
|
||||
HadKey bool
|
||||
|
||||
Fields []Field
|
||||
}
|
||||
|
||||
// Field ...
|
||||
type Field struct {
|
||||
Key string
|
||||
Value string
|
||||
}
|
||||
Reference in New Issue
Block a user