parser

package
v3.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 31, 2022 License: MIT Imports: 7 Imported by: 1

Documentation

Overview

Package parser provides function to parse hranoprovod formatted files

Index

Examples

Constants

View Source
const (
	DefaultDateFormat  = "2006/01/02"
	DefaultCommentChar = '#'
)

Variables

This section is empty.

Functions

func ParseFileCallback

func ParseFileCallback(fileName string, c Config, callback ParseCallback) error

func ParseStreamCallback

func ParseStreamCallback(reader io.Reader, c Config, callback ParseCallback) error

ParseStreamCallback parses stream and calls callback on node or error

Types

type Config

type Config struct {
	// CommentChar contains the character used to indicate that the line is a comment
	CommentChar uint8
}

Config contains the parser configuration

func NewDefaultConfig

func NewDefaultConfig() Config

NewDefaultConfig returns the default set of parser configuration

type ErrorBadSyntax

type ErrorBadSyntax struct {
	LineNumber int
	Line       string
}

ErrorBadSyntax used when the stream contains bad syntax

func NewErrorBadSyntax

func NewErrorBadSyntax(lineNumber int, line string) *ErrorBadSyntax

NewErrorBadSyntax creates new ErrorBadSyntax error

func (*ErrorBadSyntax) Error

func (e *ErrorBadSyntax) Error() string

Error returns the error message

type ErrorConversion

type ErrorConversion struct {
	Text       string
	LineNumber int
	Line       string
	// contains filtered or unexported fields
}

ErrorConversion raised when the element value cannot be parsed as float

func NewErrorConversion

func NewErrorConversion(err error, text string, lineNumber int, line string) *ErrorConversion

NewErrorConversion creates new ErrorConversion error

func (*ErrorConversion) Error

func (e *ErrorConversion) Error() string

Error returns the error message

type ErrorIO

type ErrorIO struct {
	FileName string
	// contains filtered or unexported fields
}

ErrorIO raised on IO issue

func NewErrorIO

func NewErrorIO(err error, fileName string) *ErrorIO

NewErrorIO creates new IO Error

func (*ErrorIO) Error

func (e *ErrorIO) Error() string

Error returns the error message

type ParseCallback

type ParseCallback func(n *shared.ParserNode, err error) (stop bool, cbError error)

ParseCallback is called on node or error event when parsing the stream

type Parser

type Parser struct {
	Nodes  chan *shared.ParserNode
	Errors chan error
	Done   chan bool
	// contains filtered or unexported fields
}

Parser is the parser data structure

func NewParser

func NewParser(c Config) Parser

NewParser returns new parser

func (Parser) ParseFile

func (p Parser) ParseFile(fileName string)

ParseFile parsers the contents of file

func (Parser) ParseStream

func (p Parser) ParseStream(reader io.Reader)

ParseStream parses the contents of stream

Example
package main

import (
	"fmt"
	"strings"

	"github.com/aquilax/hranoprovod-cli/v3/parser"
)

func main() {
	p := parser.NewParser(parser.NewDefaultConfig())
	file := `2011/07/17:
  el1: 1.22
  ел 2:  4
  el/3:  3

2011/07/18:
  el1: 1.33
  ел 5:  5
  el/7:  4
  el1: 1.35
`
	go p.ParseStream(strings.NewReader(file))
	func() {
		for {
			select {
			case node := <-p.Nodes:
				fmt.Println(node.Header)
			case <-p.Errors:
				return
			case <-p.Done:
				return
			}
		}
	}()
}
Output:

2011/07/17
2011/07/18

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL