package parse
import "src.elv.sh/pkg/parse"
Package parse implements parsing of Elvish code.
The entrypoint of this package is Parse and the more low-level ParseAs.
This package defines many types that implement the Node interface, Chunk being the root node. Each node can be thought of as a AST/parse tree hybrid:
To access the semantically relevant information of a node (as an AST), use its exported fields.
To access all its children (as a parse tree), call Children.
Internally, this package uses a handwritten recursive-descent parser. There's no separate tokenization phase; the parser consumes the source text directly.
Index ¶
- func IsInlineWhitespace(r rune) bool
- func IsWhitespace(r rune) bool
- func ParseAs(src Source, n Node, cfg Config) error
- func Quote(s string) string
- func QuoteCommandName(s string) string
- func QuoteVariableName(s string) string
- func SourceText(n Node) string
- func ValidLHSVariable(p *Primary, allowSigil bool) bool
- type Array
- type Chunk
- type Compound
- type Config
- type Error
- type ErrorTag
- type ExprCtx
- type Filter
- type Form
- type Indexing
- type MapPair
- type Node
- type Pipeline
- type Primary
- type PrimaryType
- type Redir
- type RedirMode
- type Sep
- type Source
- type Tree
Functions ¶
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 ParseAs ¶
ParseAs parses the given source as a node, depending on the dynamic type of n. The returned error may contain one or more parse error, which can be unpacked with UnpackErrors.
func Quote ¶
Quote returns a valid Elvish expression that evaluates to the given string. If s is a valid bareword, it is returned as is; otherwise it is quoted, preferring the use of single quotes.
func QuoteCommandName ¶
QuoteCommandName is like Quote, but uses the slightly laxer rule for what can appear in a command name unquoted, like <.
func QuoteVariableName ¶
QuoteVariableName is like Quote, but quotes s if it contains any character that may not appear unquoted in variable names.
func SourceText ¶
SourceText returns the part of the source text that parses to the node.
func ValidLHSVariable ¶
ValidLHSVariable returns whether a Primary node containing a variable name being used as the LHS of an assignment form without the $ prefix is valid.
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) Range ¶
Range returns the range within the full 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 (*Chunk) Range ¶
Range returns the range within the full 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) Range ¶
Range returns the range within the full source text that parses to the node.
type Config ¶
type Config struct { // Destination of warnings. If nil, warnings are suppressed. WarningWriter io.Writer }
Config keeps configuration options when parsing.
type Error ¶
Error is a parse error.
func UnpackErrors ¶
UnpackErrors returns the constituent parse errors if the given error contains one or more parse errors. Otherwise it returns nil.
type ErrorTag ¶
type ErrorTag struct{}
ErrorTag parameterizes diag.Error to define Error.
func (ErrorTag) ErrorTag ¶
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 )
func (ExprCtx) String ¶
type Filter ¶
Filter is the Elvish filter DSL. It uses the same syntax as arguments and options to a command.
func (*Filter) Range ¶
Range returns the range within the full source text that parses to the node.
type Form ¶
type Form struct { Head *Compound Args []*Compound Opts []*MapPair Redirs []*Redir // contains filtered or unexported fields }
Form = { Compound-CmdExpr } { Space } { ( Compound | MapPair | Redir ) { Space } }
func (*Form) Range ¶
Range returns the range within the full source text that parses to the node.
type Indexing ¶
type Indexing struct { ExprCtx ExprCtx Head *Primary Indices []*Array // contains filtered or unexported fields }
Indexing = Primary { '[' Array ']' }
func (*Indexing) Range ¶
Range returns the range within the full 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) Range ¶
Range returns the range within the full source text that parses to the node.
type Node ¶
Node represents a parse tree as well as an AST.
func Children ¶
Children returns all children of the node in the parse tree.
func Parent ¶
Parent returns the parent of a node. It returns nil if the node is the root of the parse tree.
type Pipeline ¶
Pipeline = Form { '|' Form }
func (*Pipeline) Range ¶
Range returns the range within the full 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 Lambda 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) Range ¶
Range returns the range within the full 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) Range ¶
Range returns the range within the full source text that parses to the node.
type RedirMode ¶
type RedirMode int
RedirMode records the mode of an IO redirection.
Possible values for RedirMode.
func (RedirMode) String ¶
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 NewSep ¶
NewSep makes a new Sep.
func (*Sep) Range ¶
Range returns the range within the full source text that parses to the node.
type Source ¶
Source describes a piece of source code.
func SourceForTest ¶
SourceForTest returns a Source used for testing.
func (Source) IsStructMap ¶
func (src Source) IsStructMap()
IsStructMap marks that Source is a structmap.
type Tree ¶
Tree represents a parsed tree.
func Parse ¶
Parse parses the given source. The returned error may contain one or more parse error, which can be unpacked with UnpackErrors.
Source Files ¶
node.go parse.go parser.go pprint.go quote.go source.go zstring.go
Directories ¶
Path | Synopsis |
---|---|
pkg/parse/cmpd | Package cmpd contains utilities for working with compound nodes. |
pkg/parse/np | Package np provides utilities for working with node paths from a leaf of a parse tree to the root. |
pkg/parse/parseutil | Package parseutil contains utilities built on top of the parse package. |
- Version
- v0.21.0 (latest)
- Published
- Aug 13, 2024
- Platform
- linux/amd64
- Imports
- 11 packages
- Last checked
- 2 days ago –
Tools for package owners.