parser

package
v2.4.7 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package parser provides function to parse hranoprovod formatted files

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseStreamCallback

func ParseStreamCallback(reader io.Reader, commentChar uint8, callback ParseCallback)

ParseStreamCallback parses stream and calls callback on node or error

Types

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
}

ErrorConversion raised when the element value cannot be parsed as float

func NewErrorConversion

func NewErrorConversion(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 Options

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

Options contains the parser related options

func NewDefaultOptions

func NewDefaultOptions() *Options

NewDefaultOptions returns the default set of parser options

type ParseCallback

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

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(options *Options) 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/v2/parser"
)

func main() {
	p := parser.NewParser(parser.NewDefaultOptions())
	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