package js
import "github.com/tdewolff/parse/v2/js"
Package js is an ECMAScript5.1 lexer following the specifications at http://www.ecma-international.org/ecma-262/5.1/.
Index ¶
- Variables
- func AsDecimalLiteral(b []byte) bool
- func AsIdentifierName(b []byte) bool
- func IsIdentifier(tt TokenType) bool
- func IsIdentifierContinue(b []byte) bool
- func IsIdentifierEnd(b []byte) bool
- func IsIdentifierName(tt TokenType) bool
- func IsIdentifierStart(b []byte) bool
- func IsNumeric(tt TokenType) bool
- func IsOperator(tt TokenType) bool
- func IsPunctuator(tt TokenType) bool
- func IsReservedWord(tt TokenType) bool
- func Walk(v IVisitor, n INode)
- type AST
- type Alias
- func (alias Alias) JS() string
- func (n Alias) JSON() (string, error)
- func (alias Alias) String() string
- type Arg
- type Args
- type ArrayExpr
- func (n ArrayExpr) JS() string
- func (n ArrayExpr) JSON() (string, error)
- func (n ArrayExpr) String() string
- type ArrowFunc
- func (n ArrowFunc) JS() string
- func (n ArrowFunc) JSON() (string, error)
- func (n ArrowFunc) String() string
- type BinaryExpr
- func (n BinaryExpr) JS() string
- func (n BinaryExpr) JSON() (string, error)
- func (n BinaryExpr) String() string
- type BindingArray
- func (n BindingArray) JS() string
- func (n BindingArray) JSON() (string, error)
- func (n BindingArray) String() string
- type BindingElement
- func (n BindingElement) JS() string
- func (n BindingElement) JSON() (string, error)
- func (n BindingElement) String() string
- type BindingObject
- func (n BindingObject) JS() string
- func (n BindingObject) JSON() (string, error)
- func (n BindingObject) String() string
- type BindingObjectItem
- func (n BindingObjectItem) JS() string
- func (n BindingObjectItem) JSON() (string, error)
- func (n BindingObjectItem) String() string
- type BlockStmt
- func (n BlockStmt) JS() string
- func (n BlockStmt) JSON() (string, error)
- func (n BlockStmt) String() string
- type BranchStmt
- func (n BranchStmt) JS() string
- func (n BranchStmt) JSON() (string, error)
- func (n BranchStmt) String() string
- type CallExpr
- func (n CallExpr) JS() string
- func (n CallExpr) JSON() (string, error)
- func (n CallExpr) String() string
- type CaseClause
- func (n CaseClause) JS() string
- func (n CaseClause) JSON() (string, error)
- func (n CaseClause) String() string
- type ClassDecl
- func (n ClassDecl) JS() string
- func (n ClassDecl) JSON() (string, error)
- func (n ClassDecl) String() string
- type CondExpr
- func (n CondExpr) JS() string
- func (n CondExpr) JSON() (string, error)
- func (n CondExpr) String() string
- type DebuggerStmt
- func (n DebuggerStmt) JS() string
- func (n DebuggerStmt) JSON() (string, error)
- func (n DebuggerStmt) String() string
- type DeclType
- type DirectivePrologueStmt
- func (n DirectivePrologueStmt) JS() string
- func (n DirectivePrologueStmt) JSON() (string, error)
- func (n DirectivePrologueStmt) String() string
- type DoWhileStmt
- func (n DoWhileStmt) JS() string
- func (n DoWhileStmt) JSON() (string, error)
- func (n DoWhileStmt) String() string
- type DotExpr
- func (n DotExpr) JS() string
- func (n DotExpr) JSON() (string, error)
- func (n DotExpr) String() string
- type Element
- func (n Element) JS() string
- func (n Element) JSON() (string, error)
- func (n Element) String() string
- type EmptyStmt
- func (n EmptyStmt) JS() string
- func (n EmptyStmt) JSON() (string, error)
- func (n EmptyStmt) String() string
- type ExportStmt
- func (n ExportStmt) JS() string
- func (n ExportStmt) JSON() (string, error)
- func (n ExportStmt) String() string
- type ExprStmt
- func (n ExprStmt) JS() string
- func (n ExprStmt) JSON() (string, error)
- func (n ExprStmt) String() string
- type FieldDefinition
- func (n FieldDefinition) JS() string
- func (n FieldDefinition) JSON() (string, error)
- func (n FieldDefinition) String() string
- type ForInStmt
- func (n ForInStmt) JS() string
- func (n ForInStmt) JSON() (string, error)
- func (n ForInStmt) String() string
- type ForOfStmt
- func (n ForOfStmt) JS() string
- func (n ForOfStmt) JSON() (string, error)
- func (n ForOfStmt) String() string
- type ForStmt
- func (n ForStmt) JS() string
- func (n ForStmt) JSON() (string, error)
- func (n ForStmt) String() string
- type FuncDecl
- func (n FuncDecl) JS() string
- func (n FuncDecl) JSON() (string, error)
- func (n FuncDecl) String() string
- type GroupExpr
- func (n GroupExpr) JS() string
- func (n GroupExpr) JSON() (string, error)
- func (n GroupExpr) String() string
- type IBinding
- type IExpr
- type INode
- type IStmt
- type IVisitor
- type IfStmt
- type ImportMetaExpr
- func (n ImportMetaExpr) JS() string
- func (n ImportMetaExpr) JSON() (string, error)
- func (n ImportMetaExpr) String() string
- type ImportStmt
- func (n ImportStmt) JS() string
- func (n ImportStmt) JSON() (string, error)
- func (n ImportStmt) String() string
- type IndexExpr
- func (n IndexExpr) JS() string
- func (n IndexExpr) JSON() (string, error)
- func (n IndexExpr) String() string
- type LabelledStmt
- func (n LabelledStmt) JS() string
- func (n LabelledStmt) JSON() (string, error)
- func (n LabelledStmt) String() string
- type Lexer
- func NewLexer(r *parse.Input) *Lexer
- func (l *Lexer) Err() error
- func (l *Lexer) Next() (TokenType, []byte)
- func (l *Lexer) RegExp() (TokenType, []byte)
- type LiteralExpr
- func (n LiteralExpr) JS() string
- func (n LiteralExpr) JSON() (string, error)
- func (n LiteralExpr) String() string
- type MethodDecl
- func (n MethodDecl) JS() string
- func (n MethodDecl) JSON() (string, error)
- func (n MethodDecl) String() string
- type NewExpr
- func (n NewExpr) JS() string
- func (n NewExpr) JSON() (string, error)
- func (n NewExpr) String() string
- type NewTargetExpr
- func (n NewTargetExpr) JS() string
- func (n NewTargetExpr) JSON() (string, error)
- func (n NewTargetExpr) String() string
- type ObjectExpr
- func (n ObjectExpr) JS() string
- func (n ObjectExpr) JSON() (string, error)
- func (n ObjectExpr) String() string
- type OpPrec
- type OptChainExpr
- func (n OptChainExpr) JS() string
- func (n OptChainExpr) JSON() (string, error)
- func (n OptChainExpr) String() string
- type Params
- type Parser
- type Property
- func (n Property) JS() string
- func (n Property) JSON() (string, error)
- func (n Property) String() string
- type PropertyName
- func (n PropertyName) IsComputed() bool
- func (n PropertyName) IsIdent(data []byte) bool
- func (n PropertyName) IsSet() bool
- func (n PropertyName) JS() string
- func (n PropertyName) JSON() (string, error)
- func (n PropertyName) String() string
- type ReturnStmt
- func (n ReturnStmt) JS() string
- func (n ReturnStmt) JSON() (string, error)
- func (n ReturnStmt) String() string
- type Scope
- func (s *Scope) Declare(decl DeclType, name []byte) (*Var, bool)
- func (s *Scope) HoistUndeclared()
- func (s *Scope) MarkArguments()
- func (s *Scope) MarkForInit()
- func (s Scope) String() string
- func (s *Scope) UndeclareScope()
- func (s *Scope) Unscope()
- func (s *Scope) Use(name []byte) *Var
- type SwitchStmt
- func (n SwitchStmt) JS() string
- func (n SwitchStmt) JSON() (string, error)
- func (n SwitchStmt) String() string
- type TemplateExpr
- func (n TemplateExpr) JS() string
- func (n TemplateExpr) JSON() (string, error)
- func (n TemplateExpr) String() string
- type TemplatePart
- func (n TemplatePart) JS() string
- func (n TemplatePart) JSON() (string, error)
- func (n TemplatePart) String() string
- type ThrowStmt
- func (n ThrowStmt) JS() string
- func (n ThrowStmt) JSON() (string, error)
- func (n ThrowStmt) String() string
- type TokenType
- type TryStmt
- func (n TryStmt) JS() string
- func (n TryStmt) JSON() (string, error)
- func (n TryStmt) String() string
- type UnaryExpr
- func (n UnaryExpr) JS() string
- func (n UnaryExpr) JSON() (string, error)
- func (n UnaryExpr) String() string
- type Var
- func (v Var) JS() string
- func (n Var) JSON() (string, error)
- func (v *Var) Name() []byte
- func (v Var) String() string
- type VarArray
- type VarDecl
- func (n VarDecl) JS() string
- func (n VarDecl) JSON() (string, error)
- func (n VarDecl) String() string
- type VarsByUses
- func (vs VarsByUses) Len() int
- func (vs VarsByUses) Less(i, j int) bool
- func (vs VarsByUses) Swap(i, j int)
- type WhileStmt
- func (n WhileStmt) JS() string
- func (n WhileStmt) JSON() (string, error)
- func (n WhileStmt) String() string
- type WithStmt
- func (n WithStmt) JS() string
- func (n WithStmt) JSON() (string, error)
- func (n WithStmt) String() string
- type YieldExpr
Examples ¶
Variables ¶
var Keywords = map[string]TokenType{ "await": AwaitToken, "break": BreakToken, "case": CaseToken, "catch": CatchToken, "class": ClassToken, "const": ConstToken, "continue": ContinueToken, "debugger": DebuggerToken, "default": DefaultToken, "delete": DeleteToken, "do": DoToken, "else": ElseToken, "enum": EnumToken, "export": ExportToken, "extends": ExtendsToken, "false": FalseToken, "finally": FinallyToken, "for": ForToken, "function": FunctionToken, "if": IfToken, "import": ImportToken, "in": InToken, "instanceof": InstanceofToken, "new": NewToken, "null": NullToken, "return": ReturnToken, "super": SuperToken, "switch": SwitchToken, "this": ThisToken, "throw": ThrowToken, "true": TrueToken, "try": TryToken, "typeof": TypeofToken, "var": VarToken, "void": VoidToken, "while": WhileToken, "with": WithToken, "yield": YieldToken, "let": LetToken, "static": StaticToken, "implements": ImplementsToken, "interface": InterfaceToken, "package": PackageToken, "private": PrivateToken, "protected": ProtectedToken, "public": PublicToken, "as": AsToken, "async": AsyncToken, "from": FromToken, "get": GetToken, "meta": MetaToken, "of": OfToken, "set": SetToken, "target": TargetToken, }
Keywords is a map of reserved, strict, and other keywords
Functions ¶
func AsDecimalLiteral ¶
AsDecimalLiteral returns true if a valid decimal literal is given.
func AsIdentifierName ¶
AsIdentifierName returns true if a valid identifier name is given.
func IsIdentifier ¶
IsIdentifier matches Identifier, i.e. IdentifierName but not ReservedWord. Does not match yield or await.
func IsIdentifierContinue ¶
IsIdentifierContinue returns true if the byte-slice start is a continuation of an identifier
func IsIdentifierEnd ¶
IsIdentifierEnd returns true if the byte-slice end is a start or continuation of an identifier
func IsIdentifierName ¶
IsIdentifierName matches IdentifierName, i.e. any identifier
func IsIdentifierStart ¶
IsIdentifierStart returns true if the byte-slice start is the start of an identifier
func IsNumeric ¶
IsNumeric return true if token is numeric.
func IsOperator ¶
IsOperator return true if token is an operator.
func IsPunctuator ¶
IsPunctuator return true if token is a punctuator.
func IsReservedWord ¶
IsReservedWord matches ReservedWord
func Walk ¶
Walk traverses an AST in depth-first order
Types ¶
type AST ¶
AST is the full ECMAScript abstract syntax tree.
func Parse ¶
Parse returns a JS AST tree of.
func (*AST) String ¶
type Alias ¶
Alias is a name space import or import/export specifier for import/export statements.
func (Alias) JS ¶
JS converts the node back to valid JavaScript
func (Alias) JSON ¶
JSON converts the node back to valid JSON
func (Alias) String ¶
type Arg ¶
func (Arg) JS ¶
JS converts the node back to valid JavaScript
func (Arg) JSON ¶
JSON converts the node back to valid JSON
func (Arg) String ¶
type Args ¶
type Args struct { List []Arg }
Args is a list of arguments as used by new and call expressions.
func (Args) JS ¶
JS converts the node back to valid JavaScript
func (Args) JSON ¶
JSON converts the node back to valid JSON
func (Args) String ¶
type ArrayExpr ¶
type ArrayExpr struct { List []Element }
ArrayExpr is an array literal.
func (ArrayExpr) JS ¶
JS converts the node back to valid JavaScript
func (ArrayExpr) JSON ¶
JSON converts the node back to valid JSON
func (ArrayExpr) String ¶
type ArrowFunc ¶
ArrowFunc is an (async) arrow function.
func (ArrowFunc) JS ¶
JS converts the node back to valid JavaScript
func (ArrowFunc) JSON ¶
JSON converts the node back to valid JSON
func (ArrowFunc) String ¶
type BinaryExpr ¶
BinaryExpr is a binary expression.
func (BinaryExpr) JS ¶
func (n BinaryExpr) JS() string
JS converts the node back to valid JavaScript
func (BinaryExpr) JSON ¶
func (n BinaryExpr) JSON() (string, error)
JSON converts the node back to valid JSON
func (BinaryExpr) String ¶
func (n BinaryExpr) String() string
type BindingArray ¶
type BindingArray struct { List []BindingElement Rest IBinding // can be nil }
BindingArray is an array binding pattern.
func (BindingArray) JS ¶
func (n BindingArray) JS() string
JS converts the node back to valid JavaScript
func (BindingArray) JSON ¶
func (n BindingArray) JSON() (string, error)
JSON converts the node back to valid JSON
func (BindingArray) String ¶
func (n BindingArray) String() string
type BindingElement ¶
type BindingElement struct { Binding IBinding // can be nil (in case of ellision) Default IExpr // can be nil }
BindingElement is a binding element.
func (BindingElement) JS ¶
func (n BindingElement) JS() string
JS converts the node back to valid JavaScript
func (BindingElement) JSON ¶
func (n BindingElement) JSON() (string, error)
JSON converts the node back to valid JSON
func (BindingElement) String ¶
func (n BindingElement) String() string
type BindingObject ¶
type BindingObject struct { List []BindingObjectItem Rest *Var // can be nil }
BindingObject is an object binding pattern.
func (BindingObject) JS ¶
func (n BindingObject) JS() string
JS converts the node back to valid JavaScript
func (BindingObject) JSON ¶
func (n BindingObject) JSON() (string, error)
JSON converts the node back to valid JSON
func (BindingObject) String ¶
func (n BindingObject) String() string
type BindingObjectItem ¶
type BindingObjectItem struct { Key *PropertyName // can be nil Value BindingElement }
BindingObjectItem is a binding property.
func (BindingObjectItem) JS ¶
func (n BindingObjectItem) JS() string
JS converts the node back to valid JavaScript
func (BindingObjectItem) JSON ¶
func (n BindingObjectItem) JSON() (string, error)
JSON converts the node back to valid JSON
func (BindingObjectItem) String ¶
func (n BindingObjectItem) String() string
type BlockStmt ¶
BlockStmt is a block statement.
func (BlockStmt) JS ¶
JS converts the node back to valid JavaScript
func (BlockStmt) JSON ¶
JSON converts the node back to valid JSON
func (BlockStmt) String ¶
type BranchStmt ¶
BranchStmt is a continue or break statement.
func (BranchStmt) JS ¶
func (n BranchStmt) JS() string
JS converts the node back to valid JavaScript
func (BranchStmt) JSON ¶
func (n BranchStmt) JSON() (string, error)
JSON converts the node back to valid JSON
func (BranchStmt) String ¶
func (n BranchStmt) String() string
type CallExpr ¶
CallExpr is a call expression.
func (CallExpr) JS ¶
JS converts the node back to valid JavaScript
func (CallExpr) JSON ¶
JSON converts the node back to valid JSON
func (CallExpr) String ¶
type CaseClause ¶
CaseClause is a case clause or default clause for a switch statement.
func (CaseClause) JS ¶
func (n CaseClause) JS() string
JS converts the node back to valid JavaScript
func (CaseClause) JSON ¶
func (n CaseClause) JSON() (string, error)
JSON converts the node back to valid JSON
func (CaseClause) String ¶
func (n CaseClause) String() string
type ClassDecl ¶
type ClassDecl struct { Name *Var // can be nil Extends IExpr // can be nil Definitions []FieldDefinition Methods []*MethodDecl }
ClassDecl is a class declaration.
func (ClassDecl) JS ¶
JS converts the node back to valid JavaScript
func (ClassDecl) JSON ¶
JSON converts the node back to valid JSON
func (ClassDecl) String ¶
type CondExpr ¶
type CondExpr struct { Cond, X, Y IExpr }
CondExpr is a conditional expression.
func (CondExpr) JS ¶
JS converts the node back to valid JavaScript
func (CondExpr) JSON ¶
JSON converts the node back to valid JSON
func (CondExpr) String ¶
type DebuggerStmt ¶
type DebuggerStmt struct { }
DebuggerStmt is a debugger statement.
func (DebuggerStmt) JS ¶
func (n DebuggerStmt) JS() string
JS converts the node back to valid JavaScript
func (DebuggerStmt) JSON ¶
func (n DebuggerStmt) JSON() (string, error)
JSON converts the node back to valid JSON
func (DebuggerStmt) String ¶
func (n DebuggerStmt) String() string
type DeclType ¶
type DeclType uint16
DeclType specifies the kind of declaration.
const ( NoDecl DeclType = iota // undeclared variables VariableDecl // var FunctionDecl // function ArgumentDecl // function and method arguments LexicalDecl // let, const, class CatchDecl // catch statement argument ExprDecl // function expression name or class expression name )
DeclType values.
func (DeclType) String ¶
type DirectivePrologueStmt ¶
type DirectivePrologueStmt struct { Value []byte }
DirectivePrologueStmt is a string literal at the beginning of a function or module (usually "use strict").
func (DirectivePrologueStmt) JS ¶
func (n DirectivePrologueStmt) JS() string
JS converts the node back to valid JavaScript
func (DirectivePrologueStmt) JSON ¶
func (n DirectivePrologueStmt) JSON() (string, error)
JSON converts the node back to valid JSON
func (DirectivePrologueStmt) String ¶
func (n DirectivePrologueStmt) String() string
type DoWhileStmt ¶
DoWhileStmt is a do-while iteration statement.
func (DoWhileStmt) JS ¶
func (n DoWhileStmt) JS() string
JS converts the node back to valid JavaScript
func (DoWhileStmt) JSON ¶
func (n DoWhileStmt) JSON() (string, error)
JSON converts the node back to valid JSON
func (DoWhileStmt) String ¶
func (n DoWhileStmt) String() string
type DotExpr ¶
type DotExpr struct { X IExpr Y LiteralExpr Prec OpPrec }
DotExpr is a member/call expression, super property, or optional chain with a dot expression.
func (DotExpr) JS ¶
JS converts the node back to valid JavaScript
func (DotExpr) JSON ¶
JSON converts the node back to valid JSON
func (DotExpr) String ¶
type Element ¶
Element is an array literal element.
func (Element) JS ¶
JS converts the node back to valid JavaScript
func (Element) JSON ¶
JSON converts the node back to valid JSON
func (Element) String ¶
type EmptyStmt ¶
type EmptyStmt struct { }
EmptyStmt is an empty statement.
func (EmptyStmt) JS ¶
JS converts the node back to valid JavaScript
func (EmptyStmt) JSON ¶
JSON converts the node back to valid JSON
func (EmptyStmt) String ¶
type ExportStmt ¶
ExportStmt is an export statement.
func (ExportStmt) JS ¶
func (n ExportStmt) JS() string
JS converts the node back to valid JavaScript
func (ExportStmt) JSON ¶
func (n ExportStmt) JSON() (string, error)
JSON converts the node back to valid JSON
func (ExportStmt) String ¶
func (n ExportStmt) String() string
type ExprStmt ¶
type ExprStmt struct { Value IExpr }
ExprStmt is an expression statement.
func (ExprStmt) JS ¶
JS converts the node back to valid JavaScript
func (ExprStmt) JSON ¶
JSON converts the node back to valid JSON
func (ExprStmt) String ¶
type FieldDefinition ¶
type FieldDefinition struct { Name PropertyName Init IExpr }
FieldDefinition is a field definition in a class declaration.
func (FieldDefinition) JS ¶
func (n FieldDefinition) JS() string
JS converts the node back to valid JavaScript
func (FieldDefinition) JSON ¶
func (n FieldDefinition) JSON() (string, error)
JSON converts the node back to valid JSON
func (FieldDefinition) String ¶
func (n FieldDefinition) String() string
type ForInStmt ¶
ForInStmt is a for-in iteration statement.
func (ForInStmt) JS ¶
JS converts the node back to valid JavaScript
func (ForInStmt) JSON ¶
JSON converts the node back to valid JSON
func (ForInStmt) String ¶
type ForOfStmt ¶
ForOfStmt is a for-of iteration statement.
func (ForOfStmt) JS ¶
JS converts the node back to valid JavaScript
func (ForOfStmt) JSON ¶
JSON converts the node back to valid JSON
func (ForOfStmt) String ¶
type ForStmt ¶
type ForStmt struct { Init IExpr // can be nil Cond IExpr // can be nil Post IExpr // can be nil Body *BlockStmt }
ForStmt is a regular for iteration statement.
func (ForStmt) JS ¶
JS converts the node back to valid JavaScript
func (ForStmt) JSON ¶
JSON converts the node back to valid JSON
func (ForStmt) String ¶
type FuncDecl ¶
type FuncDecl struct { Async bool Generator bool Name *Var // can be nil Params Params Body BlockStmt }
FuncDecl is an (async) (generator) function declaration or expression.
func (FuncDecl) JS ¶
JS converts the node back to valid JavaScript
func (FuncDecl) JSON ¶
JSON converts the node back to valid JSON
func (FuncDecl) String ¶
type GroupExpr ¶
type GroupExpr struct { X IExpr }
GroupExpr is a parenthesized expression.
func (GroupExpr) JS ¶
JS converts the node back to valid JavaScript
func (GroupExpr) JSON ¶
JSON converts the node back to valid JSON
func (GroupExpr) String ¶
type IBinding ¶
type IBinding interface { INode // contains filtered or unexported methods }
IBinding is a dummy interface for bindings.
type IExpr ¶
type IExpr interface { INode // contains filtered or unexported methods }
IExpr is a dummy interface for expressions.
type INode ¶
INode is an interface for AST nodes
type IStmt ¶
type IStmt interface { INode // contains filtered or unexported methods }
IStmt is a dummy interface for statements.
type IVisitor ¶
IVisitor represents the AST Visitor Each INode encountered by `Walk` is passed to `Enter`, children nodes will be ignored if the returned IVisitor is nil `Exit` is called upon the exit of a node
type IfStmt ¶
IfStmt is an if statement.
func (IfStmt) JS ¶
JS converts the node back to valid JavaScript
func (IfStmt) JSON ¶
JSON converts the node back to valid JSON
func (IfStmt) String ¶
type ImportMetaExpr ¶
type ImportMetaExpr struct { }
ImportMetaExpr is a import meta meta property.
func (ImportMetaExpr) JS ¶
func (n ImportMetaExpr) JS() string
JS converts the node back to valid JavaScript
func (ImportMetaExpr) JSON ¶
func (n ImportMetaExpr) JSON() (string, error)
JSON converts the node back to valid JSON
func (ImportMetaExpr) String ¶
func (n ImportMetaExpr) String() string
type ImportStmt ¶
ImportStmt is an import statement.
func (ImportStmt) JS ¶
func (n ImportStmt) JS() string
JS converts the node back to valid JavaScript
func (ImportStmt) JSON ¶
func (n ImportStmt) JSON() (string, error)
JSON converts the node back to valid JSON
func (ImportStmt) String ¶
func (n ImportStmt) String() string
type IndexExpr ¶
IndexExpr is a member/call expression, super property, or optional chain with an index expression.
func (IndexExpr) JS ¶
JS converts the node back to valid JavaScript
func (IndexExpr) JSON ¶
JSON converts the node back to valid JSON
func (IndexExpr) String ¶
type LabelledStmt ¶
LabelledStmt is a labelled statement.
func (LabelledStmt) JS ¶
func (n LabelledStmt) JS() string
JS converts the node back to valid JavaScript
func (LabelledStmt) JSON ¶
func (n LabelledStmt) JSON() (string, error)
JSON converts the node back to valid JSON
func (LabelledStmt) String ¶
func (n LabelledStmt) String() string
type Lexer ¶
type Lexer struct {
// contains filtered or unexported fields
}
Lexer is the state for the lexer.
func NewLexer ¶
func NewLexer(r *parse.Input) *Lexer
NewLexer returns a new Lexer for a given io.Reader.
Code:
Output:Example¶
{
l := NewLexer(parse.NewInputString("var x = 'lorem ipsum';"))
out := ""
for {
tt, data := l.Next()
if tt == ErrorToken {
break
}
out += string(data)
}
fmt.Println(out)
// Output: var x = 'lorem ipsum';
}
var x = 'lorem ipsum';
func (*Lexer) Err ¶
Err returns the error encountered during lexing, this is often io.EOF but also other errors can be returned.
func (*Lexer) Next ¶
Next returns the next Token. It returns ErrorToken when an error was encountered. Using Err() one can retrieve the error message.
func (*Lexer) RegExp ¶
RegExp reparses the input stream for a regular expression. It is assumed that we just received DivToken or DivEqToken with Next(). This function will go back and read that as a regular expression.
type LiteralExpr ¶
LiteralExpr can be this, null, boolean, numeric, string, or regular expression literals.
func (LiteralExpr) JS ¶
func (n LiteralExpr) JS() string
JS converts the node back to valid JavaScript
func (LiteralExpr) JSON ¶
func (n LiteralExpr) JSON() (string, error)
JSON converts the node back to valid JSON
func (LiteralExpr) String ¶
func (n LiteralExpr) String() string
type MethodDecl ¶
type MethodDecl struct { Static bool Async bool Generator bool Get bool Set bool Name PropertyName Params Params Body BlockStmt }
MethodDecl is a method definition in a class declaration.
func (MethodDecl) JS ¶
func (n MethodDecl) JS() string
JS converts the node back to valid JavaScript
func (MethodDecl) JSON ¶
func (n MethodDecl) JSON() (string, error)
JSON converts the node back to valid JSON
func (MethodDecl) String ¶
func (n MethodDecl) String() string
type NewExpr ¶
NewExpr is a new expression or new member expression.
func (NewExpr) JS ¶
JS converts the node back to valid JavaScript
func (NewExpr) JSON ¶
JSON converts the node back to valid JSON
func (NewExpr) String ¶
type NewTargetExpr ¶
type NewTargetExpr struct { }
NewTargetExpr is a new target meta property.
func (NewTargetExpr) JS ¶
func (n NewTargetExpr) JS() string
JS converts the node back to valid JavaScript
func (NewTargetExpr) JSON ¶
func (n NewTargetExpr) JSON() (string, error)
JSON converts the node back to valid JSON
func (NewTargetExpr) String ¶
func (n NewTargetExpr) String() string
type ObjectExpr ¶
type ObjectExpr struct { List []Property }
ObjectExpr is an object literal.
func (ObjectExpr) JS ¶
func (n ObjectExpr) JS() string
JS converts the node back to valid JavaScript
func (ObjectExpr) JSON ¶
func (n ObjectExpr) JSON() (string, error)
JSON converts the node back to valid JSON
func (ObjectExpr) String ¶
func (n ObjectExpr) String() string
type OpPrec ¶
type OpPrec int
OpPrec is the operator precedence
const ( OpExpr OpPrec = iota // a,b OpAssign // a?b:c, yield x, ()=>x, async ()=>x, a=b, a+=b, ... OpCoalesce // a??b OpOr // a||b OpAnd // a&&b OpBitOr // a|b OpBitXor // a^b OpBitAnd // a&b OpEquals // a==b, a!=b, a===b, a!==b OpCompare // a<b, a>b, a<=b, a>=b, a instanceof b, a in b OpShift // a<<b, a>>b, a>>>b OpAdd // a+b, a-b OpMul // a*b, a/b, a%b OpExp // a**b OpUnary // ++x, --x, delete x, void x, typeof x, +x, -x, ~x, !x, await x OpUpdate // x++, x-- OpLHS // CallExpr/OptChainExpr or NewExpr OpCall // a?.b, a(b), super(a), import(a) OpNew // new a OpMember // a[b], a.b, a`b`, super[x], super.x, new.target, import.meta, new a(b) OpPrimary // literal, function, class, parenthesized )
OpPrec values.
func (OpPrec) String ¶
type OptChainExpr ¶
type OptChainExpr struct { X IExpr Y IExpr // can be CallExpr, IndexExpr, LiteralExpr, or TemplateExpr }
OptChainExpr is an optional chain.
func (OptChainExpr) JS ¶
func (n OptChainExpr) JS() string
JS converts the node back to valid JavaScript
func (OptChainExpr) JSON ¶
func (n OptChainExpr) JSON() (string, error)
JSON converts the node back to valid JSON
func (OptChainExpr) String ¶
func (n OptChainExpr) String() string
type Params ¶
type Params struct { List []BindingElement Rest IBinding // can be nil }
Params is a list of parameters for functions, methods, and arrow function.
func (Params) JS ¶
JS converts the node back to valid JavaScript
func (Params) JSON ¶
JSON converts the node back to valid JSON
func (Params) String ¶
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser is the state for the parser.
type Property ¶
type Property struct { // either Name or Spread are set. When Spread is set then Value is AssignmentExpression // if Init is set then Value is IdentifierReference, otherwise it can also be MethodDefinition Name *PropertyName // can be nil Spread bool Value IExpr Init IExpr // can be nil }
Property is a property definition in an object literal.
func (Property) JS ¶
JS converts the node back to valid JavaScript
func (Property) JSON ¶
JSON converts the node back to valid JSON
func (Property) String ¶
type PropertyName ¶
type PropertyName struct { Literal LiteralExpr Computed IExpr // can be nil }
PropertyName is a property name for binding properties, method names, and in object literals.
func (PropertyName) IsComputed ¶
func (n PropertyName) IsComputed() bool
IsComputed returns true if PropertyName is computed.
func (PropertyName) IsIdent ¶
func (n PropertyName) IsIdent(data []byte) bool
IsIdent returns true if PropertyName equals the given identifier name.
func (PropertyName) IsSet ¶
func (n PropertyName) IsSet() bool
IsSet returns true is PropertyName is not nil.
func (PropertyName) JS ¶
func (n PropertyName) JS() string
JS converts the node back to valid JavaScript
func (PropertyName) JSON ¶
func (n PropertyName) JSON() (string, error)
JSON converts the node back to valid JSON
func (PropertyName) String ¶
func (n PropertyName) String() string
type ReturnStmt ¶
type ReturnStmt struct { Value IExpr // can be nil }
ReturnStmt is a return statement.
func (ReturnStmt) JS ¶
func (n ReturnStmt) JS() string
JS converts the node back to valid JavaScript
func (ReturnStmt) JSON ¶
func (n ReturnStmt) JSON() (string, error)
JSON converts the node back to valid JSON
func (ReturnStmt) String ¶
func (n ReturnStmt) String() string
type Scope ¶
type Scope struct { Parent, Func *Scope // Parent is nil for global scope, Parent equals Func for function scope Declared VarArray // Link in Var are always nil Undeclared VarArray NumVarDecls uint16 // number of variable declaration statements in a function scope NumForInit uint16 // offset into Declared to mark variables used in for initializer NumArguments uint16 // offset into Undeclared to mark variables used in arguments IsGlobalOrFunc bool HasWith bool }
Scope is a function or block scope with a list of variables declared and used.
func (*Scope) Declare ¶
Declare declares a new variable.
func (*Scope) HoistUndeclared ¶
func (s *Scope) HoistUndeclared()
HoistUndeclared copies all undeclared variables of the current scope to the parent scope.
func (*Scope) MarkArguments ¶
func (s *Scope) MarkArguments()
MarkArguments marks the undeclared variables in the current scope as function arguments. It ensures different b's in `function f(a=b){var b}`.
func (*Scope) MarkForInit ¶
func (s *Scope) MarkForInit()
MarkForInit marks the declared variables in current scope as for statement initializer to distinguish from declarations in body.
func (Scope) String ¶
func (*Scope) UndeclareScope ¶
func (s *Scope) UndeclareScope()
UndeclareScope undeclares all declared variables in the current scope and adds them to the parent scope. Called when possible arrow func ends up being a parenthesized expression, scope is not further used.
func (*Scope) Unscope ¶
func (s *Scope) Unscope()
Unscope moves all declared variables of the current scope to the parent scope. Undeclared variables are already in the parent scope.
func (*Scope) Use ¶
Use increments the usage of a variable.
type SwitchStmt ¶
type SwitchStmt struct { Init IExpr List []CaseClause Scope }
SwitchStmt is a switch statement.
func (SwitchStmt) JS ¶
func (n SwitchStmt) JS() string
JS converts the node back to valid JavaScript
func (SwitchStmt) JSON ¶
func (n SwitchStmt) JSON() (string, error)
JSON converts the node back to valid JSON
func (SwitchStmt) String ¶
func (n SwitchStmt) String() string
type TemplateExpr ¶
type TemplateExpr struct { Tag IExpr // can be nil List []TemplatePart Tail []byte Prec OpPrec }
TemplateExpr is a template literal or member/call expression, super property, or optional chain with template literal.
func (TemplateExpr) JS ¶
func (n TemplateExpr) JS() string
JS converts the node back to valid JavaScript
func (TemplateExpr) JSON ¶
func (n TemplateExpr) JSON() (string, error)
JSON converts the node back to valid JSON
func (TemplateExpr) String ¶
func (n TemplateExpr) String() string
type TemplatePart ¶
TemplatePart is a template head or middle.
func (TemplatePart) JS ¶
func (n TemplatePart) JS() string
JS converts the node back to valid JavaScript
func (TemplatePart) JSON ¶
func (n TemplatePart) JSON() (string, error)
JSON converts the node back to valid JSON
func (TemplatePart) String ¶
func (n TemplatePart) String() string
type ThrowStmt ¶
type ThrowStmt struct { Value IExpr }
ThrowStmt is a throw statement.
func (ThrowStmt) JS ¶
JS converts the node back to valid JavaScript
func (ThrowStmt) JSON ¶
JSON converts the node back to valid JSON
func (ThrowStmt) String ¶
type TokenType ¶
type TokenType uint16 // from LSB to MSB: 8 bits for tokens per category, 1 bit for numeric, 1 bit for punctuator, 1 bit for operator, 1 bit for identifier, 4 bits unused
TokenType determines the type of token, eg. a number or a semicolon.
const ( ErrorToken TokenType = iota // extra token when errors occur WhitespaceToken LineTerminatorToken // \r \n \r\n CommentToken CommentLineTerminatorToken StringToken TemplateToken TemplateStartToken TemplateMiddleToken TemplateEndToken RegExpToken PrivateIdentifierToken )
TokenType values.
const ( NumericToken TokenType = 0x0100 + iota DecimalToken BinaryToken OctalToken HexadecimalToken BigIntToken )
Numeric token values.
const ( PunctuatorToken TokenType = 0x0200 + iota OpenBraceToken // { CloseBraceToken // } OpenParenToken // ( CloseParenToken // ) OpenBracketToken // [ CloseBracketToken // ] DotToken // . SemicolonToken // ; CommaToken // , QuestionToken // ? ColonToken // : ArrowToken // => EllipsisToken // ... )
Punctuator token values.
const ( OperatorToken TokenType = 0x0600 + iota EqToken // = EqEqToken // == EqEqEqToken // === NotToken // ! NotEqToken // != NotEqEqToken // !== LtToken // < LtEqToken // <= LtLtToken // << LtLtEqToken // <<= GtToken // > GtEqToken // >= GtGtToken // >> GtGtEqToken // >>= GtGtGtToken // >>> GtGtGtEqToken // >>>= AddToken // + AddEqToken // += IncrToken // ++ SubToken // - SubEqToken // -= DecrToken // -- MulToken // * MulEqToken // *= ExpToken // ** ExpEqToken // **= DivToken // / DivEqToken // /= ModToken // % ModEqToken // %= BitAndToken // & BitOrToken // | BitXorToken // ^ BitNotToken // ~ BitAndEqToken // &= BitOrEqToken // |= BitXorEqToken // ^= AndToken // && OrToken // || NullishToken // ?? AndEqToken // &&= OrEqToken // ||= NullishEqToken // ??= OptChainToken // ?. // unused in lexer PosToken // +a NegToken // -a PreIncrToken // ++a PreDecrToken // --a PostIncrToken // a++ PostDecrToken // a-- )
Operator token values.
const ( ReservedToken TokenType = 0x0800 + iota AwaitToken BreakToken CaseToken CatchToken ClassToken ConstToken ContinueToken DebuggerToken DefaultToken DeleteToken DoToken ElseToken EnumToken ExportToken ExtendsToken FalseToken FinallyToken ForToken FunctionToken IfToken ImportToken InToken InstanceofToken NewToken NullToken ReturnToken SuperToken SwitchToken ThisToken ThrowToken TrueToken TryToken TypeofToken YieldToken VarToken VoidToken WhileToken WithToken )
Reserved token values.
const ( IdentifierToken TokenType = 0x1000 + iota AsToken AsyncToken FromToken GetToken ImplementsToken InterfaceToken LetToken MetaToken OfToken PackageToken PrivateToken ProtectedToken PublicToken SetToken StaticToken TargetToken )
Identifier token values.
func (TokenType) Bytes ¶
Bytes returns the string representation of a TokenType.
func (TokenType) String ¶
type TryStmt ¶
type TryStmt struct { Body *BlockStmt Binding IBinding // can be nil Catch *BlockStmt // can be nil Finally *BlockStmt // can be nil }
TryStmt is a try statement.
func (TryStmt) JS ¶
JS converts the node back to valid JavaScript
func (TryStmt) JSON ¶
JSON converts the node back to valid JSON
func (TryStmt) String ¶
type UnaryExpr ¶
UnaryExpr is an update or unary expression.
func (UnaryExpr) JS ¶
JS converts the node back to valid JavaScript
func (UnaryExpr) JSON ¶
JSON converts the node back to valid JSON
func (UnaryExpr) String ¶
type Var ¶
type Var struct { Data []byte Link *Var // is set when merging variable uses, as in: {a} {var a} where the first links to the second, only used for undeclared variables Uses uint16 Decl DeclType }
Var is a variable, where Decl is the type of declaration and can be var|function for function scoped variables, let|const|class for block scoped variables.
func (Var) JS ¶
JS converts the node back to valid JavaScript
func (Var) JSON ¶
JSON converts the node back to valid JSON
func (*Var) Name ¶
Name returns the variable name.
func (Var) String ¶
type VarArray ¶
type VarArray []*Var
VarArray is a set of variables in scopes.
func (VarArray) String ¶
type VarDecl ¶
type VarDecl struct { TokenType List []BindingElement }
VarDecl is a variable statement or lexical declaration.
func (VarDecl) JS ¶
JS converts the node back to valid JavaScript
func (VarDecl) JSON ¶
JSON converts the node back to valid JSON
func (VarDecl) String ¶
type VarsByUses ¶
type VarsByUses VarArray
VarsByUses is sortable by uses in descending order.
func (VarsByUses) Len ¶
func (vs VarsByUses) Len() int
func (VarsByUses) Less ¶
func (vs VarsByUses) Less(i, j int) bool
func (VarsByUses) Swap ¶
func (vs VarsByUses) Swap(i, j int)
type WhileStmt ¶
WhileStmt is a while iteration statement.
func (WhileStmt) JS ¶
JS converts the node back to valid JavaScript
func (WhileStmt) JSON ¶
JSON converts the node back to valid JSON
func (WhileStmt) String ¶
type WithStmt ¶
WithStmt is a with statement.
func (WithStmt) JS ¶
JS converts the node back to valid JavaScript
func (WithStmt) JSON ¶
JSON converts the node back to valid JSON
func (WithStmt) String ¶
type YieldExpr ¶
YieldExpr is a yield expression.
func (YieldExpr) JS ¶
JS converts the node back to valid JavaScript
func (YieldExpr) JSON ¶
JSON converts the node back to valid JSON
func (YieldExpr) String ¶
Source Files ¶
ast.go lex.go parse.go table.go tokentype.go util.go walk.go
- Version
- v2.5.21
- Published
- Sep 11, 2021
- Platform
- js/wasm
- Imports
- 9 packages
- Last checked
- 2 hours ago –
Tools for package owners.