parser

package
v0.29.0 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2021 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package parser provide a common text parser, using delimiters.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Lines

func Lines(file string) ([]string, error)

Lines parse the content of path and return non-empty lines.

Types

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser implement text parsing.

func New

func New(content, delims string) (p *Parser)

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

func Open(file, delims string) (p *Parser, err error)

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

func (p *Parser) AddDelimiters(delims string)

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

func (p *Parser) Line() (string, rune)

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) Lines

func (p *Parser) Lines() []string

Lines return all non-empty lines from the content.

func (*Parser) Load

func (p *Parser) Load(content, delims string)

Load the new content and delimiters.

func (*Parser) ReadEnclosed

func (p *Parser) ReadEnclosed(open, closed rune) (string, rune)

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

func (p *Parser) RemoveDelimiters(dels string)

RemoveDelimiters from current parser.

func (*Parser) Skip

func (p *Parser) Skip(n int)

Skip parsing n characters or EOF if n is greater then length of content.

func (*Parser) SkipHorizontalSpaces

func (p *Parser) SkipHorizontalSpaces() rune

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

func (p *Parser) SkipLine() rune

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

func (p *Parser) Stop() (remain string, pos int)

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

func (p *Parser) Token() (string, rune)

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

func (p *Parser) TokenEscaped(esc rune) (string, rune)

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".

Jump to

Keyboard shortcuts

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