peg

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: May 13, 2022 License: BSD-3-Clause Imports: 5 Imported by: 21

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeRuneInString

func DecodeRuneInString(s string) (rune, int)

DecodeRuneInString is utf8.DecodeRuneInString. It's here so parsers can just include peg, and not also need unicode/utf8.

func DedupFails

func DedupFails(node *Fail)

DedupFails removes duplicate fail branches from the tree, keeping only the first occurrence of each. This is useful for example before printing the Fail tree, because the non-deduped Fail tree can be exponential in the input size.

func Pretty

func Pretty(n nodeOrFail) string

Pretty returns a human-readable string of a Node or Fail and the subtree beneath it. The output looks like:

<n.Name>{
	<Pretty(n.Kids)[0])>,
	<Pretty(n.Kids[1])>,
	…
	<Pretty(n.Kids[n-1])>,
}

func PrettyWrite

func PrettyWrite(w io.Writer, n nodeOrFail) error

PrettyWrite is like Pretty but outputs to an io.Writer.

Types

type Error

type Error struct {
	// FilePath is the path of the input file containing the error.
	FilePath string
	// Loc is the location of the error.
	Loc Loc
	// Message is the error message.
	Message string
}

Error implements error, prefixing an error message with location information for the error.

func SimpleError

func SimpleError(text string, node *Fail) Error

SimpleError returns an error with a basic error message that describes what was expected at all of the leaf fails with the greatest position in the tree.

The FilePath field of the returned Error is the empty string. The caller can set this field if to prefix the location with the path to an input file.

func (Error) Error

func (err Error) Error() string

type Fail

type Fail struct {
	// Name is the name of the Rule associated with the node,
	// or the empty string if the Fail is a terminal expression failure.
	Name string

	// Pos is the byte offset into the input of the Fail.
	Pos int

	// Kids are the immediate succors of this Fail.
	// Kids is only non-nil for named Fail nodes.
	Kids []*Fail

	// Want is a string describing what was expected at the error position.
	// It is only non-empty for unnamed Fail nodes.
	//
	// It can be of one of the following forms:
	// 	"…" indicating a failed literal match, where the text between the quotes is the expected literal using Go escaping.
	// 	. indicating a failed . match.
	// 	[…] indicating a failed character class match, where the text between the [ and ] is the character class.
	// 	!… where the text after ! is the string representation of a failed predicate subexpression.
	// 	&… where the text after & is the string representation of a failed predicate subexpression.
	// 	… the error-name of a rule.
	// 		For example, "int" in rule: Integer "int" <- [0-9].
	Want string
}

A Fail is a node in a failed-parse tree. A failed-parse tree contains all paths in a failed parse that lead to the furthest error location in the input text. There are two types of nodes: named and unnamed. Named nodes represent grammar rules that failed to parse. Unnamed nodes represent terminal expressions that failed to parse.

func LeafFails

func LeafFails(node *Fail) []*Fail

LeafFails returns all fails in the tree with the greatest Pos.

type Loc

type Loc struct {
	Byte   int
	Rune   int
	Line   int
	Column int
}

A Loc is a location in the input text.

func Location

func Location(text string, byte int) Loc

Location returns the Loc at the corresponding byte offset in the text.

type Node

type Node struct {
	// Name is the name of the Rule associated with the node,
	// or the empty string for anonymous Nodes
	// that are not associated with any Rule.
	Name string

	// Text is the input text of the Node's subtree.
	Text string

	// Kids are the immediate successors of this node.
	Kids []*Node
}

A Node is a node in a Peggy parse tree.

Jump to

Keyboard shortcuts

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