parse

package
v0.0.0-...-5283227 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2014 License: BSD-2-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package parse parses elvish source. Derived from stdlib package text/template/parse.

Index

Constants

This section is empty.

Variables

View Source
var ItemTypeNames = []string{
	"ItemError",
	"ItemEOF",
	"ItemEndOfLine",
	"ItemSpace",
	"ItemBare",
	"ItemSingleQuoted",
	"ItemDoubleQuoted",
	"ItemRedirLeader",
	"ItemStatusRedirLeader",
	"ItemPipe",
	"ItemQuestionLParen",
	"ItemLParen",
	"ItemRParen",
	"ItemLBracket",
	"ItemRBracket",
	"ItemLBrace",
	"ItemRBrace",
	"ItemDollar",
	"ItemCaret",
	"ItemSemicolon",
	"ItemAmpersand",
}

ItemType names.

Functions

func Atou

func Atou(s string) (uintptr, error)

Atou is basically shorthand for strconv.ParseUint(s, 10, 0) but returns the first argument as uintptr. Useful for parsing fd.

func StartsBare

func StartsBare(r rune) bool

StartsBare determines whether r may be the first rune of a bareword.

func TerminatesBare

func TerminatesBare(r rune) bool

TerminatesBare determines whether r terminates a bareword.

Types

type ChunkNode

type ChunkNode struct {
	Pos
	Nodes []*PipelineNode
}

ChunkNode is a list of FormNode's.

func Parse

func Parse(name, text string) (*ChunkNode, error)

Parse is a shorthand for constructing a Paser, call Parse and take out its Root.

type CloseRedir

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

CloseRedir represents the closing of a fd, like >[2=].

func (*CloseRedir) Fd

func (r *CloseRedir) Fd() uintptr

type ClosureNode

type ClosureNode struct {
	Pos
	ArgNames   *TermListNode
	Chunk      *ChunkNode
	Annotation interface{}
}

ClosureNode holds a closure literal.

type Context

type Context struct {
	Typ         ContextType
	CommandTerm *TermNode
	PrevTerms   *TermListNode
	PrevFactors *TermNode
	ThisFactor  *FactorNode
}

Context contains information from the AST useful for tab completion.

func Complete

func Complete(name, text string) (*Context, error)

Complete is a shorthand for constructing a Paser, call Parse and take out its Ctx.

func (*Context) EvalPlain

func (c *Context) EvalPlain() (pctx *PlainContext)

type ContextType

type ContextType int

ContextType categorizes Context.

const (
	CommandContext ContextType = iota
	ArgContext
	RedirFilenameContext
)

ContextType values.

type FactorNode

type FactorNode struct {
	Pos
	Typ  FactorType
	Node Node
}

FactorNode represents a factor.

type FactorType

type FactorType int

FactorType determines the type of a FactorNode.

const (
	StringFactor        FactorType = iota // string literal: a `a` a
	VariableFactor                        // variable: $a
	TableFactor                           // table: [a b c &k v]
	ClosureFactor                         // closure: {|a| cmd}
	ListFactor                            // list: {a b c}
	OutputCaptureFactor                   // output capture: (cmd1|cmd2)
	StatusCaptureFactor                   // status capture: ?(cmd1|cmd2)
)

FactorType constants.

type FdRedir

type FdRedir struct {
	OldFd uintptr
	// contains filtered or unexported fields
}

FdRedir represents redirection into another fd, like >[2=3].

func NewFdRedir

func NewFdRedir(pos Pos, fd, oldFd uintptr) *FdRedir

NewFdRedir creates a new FdRedir. Public since we need to turn FilenameRedir -> FdRedir when evaluating commands.

func (*FdRedir) Fd

func (r *FdRedir) Fd() uintptr

type FilenameRedir

type FilenameRedir struct {
	Flag     int
	Filename *TermNode
	// contains filtered or unexported fields
}

FilenameRedir represents redirection into a file, like >a.txt

func (*FilenameRedir) Fd

func (r *FilenameRedir) Fd() uintptr

type FormNode

type FormNode struct {
	Pos
	Command     *TermNode
	Args        *TermListNode
	Redirs      []Redir
	StatusRedir string
}

FormNode holds a form.

type Item

type Item struct {
	Typ ItemType // The type of this Item.
	Pos Pos      // The starting position, in bytes, of this Item in the input string.
	Val string   // The value of this Item.
	End ItemEnd  // How an Item ends.
}

Item represents a token or text string returned from the scanner.

func (Item) GoString

func (i Item) GoString() string

GoString returns the Go representation of an Item.

func (Item) String

func (i Item) String() string

type ItemEnd

type ItemEnd int

ItemEnd describes the ending of lex items.

const (
	MayTerminate ItemEnd = 1 << iota
	MayContinue
	ItemTerminated   ItemEnd = MayTerminate
	ItemUnterminated ItemEnd = MayContinue
	ItemAmbiguious   ItemEnd = MayTerminate | MayContinue
)

ItemEnd constants.

type ItemType

type ItemType int

ItemType identifies the type of lex items.

const (
	ItemError ItemType = iota // error occurred; value is text of error

	ItemEOF               // end of file, always the last Item yielded
	ItemEndOfLine         // a single EOL
	ItemSpace             // run of spaces separating arguments
	ItemBare              // a bare string literal
	ItemSingleQuoted      // a single-quoted string literal
	ItemDoubleQuoted      // a double-quoted string literal
	ItemRedirLeader       // IO redirection leader
	ItemStatusRedirLeader // status redirection leader, "?>"
	ItemPipe              // pipeline connector, '|'
	ItemQuestionLParen    // question + left paren "?("
	ItemLParen            // left paren '('
	ItemRParen            // right paren ')'
	ItemLBracket          // left bracket '['
	ItemRBracket          // right bracket ']'
	ItemLBrace            // left brace '{'
	ItemRBrace            // right brace '}'
	ItemDollar            // dollar sign '$'
	ItemCaret             // caret sign '^'
	ItemSemicolon         // semicolon ';'
	ItemAmpersand         // ampersand '&'
	ItemTypeCount
)

ItemType constants.

func (ItemType) String

func (it ItemType) String() string

type Lexer

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

Lexer holds the state of the scanner.

func Lex

func Lex(name, input string) *Lexer

Lex creates a new scanner for the input string.

func (*Lexer) Chan

func (l *Lexer) Chan() chan Item

Chan returns a channel of Item's.

func (*Lexer) NextItem

func (l *Lexer) NextItem() Item

NextItem returns the next Item from the input.

type Node

type Node interface {
	Position() Pos // byte position of start of node in full original input string
	// contains filtered or unexported methods
}

A Node is an element in the parse tree. The interface is trivial. The interface contains an unexported method so that only types local to this package can satisfy it.

type Parser

type Parser struct {
	Name string // name of the script represented by the tree.

	Root *ChunkNode // top-level root of the tree.
	Ctx  *Context
	// contains filtered or unexported fields
}

Parser maintains the states during parsing.

func NewParser

func NewParser(name string) *Parser

NewParser allocates a new parse tree with the given name.

func (*Parser) Parse

func (p *Parser) Parse(text string, completing bool) (err error)

Parse parses the script to construct a representation of the script for execution.

type PipelineNode

type PipelineNode struct {
	Pos
	Nodes []*FormNode
}

PipelineNode is a list of FormNode's.

type PlainContext

type PlainContext struct {
	Typ         ContextType
	CommandTerm string
	PrevTerms   []string
	PrevFactors string
	ThisFactor  *FactorNode
}

type Pos

type Pos int

Pos represents a byte position in the original input text from which this source was parsed.

func (Pos) Position

func (p Pos) Position() Pos

Position returns p itself.

type Redir

type Redir interface {
	Node
	Fd() uintptr
	// contains filtered or unexported methods
}

Redir represents a single IO redirection. Its concrete type may be one of the *Redir types below.

type StringNode

type StringNode struct {
	Pos
	Quoted string // The original text of the string, with quotes.
	Text   string // The string, after quote processing.
}

StringNode holds a string literal. The value has been "unquoted".

type TableNode

type TableNode struct {
	Pos
	List []*TermNode
	Dict []*TablePair
}

TableNode holds a table literal.

type TablePair

type TablePair struct {
	Key   *TermNode
	Value *TermNode
}

TablePair represents a key/value pair in table literal.

type TermListNode

type TermListNode struct {
	Pos
	Nodes []*TermNode
}

TermListNode is a list of TermNode's.

type TermNode

type TermNode struct {
	Pos
	Nodes []*FactorNode
}

TermNode is a list of FactorNode's.

Notes

Bugs

  • When completing, unterminated quoted string results in errors

Jump to

Keyboard shortcuts

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