Documentation ¶
Overview ¶
Package parse implements the elvish parser.
The parser builds a hybrid of AST (abstract syntax tree) and parse tree (a.k.a. concrete syntax tree). The AST part only includes parts that are semantically significant -- i.e. skipping whitespaces and symbols that do not alter the semantics, and is embodied in the fields of each *Node type. The parse tree part corresponds to all the text in the original source text, and is embodied in the children of each *Node type.
Index ¶
- func As(srcname, src string, n Node) error
- func IsInlineWhitespace(r rune) bool
- func IsWhitespace(r rune) bool
- func PPrintAST(n Node, w io.Writer)
- func PPrintParseTree(n Node, w io.Writer)
- func Quote(s string) string
- type Array
- type Assignment
- type Chunk
- type Compound
- type Error
- type ExitusRedir
- type ExprCtx
- type Form
- type Indexing
- type MapPair
- type MultiError
- type Node
- type Pipeline
- type Primary
- type PrimaryType
- type Redir
- type RedirMode
- type Sep
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func As ¶
As parses the given source as a node, depending on the dynamic type of n. If the error is not nil, it always has type MultiError.
func IsInlineWhitespace ¶
IsInlineWhitespace reports whether r is an inline whitespace character. Currently this includes space (Unicode 0x20) and tab (Unicode 0x9).
func IsWhitespace ¶
IsWhitespace reports whether r is a whitespace. Currently this includes inline whitespace characters and newline (Unicode 0xa).
func PPrintParseTree ¶
PPrintParseTree pretty-prints the parse tree part of a Node to a Writer.
Types ¶
type Array ¶
type Array struct { Compounds []*Compound // When non-empty, records the occurrences of semicolons by the indices of // the compounds they appear before. For instance, [; ; a b; c d;] results // in Semicolons={0 0 2 4}. Semicolons []int // contains filtered or unexported fields }
Array = { Space | '\n' } { Compound { Space | '\n' } }
func (*Array) Children ¶
func (n *Array) Children() []Node
Children returns all children of the node in the parse tree.
func (*Array) Parent ¶
func (n *Array) Parent() Node
Parent returns the parent node. If the node is the root of the syntax tree, the parent is nil.
func (*Array) Range ¶
Range returns the range within the original (full) source text that parses to the node.
func (*Array) SourceText ¶
func (n *Array) SourceText() string
SourceText returns the part of the source text that parses to the node.
type Assignment ¶
Assignment = Indexing '=' Compound
func (*Assignment) Children ¶
func (n *Assignment) Children() []Node
Children returns all children of the node in the parse tree.
func (*Assignment) Parent ¶
func (n *Assignment) Parent() Node
Parent returns the parent node. If the node is the root of the syntax tree, the parent is nil.
func (*Assignment) Range ¶
Range returns the range within the original (full) source text that parses to the node.
func (*Assignment) SourceText ¶
func (n *Assignment) SourceText() string
SourceText returns the part of the source text that parses to the node.
type Chunk ¶
type Chunk struct { Pipelines []*Pipeline // contains filtered or unexported fields }
Chunk = { PipelineSep | Space } { Pipeline { PipelineSep | Space } }
func AsChunk ¶
AsChunk parses the given source as a Chunk. If the error is not nil, it always has type MultiError.
func (*Chunk) Children ¶
func (n *Chunk) Children() []Node
Children returns all children of the node in the parse tree.
func (*Chunk) Parent ¶
func (n *Chunk) Parent() Node
Parent returns the parent node. If the node is the root of the syntax tree, the parent is nil.
func (*Chunk) Range ¶
Range returns the range within the original (full) source text that parses to the node.
func (*Chunk) SourceText ¶
func (n *Chunk) SourceText() string
SourceText returns the part of the source text that parses to the node.
type Compound ¶
type Compound struct { ExprCtx ExprCtx Indexings []*Indexing // contains filtered or unexported fields }
Compound = { Indexing }
func (*Compound) Children ¶
func (n *Compound) Children() []Node
Children returns all children of the node in the parse tree.
func (*Compound) Parent ¶
func (n *Compound) Parent() Node
Parent returns the parent node. If the node is the root of the syntax tree, the parent is nil.
func (*Compound) Range ¶
Range returns the range within the original (full) source text that parses to the node.
func (*Compound) SourceText ¶
func (n *Compound) SourceText() string
SourceText returns the part of the source text that parses to the node.
type ExitusRedir ¶
type ExitusRedir struct { Dest *Compound // contains filtered or unexported fields }
ExitusRedir = '?' '>' { Space } Compound
func (*ExitusRedir) Children ¶
func (n *ExitusRedir) Children() []Node
Children returns all children of the node in the parse tree.
func (*ExitusRedir) Parent ¶
func (n *ExitusRedir) Parent() Node
Parent returns the parent node. If the node is the root of the syntax tree, the parent is nil.
func (*ExitusRedir) Range ¶
Range returns the range within the original (full) source text that parses to the node.
func (*ExitusRedir) SourceText ¶
func (n *ExitusRedir) SourceText() string
SourceText returns the part of the source text that parses to the node.
type ExprCtx ¶
type ExprCtx int
ExprCtx represents special contexts of expression parsing.
const ( // NormalExpr represents a normal expression, namely none of the special // ones below. It is the default value. NormalExpr ExprCtx = iota // CmdExpr represents an expression used as the command in a form. In this // context, unquoted <>*^ are treated as bareword characters. CmdExpr // LHSExpr represents an expression used as the left-hand-side in either // assignments or map pairs. In this context, an unquoted = serves as an // expression terminator and is thus not treated as a bareword character. LHSExpr // BracedElemExpr represents an expression used as an element in a braced // expression. In this context, an unquoted , serves as an expression // terminator and is thus not treated as a bareword character. BracedElemExpr )
type Form ¶
type Form struct { Assignments []*Assignment Head *Compound // Left-hand-sides for the spacey assignment. Right-hand-sides are in Args. Vars []*Compound Args []*Compound Opts []*MapPair Redirs []*Redir ExitusRedir *ExitusRedir // contains filtered or unexported fields }
Form = { Space } { { Assignment } { Space } }
{ Compound } { Space } { ( Compound | MapPair | Redir | ExitusRedir ) { Space } }
func (*Form) Children ¶
func (n *Form) Children() []Node
Children returns all children of the node in the parse tree.
func (*Form) Parent ¶
func (n *Form) Parent() Node
Parent returns the parent node. If the node is the root of the syntax tree, the parent is nil.
func (*Form) Range ¶
Range returns the range within the original (full) source text that parses to the node.
func (*Form) SourceText ¶
func (n *Form) SourceText() string
SourceText returns the part of the source text that parses to the node.
type Indexing ¶
type Indexing struct { ExprCtx ExprCtx Head *Primary Indicies []*Array // contains filtered or unexported fields }
Indexing = Primary { '[' Array ']' }
func (*Indexing) Children ¶
func (n *Indexing) Children() []Node
Children returns all children of the node in the parse tree.
func (*Indexing) Parent ¶
func (n *Indexing) Parent() Node
Parent returns the parent node. If the node is the root of the syntax tree, the parent is nil.
func (*Indexing) Range ¶
Range returns the range within the original (full) source text that parses to the node.
func (*Indexing) SourceText ¶
func (n *Indexing) SourceText() string
SourceText returns the part of the source text that parses to the node.
type MapPair ¶
type MapPair struct {
Key, Value *Compound
// contains filtered or unexported fields
}
MapPair = '&' { Space } Compound { Space } Compound
func (*MapPair) Children ¶
func (n *MapPair) Children() []Node
Children returns all children of the node in the parse tree.
func (*MapPair) Parent ¶
func (n *MapPair) Parent() Node
Parent returns the parent node. If the node is the root of the syntax tree, the parent is nil.
func (*MapPair) Range ¶
Range returns the range within the original (full) source text that parses to the node.
func (*MapPair) SourceText ¶
func (n *MapPair) SourceText() string
SourceText returns the part of the source text that parses to the node.
type MultiError ¶
type MultiError struct {
Entries []*Error
}
MultiError stores multiple Error's and can pretty print them.
func (*MultiError) Error ¶
func (me *MultiError) Error() string
Error returns a string representation of the error.
func (*MultiError) PPrint ¶
func (me *MultiError) PPrint(indent string) string
PPrint pretty-prints the error.
type Node ¶
type Node interface { diag.Ranger SourceText() string Parent() Node Children() []Node // contains filtered or unexported methods }
Node represents a parse tree as well as an AST.
type Pipeline ¶
Pipeline = Form { '|' Form }
func (*Pipeline) Children ¶
func (n *Pipeline) Children() []Node
Children returns all children of the node in the parse tree.
func (*Pipeline) Parent ¶
func (n *Pipeline) Parent() Node
Parent returns the parent node. If the node is the root of the syntax tree, the parent is nil.
func (*Pipeline) Range ¶
Range returns the range within the original (full) source text that parses to the node.
func (*Pipeline) SourceText ¶
func (n *Pipeline) SourceText() string
SourceText returns the part of the source text that parses to the node.
type Primary ¶
type Primary struct { ExprCtx ExprCtx Type PrimaryType // The unquoted string value. Valid for Bareword, SingleQuoted, // DoubleQuoted, Variable, Wildcard and Tilde. Value string Elements []*Compound // Valid for List and Labda Chunk *Chunk // Valid for OutputCapture, ExitusCapture and Lambda MapPairs []*MapPair // Valid for Map and Lambda Braced []*Compound // Valid for Braced // contains filtered or unexported fields }
Primary is the smallest expression unit.
func (*Primary) Children ¶
func (n *Primary) Children() []Node
Children returns all children of the node in the parse tree.
func (*Primary) Parent ¶
func (n *Primary) Parent() Node
Parent returns the parent node. If the node is the root of the syntax tree, the parent is nil.
func (*Primary) Range ¶
Range returns the range within the original (full) source text that parses to the node.
func (*Primary) SourceText ¶
func (n *Primary) SourceText() string
SourceText returns the part of the source text that parses to the node.
type PrimaryType ¶
type PrimaryType int
PrimaryType is the type of a Primary.
const ( BadPrimary PrimaryType = iota Bareword SingleQuoted DoubleQuoted Variable Wildcard Tilde ExceptionCapture OutputCapture List Lambda Map Braced )
Possible values for PrimaryType.
func QuoteAs ¶
func QuoteAs(s string, q PrimaryType) (string, PrimaryType)
QuoteAs returns a representation of s in elvish syntax, preferring the syntax specified by q, which must be one of Bareword, SingleQuoted, or DoubleQuoted. It returns the quoted string and the actual quoting.
func (PrimaryType) String ¶
func (i PrimaryType) String() string
type Redir ¶
type Redir struct { Left *Compound Mode RedirMode RightIsFd bool Right *Compound // contains filtered or unexported fields }
Redir = { Compound } { '<'|'>'|'<>'|'>>' } { Space } ( '&'? Compound )
func (*Redir) Children ¶
func (n *Redir) Children() []Node
Children returns all children of the node in the parse tree.
func (*Redir) Parent ¶
func (n *Redir) Parent() Node
Parent returns the parent node. If the node is the root of the syntax tree, the parent is nil.
func (*Redir) Range ¶
Range returns the range within the original (full) source text that parses to the node.
func (*Redir) SourceText ¶
func (n *Redir) SourceText() string
SourceText returns the part of the source text that parses to the node.
type Sep ¶
type Sep struct {
// contains filtered or unexported fields
}
Sep is the catch-all node type for leaf nodes that lack internal structures and semantics, and serve solely for syntactic purposes. The parsing of separators depend on the Parent node; as such it lacks a genuine parse method.
func (*Sep) Children ¶
func (n *Sep) Children() []Node
Children returns all children of the node in the parse tree.
func (*Sep) Parent ¶
func (n *Sep) Parent() Node
Parent returns the parent node. If the node is the root of the syntax tree, the parent is nil.
func (*Sep) Range ¶
Range returns the range within the original (full) source text that parses to the node.
func (*Sep) SourceText ¶
func (n *Sep) SourceText() string
SourceText returns the part of the source text that parses to the node.