gparselib

package module
v0.5.7 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2024 License: MIT Imports: 10 Imported by: 2

README

gparselib

Parser library developed with the flowdev technology for the Go programming language.

Documentation

Overview

Package gparselib contains a library for building parsers. More specifically you can implement PEG (Parser Expression Grammar) parsers with it.

Index

Constants

View Source
const (
	FeedbackUnknown = FeedbackKind(iota) // should never be used directly but is default value
	FeedbackInfo
	FeedbackWarning
	FeedbackPotentialProblem
	FeedbackError
)

Enumeration of the kinds of feedback we can handle.

Variables

This section is empty.

Functions

This section is empty.

Types

type FeedbackItem

type FeedbackItem struct {
	Pos  int
	Kind FeedbackKind
	Msg  fmt.Stringer
}

FeedbackItem is just one item of feedback.

func (*FeedbackItem) String

func (fi *FeedbackItem) String() string

type FeedbackKind

type FeedbackKind int

FeedbackKind is just an enumeration of kinds of feedback.

type ParseData

type ParseData struct {
	Source     SourceData
	Result     *ParseResult
	SubResults []*ParseResult
}

ParseData contains all data needed during parsing.

func NewParseData

func NewParseData(name string, content string) *ParseData

NewParseData creates a new, completely initialized ParseData.

func ParseAll

func ParseAll(
	pd *ParseData, ctx interface{},
	pluginSubparsers []SubparserOp, pluginSemantics SemanticsOp,
) (*ParseData, interface{})

ParseAll calls multiple subparsers and all have to match for a successful result.

func ParseAny

func ParseAny(
	pd *ParseData, ctx interface{},
	pluginSubparsers []SubparserOp, pluginSemantics SemanticsOp,
) (*ParseData, interface{})

ParseAny calls multiple subparsers until one matches. The result is only unsuccessful if no subparser matches.

func ParseBest

func ParseBest(
	pd *ParseData, ctx interface{},
	pluginSubparsers []SubparserOp, pluginSemantics SemanticsOp,
) (*ParseData, interface{})

ParseBest calls all subparsers and chooses the one with the longest match. The result is only unsuccessful if no subparser matches.

func ParseBlockComment

func ParseBlockComment(
	pd *ParseData, ctx interface{},
	pluginSemantics SemanticsOp,
	cfgStart, cfgEnd string,
) (*ParseData, interface{}, error)

ParseBlockComment parses a comment until the end of the line. The strings that start and end the comment (e.g.: `/*`, `*/`) have to be configured. A comment start or end inside a string literal (', " and `) is ignored. If the start or end of the comment is empty an error is returned.

func ParseEOF

func ParseEOF(
	pd *ParseData, ctx interface{},
	pluginSemantics SemanticsOp,
) (*ParseData, interface{})

ParseEOF only matches at the end of the input.

func ParseGoodRunes

func ParseGoodRunes(
	pd *ParseData, ctx interface{},
	pluginSemantics SemanticsOp,
	cfgAccept func(rune) bool,
) (*ParseData, interface{})

ParseGoodRunes parses as long as the runes are accepted by the configured function. If no good rune is found, an error is returned.

func ParseIdent

func ParseIdent(
	pd *ParseData, ctx interface{},
	pluginSemantics SemanticsOp,
	cfgFirstChar, cfgFollowingChars string,
) (*ParseData, interface{})

ParseIdent parses an identifier at the current position of the parser. If allows Unicode letters for the first character and Unicode letters and Unicode numbers for all following characters. The configuration has to be the additional characters allowed for the first and following characters.

func ParseLineComment

func ParseLineComment(
	pd *ParseData, ctx interface{},
	pluginSemantics SemanticsOp,
	cfgStart string,
) (*ParseData, interface{}, error)

ParseLineComment parses a comment until the end of the line. The string that starts the comment (e.g.: `//`) has to be configured. If the start of the comment is empty an error is returned.

func ParseLiteral

func ParseLiteral(
	pd *ParseData, ctx interface{},
	pluginSemantics SemanticsOp,
	cfgLiteral string,
) (*ParseData, interface{})

ParseLiteral parses a literal value at the current position of the parser. The configuration has to be the literal string we expect.

func ParseMulti

func ParseMulti(
	pd *ParseData, ctx interface{},
	pluginSubparser SubparserOp, pluginSemantics SemanticsOp,
	cfgMin, cfgMax int,
) (*ParseData, interface{})

ParseMulti uses a subparser multiple times. The minimum times the subparser has to match and the maximum times the subparser can match have to be configured.

func ParseMulti0

func ParseMulti0(
	pd *ParseData, ctx interface{},
	pluginSubparser SubparserOp, pluginSemantics SemanticsOp,
) (*ParseData, interface{})

ParseMulti0 uses its subparser at least one time without upper bound. But the result is still positive even if the subparser didn't match a single time.

func ParseMulti1

func ParseMulti1(
	pd *ParseData, ctx interface{},
	pluginSubparser SubparserOp, pluginSemantics SemanticsOp,
) (*ParseData, interface{})

ParseMulti1 uses its subparser at least one time without upper bound. The result is positive as long as the subparser matches at least one time.

func ParseNatural

func ParseNatural(
	pd *ParseData, ctx interface{},
	pluginSemantics SemanticsOp,
	cfgRadix int,
) (*ParseData, interface{}, error)

ParseNatural parses a natural number at the current position of the parser. The configuration has to be the radix of accepted numbers (e.g.: 10). If the radix is smaller than 2 or larger than 36 an error is returned.

func ParseOptional

func ParseOptional(
	pd *ParseData, ctx interface{},
	pluginSubparser SubparserOp, pluginSemantics SemanticsOp,
) (*ParseData, interface{})

ParseOptional uses its subparser exaclty one time. But the result is still positive even if the subparser didn't match.

func ParseSpace

func ParseSpace(
	pd *ParseData, ctx interface{},
	pluginSemantics SemanticsOp,
	cfgEOLOK bool,
) (*ParseData, interface{})

ParseSpace parses one or more space characters. Space is defined by unicode.IsSpace(). It can be configured wether EOL ('\n') is to be interpreted as space or not.

func (*ParseData) AddError

func (pd *ParseData) AddError(pos int, msg string, baseErr error)

AddError adds a new parse error to the result feedback.

func (*ParseData) AddInfo added in v0.5.0

func (pd *ParseData) AddInfo(pos int, msg string)

AddInfo adds a new parse information to the result feedback.

func (*ParseData) AddWarning added in v0.5.0

func (pd *ParseData) AddWarning(pos int, msg string)

AddWarning adds a new parser warning to the result feedback.

func (*ParseData) CleanFeedback added in v0.5.2

func (pd *ParseData) CleanFeedback(cleanEnd bool)

CleanFeedback returns parser errors as a single error and additional feedback.

func (*ParseData) GetFeedback added in v0.5.0

func (pd *ParseData) GetFeedback() (string, error)

GetFeedback returns parser errors as a single error and additional feedback.

func (*ParseData) ResetSourcePos

func (pd *ParseData) ResetSourcePos(pos int)

ResetSourcePos resets the source position to an old value. This is usually needed when semantic errors occur. If the given pos is negative, the position of the current result is used.

type ParseResult

type ParseResult struct {
	Pos      int
	Text     string
	Value    interface{}
	ErrPos   int
	Feedback []*FeedbackItem
}

ParseResult contains the result of parsing including semantic value and feedback.

func (*ParseResult) HasError

func (pr *ParseResult) HasError() bool

HasError searches the feedback for errors and returns only true if it found one.

type RegexpParser

type RegexpParser regexp.Regexp

RegexpParser parses text according to a predefined regular expression. The regular expression (e.g.: `^[a-z]+`) has to be configured. If the regular expression doesn't start with a `^` it will be added automatically. If the regular expression can't be compiled an error is returned.

func NewRegexpParser

func NewRegexpParser(cfgRegexp string) (*RegexpParser, error)

NewRegexpParser creates a new parser for the given regular expression. If the regular expression is invalid an error is returned.

func (*RegexpParser) ParseRegexp

func (pr *RegexpParser) ParseRegexp(
	pd *ParseData, ctx interface{},
	pluginSemantics SemanticsOp,
) (*ParseData, interface{})

ParseRegexp is the input port of the RegexpParser operation.

type SemanticsOp

type SemanticsOp func(pd *ParseData, ctx interface{}) (*ParseData, interface{})

SemanticsOp is a simple filter for parser and context data.

type SourceData

type SourceData struct {
	Name string
	// contains filtered or unexported fields
}

SourceData contains the name of the source for parsing, its contents and unexported stuff.

func NewSourceData

func NewSourceData(name string, content string) SourceData

NewSourceData creates a new, completely initialized SourceData.

func (SourceData) Where

func (sd SourceData) Where(pos int) string

Where describes the given integer position in a human-readable way.

type SubparserOp

type SubparserOp func(pd *ParseData, ctx interface{}) (*ParseData, interface{})

SubparserOp is a simple filter to the outside and gets the same data as the parent parser.

func NewParseAllPlugin

func NewParseAllPlugin(
	pluginSubparsers []SubparserOp, pluginSemantics SemanticsOp,
) SubparserOp

NewParseAllPlugin creates a plugin sporting a parser calling all subparsers.

func NewParseAnyPlugin

func NewParseAnyPlugin(
	pluginSubparsers []SubparserOp, pluginSemantics SemanticsOp,
) SubparserOp

NewParseAnyPlugin creates a plugin sporting a parser calling any successful subparser.

func NewParseBestPlugin

func NewParseBestPlugin(
	pluginSubparsers []SubparserOp, pluginSemantics SemanticsOp,
) SubparserOp

NewParseBestPlugin creates a plugin sporting a parser calling the best successful subparser.

func NewParseBlockCommentPlugin

func NewParseBlockCommentPlugin(
	pluginSemantics SemanticsOp,
	cfgStart, cfgEnd string,
) (SubparserOp, error)

NewParseBlockCommentPlugin creates a plugin sporting a number parser.

func NewParseEOFPlugin

func NewParseEOFPlugin(pluginSemantics SemanticsOp) SubparserOp

NewParseEOFPlugin creates a plugin sporting an EOF parser.

func NewParseGoodRunesPlugin

func NewParseGoodRunesPlugin(pluginSemantics SemanticsOp, cfgAccept func(rune) bool) SubparserOp

NewParseGoodRunesPlugin creates a plugin sporting a parser for accepted runes.

func NewParseIdentPlugin

func NewParseIdentPlugin(pluginSemantics SemanticsOp, cfgFirstChar, cfgFollowingChars string) SubparserOp

NewParseIdentPlugin creates a plugin sporting an identifier parser.

func NewParseLineCommentPlugin

func NewParseLineCommentPlugin(
	pluginSemantics SemanticsOp,
	cfgStart string,
) (SubparserOp, error)

NewParseLineCommentPlugin creates a plugin sporting a line comment parser.

func NewParseLiteralPlugin

func NewParseLiteralPlugin(pluginSemantics SemanticsOp, cfgLiteral string) SubparserOp

NewParseLiteralPlugin creates a plugin sporting a literal parser.

func NewParseMulti0Plugin

func NewParseMulti0Plugin(pluginSubparser SubparserOp, pluginSemantics SemanticsOp) SubparserOp

NewParseMulti0Plugin creates a plugin sporting a parser calling a subparser multiple times.

func NewParseMulti1Plugin

func NewParseMulti1Plugin(pluginSubparser SubparserOp, pluginSemantics SemanticsOp) SubparserOp

NewParseMulti1Plugin creates a plugin sporting a parser calling a subparser at least one time.

func NewParseMultiPlugin

func NewParseMultiPlugin(
	pluginSubparser SubparserOp, pluginSemantics SemanticsOp,
	cfgMin, cfgMax int,
) SubparserOp

NewParseMultiPlugin creates a plugin sporting a parser calling a subparser multiple times.

func NewParseNaturalPlugin

func NewParseNaturalPlugin(pluginSemantics SemanticsOp, cfgRadix int) (SubparserOp, error)

NewParseNaturalPlugin creates a plugin sporting a number parser.

func NewParseOptionalPlugin

func NewParseOptionalPlugin(pluginSubparser SubparserOp, pluginSemantics SemanticsOp) SubparserOp

NewParseOptionalPlugin creates a plugin sporting a parser calling a subparser once and ignoring errors in it.

func NewParseRegexpPlugin

func NewParseRegexpPlugin(
	pluginSemantics SemanticsOp,
	cfgRegexp string,
) (SubparserOp, error)

NewParseRegexpPlugin creates a plugin sporting a regular expression parser.

func NewParseSpacePlugin

func NewParseSpacePlugin(pluginSemantics SemanticsOp, cfgEOLOK bool) SubparserOp

NewParseSpacePlugin creates a plugin sporting a space parser.

Jump to

Keyboard shortcuts

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