ast

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2022 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Lexer lexes HLB into tokens for the parser.
	Lexer = lexer.MustStateful(lexer.Rules{
		"Root": {
			{"Keyword", `\b(import|export|with|as)\b`, nil},
			{"Numeric", `\b(0(b|B|o|O|x|X)[a-fA-F0-9]+)\b`, nil},
			{"Decimal", `\b(0|[1-9][0-9]*)\b`, nil},
			{"Bool", `\b(true|false)\b`, nil},
			{"String", `"`, lexer.Push("String")},
			{"RawString", "`", lexer.Push("RawString")},
			{"Heredoc", `<<[-~]?(\w+)\b`, lexer.Push("Heredoc")},
			{"RawHeredoc", "<<[-~]?`(\\w+)`", lexer.Push("RawHeredoc")},
			{"Block", `{`, lexer.Push("Block")},
			{"Paren", `\(`, lexer.Push("Paren")},
			{"Ident", `[\w:]+`, lexer.Push("Reference")},
			{"Operator", `;`, nil},
			{"Newline", `\n`, nil},
			{"Comment", `#[^\n]*\n`, nil},
			{"Whitespace", `[\r\t ]+`, nil},
		},
		"Reference": {
			{"Dot", `\.`, nil},
			{"Ident", `[\w:]+`, nil},
			lexer.Return(),
		},
		"String": {
			{"StringEnd", `"`, lexer.Pop()},
			{"Escaped", `\\.`, nil},
			{"Interpolated", `\${`, lexer.Push("Interpolated")},
			{"Char", `\$|[^"$\\]+`, nil},
		},
		"RawString": {
			{"RawStringEnd", "`", lexer.Pop()},
			{"RawChar", "[^`]+", nil},
		},
		"Heredoc": {
			{"HeredocEnd", `\b\1\b`, lexer.Pop()},
			{"Spaces", `\s+`, nil},
			{"Escaped", `\\.`, nil},
			{"Interpolated", `\${`, lexer.Push("Interpolated")},
			{"Text", `\$|[^\s$]+`, nil},
		},
		"RawHeredoc": {
			{"RawHeredocEnd", `\b\1\b`, lexer.Pop()},
			{"Spaces", `\s+`, nil},
			{"RawText", `[^\s]+`, nil},
		},
		"Interpolated": {
			{"BlockEnd", `}`, lexer.Pop()},
			lexer.Include("Root"),
		},
		"Block": {
			{"BlockEnd", `}`, lexer.Pop()},
			lexer.Include("Root"),
		},
		"Paren": {
			{"ParenEnd", `\)`, lexer.Pop()},
			{"Delimit", `,`, nil},
			lexer.Include("Root"),
		},
	})

	// Parser parses HLB into a concrete syntax tree rooted from a Module.
	Parser = participle.MustBuild(
		&Module{},
		participle.Lexer(Lexer),
		participle.Elide("Whitespace"),
	)
)

Functions

func IsIntersect

func IsIntersect(start, end lexer.Position, line, column int) bool

IsIntersect returns true if a line column is within a start and end position.

func IsPositionWithinNode

func IsPositionWithinNode(node Node, line, column int) bool

IsPositionWithinNode returns true if a line column is within a node's position.

func Match

func Match(root Node, opts MatchOpts, funs ...interface{})

Match walks a CST and invokes given functions if their arguments match a non-contiguous sequence of current path walked. This is useful when you want to walk to a specific type of Node, while having access to specific parents of the Node.

The function arguments must all implement the Node interface, and may be a non-contiguous sequence. That is, you don't have to specify every CST structure.

The sequence is matched right to left, from the deepest node first. The final argument will always be the current node being visited.

When multiple functions are matched, they are invoked in the order given to Match. That way, you can write functions that positively match, and then provide a more general function as a catch all without walking the CST a second time.

For example, you can invoke Match to find CallStmts inside FuncLits: ```go Match(root, MatchOpts{},

func(lit *FuncLit, call *CallStmt) {
	fmt.Println(lit.Pos, call.Pos)
},

) ```

func StopNodeFilter

func StopNodeFilter(n Node) bool

StopNodeFilter returns true if Node n is a StopNode.

func Walk

func Walk(node Node, v Visitor)

Walk traverses an CST in depth-first order: It starts by calling v.Visit(node); node must not be nil. If the visitor w returned by v.Visit(node) is not nil, Walk is invoked recursively with visitor w for each of the non-nil children of node, followed by a call of w.Visit(nil).

func WithModules

func WithModules(ctx context.Context, mods *ModuleLookup) context.Context

Types

type As

type As struct {
	Mixin
	Text string `parser:"@'as'"`
}

As represents the keyword "as".

func (*As) String

func (a *As) String() string

func (*As) Unparse

func (a *As) Unparse(opts ...UnparseOption) string

type Backtick

type Backtick struct {
	Mixin
	Text string `parser:"@(RawString | RawStringEnd)"`
}

Backtick represents the "`" backtick.

func (*Backtick) String

func (b *Backtick) String() string

func (*Backtick) Unparse

func (b *Backtick) Unparse(opts ...UnparseOption) string

type BasicLit

type BasicLit struct {
	Mixin
	Decimal    *int          `parser:"( @Decimal"`
	Numeric    *NumericLit   `parser:"| @Numeric"`
	Bool       *bool         `parser:"| @Bool"`
	Str        *StringLit    `parser:"| @@"`
	RawString  *RawStringLit `parser:"| @@"`
	Heredoc    *Heredoc      `parser:"| @@"`
	RawHeredoc *RawHeredoc   `parser:"| @@ )"`
}

BasicLit represents a literal of basic type.

func (*BasicLit) Kind

func (bl *BasicLit) Kind() Kind

Kind returns the type of the basic literal.

func (*BasicLit) String

func (bl *BasicLit) String() string

func (*BasicLit) Unparse

func (bl *BasicLit) Unparse(opts ...UnparseOption) string

type Bind

type Bind struct {
	Mixin
	Field  *Field
	Source *Ident `parser:"@@"`
	Target *Ident `parser:"@@"`
}

Bind represents the binding of a CallStmt's side effect Source to the identifier Target.

func (*Bind) String

func (b *Bind) String() string

func (*Bind) Unparse

func (b *Bind) Unparse(opts ...UnparseOption) string

type BindClause

type BindClause struct {
	Mixin
	Closure *FuncDecl
	Effects *FieldList
	As      *As       `parser:"@@"`
	Ident   *Ident    `parser:"( @@"`
	Binds   *BindList `parser:"| @@ )?"`
}

BindClause represents the entire "as ..." clause on a CallStmt, with either a default side effect or a list of Binds.

func (*BindClause) SourceBinding

func (bc *BindClause) SourceBinding(source string) *Binding

func (*BindClause) String

func (bc *BindClause) String() string

func (*BindClause) TargetBinding

func (bc *BindClause) TargetBinding(target string) *Binding

func (*BindClause) Unparse

func (bc *BindClause) Unparse(opts ...UnparseOption) string

type BindList

type BindList struct {
	Mixin
	Start     *OpenParen  `parser:"@@"`
	Stmts     []*BindStmt `parser:"@@*"`
	Terminate *CloseParen `parser:"@@"`
}

BindList is a parenthetical list of Binds.

func (*BindList) Binds

func (bl *BindList) Binds() []*Bind

func (*BindList) String

func (bl *BindList) String() string

func (*BindList) Unparse

func (bl *BindList) Unparse(opts ...UnparseOption) string

type BindStmt

type BindStmt struct {
	Mixin
	Bind     *Bind         `parser:"( @@ Delimit?"`
	Newline  *Newline      `parser:"| @@"`
	Comments *CommentGroup `parser:"| @@ )"`
}

BindStmt represents a statement in list of binds.

func (*BindStmt) String

func (bs *BindStmt) String() string

func (*BindStmt) Unparse

func (bs *BindStmt) Unparse(opts ...UnparseOption) string

type Binding

type Binding struct {
	Name  *Ident
	Bind  *BindClause
	Field *Field
}

Binding is a value type that represents the call site where a single side effect is bound.

func (*Binding) Binds

func (b *Binding) Binds() string

type Binds

type Binds struct {
	Mixin
	Text string `parser:"@'binds'"`
}

Binds represents the keyword "binds".

func (*Binds) String

func (b *Binds) String() string

func (*Binds) Unparse

func (b *Binds) Unparse(opts ...UnparseOption) string

type BlockStmt

type BlockStmt struct {
	Mixin
	Scope     *Scope
	Type      *Type
	Closure   *FuncDecl
	Start     *OpenBrace  `parser:"@@"`
	List      []*Stmt     `parser:"@@*"`
	Terminate *CloseBrace `parser:"@@"`
}

BlockStmt represents a braced statement list.

func NewBlockStmt

func NewBlockStmt(stmts ...*Stmt) *BlockStmt

func (*BlockStmt) Kind

func (bs *BlockStmt) Kind() Kind

func (*BlockStmt) Stmts

func (bs *BlockStmt) Stmts() []*Stmt

func (*BlockStmt) String

func (bs *BlockStmt) String() string

func (*BlockStmt) Unparse

func (bs *BlockStmt) Unparse(opts ...UnparseOption) string

type BuiltinDecl

type BuiltinDecl struct {
	*Module
	Name           string
	Kinds          []Kind
	FuncDeclByKind map[Kind]*FuncDecl
}

BuiltinDecl is a synthetic declaration representing a builtin name. Special type checking rules apply to builtins.

func (*BuiltinDecl) FuncDecl

func (bd *BuiltinDecl) FuncDecl(kind Kind) *FuncDecl

func (*BuiltinDecl) String

func (bd *BuiltinDecl) String() string

func (*BuiltinDecl) Unparse

func (bd *BuiltinDecl) Unparse(opts ...UnparseOption) string

type CallExpr

type CallExpr struct {
	Mixin
	Sig  []Kind
	Name *IdentExpr `parser:"@@"`
	List *ExprList  `parser:"@@?"`
}

CallExpr represents a short-hand way of invoking a function as an expression.

func (*CallExpr) Arguments

func (ce *CallExpr) Arguments() []*Expr

func (*CallExpr) Breakpoint

func (ce *CallExpr) Breakpoint() bool

func (*CallExpr) Ident

func (ce *CallExpr) Ident() *Ident

func (*CallExpr) Signature

func (ce *CallExpr) Signature() []Kind

func (*CallExpr) String

func (ce *CallExpr) String() string

func (*CallExpr) Subject

func (ce *CallExpr) Subject() Node

func (*CallExpr) Unparse

func (ce *CallExpr) Unparse(opts ...UnparseOption) string

type CallNode

type CallNode interface {
	Node
	Ident() *Ident
	Signature() []Kind
	Arguments() []*Expr
}

type CallStmt

type CallStmt struct {
	Mixin
	Doc        *CommentGroup
	Sig        []Kind
	Name       *IdentExpr  `parser:"@@"`
	Args       []*Expr     `parser:"@@*"`
	WithClause *WithClause `parser:"@@?"`
	BindClause *BindClause `parser:"@@?"`
	Terminate  *StmtEnd    `parser:"@@?"`
}

CallStmt represents an function name followed by an argument list, and an optional WithClause.

func (*CallStmt) Arguments

func (cs *CallStmt) Arguments() []*Expr

func (*CallStmt) Breakpoint

func (cs *CallStmt) Breakpoint() bool

func (*CallStmt) Ident

func (cs *CallStmt) Ident() *Ident

func (*CallStmt) Signature

func (cs *CallStmt) Signature() []Kind

func (*CallStmt) String

func (cs *CallStmt) String() string

func (*CallStmt) Subject

func (cs *CallStmt) Subject() Node

func (*CallStmt) Unparse

func (cs *CallStmt) Unparse(opts ...UnparseOption) string

type CloseBrace

type CloseBrace struct {
	Mixin
	Text string `parser:"@BlockEnd"`
}

CloseBrace represents the "}" brace.

type CloseParen

type CloseParen struct {
	Mixin
	Text string `parser:"@ParenEnd"`
}

CloseParent represents the ")" parenthese.

type Comment

type Comment struct {
	Mixin
	Text string `parser:"@Comment"`
}

Comment represents a single comment.

func (*Comment) String

func (c *Comment) String() string

func (*Comment) Unparse

func (c *Comment) Unparse(opts ...UnparseOption) string

type CommentGroup

type CommentGroup struct {
	Mixin
	List []*Comment `parser:"( @@ )+"`
}

CommentGroup represents a sequence of comments with no other tokens and no empty lines between.

func (*CommentGroup) NumComments

func (g *CommentGroup) NumComments() int

NumComments returns the number of comments in CommentGroup.

func (*CommentGroup) String

func (cg *CommentGroup) String() string

func (*CommentGroup) Unparse

func (cg *CommentGroup) Unparse(opts ...UnparseOption) string

type Decl

type Decl struct {
	Mixin
	Import   *ImportDecl   `parser:"( @@"`
	Export   *ExportDecl   `parser:"| @@"`
	Func     *FuncDecl     `parser:"| @@"`
	Newline  *Newline      `parser:"| @@"`
	Comments *CommentGroup `parser:"| @@ )"`
}

Decl represents a declaration node.

func (*Decl) String

func (d *Decl) String() string

func (*Decl) Unparse

func (d *Decl) Unparse(opts ...UnparseOption) string

type DeprecatedImportDecl

type DeprecatedImportDecl struct {
	Pos        lexer.Position
	Import     *Import     `parser:"@@"`
	Ident      *Ident      `parser:"@@"`
	ImportFunc *ImportFunc `parser:"( @@"`
	ImportPath *ImportPath `parser:"| @@ )"`
}

DeprecatedImportDecl represents an import declaration.

func (*DeprecatedImportDecl) End

func (*DeprecatedImportDecl) Position

func (d *DeprecatedImportDecl) Position() lexer.Position

type Directory

type Directory interface {
	Path() string

	Digest() digest.Digest

	Definition() *llb.Definition

	Open(filename string) (io.ReadCloser, error)

	Stat(filename string) (os.FileInfo, error)

	Close() error
}

Directory represents the abstract directory that modules can be read from. The directory can be a local path or the filesystem of a remote import.

type EffectsClause

type EffectsClause struct {
	Mixin
	Binds   *Binds     `parser:"@@"`
	Effects *FieldList `parser:"@@"`
}

EffectsClause represents the side effect "binds ..." clause for a function.

func NewEffectsClause

func NewEffectsClause(effect ...*Field) *EffectsClause

func (*EffectsClause) String

func (ec *EffectsClause) String() string

func (*EffectsClause) Unparse

func (ec *EffectsClause) Unparse(opts ...UnparseOption) string

type Export

type Export struct {
	Mixin
	Text string `parser:"@'export'"`
}

Export represents the keyword "export".

func (*Export) String

func (e *Export) String() string

func (*Export) Unparse

func (e *Export) Unparse(opts ...UnparseOption) string

type ExportDecl

type ExportDecl struct {
	Mixin
	Export *Export `parser:"@@"`
	Name   *Ident  `parser:"@@"`
}

ExportDecl represents an export declaration.

func (*ExportDecl) String

func (ed *ExportDecl) String() string

func (*ExportDecl) Unparse

func (ed *ExportDecl) Unparse(opts ...UnparseOption) string

type Expr

type Expr struct {
	Mixin
	FuncLit  *FuncLit  `parser:"( @@"`
	BasicLit *BasicLit `parser:"| @@"`
	CallExpr *CallExpr `parser:"| @@ )"`
}

Expr represents an expression node.

func NewBoolExpr

func NewBoolExpr(v bool) *Expr

func NewDecimalExpr

func NewDecimalExpr(v int) *Expr

func NewFuncLitExpr

func NewFuncLitExpr(kind Kind, stmts ...*Stmt) *Expr

func NewNumericExpr

func NewNumericExpr(v int64, base int) *Expr

func NewStringExpr

func NewStringExpr(v string) *Expr

func (*Expr) Kind

func (e *Expr) Kind() Kind

func (*Expr) String

func (e *Expr) String() string

func (*Expr) Unparse

func (e *Expr) Unparse(opts ...UnparseOption) string

type ExprField

type ExprField struct {
	Mixin
	Expr     *Expr         `parser:"( @@ Delimit?"`
	Newline  *Newline      `parser:"| @@"`
	Comments *CommentGroup `parser:"| @@ )"`
}

ExprField represents a statement in a list of expressions.

func (*ExprField) String

func (ef *ExprField) String() string

func (*ExprField) Unparse

func (ef *ExprField) Unparse(opts ...UnparseOption) string

type ExprList

type ExprList struct {
	Mixin
	Start     *OpenParen   `parser:"@@"`
	Fields    []*ExprField `parser:"@@*"`
	Terminate *CloseParen  `parser:"@@"`
}

ExprList represents a list of expressions enclosed in parentheses.

func (*ExprList) String

func (el *ExprList) String() string

func (*ExprList) Unparse

func (el *ExprList) Unparse(opts ...UnparseOption) string

type ExprStmt

type ExprStmt struct {
	Mixin
	Expr      *Expr    `parser:"@@"`
	Terminate *StmtEnd `parser:"@@?"`
}

ExprStmt represents a statement returning an expression.

func (*ExprStmt) String

func (es *ExprStmt) String() string

func (*ExprStmt) Unparse

func (es *ExprStmt) Unparse(opts ...UnparseOption) string

type Field

type Field struct {
	Mixin
	Modifier *Modifier `parser:"@@?"`
	Type     *Type     `parser:"@@"`
	Name     *Ident    `parser:"@@"`
}

Field represents a parameter declaration in a signature.

func NewField

func NewField(kind Kind, name string, variadic bool) *Field

func (*Field) Kind

func (f *Field) Kind() Kind

func (*Field) String

func (f *Field) String() string

func (*Field) Unparse

func (f *Field) Unparse(opts ...UnparseOption) string

type FieldList

type FieldList struct {
	Mixin
	Start     *OpenParen   `parser:"@@"`
	Stmts     []*FieldStmt `parser:"@@*"`
	Terminate *CloseParen  `parser:"@@"`
}

FieldList represents a list of Fields, enclosed by parentheses.

func NewFieldList

func NewFieldList(params ...*Field) *FieldList

func (*FieldList) Fields

func (fl *FieldList) Fields() []*Field

func (*FieldList) NumFields

func (fl *FieldList) NumFields() int

NumFields returns the number of fields in FieldList.

func (*FieldList) String

func (fl *FieldList) String() string

func (*FieldList) Unparse

func (fl *FieldList) Unparse(opts ...UnparseOption) string

type FieldStmt

type FieldStmt struct {
	Mixin
	Field    *Field        `parser:"( @@ Delimit?"`
	Newline  *Newline      `parser:"| @@"`
	Comments *CommentGroup `parser:"| @@ )"`
}

FieldStmt represents a statement in a list of fields.

func (*FieldStmt) String

func (fs *FieldStmt) String() string

func (*FieldStmt) Unparse

func (fs *FieldStmt) Unparse(opts ...UnparseOption) string

type From

type From struct {
	Mixin
	Text string `parser:"@'from'"`
}

From represents the keyword "from".

func (*From) String

func (f *From) String() string

func (*From) Unparse

func (f *From) Unparse(opts ...UnparseOption) string

type FuncDecl

type FuncDecl struct {
	Mixin
	Scope *Scope
	Doc   *CommentGroup
	Sig   *FuncSignature `parser:"@@"`
	Body  *BlockStmt     `parser:"@@?"`
}

FuncDecl represents a function declaration.

func (*FuncDecl) Kind

func (fd *FuncDecl) Kind() Kind

func (*FuncDecl) String

func (fd *FuncDecl) String() string

func (*FuncDecl) Unparse

func (fd *FuncDecl) Unparse(opts ...UnparseOption) string

type FuncLit

type FuncLit struct {
	Mixin
	Type *Type      `parser:"@@"`
	Body *BlockStmt `parser:"@@"`
}

FuncLit represents a literal block prefixed by its type. If the type is missing then it's assumed to be a fs block literal.

func NewFuncLit

func NewFuncLit(kind Kind, stmts ...*Stmt) *FuncLit

func (*FuncLit) Kind

func (fl *FuncLit) Kind() Kind

func (*FuncLit) String

func (fl *FuncLit) String() string

func (*FuncLit) Unparse

func (fl *FuncLit) Unparse(opts ...UnparseOption) string

type FuncSignature

type FuncSignature struct {
	Mixin
	Type    *Type          `parser:"@@"`
	Name    *Ident         `parser:"@@"`
	Params  *FieldList     `parser:"@@"`
	Effects *EffectsClause `parser:"@@?"`
}

FuncSignature represents a function signature.

func (*FuncSignature) Kind

func (fs *FuncSignature) Kind() Kind

func (*FuncSignature) String

func (fs *FuncSignature) String() string

func (*FuncSignature) Subject

func (fs *FuncSignature) Subject() Node

func (*FuncSignature) Unparse

func (fs *FuncSignature) Unparse(opts ...UnparseOption) string

type Heredoc

type Heredoc struct {
	Mixin
	Value     string
	Start     string             `parser:"@Heredoc"`
	Fragments []*HeredocFragment `parser:"@@*"`
	Terminate *HeredocEnd        `parser:"@@"`
}

Heredoc represents a multiline heredoc type that supports string interpolation.

func (*Heredoc) String

func (h *Heredoc) String() string

func (*Heredoc) Unparse

func (h *Heredoc) Unparse(opts ...UnparseOption) string

type HeredocEnd

type HeredocEnd struct {
	Mixin
	Text string `parser:"@(HeredocEnd | RawHeredocEnd)"`
}

HeredocEnd represents the same identifier used to begin the heredoc block.

func (*HeredocEnd) String

func (he *HeredocEnd) String() string

func (*HeredocEnd) Unparse

func (he *HeredocEnd) Unparse(opts ...UnparseOption) string

type HeredocFragment

type HeredocFragment struct {
	Mixin
	Spaces       *string       `parser:"( @Spaces"`
	Escaped      *string       `parser:"| @Escaped"`
	Interpolated *Interpolated `parser:"| @@"`
	Text         *string       `parser:"| @(Text | RawText) )"`
}

HeredocFragment represents a piece of a heredoc.

func (*HeredocFragment) String

func (hf *HeredocFragment) String() string

func (*HeredocFragment) Unparse

func (hf *HeredocFragment) Unparse(opts ...UnparseOption) string

type Ident

type Ident struct {
	Mixin
	Text string `parser:"@Ident"`
}

Ident represents an identifier.

func NewIdent

func NewIdent(name string) *Ident

func (*Ident) String

func (i *Ident) String() string

func (*Ident) Unparse

func (i *Ident) Unparse(opts ...UnparseOption) string

type IdentExpr

type IdentExpr struct {
	Mixin
	Ident     *Ident     `parser:"@@"`
	Reference *Reference `parser:"@@?"`
}

IdentExpr represents an identifier that may be referencing an identifier from an imported module.

func NewIdentExpr

func NewIdentExpr(name string) *IdentExpr

func (*IdentExpr) String

func (ie *IdentExpr) String() string

func (*IdentExpr) Unparse

func (ie *IdentExpr) Unparse(opts ...UnparseOption) string

type Import

type Import struct {
	Mixin
	Text string `parser:"@'import'"`
}

Import represents the keyword "import".

func (*Import) String

func (i *Import) String() string

func (*Import) Unparse

func (i *Import) Unparse(opts ...UnparseOption) string

type ImportDecl

type ImportDecl struct {
	Mixin
	Import         *Import    `parser:"@@"`
	Name           *Ident     `parser:"@@"`
	DeprecatedPath *StringLit `parser:"( @@"`
	From           *From      `parser:"| @@"`
	Expr           *Expr      `parser:"@@ )"`
}

ImportDecl represents an import declaration.

func (*ImportDecl) String

func (id *ImportDecl) String() string

func (*ImportDecl) Unparse

func (id *ImportDecl) Unparse(opts ...UnparseOption) string

type ImportFunc

type ImportFunc struct {
	Pos  lexer.Position
	From *From    `parser:"@@"`
	Func *FuncLit `parser:"@@"`
}

Import represents the function for a remote import.

func (*ImportFunc) End

func (i *ImportFunc) End() lexer.Position

func (*ImportFunc) Position

func (i *ImportFunc) Position() lexer.Position

type ImportPath

type ImportPath struct {
	Pos  lexer.Position
	Path *StringLit `parser:"@@"`
}

ImportPath represents the relative path to a local import.

func (*ImportPath) End

func (i *ImportPath) End() lexer.Position

func (*ImportPath) Position

func (i *ImportPath) Position() lexer.Position

type Interpolated

type Interpolated struct {
	Mixin
	Start     *OpenInterpolated `parser:"@@"`
	Expr      *Expr             `parser:"@@?"`
	Terminate *CloseBrace       `parser:"@@"`
}

Interpolated represents an interpolated expression in a string or heredoc fragment.

func (*Interpolated) String

func (i *Interpolated) String() string

func (*Interpolated) Unparse

func (i *Interpolated) Unparse(opts ...UnparseOption) string

type Introspector

type Introspector interface {
	// Path returns the full CST path including the current node.
	Path() []Node
}

Introspector provides methods for visitors introspect the current node.

type Kind

type Kind string

Kind is an identifier that represents a builtin type.

const (
	None       Kind = "none"
	String     Kind = "string"
	Int        Kind = "int"
	Bool       Kind = "bool"
	Filesystem Kind = "fs"
	Pipeline   Kind = "pipeline"
	Option     Kind = "option"
)

func (Kind) Primary

func (k Kind) Primary() Kind

func (Kind) Secondary

func (k Kind) Secondary() Kind

type KindSet

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

func NewKindSet

func NewKindSet(kinds ...Kind) *KindSet

func (*KindSet) Has

func (ks *KindSet) Has(kind Kind) bool

func (*KindSet) Kinds

func (ks *KindSet) Kinds() (kinds []Kind)

type MatchOpts

type MatchOpts struct {
	// Filter is called to see if the node should be walked. If nil, then all
	// nodes are walked.
	Filter func(Node) bool

	// AllowDuplicates is enabled to allow duplicate CST structures between
	// arguments in the functions provided.
	//
	// For example, if a function is defined as `func(*FuncDecl, *CallStmt)`
	// sequences like `[*FuncDecl, ..., *CallStmt, ..., *CallStmt]` are not
	// matched by default. Allowing duplicates will match instead.
	AllowDuplicates bool
}

MatchOpts provides options while walking the CST.

type Mixin

type Mixin struct {
	Pos    lexer.Position
	EndPos lexer.Position
}

func (Mixin) End

func (m Mixin) End() lexer.Position

func (Mixin) Position

func (m Mixin) Position() lexer.Position

func (Mixin) Spanf

func (m Mixin) Spanf(t diagnostic.Type, format string, a ...interface{}) diagnostic.Option

func (Mixin) String

func (m Mixin) String() string

func (Mixin) Unparse

func (m Mixin) Unparse(opts ...UnparseOption) string

func (Mixin) WithError

func (m Mixin) WithError(err error, opts ...diagnostic.Option) error

type Modifier

type Modifier struct {
	Mixin
	Variadic *Variadic `parser:"@@"`
}

Modifier represents a term to modify the behaviour of a field.

func (*Modifier) String

func (m *Modifier) String() string

func (*Modifier) Unparse

func (m *Modifier) Unparse(opts ...UnparseOption) string

type Module

type Module struct {
	Mixin
	Scope     *Scope
	Directory Directory
	URI       string
	Doc       *CommentGroup
	Decls     []*Decl `parser:"@@*"`
}

Module represents a HLB source file. HLB is file-scoped, so every file represents a module.

Initially, the Parser will fill in this struct as a parse tree / concrete syntax tree, but a second pass from the Checker will type check and fill in fields without parser struct tags like scopes and doc linking.

func (*Module) String

func (m *Module) String() string

func (*Module) Unparse

func (m *Module) Unparse(opts ...UnparseOption) string

type ModuleLookup

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

func Modules

func Modules(ctx context.Context) *ModuleLookup

func NewModules

func NewModules() *ModuleLookup

func (*ModuleLookup) Get

func (ml *ModuleLookup) Get(filename string) *Module

func (*ModuleLookup) Set

func (ml *ModuleLookup) Set(filename string, mod *Module)

type Newline

type Newline struct {
	Mixin
	Text string `parser:"@Newline"`
}

Newline represents the "\n" newline.

func (*Newline) String

func (n *Newline) String() string

func (*Newline) Unparse

func (n *Newline) Unparse(opts ...UnparseOption) string

type Node

type Node interface {
	// Stringer is implemented to unparse a node back into formatted HLB.
	fmt.Stringer

	Unparse(opts ...UnparseOption) string

	// Position returns position of the first character belonging to the node.
	Position() lexer.Position

	// End returns position of the first character immediately after the node.
	End() lexer.Position

	// WithError returns an error decorated with diagnostics for the node.
	WithError(err error, opts ...diagnostic.Option) error

	// Spanf returns an annotation for the node.
	Spanf(t diagnostic.Type, format string, a ...interface{}) diagnostic.Option
}

Node is implemented by all nodes in the CST.

func Find

func Find(root Node, line, column int, filter func(Node) bool) Node

Find finds the deepest node that is on a specified line or column.

If column is 0, it ignores column until it finds a match, applying the matched node's start to end column constraints to its deeper matches.

If filter is specified, each match will only be kept if filter returns true.

func Search(root Node, query string, opts ...SearchOption) Node

Search searches for the deepest node that contains the query.

type NumericLit

type NumericLit struct {
	Mixin
	Value int64
	Base  int
}

NumericLit represents a number literal with a non-decimal base.

func (*NumericLit) Capture

func (nl *NumericLit) Capture(tokens []string) error

func (*NumericLit) End

func (nl *NumericLit) End() lexer.Position

func (*NumericLit) Position

func (nl *NumericLit) Position() lexer.Position

func (*NumericLit) String

func (nl *NumericLit) String() string

func (*NumericLit) Unparse

func (nl *NumericLit) Unparse(opts ...UnparseOption) string

type ObjKind

type ObjKind int

ObjKind describes what an object represents.

const (
	BadKind ObjKind = iota
	DeclKind
	FieldKind
)

The list of possible Object types.

type Object

type Object struct {
	Kind     Kind
	Ident    *Ident
	Node     Node
	Data     interface{}
	Exported bool
}

Object represents a named language entity such as a function, or variable.

type OpenBrace

type OpenBrace struct {
	Mixin
	Text string `parser:"@Block"`
}

OpenBrace represents the "{" brace.

type OpenInterpolated

type OpenInterpolated struct {
	Mixin
	Text string `parser:"@Interpolated"`
}

OpenInterpolated represents the "${" dollar brace of a interpolated expression.

func (*OpenInterpolated) String

func (oi *OpenInterpolated) String() string

func (*OpenInterpolated) Unparse

func (oi *OpenInterpolated) Unparse(opts ...UnparseOption) string

type OpenParen

type OpenParen struct {
	Mixin
	Text string `parser:"@Paren"`
}

OpenParen represents the "(" parenthese.

type Quote

type Quote struct {
	Mixin
	Text string `parser:"@(String | StringEnd)"`
}

Quote represents the `"` double quote.

func (*Quote) String

func (q *Quote) String() string

func (*Quote) Unparse

func (q *Quote) Unparse(opts ...UnparseOption) string

type RawHeredoc

type RawHeredoc struct {
	Mixin
	Start     string             `parser:"@RawHeredoc"`
	Fragments []*HeredocFragment `parser:"@@*"`
	Terminate *HeredocEnd        `parser:"@@"`
}

RawHeredoc represents a heredoc with no string interpolation.

func (*RawHeredoc) String

func (rh *RawHeredoc) String() string

func (*RawHeredoc) Unparse

func (rh *RawHeredoc) Unparse(opts ...UnparseOption) string

type RawStringLit

type RawStringLit struct {
	Mixin
	Start     *Backtick `parser:"@@"`
	Text      string    `parser:"@RawChar"`
	Terminate *Backtick `parser:"@@"`
}

RawStringLit represents a string literal that has only regular string characters. Nothing can be escaped or interpolated.

func (*RawStringLit) String

func (rsl *RawStringLit) String() string

func (*RawStringLit) Unparse

func (rsl *RawStringLit) Unparse(opts ...UnparseOption) string

type Reference

type Reference struct {
	Mixin
	Dot   string `parser:"@Dot"`
	Ident *Ident `parser:"@@"`
}

Reference represents the exported identifier from an imported module.

func (*Reference) String

func (r *Reference) String() string

func (*Reference) Unparse

func (r *Reference) Unparse(opts ...UnparseOption) string

type Scope

type Scope struct {
	Node
	Level   ScopeLevel
	Outer   *Scope
	Objects map[string]*Object
}

Scope maintains the set of named language entities declared in the scope and a link to the immediately surrounding (outer) scope.

func NewScope

func NewScope(outer *Scope, level ScopeLevel, node Node) *Scope

NewScope creates a new scope linking to an outer scope.

func (*Scope) ByLevel

func (s *Scope) ByLevel(level ScopeLevel) *Scope

func (*Scope) Depth

func (s *Scope) Depth() int

func (*Scope) Identifiers

func (s *Scope) Identifiers(kset *KindSet) (idents []string)

func (*Scope) Insert

func (s *Scope) Insert(obj *Object)

Insert inserts a named object obj into the scope.

func (*Scope) Locals

func (s *Scope) Locals() []*Object

func (*Scope) Lookup

func (s *Scope) Lookup(name string) *Object

Lookup returns the object with the given name if it is found in scope, otherwise it returns nil.

func (*Scope) Suggestion

func (s *Scope) Suggestion(name string, kset *KindSet) *Object

type ScopeLevel

type ScopeLevel string
var (
	BuiltinScope  ScopeLevel = "Builtins"
	ModuleScope   ScopeLevel = "Module"
	FunctionScope ScopeLevel = "Function"
	BlockScope    ScopeLevel = "Block"
	ArgsScope     ScopeLevel = "Arguments"
)

type SearchOption

type SearchOption func(*searcher)

SearchOption provides configuration for Search.

func WithSkip

func WithSkip(skip int) SearchOption

WithSkip specifies how many matches to skip before returning.

type Stmt

type Stmt struct {
	Mixin
	Call     *CallStmt     `parser:"( @@"`
	Expr     *ExprStmt     `parser:"| @@"`
	Newline  *Newline      `parser:"| @@"`
	Comments *CommentGroup `parser:"| @@ )"`
}

Stmt represents a statement node.

func NewCallStmt

func NewCallStmt(name string, args []*Expr, with *WithClause, binds *BindClause) *Stmt

func (*Stmt) String

func (s *Stmt) String() string

func (*Stmt) Unparse

func (s *Stmt) Unparse(opts ...UnparseOption) string

type StmtEnd

type StmtEnd struct {
	Mixin
	Semicolon *string  `parser:"( @';'"`
	Newline   *Newline `parser:"| @@"`
	Comment   *Comment `parser:"| @@ )"`
}

StmtEnd represents the end of a statement.

func (*StmtEnd) String

func (se *StmtEnd) String() string

func (*StmtEnd) Unparse

func (se *StmtEnd) Unparse(opts ...UnparseOption) string

type StopNode

type StopNode interface {
	Node
	Subject() Node
}

type StringFragment

type StringFragment struct {
	Mixin
	Escaped      *string       `parser:"( @Escaped"`
	Interpolated *Interpolated `parser:"| @@"`
	Text         *string       `parser:"| @Char )"`
}

StringFragment represents a piece of a string literal.

func (*StringFragment) String

func (sf *StringFragment) String() string

func (*StringFragment) Unparse

func (sf *StringFragment) Unparse(opts ...UnparseOption) string

type StringLit

type StringLit struct {
	Mixin
	Start     *Quote            `parser:"@@"`
	Fragments []*StringFragment `parser:"@@*"`
	Terminate *Quote            `parser:"@@"`
}

StringLit represents a string literal that can contain escaped characters, interpolated expressions and regular string characters.

func (*StringLit) String

func (sl *StringLit) String() string

func (*StringLit) Unparse

func (sl *StringLit) Unparse(opts ...UnparseOption) string

func (*StringLit) Unquoted

func (sl *StringLit) Unquoted(opts ...UnparseOption) string

type Type

type Type struct {
	Mixin
	Kind Kind `parser:"@Ident"`
}

Type represents an object type.

func NewType

func NewType(kind Kind) *Type

func (*Type) String

func (t *Type) String() string

func (*Type) Unparse

func (t *Type) Unparse(opts ...UnparseOption) string

type UnparseInfo

type UnparseInfo struct {
	Indent    int
	NoNewline bool
	NoStmtEnd bool
}

type UnparseOption

type UnparseOption func(*UnparseInfo)

func WithIndent

func WithIndent(depth int) UnparseOption

func WithNoNewline

func WithNoNewline() UnparseOption

func WithNoStmtEnd

func WithNoStmtEnd() UnparseOption

type Variadic

type Variadic struct {
	Mixin
	Text string `parser:"@'variadic'"`
}

Variadic represents a modifier for variadic fields. Variadic must only modify the last field of a FieldList.

func (*Variadic) String

func (v *Variadic) String() string

func (*Variadic) Unparse

func (v *Variadic) Unparse(opts ...UnparseOption) string

type Visitor

type Visitor interface {
	// Visit visits the current node.
	Visit(i Introspector, n Node) (w Visitor)
}

A Visitor's Visit method is invoked for each node encountered by Walk. If the result visitor w is not nil, Walk visits each of the children of node with the visitor w, followed by a call of w.Visit(nil).

type With

type With struct {
	Mixin
	Text string `parser:"@'with'"`
}

With represents the keyword "with".

func (*With) String

func (w *With) String() string

func (*With) Unparse

func (w *With) Unparse(opts ...UnparseOption) string

type WithClause

type WithClause struct {
	Mixin
	Closure *FuncDecl
	With    *With `parser:"@@"`
	Expr    *Expr `parser:"@@"`
}

WithClause represents optional arguments for a CallStmt.

func (*WithClause) String

func (wc *WithClause) String() string

func (*WithClause) Unparse

func (wc *WithClause) Unparse(opts ...UnparseOption) string

Jump to

Keyboard shortcuts

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