Documentation ¶
Overview ¶
Package parser provide a common text parser, using delimiters.
Index ¶
- func Lines(file string) ([]string, error)
- type Parser
- func (p *Parser) AddDelimiters(delims string)
- func (p *Parser) Close()
- func (p *Parser) Line() (string, rune)
- func (p *Parser) Lines() []string
- func (p *Parser) Load(content, delims string)
- func (p *Parser) ReadEnclosed(open, closed rune) (string, rune)
- func (p *Parser) RemoveDelimiters(dels string)
- func (p *Parser) Skip(n int)
- func (p *Parser) SkipHorizontalSpaces() rune
- func (p *Parser) SkipLine() rune
- func (p *Parser) Stop() (remain string, pos int)
- func (p *Parser) Token() (string, rune)
- func (p *Parser) TokenEscaped(esc rune) (string, rune)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser implement text parsing.
func New ¶
New create and initialize parser from content and delimiters.
Example ¶
content := "[test]\nkey = value" p := New(content, "=[]") for { token, del := p.Token() token = strings.TrimSpace(token) fmt.Printf("%q %q\n", token, del) if del == 0 { break } }
Output: "" '[' "test" ']' "key" '=' "value" '\x00'
func Open ¶
Open create and initialize the parser using predefined delimiters. All the content of file will be loaded first. If delimiters is empty, it would default to all whitespaces characters.
func (*Parser) AddDelimiters ¶
AddDelimiters append new delimiter to existing parser.
func (*Parser) Close ¶
func (p *Parser) Close()
Close the parser by resetting all its internal state to zero value.
func (*Parser) Line ¶ added in v0.18.0
Line read and return a single line. On success it will return a string without '\n' and new line character. In case of EOF it will return the last line and 0.
func (*Parser) ReadEnclosed ¶
ReadEnclosed read the token inside opening and closing characters, ignoring all delimiters that previously set.
It will return the parsed token and closed character if closed character found, otherwise it will token with 0.
func (*Parser) RemoveDelimiters ¶
RemoveDelimiters from current parser.
func (*Parser) SkipHorizontalSpaces ¶
SkipHorizontalSpaces skip all space (" "), tab ("\t"), carriage return ("\r"), and form feed ("\f") characters; and return the first character found, probably new line.
func (*Parser) SkipLine ¶
SkipLine skip all characters until new line. It will return the first character after new line or 0 if EOF.
func (*Parser) Stop ¶ added in v0.20.1
Stop the parser, return the remaining unparsed content and its last position, and then call Close to reset the internal state back to zero.
func (*Parser) Token ¶
Token read the next token from content until one of the delimiter found. if no delimiter found, its mean all of content has been read, the returned delimiter will be 0.
func (*Parser) TokenEscaped ¶
TokenEscaped read the next token from content until one of the delimiter found, unless its escaped with value of esc character.
For example, if the content is "a b" and one of the delimiter is " ", escaping it with "\" will return as "a b" not "a".