Documentation
¶
Overview ¶
Package syntax turns Lua 5.4 source text into an abstract syntax tree. It knows nothing about the VM: the compiler in the glua package walks the tree it produces.
Index ¶
- func IsMultiValue(e Expr) bool
- func IsReserved(s string) bool
- func ParseNumber(s string) (i int64, f float64, isInt, ok bool)
- type AssignStmt
- type BinaryExpr
- type Block
- type BreakStmt
- type CallExpr
- type CallStmt
- type DoStmt
- type Error
- type Expr
- type ExprEnv
- type FalseExpr
- type Field
- type FloatExpr
- type FuncInfo
- type FunctionExpr
- type FunctionStmt
- type GenericForStmt
- type GotoStmt
- type IfStmt
- type IndexExpr
- type Info
- type IntExpr
- type Kind
- type LabelStmt
- type Lexer
- type LocalFunctionStmt
- type LocalStmt
- type MethodCallExpr
- type NameExpr
- type NilExpr
- type NumericForStmt
- type ParenExpr
- type Ref
- type RefKind
- type RepeatStmt
- type ReturnStmt
- type Stmt
- type StringExpr
- type TableExpr
- type Token
- type TrueExpr
- type UnaryExpr
- type Upval
- type Var
- type VarargExpr
- type WhileStmt
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsMultiValue ¶
IsMultiValue reports whether e can produce several values: a call or '...' that is not wrapped in parentheses.
func IsReserved ¶
IsReserved reports whether s is a reserved word of Lua.
func ParseNumber ¶
ParseNumber converts a Lua numeral, without sign or surrounding space, as C Lua's luaO_str2num does: a decimal integer that fits in 64 bits, a hexadecimal integer (wrapping around), or else a float, decimal or hexadecimal.
Types ¶
type AssignStmt ¶
AssignStmt is Targets = Values. Each target is a NameExpr or IndexExpr. TargetLines has, per target, the line of the ',' or '=' after it, where C Lua reports an assignment to a const variable.
func (*AssignStmt) Pos ¶
func (s *AssignStmt) Pos() int
type BinaryExpr ¶
BinaryExpr is L Op R. Op is one of the operator kinds, And or Or.
func (*BinaryExpr) Pos ¶
func (e *BinaryExpr) Pos() int
type Block ¶
Block is a list of statements; a ReturnStmt can only be last. EndLine is the line of the token that closes it.
type Error ¶
Error is a syntax error. Its message has C Lua's form, such as "unexpected symbol near 'x'".
type Expr ¶
type Expr interface {
Pos() int
// contains filtered or unexported methods
}
Expr is an expression node. Line is where the node's code should be reported: the start of a call's prefix, the operator of a binary expression, the first token otherwise.
type ExprEnv ¶
ExprEnv makes a chunk an expression over a Go struct: its main function takes the struct as the parameter Param, and a free name that is one of Fields is a field of it. Any other free name must be Known. Fields maps each name to whether it may be assigned.
type Field ¶
type Field struct {
Key, Value Expr
}
Field is one entry of a table constructor. Key is nil for a positional entry.
type FuncInfo ¶
type FuncInfo struct {
Node *FunctionExpr // nil for the pseudo function that holds the main chunk's _ENV
Parent *FuncInfo
Upvals []*Upval // in order of first use
}
FuncInfo is what resolution found out about one function.
type FunctionExpr ¶
type FunctionExpr struct {
Line, EndLine int
CloseLine int // line of the token after the closing 'end', where C Lua reports pending gotos
Params []string // a method's "self" comes first
IsVararg bool
Body *Block
Name string // for messages: "f", "a.b:c", "<anonymous>"
}
func Parse ¶
func Parse(source, src string) (fn *FunctionExpr, err error)
Parse parses a chunk into the body of its main function, which takes '...'.
func ParseFile ¶
func ParseFile(source, src string) (fn *FunctionExpr, err error)
ParseFile is Parse for the contents of a file: a first line starting with '#' is skipped.
func ParseFixed ¶
func ParseFixed(source, src string) (fn *FunctionExpr, err error)
ParseFixed is Parse for a chunk whose main function is not vararg, such as an expression over a Go struct: '...' in it is an error.
func (*FunctionExpr) Pos ¶
func (e *FunctionExpr) Pos() int
type FunctionStmt ¶
type FunctionStmt struct {
Line int
Target Expr
Func *FunctionExpr
}
FunctionStmt is "function a.b.c() end" or "function a.b:c() end": Target = Func. Target is a NameExpr or IndexExpr; for a method, Func's first parameter is self.
func (*FunctionStmt) Pos ¶
func (s *FunctionStmt) Pos() int
type GenericForStmt ¶
type GenericForStmt struct {
Line int
ExprLine int // line of the token after 'in', where C Lua places the iterator call
Names []string
Exprs []Expr
Body *Block
}
func (*GenericForStmt) Pos ¶
func (s *GenericForStmt) Pos() int
type Info ¶
type Info struct {
Main *FuncInfo
Funcs map[*FunctionExpr]*FuncInfo
Names map[*NameExpr]Ref
// Decls holds the variables each declaration introduces, in order: for a LocalStmt,
// LocalFunctionStmt, NumericForStmt, GenericForStmt, or a FunctionExpr's parameters.
Decls map[any][]*Var
// ExplicitEnv is set when the chunk declares or assigns _ENV. Free names are then
// fields of whichever _ENV is visible, and EnvVar is the main chunk's own _ENV.
ExplicitEnv bool
EnvVar *Var
}
Info is the result of resolution.
func Resolve ¶
func Resolve(source string, fn *FunctionExpr) (info *Info, err error)
Resolve binds every name in the chunk fn and checks what C Lua checks while parsing: gotos and labels, break outside a loop, assignments to const variables, and the limits on locals and upvalues.
func ResolveExpr ¶
func ResolveExpr(source string, fn *FunctionExpr, xenv *ExprEnv) (info *Info, err error)
ResolveExpr is Resolve for a chunk whose free names env resolves. fn must take env.Param as its first parameter. A name neither a field nor known, an assignment to a read-only field and an assignment to a global are errors.
type Kind ¶
type Kind uint8
Kind is the kind of a token.
const ( EOF Kind = iota // literals and names Name String Int Float // reserved words And Break Do Else Elseif End False For Function Goto If In Local Nil Not Or Repeat Return Then True Until While // operators and punctuation Plus // + Minus // - Star // * Slash // / DSlash // // Percent // % Caret // ^ Hash // # Amp // & Tilde // ~ Pipe // | Shl // << Shr // >> Concat // .. Dots // ... Eq // == Ne // ~= Le // <= Ge // >= Lt // < Gt // > Assign // = LParen // ( RParen // ) LBrace // { RBrace // } LBracket // [ RBracket // ] DColon // :: Semi // ; Colon // : Comma // , Dot // . )
const Other Kind = 255
Other is the kind of a byte that starts no token, such as '@'. C Lua hands such bytes to the parser, which reports "unexpected symbol near '@'".
type LabelStmt ¶
LabelStmt is ::Name::. AfterLine is the line of the token after it, where C Lua reports a duplicate label or a goto that jumps into a local's scope.
type Lexer ¶
type Lexer struct {
// contains filtered or unexported fields
}
Lexer splits Lua source into tokens. It reports errors by panicking with *Error, which the parser recovers.
type LocalFunctionStmt ¶
type LocalFunctionStmt struct {
Line int
Name string
Func *FunctionExpr
}
LocalFunctionStmt is "local function name() end"; the name is in scope in the body.
func (*LocalFunctionStmt) Pos ¶
func (s *LocalFunctionStmt) Pos() int
type LocalStmt ¶
LocalStmt is "local a <const>, b = ...". Attribs has "", "const" or "close" per name.
type MethodCallExpr ¶
MethodCallExpr is Obj:Name(Args).
func (*MethodCallExpr) Pos ¶
func (e *MethodCallExpr) Pos() int
type NumericForStmt ¶
type NumericForStmt struct {
Line int
Var string
Start, Limit, Step Expr // Step may be nil
Body *Block
}
func (*NumericForStmt) Pos ¶
func (s *NumericForStmt) Pos() int
type ParenExpr ¶
ParenExpr is (X). It cuts a call or '...' to one value and makes X no longer assignable.
type Ref ¶
type Ref struct {
Kind RefKind
Var *Var // RefLocal, RefUpval
Upval *Upval // RefUpval
// Env, for a RefGlobal in a chunk that declares or assigns _ENV, is the binding of the
// _ENV that is visible there; the name is a field of it. It is nil in the common case,
// where free names go straight to the closure's globals table.
Env *Ref
// Field, in an expression chunk, is the field of the environment parameter a free name
// names; Var and Upval then bind that parameter.
Field string
}
Ref is the binding of one NameExpr.
type RepeatStmt ¶
func (*RepeatStmt) Pos ¶
func (s *RepeatStmt) Pos() int
type ReturnStmt ¶
func (*ReturnStmt) Pos ¶
func (s *ReturnStmt) Pos() int
type Stmt ¶
type Stmt interface {
Pos() int
// contains filtered or unexported methods
}
Stmt is a statement node.
type StringExpr ¶
func (*StringExpr) Pos ¶
func (e *StringExpr) Pos() int
type Token ¶
type Token struct {
Kind Kind
Line int // 1-based
EndLine int // the line the lexer is on after the token, which C Lua reports errors at
Col int // 1-based byte column of the first character
Str string // the name, or the string's contents after escapes
Int int64 // the value of an Int token
Num float64 // the value of a Float token
// contains filtered or unexported fields
}
Token is one lexical token.
type Upval ¶
type Upval struct {
Name string
Var *Var
Parent *Upval // nil when Var is a local of the immediately enclosing function
Index int
}
Upval is one captured variable of a function: a local of the enclosing function, or one of its upvalues.
type Var ¶
type Var struct {
Name string
Attrib string // "", "const" or "close"
Func *FuncInfo
Line int
Captured bool // an inner function refers to it
Assigned bool // it is assigned somewhere other than its declaration
}
Var is a local variable: a parameter, a loop variable or a local declaration.
type VarargExpr ¶
type VarargExpr struct{ Line int }
func (*VarargExpr) Pos ¶
func (e *VarargExpr) Pos() int