ast

package
v1.21.1 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	V_ILLEGAL = iota
	V_ARGC
	V_CONVFMT
	V_FILENAME
	V_FNR
	V_FS
	V_INPUTMODE
	V_NF
	V_NR
	V_OFMT
	V_OFS
	V_ORS
	V_OUTPUTMODE
	V_RLENGTH
	V_RS
	V_RSTART
	V_RT
	V_SUBSEP

	V_LAST = V_SUBSEP
)

Variables

This section is empty.

Functions

func IsLValue

func IsLValue(expr Expr) bool

IsLValue returns true if the given expression can be used as an lvalue (on the left-hand side of an assignment, in a ++ or -- operation, or as the third argument to sub or gsub).

func PosErrorf

func PosErrorf(pos Position, format string, args ...interface{}) error

PosErrorf like fmt.Errorf, but with an explicit position.

func SpecialVarIndex

func SpecialVarIndex(name string) int

SpecialVarIndex returns the "index" of the special variable, or 0 if it's not a special variable.

func SpecialVarName

func SpecialVarName(index int) string

SpecialVarName returns the name of the special variable by index.

func Walk

func Walk(v Visitor, node Node)

Walk traverses an AST in depth-first order: It starts by calling v.Visit(node); if node is nil, it does nothing. 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 WalkExprList

func WalkExprList(v Visitor, exprs []Expr)

WalkExprList walks a visitor over a list of expression AST nodes

func WalkStmtList

func WalkStmtList(v Visitor, stmts []Stmt)

WalkStmtList walks a visitor over a list of statement AST nodes

Types

type Action

type Action struct {
	Pattern []Expr
	Stmts   Stmts
}

Action is pattern-action section of a program.

func (*Action) String

func (a *Action) String() string

type ArrayExpr

type ArrayExpr struct {
	Scope VarScope
	Index int
	Name  string
	Pos   Position
}

ArrayExpr is an array reference. Not really a stand-alone expression, except as an argument to split() or a user function call.

func ArrayRef

func ArrayRef(name string, pos Position) *ArrayExpr

ArrayRef is a constructor for *ArrayExpr

func (*ArrayExpr) String

func (e *ArrayExpr) String() string

type AssignExpr

type AssignExpr struct {
	Left  Expr // can be one of: var, array[x], $n
	Right Expr
}

AssignExpr is an expression like x = 1234.

func (*AssignExpr) String

func (e *AssignExpr) String() string

type AugAssignExpr

type AugAssignExpr struct {
	Left  Expr // can be one of: var, array[x], $n
	Op    Token
	Right Expr
}

AugAssignExpr is an assignment expression like x += 5.

func (*AugAssignExpr) String

func (e *AugAssignExpr) String() string

type BinaryExpr

type BinaryExpr struct {
	Left  Expr
	Op    Token
	Right Expr
}

BinaryExpr is an expression like 1 + 2.

func (*BinaryExpr) String

func (e *BinaryExpr) String() string

type BlockStmt

type BlockStmt struct {
	Body  Stmts
	Start Position
	End   Position
}

BlockStmt is a stand-alone block like { print "x" }.

func (*BlockStmt) EndPos

func (s *BlockStmt) EndPos() Position

func (*BlockStmt) StartPos

func (s *BlockStmt) StartPos() Position

func (*BlockStmt) String

func (s *BlockStmt) String() string

type BreakStmt

type BreakStmt struct {
	Start Position
	End   Position
}

BreakStmt is a break statement.

func (*BreakStmt) EndPos

func (s *BreakStmt) EndPos() Position

func (*BreakStmt) StartPos

func (s *BreakStmt) StartPos() Position

func (*BreakStmt) String

func (s *BreakStmt) String() string

type CallExpr

type CallExpr struct {
	Func Token
	Args []Expr
}

CallExpr is a builtin function call like length($1).

func (*CallExpr) String

func (e *CallExpr) String() string

type CondExpr

type CondExpr struct {
	Cond  Expr
	True  Expr
	False Expr
}

CondExpr is an expression like cond ? 1 : 0.

func (*CondExpr) String

func (e *CondExpr) String() string

type ContinueStmt

type ContinueStmt struct {
	Start Position
	End   Position
}

ContinueStmt is a continue statement.

func (*ContinueStmt) EndPos

func (s *ContinueStmt) EndPos() Position

func (*ContinueStmt) StartPos

func (s *ContinueStmt) StartPos() Position

func (*ContinueStmt) String

func (s *ContinueStmt) String() string

type DeleteStmt

type DeleteStmt struct {
	Array *ArrayExpr
	Index []Expr
	Start Position
	End   Position
}

DeleteStmt is a statement like delete a[k].

func (*DeleteStmt) EndPos

func (s *DeleteStmt) EndPos() Position

func (*DeleteStmt) StartPos

func (s *DeleteStmt) StartPos() Position

func (*DeleteStmt) String

func (s *DeleteStmt) String() string

type DoWhileStmt

type DoWhileStmt struct {
	Body  Stmts
	Cond  Expr
	Start Position
	End   Position
}

DoWhileStmt is a do-while loop.

func (*DoWhileStmt) EndPos

func (s *DoWhileStmt) EndPos() Position

func (*DoWhileStmt) StartPos

func (s *DoWhileStmt) StartPos() Position

func (*DoWhileStmt) String

func (s *DoWhileStmt) String() string

type ExitStmt

type ExitStmt struct {
	Status Expr
	Start  Position
	End    Position
}

ExitStmt is an exit statement.

func (*ExitStmt) EndPos

func (s *ExitStmt) EndPos() Position

func (*ExitStmt) StartPos

func (s *ExitStmt) StartPos() Position

func (*ExitStmt) String

func (s *ExitStmt) String() string

type Expr

type Expr interface {
	Node

	String() string
	// contains filtered or unexported methods
}

Expr is the abstract syntax tree for any AWK expression.

type ExprStmt

type ExprStmt struct {
	Expr  Expr
	Start Position
	End   Position
}

ExprStmt is statement like a bare function call: my_func(x).

func (*ExprStmt) EndPos

func (s *ExprStmt) EndPos() Position

func (*ExprStmt) StartPos

func (s *ExprStmt) StartPos() Position

func (*ExprStmt) String

func (s *ExprStmt) String() string

type FieldExpr

type FieldExpr struct {
	Index Expr
}

FieldExpr is an expression like $0.

func (*FieldExpr) String

func (e *FieldExpr) String() string

type ForInStmt

type ForInStmt struct {
	Var       *VarExpr
	Array     *ArrayExpr
	BodyStart Position
	Body      Stmts
	Start     Position
	End       Position
}

ForInStmt is a for loop like for (k in a) print k, a[k].

func (*ForInStmt) EndPos

func (s *ForInStmt) EndPos() Position

func (*ForInStmt) StartPos

func (s *ForInStmt) StartPos() Position

func (*ForInStmt) String

func (s *ForInStmt) String() string

type ForStmt

type ForStmt struct {
	Pre       Stmt
	Cond      Expr
	Post      Stmt
	BodyStart Position
	Body      Stmts
	Start     Position
	End       Position
}

ForStmt is a C-like for loop: for (i=0; i<10; i++) print i.

func (*ForStmt) EndPos

func (s *ForStmt) EndPos() Position

func (*ForStmt) StartPos

func (s *ForStmt) StartPos() Position

func (*ForStmt) String

func (s *ForStmt) String() string

type Function

type Function struct {
	Name   string
	Params []string
	Arrays []bool
	Body   Stmts
	Pos    Position
}

Function is the AST for a user-defined function.

func (*Function) String

func (f *Function) String() string

type GetlineExpr

type GetlineExpr struct {
	Command Expr
	Target  Expr
	File    Expr
}

GetlineExpr is an expression read from file or pipe input.

func (*GetlineExpr) String

func (e *GetlineExpr) String() string

type IfStmt

type IfStmt struct {
	Cond      Expr
	BodyStart Position
	Body      Stmts
	Else      Stmts
	Start     Position
	End       Position
}

IfStmt is an if or if-else statement.

func (*IfStmt) EndPos

func (s *IfStmt) EndPos() Position

func (*IfStmt) StartPos

func (s *IfStmt) StartPos() Position

func (*IfStmt) String

func (s *IfStmt) String() string

type InExpr

type InExpr struct {
	Index []Expr
	Array *ArrayExpr
}

InExpr is an expression like (index in array).

func (*InExpr) String

func (e *InExpr) String() string

type IncrExpr

type IncrExpr struct {
	Expr Expr
	Op   Token
	Pre  bool
}

IncrExpr is an increment or decrement expression like x++ or --y.

func (*IncrExpr) String

func (e *IncrExpr) String() string

type IndexExpr

type IndexExpr struct {
	Array *ArrayExpr
	Index []Expr
}

IndexExpr is an expression like a[k] (rvalue or lvalue).

func (*IndexExpr) String

func (e *IndexExpr) String() string

type MultiExpr

type MultiExpr struct {
	Exprs []Expr
}

MultiExpr isn't an interpretable expression, but it's used as a pseudo-expression for print[f] parsing.

func (*MultiExpr) String

func (e *MultiExpr) String() string

type NamedFieldExpr

type NamedFieldExpr struct {
	Field Expr
}

NamedFieldExpr is an expression like @"name".

func (*NamedFieldExpr) String

func (e *NamedFieldExpr) String() string

type NextStmt

type NextStmt struct {
	Start Position
	End   Position
}

NextStmt is a next statement.

func (*NextStmt) EndPos

func (s *NextStmt) EndPos() Position

func (*NextStmt) StartPos

func (s *NextStmt) StartPos() Position

func (*NextStmt) String

func (s *NextStmt) String() string

type Node

type Node interface {
	// contains filtered or unexported methods
}

Node is an interface to be satisfied by all AST elements. We need it to be able to work with AST in a generic way, like in ast.Walk().

type NumExpr

type NumExpr struct {
	Value float64
}

NumExpr is a literal number like 1234.

func (*NumExpr) String

func (e *NumExpr) String() string

type PositionError

type PositionError struct {
	// Source line/column position where the error occurred.
	Position Position
	// Error message.
	Message string
}

PositionError represents an error bound to specific position in source.

func (*PositionError) Error

func (e *PositionError) Error() string

Error returns a formatted version of the error, including the line and column numbers.

type PrintStmt

type PrintStmt struct {
	Args     []Expr
	Redirect Token
	Dest     Expr
	Start    Position
	End      Position
}

PrintStmt is a statement like print $1, $3.

func (*PrintStmt) EndPos

func (s *PrintStmt) EndPos() Position

func (*PrintStmt) StartPos

func (s *PrintStmt) StartPos() Position

func (*PrintStmt) String

func (s *PrintStmt) String() string

type PrintfStmt

type PrintfStmt struct {
	Args     []Expr
	Redirect Token
	Dest     Expr
	Start    Position
	End      Position
}

PrintfStmt is a statement like printf "%3d", 1234.

func (*PrintfStmt) EndPos

func (s *PrintfStmt) EndPos() Position

func (*PrintfStmt) StartPos

func (s *PrintfStmt) StartPos() Position

func (*PrintfStmt) String

func (s *PrintfStmt) String() string

type Program

type Program struct {
	Begin     []Stmts
	Actions   []*Action
	End       []Stmts
	Functions []*Function
}

Program is an entire AWK program.

func (*Program) String

func (p *Program) String() string

String returns an indented, pretty-printed version of the parsed program.

type RegExpr

type RegExpr struct {
	Regex string
}

RegExpr is a stand-alone regex expression, equivalent to: $0 ~ /regex/.

func (*RegExpr) String

func (e *RegExpr) String() string

type ResolvedProgram

type ResolvedProgram struct {
	Program
	Scalars map[string]int
	Arrays  map[string]int
}

ResolvedProgram is a parsed AWK program + additional data prepared by resolve step needed for subsequent interpretation

type ReturnStmt

type ReturnStmt struct {
	Value Expr
	Start Position
	End   Position
}

ReturnStmt is a return statement.

func (*ReturnStmt) EndPos

func (s *ReturnStmt) EndPos() Position

func (*ReturnStmt) StartPos

func (s *ReturnStmt) StartPos() Position

func (*ReturnStmt) String

func (s *ReturnStmt) String() string

type Stmt

type Stmt interface {
	Node
	StartPos() Position // position of first character belonging to the node
	EndPos() Position   // position of first character immediately after the node

	String() string
	// contains filtered or unexported methods
}

Stmt is the abstract syntax tree for any AWK statement.

type Stmts

type Stmts []Stmt

Stmts is a block containing multiple statements.

func (Stmts) String

func (ss Stmts) String() string

type StrExpr

type StrExpr struct {
	Value string
}

StrExpr is a literal string like "foo".

func (*StrExpr) String

func (e *StrExpr) String() string

type UnaryExpr

type UnaryExpr struct {
	Op    Token
	Value Expr
}

UnaryExpr is an expression like -1234.

func (*UnaryExpr) String

func (e *UnaryExpr) String() string

type UserCallExpr

type UserCallExpr struct {
	Native bool // false = AWK-defined function, true = native Go func
	Index  int
	Name   string
	Args   []Expr
	Pos    Position
}

UserCallExpr is a user-defined function call like my_func(1, 2, 3)

Index is the resolved function index used by the interpreter; Name is the original name used by String().

func UserCall

func UserCall(name string, args []Expr, pos Position) *UserCallExpr

UserCall is a constructor for *UserCallExpr

func (*UserCallExpr) String

func (e *UserCallExpr) String() string

type VarExpr

type VarExpr struct {
	Scope VarScope
	Index int
	Name  string
	Pos   Position
}

VarExpr is a variable reference (special var, global, or local). Index is the resolved variable index used by the interpreter; Name is the original name used by String().

func VarRef

func VarRef(name string, pos Position) *VarExpr

VarRef is a constructor for *VarExpr

func (*VarExpr) String

func (e *VarExpr) String() string

type VarScope

type VarScope int
const (
	ScopeSpecial VarScope = iota
	ScopeGlobal
	ScopeLocal
)

type Visitor

type Visitor interface {
	Visit(node 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 WhileStmt

type WhileStmt struct {
	Cond      Expr
	BodyStart Position
	Body      Stmts
	Start     Position
	End       Position
}

WhileStmt is a while loop.

func (*WhileStmt) EndPos

func (s *WhileStmt) EndPos() Position

func (*WhileStmt) StartPos

func (s *WhileStmt) StartPos() Position

func (*WhileStmt) String

func (s *WhileStmt) String() string

Jump to

Keyboard shortcuts

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