Documentation ¶
Overview ¶
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
- type AST
- type Alias
- type Arg
- type Args
- type ArrayExpr
- type ArrowFunc
- type BinaryExpr
- type BindingArray
- type BindingElement
- type BindingObject
- type BindingObjectItem
- type BlockStmt
- type BranchStmt
- type CallExpr
- type CaseClause
- type ClassDecl
- type CondExpr
- type DebuggerStmt
- type DeclType
- type DirectivePrologueStmt
- type DoWhileStmt
- type DotExpr
- type Element
- type EmptyStmt
- type ExportStmt
- type ExprStmt
- type FieldDefinition
- type ForInStmt
- type ForOfStmt
- type ForStmt
- type FuncDecl
- type GroupExpr
- type IBinding
- type IExpr
- type IStmt
- type IfStmt
- type ImportMetaExpr
- type ImportStmt
- type IndexExpr
- type LabelledStmt
- type Lexer
- type LiteralExpr
- type MethodDecl
- type NewExpr
- type NewTargetExpr
- type ObjectExpr
- type OpPrec
- type OptChainExpr
- type Params
- type Parser
- type Property
- type PropertyName
- type ReturnStmt
- type Scope
- type SwitchStmt
- type TemplateExpr
- type TemplatePart
- type ThrowStmt
- type TokenType
- type TryStmt
- type UnaryExpr
- type Var
- type VarArray
- type VarDecl
- type VarsByUses
- type WhileStmt
- type WithStmt
- type YieldExpr
Examples ¶
Constants ¶
This section is empty.
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 ¶ added in v2.5.0
AsDecimalLiteral returns true if a valid decimal literal is given.
func AsIdentifierName ¶ added in v2.5.0
AsIdentifierName returns true if a valid identifier name is given.
func IsIdentifier ¶ added in v2.5.0
IsIdentifier matches Identifier, i.e. IdentifierName but not ReservedWord. Does not match yield or await.
func IsIdentifierContinue ¶ added in v2.5.0
IsIdentifierContinue returns true if the byte-slice start is a continuation of an identifier
func IsIdentifierEnd ¶ added in v2.5.0
IsIdentifierEnd returns true if the byte-slice end is a start or continuation of an identifier
func IsIdentifierName ¶ added in v2.5.0
IsIdentifierName matches IdentifierName, i.e. any identifier
func IsIdentifierStart ¶ added in v2.5.0
IsIdentifierStart returns true if the byte-slice start is the start of an identifier
func IsOperator ¶ added in v2.5.0
IsOperator return true if token is an operator.
func IsPunctuator ¶ added in v2.5.0
IsPunctuator return true if token is a punctuator.
func IsReservedWord ¶ added in v2.5.0
IsReservedWord matches ReservedWord
Types ¶
type AST ¶ added in v2.5.0
AST is the full ECMAScript abstract syntax tree.
type Alias ¶ added in v2.5.0
Alias is a name space import or import/export specifier for import/export statements.
type Args ¶ added in v2.5.4
type Args struct {
List []Arg
}
Args is a list of arguments as used by new and call expressions.
type ArrayExpr ¶ added in v2.5.0
type ArrayExpr struct {
List []Element
}
ArrayExpr is an array literal.
type BinaryExpr ¶ added in v2.5.0
BinaryExpr is a binary expression.
func (BinaryExpr) String ¶ added in v2.5.0
func (n BinaryExpr) String() string
type BindingArray ¶ added in v2.5.0
type BindingArray struct { List []BindingElement Rest IBinding // can be nil }
BindingArray is an array binding pattern.
func (BindingArray) String ¶ added in v2.5.0
func (n BindingArray) String() string
type BindingElement ¶ added in v2.5.0
type BindingElement struct { Binding IBinding // can be nil (in case of ellision) Default IExpr // can be nil }
BindingElement is a binding element.
func (BindingElement) String ¶ added in v2.5.0
func (n BindingElement) String() string
type BindingObject ¶ added in v2.5.0
type BindingObject struct { List []BindingObjectItem Rest *Var // can be nil }
BindingObject is an object binding pattern.
func (BindingObject) String ¶ added in v2.5.0
func (n BindingObject) String() string
type BindingObjectItem ¶ added in v2.5.0
type BindingObjectItem struct { Key *PropertyName // can be nil Value BindingElement }
BindingObjectItem is a binding property.
type BranchStmt ¶ added in v2.5.0
BranchStmt is a continue or break statement.
func (BranchStmt) String ¶ added in v2.5.0
func (n BranchStmt) String() string
type CaseClause ¶ added in v2.5.0
CaseClause is a case clause or default clause for a switch statement.
type ClassDecl ¶ added in v2.5.0
type ClassDecl struct { Name *Var // can be nil Extends IExpr // can be nil Definitions []FieldDefinition Methods []*MethodDecl }
ClassDecl is a class declaration.
type CondExpr ¶ added in v2.5.0
type CondExpr struct {
Cond, X, Y IExpr
}
CondExpr is a conditional expression.
type DebuggerStmt ¶ added in v2.5.0
type DebuggerStmt struct { }
DebuggerStmt is a debugger statement.
func (DebuggerStmt) String ¶ added in v2.5.0
func (n DebuggerStmt) String() string
type DeclType ¶ added in v2.5.0
type DeclType uint16
DeclType specifies the kind of declaration.
type DirectivePrologueStmt ¶ added in v2.5.10
type DirectivePrologueStmt struct {
Value []byte
}
DirectivePrologueStmt is a string literal at the beginning of a function or module (usually "use strict").
func (DirectivePrologueStmt) String ¶ added in v2.5.10
func (n DirectivePrologueStmt) String() string
type DoWhileStmt ¶ added in v2.5.0
DoWhileStmt is a do-while iteration statement.
func (DoWhileStmt) String ¶ added in v2.5.0
func (n DoWhileStmt) String() string
type DotExpr ¶ added in v2.5.0
type DotExpr struct { X IExpr Y LiteralExpr Prec OpPrec }
DotExpr is a member/call expression, super property, or optional chain with a dot expression.
type ExportStmt ¶ added in v2.5.0
ExportStmt is an export statement.
func (ExportStmt) String ¶ added in v2.5.0
func (n ExportStmt) String() string
type ExprStmt ¶ added in v2.5.0
type ExprStmt struct {
Value IExpr
}
ExprStmt is an expression statement.
type FieldDefinition ¶ added in v2.5.12
type FieldDefinition struct { Name PropertyName Init IExpr }
FieldDefinition is a field definition in a class declaration.
func (FieldDefinition) String ¶ added in v2.5.12
func (n FieldDefinition) String() string
type ForStmt ¶ added in v2.5.0
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.
type FuncDecl ¶ added in v2.5.0
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.
type GroupExpr ¶ added in v2.5.0
type GroupExpr struct {
X IExpr
}
GroupExpr is a parenthesized expression.
type IBinding ¶ added in v2.5.0
type IBinding interface { String() string // contains filtered or unexported methods }
IBinding is a dummy interface for bindings.
type IExpr ¶ added in v2.5.0
type IExpr interface { String() string // contains filtered or unexported methods }
IExpr is a dummy interface for expressions.
type IStmt ¶ added in v2.5.0
type IStmt interface { String() string // contains filtered or unexported methods }
IStmt is a dummy interface for statements.
type ImportMetaExpr ¶ added in v2.5.0
type ImportMetaExpr struct { }
ImportMetaExpr is a import meta meta property.
func (ImportMetaExpr) String ¶ added in v2.5.0
func (n ImportMetaExpr) String() string
type ImportStmt ¶ added in v2.5.0
ImportStmt is an import statement.
func (ImportStmt) String ¶ added in v2.5.0
func (n ImportStmt) String() string
type IndexExpr ¶ added in v2.5.0
IndexExpr is a member/call expression, super property, or optional chain with an index expression.
type LabelledStmt ¶ added in v2.5.0
LabelledStmt is a labelled statement.
func (LabelledStmt) String ¶ added in v2.5.0
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.
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';
func (*Lexer) Err ¶
Err returns the error encountered during lexing, this is often io.EOF but also other errors can be returned.
type LiteralExpr ¶ added in v2.5.0
LiteralExpr can be this, null, boolean, numeric, string, or regular expression literals.
func (LiteralExpr) String ¶ added in v2.5.0
func (n LiteralExpr) String() string
type MethodDecl ¶ added in v2.5.0
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) String ¶ added in v2.5.0
func (n MethodDecl) String() string
type NewTargetExpr ¶ added in v2.5.0
type NewTargetExpr struct { }
NewTargetExpr is a new target meta property.
func (NewTargetExpr) String ¶ added in v2.5.0
func (n NewTargetExpr) String() string
type ObjectExpr ¶ added in v2.5.0
type ObjectExpr struct {
List []Property
}
ObjectExpr is an object literal.
func (ObjectExpr) String ¶ added in v2.5.0
func (n ObjectExpr) String() string
type OpPrec ¶ added in v2.5.0
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.
type OptChainExpr ¶ added in v2.5.0
type OptChainExpr struct { X IExpr Y IExpr // can be CallExpr, IndexExpr, LiteralExpr, or TemplateExpr }
OptChainExpr is an optional chain.
func (OptChainExpr) String ¶ added in v2.5.0
func (n OptChainExpr) String() string
type Params ¶ added in v2.5.0
type Params struct { List []BindingElement Rest IBinding // can be nil }
Params is a list of parameters for functions, methods, and arrow function.
type Parser ¶ added in v2.5.0
type Parser struct {
// contains filtered or unexported fields
}
Parser is the state for the parser.
type Property ¶ added in v2.5.0
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.
type PropertyName ¶ added in v2.5.0
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 ¶ added in v2.5.0
func (n PropertyName) IsComputed() bool
IsComputed returns true if PropertyName is computed.
func (PropertyName) IsIdent ¶ added in v2.5.0
func (n PropertyName) IsIdent(data []byte) bool
IsIdent returns true if PropertyName equals the given identifier name.
func (PropertyName) IsSet ¶ added in v2.5.0
func (n PropertyName) IsSet() bool
IsSet returns true is PropertyName is not nil.
func (PropertyName) String ¶ added in v2.5.0
func (n PropertyName) String() string
type ReturnStmt ¶ added in v2.5.0
type ReturnStmt struct {
Value IExpr // can be nil
}
ReturnStmt is a return statement.
func (ReturnStmt) String ¶ added in v2.5.0
func (n ReturnStmt) String() string
type Scope ¶ added in v2.5.0
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) HoistUndeclared ¶ added in v2.5.0
func (s *Scope) HoistUndeclared()
HoistUndeclared copies all undeclared variables of the current scope to the parent scope.
func (*Scope) MarkArguments ¶ added in v2.5.0
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 ¶ added in v2.5.2
func (s *Scope) MarkForInit()
MarkForInit marks the declared variables in current scope as for statement initializer to distinguish from declarations in body.
func (*Scope) UndeclareScope ¶ added in v2.5.0
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.
type SwitchStmt ¶ added in v2.5.0
type SwitchStmt struct { Init IExpr List []CaseClause Scope }
SwitchStmt is a switch statement.
func (SwitchStmt) String ¶ added in v2.5.0
func (n SwitchStmt) String() string
type TemplateExpr ¶ added in v2.5.0
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) String ¶ added in v2.5.0
func (n TemplateExpr) String() string
type TemplatePart ¶ added in v2.5.0
TemplatePart is a template head or middle.
type ThrowStmt ¶ added in v2.5.0
type ThrowStmt struct {
Value IExpr
}
ThrowStmt is a throw statement.
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.
type TryStmt ¶ added in v2.5.0
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.
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.
type VarDecl ¶ added in v2.5.0
type VarDecl struct { TokenType List []BindingElement }
VarDecl is a variable statement or lexical declaration.
type VarsByUses ¶ added in v2.5.0
type VarsByUses VarArray
VarsByUses is sortable by uses in descending order.
func (VarsByUses) Len ¶ added in v2.5.0
func (vs VarsByUses) Len() int
func (VarsByUses) Less ¶ added in v2.5.0
func (vs VarsByUses) Less(i, j int) bool
func (VarsByUses) Swap ¶ added in v2.5.0
func (vs VarsByUses) Swap(i, j int)