Documentation ¶
Overview ¶
Package parser parses ini-style files
Index ¶
- Constants
- Variables
- func GetPositionalLength(s string) lexer.Position
- func IsAny(_ rune) bool
- func IsName(r rune) bool
- func IsSectionEnd(r rune) bool
- func IsSectionStart(r rune) bool
- func IsSpaceNotNewLine(r rune) bool
- type Parser
- type TextParser
- func (p *TextParser) AcceptNewLine() bool
- func (p *TextParser) AcceptRune(r rune) bool
- func (p *TextParser) Discard()
- func (p *TextParser) Emit() (lexer.Position, string)
- func (p *TextParser) Init(r io.Reader)
- func (p *TextParser) InitBytes(b []byte)
- func (p *TextParser) InitString(s string)
- func (p *TextParser) Position() lexer.Position
- func (p *TextParser) Step()
- func (p *TextParser) StepLine()
- type TokenType
Constants ¶
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 ¶
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, }) )
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 GetPositionalLength ¶ added in v0.2.3
GetPositionalLength calculates the lexer.Position at the end of a text.
func IsName ¶
IsName indicates a rune is acceptable for section or field names
func IsSectionEnd ¶
IsSectionEnd indicates the rune ends the section declaration
func IsSectionStart ¶
IsSectionStart indicates the rune starts the section declaration
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
type TextParser ¶ added in v0.2.3
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 )