parse

package
v0.0.0-...-ed07a65 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2019 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package parse implements the elvish parser.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsArray

func IsArray(n Node) bool

func IsAssignment

func IsAssignment(n Node) bool

func IsChunk

func IsChunk(n Node) bool

func IsCompound

func IsCompound(n Node) bool

func IsExitusRedir

func IsExitusRedir(n Node) bool

func IsForm

func IsForm(n Node) bool

func IsIndexing

func IsIndexing(n Node) bool

func IsMapPair

func IsMapPair(n Node) bool

func IsPipeline

func IsPipeline(n Node) bool

func IsPrimary

func IsPrimary(n Node) bool

func IsRedir

func IsRedir(n Node) bool

func IsSep

func IsSep(n Node) bool

func IsSpace

func IsSpace(r rune) bool

func IsSpaceOrNewline

func IsSpaceOrNewline(r rune) bool

func PPrintAST

func PPrintAST(n Node) string

PPrintAST pretty-prints the AST part of a Node.

func PPrintASTTo

func PPrintASTTo(n Node, wr io.Writer)

PPrintASTTo pretty-prints the AST part of a Node to a Writer.

func PPrintParseTree

func PPrintParseTree(n Node) string

PPrintParseTree pretty-prints the parse tree part of a Node.

func PPrintParseTreeTo

func PPrintParseTreeTo(n Node, wr io.Writer)

PPrintParseTreeTo pretty-prints the parse tree part of a Node to a Writer.

func Quote

func Quote(s string) string

Quote returns a representation of s in elvish syntax. Bareword is tried first, then single quoted string and finally double quoted string.

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 GetArray

func GetArray(n Node) *Array

func ParseArray

func ParseArray(ps *Parser, allowSemicolon bool) *Array

func (*Array) Begin

func (n *Array) Begin() int

func (*Array) Children

func (n *Array) Children() []Node

func (*Array) End

func (n *Array) End() int

func (*Array) Parent

func (n *Array) Parent() Node

func (*Array) SourceText

func (n *Array) SourceText() string

type Assignment

type Assignment struct {
	Left  *Indexing
	Right *Compound
	// contains filtered or unexported fields
}

Assignment = Indexing '=' Compound

func GetAssignment

func GetAssignment(n Node) *Assignment

func ParseAssignment

func ParseAssignment(ps *Parser) *Assignment

func (*Assignment) Begin

func (n *Assignment) Begin() int

func (*Assignment) Children

func (n *Assignment) Children() []Node

func (*Assignment) End

func (n *Assignment) End() int

func (*Assignment) Parent

func (n *Assignment) Parent() Node

func (*Assignment) SourceText

func (n *Assignment) SourceText() string

type Chunk

type Chunk struct {
	Pipelines []*Pipeline
	// contains filtered or unexported fields
}

Chunk = { PipelineSep | Space } { Pipeline { PipelineSep | Space } }

func GetChunk

func GetChunk(n Node) *Chunk

func Parse

func Parse(srcname, src string) (*Chunk, error)

Parse parses Elvish source. If the error is not nil, it always has type ParseError.

func ParseChunk

func ParseChunk(ps *Parser) *Chunk

func (*Chunk) Begin

func (n *Chunk) Begin() int

func (*Chunk) Children

func (n *Chunk) Children() []Node

func (*Chunk) End

func (n *Chunk) End() int

func (*Chunk) Parent

func (n *Chunk) Parent() Node

func (*Chunk) SourceText

func (n *Chunk) SourceText() string

type Compound

type Compound struct {
	Indexings []*Indexing
	// contains filtered or unexported fields
}

Compound = { Indexing }

func GetCompound

func GetCompound(n Node) *Compound

func ParseCompound

func ParseCompound(ps *Parser, ctx ExprCtx) *Compound

func (*Compound) Begin

func (n *Compound) Begin() int

func (*Compound) Children

func (n *Compound) Children() []Node

func (*Compound) End

func (n *Compound) End() int

func (*Compound) Parent

func (n *Compound) Parent() Node

func (*Compound) SourceText

func (n *Compound) SourceText() string

type Error

type Error struct {
	Entries []*ErrorEntry
}

Error stores multiple ErrorEntry's and can pretty print them.

func (*Error) Add

func (pe *Error) Add(msg string, ctx *util.SourceRange)

func (*Error) Error

func (pe *Error) Error() string

func (*Error) Pprint

func (pe *Error) Pprint(indent string) string

type ErrorEntry

type ErrorEntry struct {
	Message string
	Context util.SourceRange
}

ErrorEntry represents one parse error.

type ExitusRedir

type ExitusRedir struct {
	Dest *Compound
	// contains filtered or unexported fields
}

ExitusRedir = '?' '>' { Space } Compound

func GetExitusRedir

func GetExitusRedir(n Node) *ExitusRedir

func ParseExitusRedir

func ParseExitusRedir(ps *Parser) *ExitusRedir

func (*ExitusRedir) Begin

func (n *ExitusRedir) Begin() int

func (*ExitusRedir) Children

func (n *ExitusRedir) Children() []Node

func (*ExitusRedir) End

func (n *ExitusRedir) End() int

func (*ExitusRedir) Parent

func (n *ExitusRedir) Parent() Node

func (*ExitusRedir) SourceText

func (n *ExitusRedir) SourceText() string

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.
	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 GetForm

func GetForm(n Node) *Form

func ParseForm

func ParseForm(ps *Parser) *Form

func (*Form) Begin

func (n *Form) Begin() int

func (*Form) Children

func (n *Form) Children() []Node

func (*Form) End

func (n *Form) End() int

func (*Form) Parent

func (n *Form) Parent() Node

func (*Form) SourceText

func (n *Form) SourceText() string

type Indexing

type Indexing struct {
	Head     *Primary
	Indicies []*Array
	// contains filtered or unexported fields
}

Indexing = Primary { '[' Array ']' }

func GetIndexing

func GetIndexing(n Node) *Indexing

func ParseIndexing

func ParseIndexing(ps *Parser, ctx ExprCtx) *Indexing

func (*Indexing) Begin

func (n *Indexing) Begin() int

func (*Indexing) Children

func (n *Indexing) Children() []Node

func (*Indexing) End

func (n *Indexing) End() int

func (*Indexing) Parent

func (n *Indexing) Parent() Node

func (*Indexing) SourceText

func (n *Indexing) SourceText() string

type MapPair

type MapPair struct {
	Key, Value *Compound
	// contains filtered or unexported fields
}

MapPair = '&' { Space } Compound { Space } Compound

func GetMapPair

func GetMapPair(n Node) *MapPair

func ParseMapPair

func ParseMapPair(ps *Parser) *MapPair

func (*MapPair) Begin

func (n *MapPair) Begin() int

func (*MapPair) Children

func (n *MapPair) Children() []Node

func (*MapPair) End

func (n *MapPair) End() int

func (*MapPair) Parent

func (n *MapPair) Parent() Node

func (*MapPair) SourceText

func (n *MapPair) SourceText() string

type Node

type Node interface {
	Parent() Node
	Begin() int
	End() int
	SourceText() string
	Children() []Node
	// contains filtered or unexported methods
}

Node represents a parse tree as well as an AST.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser maintains some mutable states of parsing.

NOTE: The str member is assumed to be valid UF-8.

func NewParser

func NewParser(srcname, src string) *Parser

NewParser creates a new parser from a piece of source text and its name.

func (*Parser) Done

func (ps *Parser) Done()

Done tells the parser that parsing has completed.

func (*Parser) Errors

func (ps *Parser) Errors() error

Errors gets the parsing errors after calling one of the parse* functions. If the return value is not nil, it is always of type Error.

func (*Parser) Source

func (ps *Parser) Source() string

Source returns the source code that is being parsed.

type Pipeline

type Pipeline struct {
	Forms      []*Form
	Background bool
	// contains filtered or unexported fields
}

Pipeline = Form { '|' Form }

func GetPipeline

func GetPipeline(n Node) *Pipeline

func ParsePipeline

func ParsePipeline(ps *Parser) *Pipeline

func (*Pipeline) Begin

func (n *Pipeline) Begin() int

func (*Pipeline) Children

func (n *Pipeline) Children() []Node

func (*Pipeline) End

func (n *Pipeline) End() int

func (*Pipeline) Parent

func (n *Pipeline) Parent() Node

func (*Pipeline) SourceText

func (n *Pipeline) SourceText() string

type Primary

type Primary struct {
	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 GetPrimary

func GetPrimary(n Node) *Primary

func ParsePrimary

func ParsePrimary(ps *Parser, ctx ExprCtx) *Primary

func (*Primary) Begin

func (n *Primary) Begin() int

func (*Primary) Children

func (n *Primary) Children() []Node

func (*Primary) End

func (n *Primary) End() int

func (*Primary) Parent

func (n *Primary) Parent() Node

func (*Primary) SourceText

func (n *Primary) SourceText() string

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, using 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 GetRedir

func GetRedir(n Node) *Redir

func ParseRedir

func ParseRedir(ps *Parser, dest *Compound) *Redir

func (*Redir) Begin

func (n *Redir) Begin() int

func (*Redir) Children

func (n *Redir) Children() []Node

func (*Redir) End

func (n *Redir) End() int

func (*Redir) Parent

func (n *Redir) Parent() Node

func (*Redir) SourceText

func (n *Redir) SourceText() string

type RedirMode

type RedirMode int

RedirMode records the mode of an IO redirection.

const (
	BadRedirMode RedirMode = iota
	Read
	Write
	ReadWrite
	Append
)

Possible values for RedirMode.

func (RedirMode) String

func (i RedirMode) String() 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 GetSep

func GetSep(n Node) *Sep

func NewSep

func NewSep(src string, begin, end int) *Sep

func (*Sep) Begin

func (n *Sep) Begin() int

func (*Sep) Children

func (n *Sep) Children() []Node

func (*Sep) End

func (n *Sep) End() int

func (*Sep) Parent

func (n *Sep) Parent() Node

func (*Sep) SourceText

func (n *Sep) SourceText() string

Directories

Path Synopsis
Package parseutil contains utilities built on top of the parse package.
Package parseutil contains utilities built on top of the parse package.

Jump to

Keyboard shortcuts

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