From 8267f2c327036c176fdb228f3b08102f451a695f Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Mon, 28 Aug 2023 23:46:05 +0000 Subject: [PATCH] parser: WIP Signed-off-by: Alejandro Mery --- parser/parser.go | 43 +++++++++++++++++++++++++++++++++++++++++++ parser/states.go | 7 +++++++ 2 files changed, 50 insertions(+) create mode 100644 parser/states.go diff --git a/parser/parser.go b/parser/parser.go index 5d399fd..ab8b919 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -10,6 +10,49 @@ 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 +} + +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 diff --git a/parser/states.go b/parser/states.go new file mode 100644 index 0000000..166f21b --- /dev/null +++ b/parser/states.go @@ -0,0 +1,7 @@ +package parser + +import "asciigoat.org/core/lexer" + +func (*Parser) initialState() (lexer.StateFn, error) { + return nil, nil +}