Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 37348b44aa | |||
| daad0f499d | |||
| a21b5c43d6 | |||
| a7233d0ded |
+6
-38
@@ -9,48 +9,16 @@ import (
|
||||
|
||||
// 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
|
||||
src *lexer.Lexer
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
func (*Parser) init() error { return nil }
|
||||
|
||||
// Run parses the source
|
||||
func (p *Parser) Run() error {
|
||||
p.setDefaults()
|
||||
if err := p.init(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return lexer.Run(p.initialState)
|
||||
}
|
||||
@@ -63,6 +31,6 @@ func NewParser(r io.Reader) *Parser {
|
||||
}
|
||||
|
||||
return &Parser{
|
||||
src: lexer.NewReader(r),
|
||||
src: lexer.NewLexerReader(r),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user