peggy: Index | Files

package peg

import ""


Package Files

fail.go loc.go peg.go pretty.go

func DecodeRuneInString Uses

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 Uses

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 Uses

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:


func PrettyWrite Uses

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

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

type Error Uses

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 Uses

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 Uses

func (err Error) Error() string

type Fail Uses

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 Uses

func LeafFails(node *Fail) []*Fail

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

type Loc Uses

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

A Loc is a location in the input text.

func Location Uses

func Location(text string, byte int) Loc

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

type Node Uses

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.

Package peg imports 5 packages (graph) and is imported by 28 packages. Updated 2018-03-14. Refresh now. Tools for package owners.