parser

package
v0.0.0-...-65f6afe Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2019 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseEscape

func ParseEscape(s string) (rune, int, error)

ParseEscape parses one C-style escape character from the beginning of string s. It returnes the parsed rune, the number of consumed bytes and error.

Types

type Grammar

type Grammar interface {
	// Parse parses the source text according to the grammar.
	Parse(source string) (*Result, error)
	// Source returns the text source of the grammar description.
	Source() string
}

func New

func New(gsource string) (Grammar, error)

New creates a new grammar.

type Node

type Node struct {
	// Label determines the type of the node, usually corresponding
	// to the rule name (LHS of the parser rule).
	Label string
	// Text is a captured text, if a rule defines a capture region.
	Text string
	// Content is a complete text that was consumed during parsing of this
	// element.  Maybe nil if the content was not recorded during parsing, or for
	// nodes that were programmatically generated.  If not nil, len(Content)
	// should be len(Children) + 1 When present, concatenating node Content in
	// order with children content results in exactly the parser input.
	// NOTE: this is not computed during parsing, but at a later stage.
	Content []string
	// The byte posittion of the first character consumed by this node rule
	// during parsing in the buffer that was passed to parser.
	Pos int
	// The number of bytes consumed by this node rule.
	Len int
	// The line number of the first character consumed by this node. 1-based.
	Row int
	// The column number of the first character consumed by this node. 0-based.
	Col int
	// The children of this node.
	Children []*Node
	// Err caches the error that resulted from application of some parsing rule at some position.
	Err error
	// Annotations stores some string-form annotations.
	Annotations map[string]string
	// TreeAnnotations stores some tree-form annotations (aka labelled children).
	TreeAnnotations map[string]*Node
	// Private fields.
	// Start is used by the bootstrap parser to implement captures. Parser generator or main parser
	// do not need it, but they also set it for compatibility with node drop/keep heuristics.
	// TODO(salikh): Remove after deprecating the bootstrap parser.
	Start int
}

func (*Node) All

func (n *Node) All(label string) []*Node

All returns the slice of node children with the specified label.

func (*Node) Child

func (n *Node) Child(label string) *Node

TODO: rename Child to First

func (*Node) DeepDup

func (n *Node) DeepDup() *Node

DeepDup makes a deep copy of a node.

func (*Node) Dump

func (n *Node) Dump() string

func (*Node) Dup

func (n *Node) Dup() *Node

Dup makes a shallow copy of a node.

func (*Node) First

func (n *Node) First(label string, start int) *Node

First returns the first child with index at least n that has the specified label. If ther is no matching child node, it returns nil.

func (*Node) ReconstructContent

func (n *Node) ReconstructContent() (string, error)

func (*Node) String

func (n *Node) String() string

type NodeStack

type NodeStack []*Node

func (*NodeStack) Pop

func (s *NodeStack) Pop() *Node

func (*NodeStack) Push

func (s *NodeStack) Push(n *Node)

type Result

type Result struct {
	G      *grammar
	Source string
	Memo   map[int]map[*rule]*Node
	Level  int
	// Final AST.
	Tree *Node
	NodeStack
}

Result encapsulates one parse result.

func (*Result) Attach

func (r *Result) Attach(n *Node)

func (*Result) ComputeContent

func (r *Result) ComputeContent()

ComputeContent annotates the parse tree with pieces of original content and line/column positions in the original parser input.

func (*Result) TopNode

func (r *Result) TopNode() *Node

Directories

Path Synopsis
Package charclass provide functions for handling character class regular expressions.
Package charclass provide functions for handling character class regular expressions.
cmd
Package example provides an example to parse source file according to grammar defined in includes.peg.
Package example provides an example to parse source file according to grammar defined in includes.peg.

Jump to

Keyboard shortcuts

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