@@ -4,7 +4,10 @@ go 1.19
|
|||||||
|
|
||||||
replace asciigoat.org/core => ../core
|
replace asciigoat.org/core => ../core
|
||||||
|
|
||||||
require github.com/mgechev/revive v1.3.3
|
require (
|
||||||
|
asciigoat.org/core v0.3.0
|
||||||
|
github.com/mgechev/revive v1.3.3
|
||||||
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/BurntSushi/toml v1.3.2 // indirect
|
github.com/BurntSushi/toml v1.3.2 // indirect
|
||||||
|
|||||||
@@ -0,0 +1,68 @@
|
|||||||
|
// Package parser parses dosini-style files
|
||||||
|
package parser
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"asciigoat.org/core/lexer"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Parser parses a dosini-style document
|
||||||
|
type Parser struct {
|
||||||
|
src *lexer.Reader
|
||||||
|
|
||||||
|
OnSection func(pos lexer.Position, name, subname string, hasSubname bool) error
|
||||||
|
OnField func(pos lexer.Position, key, value string) error
|
||||||
|
OnComment func(pos lexer.Position, comment string) error
|
||||||
|
OnError func(pos lexer.Position, content string, err error) error
|
||||||
|
}
|
||||||
|
|
||||||
|
func defaultOnSection(_ lexer.Position, _, _ string, _ bool) error { return nil }
|
||||||
|
func defaultOnField(_ lexer.Position, _, _ string) error { return nil }
|
||||||
|
func defaultOnComment(_ lexer.Position, _ string) error { return nil }
|
||||||
|
|
||||||
|
func defaultOnError(pos lexer.Position, content string, err error) error {
|
||||||
|
return &lexer.Error{
|
||||||
|
Line: pos.Line,
|
||||||
|
Column: pos.Column,
|
||||||
|
Content: content,
|
||||||
|
Err: err,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Parser) setDefaults() {
|
||||||
|
if p.OnSection == nil {
|
||||||
|
p.OnSection = defaultOnSection
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.OnField == nil {
|
||||||
|
p.OnField = defaultOnField
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.OnComment == nil {
|
||||||
|
p.OnComment = defaultOnComment
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.OnError == nil {
|
||||||
|
p.OnError = defaultOnError
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run parses the source
|
||||||
|
func (p *Parser) Run() error {
|
||||||
|
p.setDefaults()
|
||||||
|
|
||||||
|
return lexer.Run(p.initialState)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewParser creates a dosini-style parser using
|
||||||
|
// an [io.Reader] as source
|
||||||
|
func NewParser(r io.Reader) *Parser {
|
||||||
|
if r == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Parser{
|
||||||
|
src: lexer.NewReader(r),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package parser
|
||||||
|
|
||||||
|
import "asciigoat.org/core/lexer"
|
||||||
|
|
||||||
|
func (*Parser) initialState() (lexer.StateFn, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user