Documentation ¶
Overview ¶
Package txt is a generic utility for parsing and processing a text that is structured in individual lines.
Index ¶
- Variables
- func Is(matchingCharacter ...rune) func(rune) bool
- func IsSpaceOrTab(r rune) bool
- func SubRune(text []rune, start int, length int) []rune
- type Block
- type Error
- type Indentator
- type Line
- type Parseable
- func (p *Parseable) Advance(increment int)
- func (p *Parseable) Length() int
- func (p *Parseable) Peek() rune
- func (p *Parseable) PeekUntil(isMatch func(rune) bool) (Parseable, bool)
- func (p *Parseable) Remainder() Parseable
- func (p *Parseable) RemainingLength() int
- func (p *Parseable) SkipWhile(isMatch func(rune) bool)
- func (p *Parseable) ToString() string
Constants ¶
This section is empty.
Variables ¶
var Indentations = []string{" ", " ", " ", "\t"}
var LineEndings = []string{"\r\n", "\n"}
Functions ¶
func IsSpaceOrTab ¶
IsSpaceOrTab checks whether a rune is a space or a tab character.
Types ¶
type Block ¶
type Block interface { // Lines returns all lines. Lines() []Line // SignificantLines returns the lines that are not blank. The two integers // are the number of insignificant lines at the beginning and the end. SignificantLines() (significant []Line, headCount int, tailCount int) // OverallLineIndex returns the overall line index, taking into // account the context of all preceding blocks. OverallLineIndex(int) int // SetPrecedingLineCount adjusts the overall line count. SetPrecedingLineCount(int) }
Block is multiple consecutive lines with text, with no blank lines in between, but possibly one or more blank lines before or after. It’s basically like a paragraph of text, with surrounding whitespace. The Block is guaranteed to contain exactly a single sequence of significant lines, i.e. lines that contain text.
type Error ¶
type Error interface { // Error is an alias for Message. Error() string // LineNumber returns the logical line number, as shown in an editor. LineNumber() int // LineText is the original text of the line. LineText() string // Position is the cursor position in the line, excluding the indentation. Position() int // Column is the cursor position in the line, including the indentation. Column() int // Length returns the number of erroneous characters. Length() int // Code returns a unique identifier of the error kind. Code() string // Title returns a short error description. Title() string // Details returns additional information, such as hints or further explanations. Details() string // Message is a combination of Title and Details. Message() string // Origin returns the origin of the error, such as the file name. Origin() string SetOrigin(string) Error }
Error contains infos about a parsing error in a Line.
type Indentator ¶
type Indentator struct {
// contains filtered or unexported fields
}
Indentator is a utility to check to process indented text. It is initialised with the first indented line, and determines the indentation style of that line. For all subsequent lines, it can then create Parseable’s that are already pre- processed.
func NewIndentator ¶
func NewIndentator(allowedIndentationStyles []string, l Line) *Indentator
NewIndentator creates an indentator object, if the given line is indented according to the allowed styles.
func (*Indentator) NewIndentedParseable ¶
func (i *Indentator) NewIndentedParseable(l Line, atLevel int) *Parseable
NewIndentedParseable returns a Parseable with already skipped indentation. It returns `nil` if the encountered indentation level is smaller than `atLevel`. It only consumes the desired indentation and disregards any additional indentation.
func (*Indentator) Style ¶
func (i *Indentator) Style() string
type Line ¶
type Line struct { // Text contains the copy of the line. Text string // LineEnding is the encountered line ending sequence `\n` or `\r\n`. // Note that for the last line in a file, there might be no line ending. LineEnding string }
Line is a data structure that represent one line of the source file.
func NewLineFromString ¶
NewLineFromString turns data into a Line object.
func (*Line) Indentation ¶
Indentation returns the indentation sequence of a line that is indented at least once. Note: it cannot determine the level of indentation. If the line is not indented, it returns empty string.
type Parseable ¶
Parseable is utility data structure for parsing a piece of text.
func NewParseable ¶
NewParseable creates a parseable from the given line.
func (*Parseable) Peek ¶
Peek returns the next character, or `utf8.RuneError` if there is none anymore.
func (*Parseable) PeekUntil ¶
PeekUntil moves the cursor forward until the condition is satisfied, or until the end of the line is reached. It returns a Parseable containing the consumed part of the line, as well as a bool to indicate whether the condition was met (`true`) or the end of the line was encountered (`false`).
func (*Parseable) RemainingLength ¶
RemainingLength returns the number of chars until the end of the line.