9 Commits

Author SHA1 Message Date
amery 78d20c1359 WIP
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-30 23:50:34 +01:00
amery 8907575b1d Unmarshal: WIP
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-30 20:55:02 +00:00
amery 5620f484ce Decoder: WIP
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-30 20:55:02 +00:00
amery 460ecc6865 basic: WIP
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-30 20:55:02 +00:00
amery 62328d9e43 build-sys: use local asciigoat.org/core [DO-NOT-MERGE]
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-30 20:54:44 +00:00
amery 5be0785a55 parser: implement initial tokeniser
only logging position, errors and non-whitespace elements

Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-30 20:54:21 +00:00
amery 35b9d56b3d parser: add internal []Token queue to the Parser
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-30 20:54:21 +00:00
amery 604ecfaed2 parser: introduce Token and TokenType enum
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-30 20:54:21 +00:00
amery 5288cd4537 parser: add placeholder for ini Parser
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-30 20:47:12 +00:00
13 changed files with 266 additions and 20 deletions
+2
View File
@@ -0,0 +1,2 @@
// Package basic provides a basic representation of dosini-style documents
package basic
+23
View File
@@ -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
}
+48
View File
@@ -0,0 +1,48 @@
package ini
import (
"bytes"
"io"
"asciigoat.org/core"
"asciigoat.org/ini/parser"
)
// Decoder ...
type Decoder struct {
io.Closer
p *parser.Parser
}
// Decode ...
func (dec *Decoder) Decode() error {
defer dec.Close()
return dec.p.Run()
}
// NewDecoder creates a Decoder over the provided [io.Reader]
func NewDecoder(r io.Reader) *Decoder {
rc := core.NewReadCloser(r)
switch {
case rc == nil:
return nil
default:
dec := &Decoder{
p: parser.NewParser(rc),
Closer: rc,
}
return dec
}
}
// NewDecoderBytes creates a Decoder over a provided bytes array
func NewDecoderBytes(b []byte) *Decoder {
return NewDecoder(bytes.NewBuffer(b))
}
// NewDecoderString creates a Decoder over a provided string of data
func NewDecoderString(s string) *Decoder {
return NewDecoder(bytes.NewBufferString(s))
}
+2 -1
View File
@@ -7,6 +7,7 @@ replace asciigoat.org/core => ../core
require (
asciigoat.org/core v0.3.6
github.com/mgechev/revive v1.3.3
golang.org/x/tools v0.12.0
)
require (
@@ -21,6 +22,6 @@ require (
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
golang.org/x/mod v0.12.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/tools v0.12.0 // indirect
)
+2
View File
@@ -36,6 +36,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
+34 -14
View File
@@ -1,16 +1,14 @@
package parser
import (
"log"
"asciigoat.org/core/lexer"
)
// Run parses the source
func (p *Parser) Run() error {
p.setDefaults()
p.pos.Reset()
return lexer.Run(p.lexStart)
}
@@ -24,20 +22,17 @@ func (p *Parser) lexStart() (lexer.StateFn, error) {
case IsNewLine(r):
// new line
p.lexNewLine(r)
p.src.Discard()
p.pos.StepLine()
p.stepLine()
case IsSpace(r):
// whitespace
p.src.Discard()
p.pos.Step()
p.stepRune()
case IsCommentStart(r):
// switch to comment lexer
p.src.UnreadRune()
return p.lexComment, nil
case IsSectionStart(r):
// section
p.src.Discard()
p.pos.Step()
p.stepRune()
return p.lexSectionStart, nil
default:
// entry
@@ -85,15 +80,40 @@ func (p *Parser) lexComment() (lexer.StateFn, error) {
return p.lexStart, err
}
func (*Parser) lexSectionStart() (lexer.StateFn, error) { return nil, nil }
func (*Parser) lexEntryStart() (lexer.StateFn, error) { return nil, nil }
func (p *Parser) lexSectionStart() (lexer.StateFn, error) {
// remove whitespace between `[` and the name
if p.src.AcceptAll(IsSpaceNotNewLine) {
p.stepString()
}
if !p.src.AcceptAll(IsName) {
// no name
return p.emitError("section name missing", lexer.ErrUnacceptableRune)
}
p.pushString(TokenSectionName)
var s0, s1 string
// name starts
start := p.pos
s0 = p.src.String()
if p.src.AcceptAll(IsSpaceNotNewLine) {
s1 = p.src.String()
}
} else {
}
return nil, nil
}
func (*Parser) lexEntryStart() (lexer.StateFn, error) { return nil, nil }
func (p *Parser) lexToken() (lexer.StateFn, error) {
p.src.AcceptAll(IsNotSpace)
s := p.src.Emit()
log.Printf("%s: %s: %q", p.pos, "token", s)
p.pos.StepN(len(s))
p.pushString(TokenUnknown)
return p.lexStart, nil
}
+44
View File
@@ -0,0 +1,44 @@
package parser
import "log"
func (p *Parser) push(tok Token) {
n := len(p.queue)
p.queue = append(p.queue, tok)
log.Printf("queue[%v]: %s", n, tok)
}
func (p *Parser) pushString(typ TokenType) {
s := p.src.Emit()
el := Token{
Type: typ,
Value: s,
Position: p.pos,
}
p.pos.StepN(len(s))
p.push(el)
}
// stepLine discards the data and moves the position
// to the next line
func (p *Parser) stepLine() {
p.src.Discard()
p.pos.StepLine()
}
// stepRune discards the data and moves the position
// on rune forward on the same line
func (p *Parser) stepRune() {
p.src.Discard()
p.pos.Step()
}
func (p *Parser) stepString() string {
s := p.src.Emit()
p.pos.StepN(len(s))
return s
}
+24 -4
View File
@@ -1,20 +1,40 @@
package parser
import "asciigoat.org/core/lexer"
import (
"strings"
"asciigoat.org/core/lexer"
)
var (
// IsNewLine tells if a rune represents a line break or the start of one
IsNewLine = lexer.NewIsIn("\n\r")
// IsSpace tells if a rune is considered whitespace by unicode
IsSpace = lexer.IsSpace
// IsNotNewLine tells if a rune is anything other than line breaks
IsNotNewLine = lexer.NewIsNot(IsNewLine)
// IsNotSpace tells if a rune is anything other than whitespace
IsNotSpace = lexer.NewIsNot(IsSpace)
IsNotSpace = lexer.NewIsNot(lexer.IsSpace)
// IsSpace tells if a rune is considered whitespace by unicode
IsSpace = lexer.IsSpace
IsCommentStart = lexer.NewIsIn(";#")
)
// IsSpaceNotNewLine indicates a rune is whitespace but not a new line
func IsSpaceNotNewLine(r rune) bool {
return IsSpace(r) && !IsNewLine(r)
}
func IsSectionStart(r rune) bool {
return r == '['
}
func IsName(r rune) bool {
switch {
case IsSpace(r):
return false
case strings.ContainsRune("=\"'[];#", r):
return false
default:
return true
}
}
+2 -1
View File
@@ -11,7 +11,8 @@ import (
type Parser struct {
src *lexer.Reader
pos lexer.Position
pos lexer.Position
queue []Token
// OnSection is called after a [section] is parsed.
// Returning an error will abort the process.
+41
View File
@@ -0,0 +1,41 @@
package parser
//go:generate go run golang.org/x/tools/cmd/stringer -type=TokenType
import (
"fmt"
"asciigoat.org/core/lexer"
)
// A TokenType is a type of Token
type TokenType uint
const (
// TokenUnknown represents a Token that hasn't been identified
TokenUnknown TokenType = iota
// TokenSectionName represents the section name between the squared brackets
TokenSectionName
// TokenSectionSubname represents a secondary name in the section represented
// between quotes after the section name.
// e.g.
// [section_name "section_subname"]
TokenSectionSubname
// TokenComment represents a comment, including the initial ';' or '#' until
// the end of the line
TokenComment
// TokenFieldKey represents a field name in a `key = value` entry
TokenFieldKey
// TokenFieldValue represents a field value in a `key = value` entry
TokenFieldValue
)
// A Token is an element from the document
type Token struct {
Type TokenType
Position lexer.Position
Value string
}
func (t Token) String() string {
return fmt.Sprintf("%s:%v:%v: %q", t.Type, t.Position.Line, t.Position.Column, t.Value)
}
+28
View File
@@ -0,0 +1,28 @@
// Code generated by "stringer -type=TokenType"; DO NOT EDIT.
package parser
import "strconv"
func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[TokenUnknown-0]
_ = x[TokenSectionName-1]
_ = x[TokenSectionSubname-2]
_ = x[TokenComment-3]
_ = x[TokenFieldKey-4]
_ = x[TokenFieldValue-5]
}
const _TokenType_name = "TokenUnknownTokenSectionNameTokenSectionSubnameTokenCommentTokenFieldKeyTokenFieldValue"
var _TokenType_index = [...]uint8{0, 12, 28, 47, 59, 72, 87}
func (i TokenType) String() string {
if i >= TokenType(len(_TokenType_index)-1) {
return "TokenType(" + strconv.FormatInt(int64(i), 10) + ")"
}
return _TokenType_name[_TokenType_index[i]:_TokenType_index[i+1]]
}
+1
View File
@@ -4,4 +4,5 @@ package tools
import (
_ "github.com/mgechev/revive"
_ "golang.org/x/tools/cmd/stringer"
)
+15
View File
@@ -0,0 +1,15 @@
package ini
import "io"
// ReadInto ...
func ReadInto(v any, r io.Reader) error {
dec := NewDecoder(r)
return dec.Unmarshal(v)
}
// Unmarshal ...
func (dec *Decoder) Unmarshal(any) error {
return dec.p.Run()
}