parser

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2023 License: MIT Imports: 6 Imported by: 1

Documentation

Overview

Package parser parses ini-style files

Index

Constants

View Source
const (
	RuneComment      = ';' // RuneComment is the standard dosini comment character
	RuneCommentExtra = '#' // RuneCommentExtra is UNIX shell's comment character
	RuneSectionStart = '[' // RuneSectionStart indicates the start of a section declaration
	RuneSectionEnd   = ']' // RuneSectionEnd indiciates the end of a section declaration
	RuneFieldEqual   = '=' // RuneFieldEqual separates field keys from their values
)

Variables

View Source
var (
	// RunesComment is a string containing all runes acceptable to start comments
	RunesComment = string([]rune{
		RuneComment,
		RuneCommentExtra,
	})
	// RunesSpecial is a string containing all the runes with special meaning
	RunesSpecial = string([]rune{
		RuneComment,
		RuneCommentExtra,
		RuneSectionStart,
		RuneSectionEnd,
		RuneFieldEqual,
	})
)
View Source
var (
	// IsNewLine tells if the rune indicates a line break or the start of one
	IsNewLine = lexer.NewIsIn("\r\n")
	// IsNotNewLine tells if the rune is not a line break nor the start of one
	IsNotNewLine = lexer.NewIsNot(IsNewLine)
	// IsSpace tells if the rune is considered whitespace by Unicode
	IsSpace = lexer.IsSpace
	// IsNotSpace tells if the rune is not considered whitespace by Unicode
	IsNotSpace = lexer.NewIsNot(IsSpace)
	// IsCommentStart ...
	IsCommentStart = lexer.NewIsIn(RunesComment)
)

Functions

func ErrPlusPosition added in v0.2.4

func ErrPlusPosition(pos lexer.Position, e *lexer.Error) *lexer.Error

ErrPlusPosition returns a copy of the given lexer.Error offsetting the Line/Column information.

func GetPositionalLength added in v0.2.3

func GetPositionalLength(s string) lexer.Position

GetPositionalLength calculates the lexer.Position at the end of a text.

func IsAny added in v0.2.3

func IsAny(_ rune) bool

IsAny accepts any rune

func IsName

func IsName(r rune) bool

IsName indicates a rune is acceptable for section or field names

func IsSectionEnd

func IsSectionEnd(r rune) bool

IsSectionEnd indicates the rune ends the section declaration

func IsSectionStart

func IsSectionStart(r rune) bool

IsSectionStart indicates the rune starts the section declaration

func IsSpaceNotNewLine

func IsSpaceNotNewLine(r rune) bool

IsSpaceNotNewLine indicates a rune is whitespace but not a new line

func NewError added in v0.2.4

func NewError(pos lexer.Position, content, hint string, err error) *lexer.Error

NewError creates a lexer.Error using a lexer.Position

Types

type Parser

type Parser struct {

	// OnToken is called for each identified token. if it returns an error
	// parsing is interrupted.
	OnToken func(pos lexer.Position, typ TokenType, value string) error

	// OnError is called in case of a parsing error, and it's allowed
	// to replace the error returned by [Parser.Run].
	// OnError is called for io.EOF, but [Parser.Run] will consider it
	// normal termination.
	OnError func(pos lexer.Position, content string, err error) error
	// contains filtered or unexported fields
}

Parser parses a ini-style document

func NewParser

func NewParser(r io.Reader) *Parser

NewParser creates a ini-style parser using an io.Reader as source

func (*Parser) Run

func (p *Parser) Run() error

Run parses the source

type TextParser added in v0.2.3

type TextParser struct {
	*lexer.Reader
	// contains filtered or unexported fields
}

TextParser is a generic text parser.

func (*TextParser) AcceptNewLine added in v0.2.3

func (p *TextParser) AcceptNewLine() bool

AcceptNewLine checks if next is a new line. It accepts "\n", "\n\r", "\r" and "\r\n".

func (*TextParser) AcceptRune added in v0.2.3

func (p *TextParser) AcceptRune(r rune) bool

AcceptRune checks if next is the specified rune

func (*TextParser) Discard added in v0.2.3

func (p *TextParser) Discard()

Discard shadows lexer.Reader's, and takes in consideration new lines on the discarded data when moving the position

func (*TextParser) Emit added in v0.2.3

func (p *TextParser) Emit() (lexer.Position, string)

Emit returns the accepted text, its position, and moves the cursor position accordingly

func (*TextParser) Init added in v0.2.3

func (p *TextParser) Init(r io.Reader)

Init initializes the TextParser with a non-nil io.Reader.

func (*TextParser) InitBytes added in v0.2.3

func (p *TextParser) InitBytes(b []byte)

InitBytes initializes the TextParser with a byte array

func (*TextParser) InitString added in v0.2.3

func (p *TextParser) InitString(s string)

InitString initializes the TextParser with a byte array

func (*TextParser) Position added in v0.2.3

func (p *TextParser) Position() lexer.Position

Position returns the position of the first character of the accepted text

func (*TextParser) Step added in v0.2.3

func (p *TextParser) Step()

Step discards what's been accepted and increments the position assuming they all increment the column counter

func (*TextParser) StepLine added in v0.2.3

func (p *TextParser) StepLine()

StepLine discards what's been accepted and moves then position to the beginning of the next line

type TokenType

type TokenType uint

A TokenType is a type of Token

const (
	// TokenUnknown represents a Token that hasn't been identified
	TokenUnknown TokenType = iota
	// TokenSectionStart indicates the opening marker of a section declaration.
	// The left squared bracket.
	TokenSectionStart
	// TokenSectionEnd indicates the closing marker of a section declaration.
	// The right squared bracket.
	TokenSectionEnd
	// TokenSectionName represents the section name between the squared brackets
	TokenSectionName
	// TokenSectionSubname represents a secondary name in the section represented
	// between quotes after the section name.
	// e.g.
	// [section_name "section_subname"]
	TokenSectionSubname
	// TokenComment represents a comment, including the initial ';' or '#' until
	// the end of the line.
	TokenComment
	// TokenFieldKey represents a field name in a `key = value` entry
	TokenFieldKey
	// TokenFieldValue represents a field value in a `key = value` entry
	TokenFieldValue
)

func (TokenType) String

func (i TokenType) String() string

Jump to

Keyboard shortcuts

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