js

package
v2.7.14 Latest Latest
Warning

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

Go to latest
Published: May 11, 2024 License: MIT Imports: 10 Imported by: 24

README

JS API reference

This package is a JS lexer (ECMAScript 2020) written in Go. It follows the specification at ECMAScript 2020 Language Specification. The lexer takes an io.Reader and converts it into tokens until the EOF.

Installation

Run the following command

go get -u github.com/tdewolff/parse/v2/js

or add the following import and run project with go get

import "github.com/tdewolff/parse/v2/js"

Lexer

Usage

The following initializes a new Lexer with io.Reader r:

l := js.NewLexer(parse.NewInput(r))

To tokenize until EOF an error, use:

for {
	tt, text := l.Next()
	switch tt {
	case js.ErrorToken:
		// error or EOF set in l.Err()
		return
	// ...
	}
}
Regular Expressions

The ECMAScript specification for PunctuatorToken (of which the / and /= symbols) and RegExpToken depend on a parser state to differentiate between the two. The lexer will always parse the first token as / or /= operator, upon which the parser can rescan that token to scan a regular expression using RegExp().

Examples
package main

import (
	"os"

	"github.com/tdewolff/parse/v2/js"
)

// Tokenize JS from stdin.
func main() {
	l := js.NewLexer(parse.NewInput(os.Stdin))
	for {
		tt, text := l.Next()
		switch tt {
		case js.ErrorToken:
			if l.Err() != io.EOF {
				fmt.Println("Error on line", l.Line(), ":", l.Err())
			}
			return
		case js.IdentifierToken:
			fmt.Println("Identifier", string(text))
		case js.NumericToken:
			fmt.Println("Numeric", string(text))
		// ...
		}
	}
}

Parser

Usage

The following parses a file and returns an abstract syntax tree (AST).

ast, err := js.NewParser(parse.NewInputString("if (state == 5) { console.log('In state five'); }"))

See ast.go for all available data structures that can represent the abstact syntax tree.

License

Released under the MIT license.

Documentation

Overview

Package js is an ECMAScript5.1 lexer following the specifications at http://www.ecma-international.org/ecma-262/5.1/.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrInvalidJSON = fmt.Errorf("invalid JSON")
View Source
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

func AsDecimalLiteral(b []byte) bool

AsDecimalLiteral returns true if a valid decimal literal is given.

func AsIdentifierName added in v2.5.0

func AsIdentifierName(b []byte) bool

AsIdentifierName returns true if a valid identifier name is given.

func IsIdentifier added in v2.5.0

func IsIdentifier(tt TokenType) bool

IsIdentifier matches Identifier, i.e. IdentifierName but not ReservedWord. Does not match yield or await.

func IsIdentifierContinue added in v2.5.0

func IsIdentifierContinue(b []byte) bool

IsIdentifierContinue returns true if the byte-slice start is a continuation of an identifier

func IsIdentifierEnd added in v2.5.0

func IsIdentifierEnd(b []byte) bool

IsIdentifierEnd returns true if the byte-slice end is a start or continuation of an identifier

func IsIdentifierName added in v2.5.0

func IsIdentifierName(tt TokenType) bool

IsIdentifierName matches IdentifierName, i.e. any identifier

func IsIdentifierStart added in v2.5.0

func IsIdentifierStart(b []byte) bool

IsIdentifierStart returns true if the byte-slice start is the start of an identifier

func IsNumeric added in v2.5.0

func IsNumeric(tt TokenType) bool

IsNumeric return true if token is numeric.

func IsOperator added in v2.5.0

func IsOperator(tt TokenType) bool

IsOperator return true if token is an operator.

func IsPunctuator added in v2.5.0

func IsPunctuator(tt TokenType) bool

IsPunctuator return true if token is a punctuator.

func IsReservedWord added in v2.5.0

func IsReservedWord(tt TokenType) bool

IsReservedWord matches ReservedWord

func Walk added in v2.5.18

func Walk(v IVisitor, n INode)

Walk traverses an AST in depth-first order

Types

type AST added in v2.5.0

type AST struct {
	BlockStmt // module
}

AST is the full ECMAScript abstract syntax tree.

func Parse added in v2.5.0

func Parse(r *parse.Input, o Options) (*AST, error)

Parse returns a JS AST tree of.

func (AST) JS added in v2.7.0

func (ast AST) JS(w io.Writer)

JS writes JavaScript to writer.

func (AST) JSON added in v2.7.0

func (ast AST) JSON(w io.Writer) error

JSON writes JSON to writer.

func (AST) JSONString added in v2.7.0

func (ast AST) JSONString() (string, error)

JSONString returns a string of JSON if valid.

func (AST) JSString added in v2.7.0

func (ast AST) JSString() string

JSONString returns a string of JavaScript.

func (AST) String added in v2.5.0

func (ast AST) String() string

type Alias added in v2.5.0

type Alias struct {
	Name    []byte // can be nil
	Binding []byte // can be nil
}

Alias is a name space import or import/export specifier for import/export statements.

func (Alias) JS added in v2.5.18

func (alias Alias) JS(w io.Writer)

JS writes JavaScript to writer.

func (Alias) String added in v2.5.0

func (alias Alias) String() string

type Arg added in v2.5.9

type Arg struct {
	Value IExpr
	Rest  bool
}

func (Arg) JS added in v2.5.18

func (n Arg) JS(w io.Writer)

JS writes JavaScript to writer.

func (Arg) String added in v2.5.18

func (n Arg) String() string

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.

func (Args) JS added in v2.5.18

func (n Args) JS(w io.Writer)

JS writes JavaScript to writer.

func (Args) String added in v2.5.4

func (n Args) String() string

type ArrayExpr added in v2.5.0

type ArrayExpr struct {
	List []Element
}

ArrayExpr is an array literal.

func (ArrayExpr) JS added in v2.5.18

func (n ArrayExpr) JS(w io.Writer)

JS writes JavaScript to writer.

func (ArrayExpr) JSON added in v2.5.20

func (n ArrayExpr) JSON(w io.Writer) error

JSON writes JSON to writer.

func (ArrayExpr) String added in v2.5.0

func (n ArrayExpr) String() string

type ArrowFunc added in v2.5.0

type ArrowFunc struct {
	Async  bool
	Params Params
	Body   BlockStmt
}

ArrowFunc is an (async) arrow function.

func (ArrowFunc) JS added in v2.5.18

func (n ArrowFunc) JS(w io.Writer)

JS writes JavaScript to writer.

func (ArrowFunc) String added in v2.5.0

func (n ArrowFunc) String() string

type BinaryExpr added in v2.5.0

type BinaryExpr struct {
	Op   TokenType
	X, Y IExpr
}

BinaryExpr is a binary expression.

func (BinaryExpr) JS added in v2.5.18

func (n BinaryExpr) JS(w io.Writer)

JS writes JavaScript to writer.

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) JS added in v2.5.18

func (n BindingArray) JS(w io.Writer)

JS writes JavaScript to writer.

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) JS added in v2.5.18

func (n BindingElement) JS(w io.Writer)

JS writes JavaScript to writer.

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) JS added in v2.5.18

func (n BindingObject) JS(w io.Writer)

JS writes JavaScript to writer.

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.

func (BindingObjectItem) JS added in v2.5.18

func (n BindingObjectItem) JS(w io.Writer)

JS writes JavaScript to writer.

func (BindingObjectItem) String added in v2.5.18

func (n BindingObjectItem) String() string

type BlockStmt added in v2.5.0

type BlockStmt struct {
	List []IStmt
	Scope
}

BlockStmt is a block statement.

func (BlockStmt) JS added in v2.5.18

func (n BlockStmt) JS(w io.Writer)

JS writes JavaScript to writer.

func (BlockStmt) String added in v2.5.0

func (n BlockStmt) String() string

type BranchStmt added in v2.5.0

type BranchStmt struct {
	Type  TokenType
	Label []byte // can be nil
}

BranchStmt is a continue or break statement.

func (BranchStmt) JS added in v2.5.18

func (n BranchStmt) JS(w io.Writer)

JS writes JavaScript to writer.

func (BranchStmt) String added in v2.5.0

func (n BranchStmt) String() string

type CallExpr added in v2.5.0

type CallExpr struct {
	X        IExpr
	Args     Args
	Optional bool
}

CallExpr is a call expression.

func (CallExpr) JS added in v2.5.18

func (n CallExpr) JS(w io.Writer)

JS writes JavaScript to writer.

func (CallExpr) String added in v2.5.0

func (n CallExpr) String() string

type CaseClause added in v2.5.0

type CaseClause struct {
	TokenType
	Cond IExpr // can be nil
	List []IStmt
}

CaseClause is a case clause or default clause for a switch statement.

func (CaseClause) JS added in v2.5.18

func (n CaseClause) JS(w io.Writer)

JS writes JavaScript to writer.

func (CaseClause) String added in v2.5.18

func (n CaseClause) String() string

type ClassDecl added in v2.5.0

type ClassDecl struct {
	Name    *Var  // can be nil
	Extends IExpr // can be nil
	List    []ClassElement
}

ClassDecl is a class declaration.

func (ClassDecl) JS added in v2.5.18

func (n ClassDecl) JS(w io.Writer)

JS writes JavaScript to writer.

func (ClassDecl) String added in v2.5.0

func (n ClassDecl) String() string

type ClassElement added in v2.6.0

type ClassElement struct {
	StaticBlock *BlockStmt  // can be nil
	Method      *MethodDecl // can be nil
	Field
}

ClassElement is a class element that is either a static block, a field definition, or a class method

func (ClassElement) JS added in v2.6.0

func (n ClassElement) JS(w io.Writer)

JS writes JavaScript to writer.

func (ClassElement) String added in v2.6.0

func (n ClassElement) String() string

type CommaExpr added in v2.5.27

type CommaExpr struct {
	List []IExpr
}

CommaExpr is a series of comma expressions.

func (CommaExpr) JS added in v2.5.27

func (n CommaExpr) JS(w io.Writer)

JS writes JavaScript to writer.

func (CommaExpr) String added in v2.5.27

func (n CommaExpr) String() string

type Comment added in v2.6.7

type Comment struct {
	Value []byte
}

Comment block or line, usually a bang comment.

func (Comment) JS added in v2.6.7

func (n Comment) JS(w io.Writer)

JS writes JavaScript to writer.

func (Comment) String added in v2.6.7

func (n Comment) String() string

type CondExpr added in v2.5.0

type CondExpr struct {
	Cond, X, Y IExpr
}

CondExpr is a conditional expression.

func (CondExpr) JS added in v2.5.18

func (n CondExpr) JS(w io.Writer)

JS writes JavaScript to writer.

func (CondExpr) String added in v2.5.0

func (n CondExpr) String() string

type DebuggerStmt added in v2.5.0

type DebuggerStmt struct{}

DebuggerStmt is a debugger statement.

func (DebuggerStmt) JS added in v2.5.18

func (n DebuggerStmt) JS(w io.Writer)

JS writes JavaScript to writer.

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.

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 added in v2.5.0

func (decl DeclType) String() string

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) JS added in v2.5.18

JS writes JavaScript to writer.

func (DirectivePrologueStmt) String added in v2.5.10

func (n DirectivePrologueStmt) String() string

type DoWhileStmt added in v2.5.0

type DoWhileStmt struct {
	Cond IExpr
	Body IStmt
}

DoWhileStmt is a do-while iteration statement.

func (DoWhileStmt) JS added in v2.5.18

func (n DoWhileStmt) JS(w io.Writer)

JS writes JavaScript to writer.

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
	Optional bool
}

DotExpr is a member/call expression, super property, or optional chain with a dot expression.

func (DotExpr) JS added in v2.5.18

func (n DotExpr) JS(w io.Writer)

JS writes JavaScript to writer.

func (DotExpr) String added in v2.5.0

func (n DotExpr) String() string

type Element added in v2.5.0

type Element struct {
	Value  IExpr // can be nil
	Spread bool
}

Element is an array literal element.

func (Element) JS added in v2.5.18

func (n Element) JS(w io.Writer)

JS writes JavaScript to writer.

func (Element) String added in v2.5.18

func (n Element) String() string

type EmptyStmt added in v2.5.0

type EmptyStmt struct{}

EmptyStmt is an empty statement.

func (EmptyStmt) JS added in v2.5.18

func (n EmptyStmt) JS(w io.Writer)

JS writes JavaScript to writer.

func (EmptyStmt) String added in v2.5.0

func (n EmptyStmt) String() string

type ExportStmt added in v2.5.0

type ExportStmt struct {
	List    []Alias
	Module  []byte // can be nil
	Default bool
	Decl    IExpr
}

ExportStmt is an export statement.

func (ExportStmt) JS added in v2.5.18

func (n ExportStmt) JS(w io.Writer)

JS writes JavaScript to writer.

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.

func (ExprStmt) JS added in v2.5.18

func (n ExprStmt) JS(w io.Writer)

JS writes JavaScript to writer.

func (ExprStmt) String added in v2.5.0

func (n ExprStmt) String() string

type Field added in v2.6.0

type Field struct {
	Static bool
	Name   PropertyName
	Init   IExpr
}

Field is a field definition in a class declaration.

func (Field) JS added in v2.6.0

func (n Field) JS(w io.Writer)

JS writes JavaScript to writer.

func (Field) String added in v2.6.0

func (n Field) String() string

type ForInStmt added in v2.5.0

type ForInStmt struct {
	Init  IExpr
	Value IExpr
	Body  *BlockStmt
}

ForInStmt is a for-in iteration statement.

func (ForInStmt) JS added in v2.5.18

func (n ForInStmt) JS(w io.Writer)

JS writes JavaScript to writer.

func (ForInStmt) String added in v2.5.0

func (n ForInStmt) String() string

type ForOfStmt added in v2.5.0

type ForOfStmt struct {
	Await bool
	Init  IExpr
	Value IExpr
	Body  *BlockStmt
}

ForOfStmt is a for-of iteration statement.

func (ForOfStmt) JS added in v2.5.18

func (n ForOfStmt) JS(w io.Writer)

JS writes JavaScript to writer.

func (ForOfStmt) String added in v2.5.0

func (n ForOfStmt) 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.

func (ForStmt) JS added in v2.5.18

func (n ForStmt) JS(w io.Writer)

JS writes JavaScript to writer.

func (ForStmt) String added in v2.5.0

func (n ForStmt) String() string

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.

func (FuncDecl) JS added in v2.5.18

func (n FuncDecl) JS(w io.Writer)

JS writes JavaScript to writer.

func (FuncDecl) String added in v2.5.0

func (n FuncDecl) String() string

type GroupExpr added in v2.5.0

type GroupExpr struct {
	X IExpr
}

GroupExpr is a parenthesized expression.

func (GroupExpr) JS added in v2.5.18

func (n GroupExpr) JS(w io.Writer)

JS writes JavaScript to writer.

func (GroupExpr) String added in v2.5.0

func (n GroupExpr) String() string

type IBinding added in v2.5.0

type IBinding interface {
	INode
	// contains filtered or unexported methods
}

IBinding is a dummy interface for bindings.

type IExpr added in v2.5.0

type IExpr interface {
	INode
	// contains filtered or unexported methods
}

IExpr is a dummy interface for expressions.

type INode added in v2.5.18

type INode interface {
	String() string
	JS(io.Writer)
}

INode is an interface for AST nodes

type IStmt added in v2.5.0

type IStmt interface {
	INode
	// contains filtered or unexported methods
}

IStmt is a dummy interface for statements.

type IVisitor added in v2.5.18

type IVisitor interface {
	Enter(n INode) IVisitor
	Exit(n INode)
}

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 added in v2.5.0

type IfStmt struct {
	Cond IExpr
	Body IStmt
	Else IStmt // can be nil
}

IfStmt is an if statement.

func (IfStmt) JS added in v2.5.18

func (n IfStmt) JS(w io.Writer)

JS writes JavaScript to writer.

func (IfStmt) String added in v2.5.0

func (n IfStmt) String() string

type ImportMetaExpr added in v2.5.0

type ImportMetaExpr struct{}

ImportMetaExpr is a import meta meta property.

func (ImportMetaExpr) JS added in v2.5.18

func (n ImportMetaExpr) JS(w io.Writer)

JS writes JavaScript to writer.

func (ImportMetaExpr) String added in v2.5.0

func (n ImportMetaExpr) String() string

type ImportStmt added in v2.5.0

type ImportStmt struct {
	List    []Alias
	Default []byte // can be nil
	Module  []byte
}

ImportStmt is an import statement.

func (ImportStmt) JS added in v2.5.18

func (n ImportStmt) JS(w io.Writer)

JS writes JavaScript to writer.

func (ImportStmt) String added in v2.5.0

func (n ImportStmt) String() string

type IndexExpr added in v2.5.0

type IndexExpr struct {
	X        IExpr
	Y        IExpr
	Prec     OpPrec
	Optional bool
}

IndexExpr is a member/call expression, super property, or optional chain with an index expression.

func (IndexExpr) JS added in v2.5.18

func (n IndexExpr) JS(w io.Writer)

JS writes JavaScript to writer.

func (IndexExpr) String added in v2.5.0

func (n IndexExpr) String() string

type JSONer added in v2.5.22

type JSONer interface {
	JSON(io.Writer) error
}

type LabelledStmt added in v2.5.0

type LabelledStmt struct {
	Label []byte
	Value IStmt
}

LabelledStmt is a labelled statement.

func (LabelledStmt) JS added in v2.5.18

func (n LabelledStmt) JS(w io.Writer)

JS writes JavaScript to writer.

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

func (l *Lexer) Err() error

Err returns the error encountered during lexing, this is often io.EOF but also other errors can be returned.

func (*Lexer) Next

func (l *Lexer) Next() (TokenType, []byte)

Next returns the next Token. It returns ErrorToken when an error was encountered. Using Err() one can retrieve the error message.

func (*Lexer) RegExp added in v2.5.0

func (l *Lexer) RegExp() (TokenType, []byte)

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 added in v2.5.0

type LiteralExpr struct {
	TokenType
	Data []byte
}

LiteralExpr can be this, null, boolean, numeric, string, or regular expression literals.

func (LiteralExpr) JS added in v2.5.18

func (n LiteralExpr) JS(w io.Writer)

JS writes JavaScript to writer.

func (LiteralExpr) JSON added in v2.5.20

func (n LiteralExpr) JSON(w io.Writer) error

JSON writes JSON to writer.

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) JS added in v2.5.18

func (n MethodDecl) JS(w io.Writer)

JS writes JavaScript to writer.

func (MethodDecl) String added in v2.5.0

func (n MethodDecl) String() string

type NewExpr added in v2.5.0

type NewExpr struct {
	X    IExpr
	Args *Args // can be nil
}

NewExpr is a new expression or new member expression.

func (NewExpr) JS added in v2.5.18

func (n NewExpr) JS(w io.Writer)

JS writes JavaScript to writer.

func (NewExpr) String added in v2.5.0

func (n NewExpr) String() string

type NewTargetExpr added in v2.5.0

type NewTargetExpr struct{}

NewTargetExpr is a new target meta property.

func (NewTargetExpr) JS added in v2.5.18

func (n NewTargetExpr) JS(w io.Writer)

JS writes JavaScript to writer.

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) JS added in v2.5.18

func (n ObjectExpr) JS(w io.Writer)

JS writes JavaScript to writer.

func (ObjectExpr) JSON added in v2.5.20

func (n ObjectExpr) JSON(w io.Writer) error

JSON writes JSON to writer.

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.

func (OpPrec) String added in v2.5.0

func (prec OpPrec) String() string

type Options added in v2.5.23

type Options struct {
	WhileToFor bool
	Inline     bool
}

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.

func (Params) JS added in v2.5.18

func (n Params) JS(w io.Writer)

JS writes JavaScript to writer.

func (Params) String added in v2.5.0

func (n Params) String() string

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.

func (Property) JS added in v2.5.18

func (n Property) JS(w io.Writer)

JS writes JavaScript to writer.

func (Property) JSON added in v2.5.20

func (n Property) JSON(w io.Writer) error

JSON writes JSON to writer.

func (Property) String added in v2.5.0

func (n Property) String() string

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) JS added in v2.5.18

func (n PropertyName) JS(w io.Writer)

JS writes JavaScript to writer.

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) JS added in v2.5.18

func (n ReturnStmt) JS(w io.Writer)

JS writes JavaScript to writer.

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
	Declared       VarArray // Link in Var are always nil
	Undeclared     VarArray
	VarDecls       []*VarDecl
	NumForDecls    uint16 // offset into Declared to mark variables used in for statements
	NumFuncArgs    uint16 // offset into Declared to mark variables used in function arguments
	NumArgUses     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) AddUndeclared added in v2.5.24

func (s *Scope) AddUndeclared(v *Var)

add undeclared variable to scope, this is called for the block scope when declaring a var in it

func (*Scope) Declare added in v2.5.0

func (s *Scope) Declare(decl DeclType, name []byte) (*Var, bool)

Declare declares a new variable.

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) MarkForStmt added in v2.5.24

func (s *Scope) MarkForStmt()

MarkForStmt marks the declared variables in current scope as for statement initializer to distinguish from declarations in body.

func (*Scope) MarkFuncArgs added in v2.5.32

func (s *Scope) MarkFuncArgs()

MarkFuncArgs marks the declared/undeclared variables in the current scope as function arguments.

func (Scope) String added in v2.5.0

func (s Scope) String() string

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.

func (*Scope) Unscope added in v2.5.17

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 added in v2.5.0

func (s *Scope) Use(name []byte) *Var

Use increments the usage of a variable.

type SwitchStmt added in v2.5.0

type SwitchStmt struct {
	Init IExpr
	List []CaseClause
	Scope
}

SwitchStmt is a switch statement.

func (SwitchStmt) JS added in v2.5.18

func (n SwitchStmt) JS(w io.Writer)

JS writes JavaScript to writer.

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
	Optional bool
}

TemplateExpr is a template literal or member/call expression, super property, or optional chain with template literal.

func (TemplateExpr) JS added in v2.5.18

func (n TemplateExpr) JS(w io.Writer)

JS writes JavaScript to writer.

func (TemplateExpr) JSON added in v2.5.20

func (n TemplateExpr) JSON(w io.Writer) error

JSON writes JSON to writer.

func (TemplateExpr) String added in v2.5.0

func (n TemplateExpr) String() string

type TemplatePart added in v2.5.0

type TemplatePart struct {
	Value []byte
	Expr  IExpr
}

TemplatePart is a template head or middle.

func (TemplatePart) JS added in v2.5.18

func (n TemplatePart) JS(w io.Writer)

JS writes JavaScript to writer.

func (TemplatePart) String added in v2.5.18

func (n TemplatePart) String() string

type ThrowStmt added in v2.5.0

type ThrowStmt struct {
	Value IExpr
}

ThrowStmt is a throw statement.

func (ThrowStmt) JS added in v2.5.18

func (n ThrowStmt) JS(w io.Writer)

JS writes JavaScript to writer.

func (ThrowStmt) String added in v2.5.0

func (n ThrowStmt) String() 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
	IntegerToken
)

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 added in v2.5.0

func (tt TokenType) Bytes() []byte

Bytes returns the string representation of a TokenType.

func (TokenType) String

func (tt TokenType) String() string

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.

func (TryStmt) JS added in v2.5.18

func (n TryStmt) JS(w io.Writer)

JS writes JavaScript to writer.

func (TryStmt) String added in v2.5.0

func (n TryStmt) String() string

type UnaryExpr added in v2.5.0

type UnaryExpr struct {
	Op TokenType
	X  IExpr
}

UnaryExpr is an update or unary expression.

func (UnaryExpr) JS added in v2.5.18

func (n UnaryExpr) JS(w io.Writer)

JS writes JavaScript to writer.

func (UnaryExpr) JSON added in v2.5.20

func (n UnaryExpr) JSON(w io.Writer) error

JSON writes JSON to writer.

func (UnaryExpr) String added in v2.5.0

func (n UnaryExpr) String() 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) Info added in v2.7.0

func (v *Var) Info() string

func (Var) JS added in v2.5.18

func (v Var) JS(w io.Writer)

JS writes JavaScript to writer.

func (*Var) Name added in v2.5.0

func (v *Var) Name() []byte

Name returns the variable name.

func (Var) String added in v2.5.0

func (v Var) String() string

type VarArray added in v2.5.0

type VarArray []*Var

VarArray is a set of variables in scopes.

func (VarArray) String added in v2.5.0

func (vs VarArray) String() string

type VarDecl added in v2.5.0

type VarDecl struct {
	TokenType
	List             []BindingElement
	Scope            *Scope
	InFor, InForInOf bool
}

VarDecl is a variable statement or lexical declaration.

func (VarDecl) JS added in v2.5.18

func (n VarDecl) JS(w io.Writer)

JS writes JavaScript to writer.

func (VarDecl) String added in v2.5.0

func (n VarDecl) String() string

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)

type WhileStmt added in v2.5.0

type WhileStmt struct {
	Cond IExpr
	Body IStmt
}

WhileStmt is a while iteration statement.

func (WhileStmt) JS added in v2.5.18

func (n WhileStmt) JS(w io.Writer)

JS writes JavaScript to writer.

func (WhileStmt) String added in v2.5.0

func (n WhileStmt) String() string

type WithStmt added in v2.5.0

type WithStmt struct {
	Cond IExpr
	Body IStmt
}

WithStmt is a with statement.

func (WithStmt) JS added in v2.5.18

func (n WithStmt) JS(w io.Writer)

JS writes JavaScript to writer.

func (WithStmt) String added in v2.5.0

func (n WithStmt) String() string

type YieldExpr added in v2.5.0

type YieldExpr struct {
	Generator bool
	X         IExpr // can be nil
}

YieldExpr is a yield expression.

func (YieldExpr) JS added in v2.5.18

func (n YieldExpr) JS(w io.Writer)

JS writes JavaScript to writer.

func (YieldExpr) String added in v2.5.0

func (n YieldExpr) String() string

Jump to

Keyboard shortcuts

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