From e0155b51034faf7472faf0476857853a24002a93 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Wed, 30 Aug 2023 02:26:04 +0100 Subject: [PATCH] parser: callbacks [WIP] Signed-off-by: Alejandro Mery --- parser/lexer.go | 2 ++ parser/parser.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/parser/lexer.go b/parser/lexer.go index b1a4a3e..2db36e9 100644 --- a/parser/lexer.go +++ b/parser/lexer.go @@ -8,6 +8,8 @@ import ( // Run parses the source func (p *Parser) Run() error { + p.setDefaults() + p.pos.Reset() return lexer.Run(p.lexStart) } diff --git a/parser/parser.go b/parser/parser.go index 723e589..739b657 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -12,6 +12,42 @@ type Parser struct { src *lexer.Reader pos lexer.Position + + 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 + } } // NewParser creates a dosini-style parser using