package
2.7.20
Repository: https://github.com/tdewolff/parse.git
Documentation: pkg.go.dev

# 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.

# Functions

AsDecimalLiteral returns true if a valid decimal literal is given.
AsIdentifierName returns true if a valid identifier name is given.
IsIdentifier matches Identifier, i.e.
IsIdentifierContinue returns true if the byte-slice start is a continuation of an identifier.
IsIdentifierEnd returns true if the byte-slice end is a start or continuation of an identifier.
IsIdentifierName matches IdentifierName, i.e.
IsIdentifierStart returns true if the byte-slice start is the start of an identifier.
IsNumeric return true if token is numeric.
IsOperator return true if token is an operator.
IsPunctuator return true if token is a punctuator.
IsReservedWord matches ReservedWord.
NewLexer returns a new Lexer for a given io.Reader.
Parse returns a JS AST tree of.
Walk traverses an AST in depth-first order.

# Constants

+=.
+.
&&=.
&&.
function and method arguments.
=>.
Identifier token values.
Identifier token values.
Reserved token values.
Numeric token values.
&=.
&.
~.
|=.
|.
^=.
^.
Reserved token values.
Reserved token values.
catch statement argument.
Reserved token values.
Reserved token values.
}.
].
).
:.
,.
TokenType values.
TokenType values.
Reserved token values.
Reserved token values.
Reserved token values.
Numeric token values.
--.
Reserved token values.
Reserved token values.
/=.
/.
Reserved token values.
.
...
Reserved token values.
Reserved token values.
===.
==.
=.
extra token when errors occur.
**=.
Reserved token values.
function expression name or class expression name.
**.
Reserved token values.
Reserved token values.
Reserved token values.
Reserved token values.
Identifier token values.
function.
Reserved token values.
Identifier token values.
>=.
>>=.
>>>=.
>>>.
>>.
>.
Numeric token values.
Identifier token values.
Reserved token values.
Identifier token values.
Reserved token values.
++.
Reserved token values.
Numeric token values.
Identifier token values.
Reserved token values.
Identifier token values.
let, const, class.
\r \n \r\n.
<=.
<<=.
<<.
<.
Identifier token values.
%=.
%.
*=.
*.
-a.
Reserved token values.
undeclared variables.
!==.
!=.
!.
??=.
??.
Reserved token values.
Numeric token values.
Numeric token values.
Identifier token values.
a+b, a-b.
a&&b.
a?b:c, yield x, ()=>x, async ()=>x, a=b, a+=b, ...
a&b.
a|b.
a^b.
a?.b, a(b), super(a), import(a).
a??b.
a<b, a>b, a<=b, a>=b, a instanceof b, a in b.
{.
[.
(.
a==b, a!=b, a===b, a!==b.
Operator token values.
a**b.
a,b.
CallExpr/OptChainExpr or NewExpr.
a[b], a.b, a`b`, super[x], super.x, new.target, import.meta, new a(b).
a*b, a/b, a%b.
new a.
a||b.
literal, function, class, parenthesized.
a<<b, a>>b, a>>>b.
?.
++x, --x, delete x, void x, typeof x, +x, -x, ~x, !x, await x.
x++, x--.
||=.
||.
Identifier token values.
a--.
a++.
+a.
--a.
++a.
TokenType values.
Identifier token values.
Identifier token values.
Identifier token values.
Punctuator token values.
?.
TokenType values.
Reserved token values.
Reserved token values.
;.
Identifier token values.
Identifier token values.
TokenType values.
-=.
-.
Reserved token values.
Reserved token values.
Identifier token values.
TokenType values.
TokenType values.
TokenType values.
TokenType values.
Reserved token values.
Reserved token values.
Reserved token values.
Reserved token values.
Reserved token values.
var.
Reserved token values.
Reserved token values.
Reserved token values.
TokenType values.
Reserved token values.
Reserved token values.

# Variables

No description provided by the author
Keywords is a map of reserved, strict, and other keywords.
No description provided by the author
No description provided by the author

# Structs

Alias is a name space import or import/export specifier for import/export statements.
No description provided by the author
Args is a list of arguments as used by new and call expressions.
ArrayExpr is an array literal.
ArrowFunc is an (async) arrow function.
AST is the full ECMAScript abstract syntax tree.
BinaryExpr is a binary expression.
BindingArray is an array binding pattern.
BindingElement is a binding element.
BindingObject is an object binding pattern.
BindingObjectItem is a binding property.
BlockStmt is a block statement.
BranchStmt is a continue or break statement.
CallExpr is a call expression.
CaseClause is a case clause or default clause for a switch statement.
ClassDecl is a class declaration.
ClassElement is a class element that is either a static block, a field definition, or a class method.
CommaExpr is a series of comma expressions.
Comment block or line, usually a bang comment.
CondExpr is a conditional expression.
DebuggerStmt is a debugger statement.
DirectivePrologueStmt is a string literal at the beginning of a function or module (usually "use strict").
DotExpr is a member/call expression, super property, or optional chain with a dot expression.
DoWhileStmt is a do-while iteration statement.
Element is an array literal element.
EmptyStmt is an empty statement.
ExportStmt is an export statement.
ExprStmt is an expression statement.
Field is a field definition in a class declaration.
ForInStmt is a for-in iteration statement.
ForOfStmt is a for-of iteration statement.
ForStmt is a regular for iteration statement.
FuncDecl is an (async) (generator) function declaration or expression.
GroupExpr is a parenthesized expression.
IfStmt is an if statement.
ImportMetaExpr is a import meta meta property.
ImportStmt is an import statement.
IndexExpr is a member/call expression, super property, or optional chain with an index expression.
LabelledStmt is a labelled statement.
Lexer is the state for the lexer.
LiteralExpr can be this, null, boolean, numeric, string, or regular expression literals.
MethodDecl is a method definition in a class declaration.
NewExpr is a new expression or new member expression.
NewTargetExpr is a new target meta property.
ObjectExpr is an object literal.
No description provided by the author
Params is a list of parameters for functions, methods, and arrow function.
Parser is the state for the parser.
Property is a property definition in an object literal.
PropertyName is a property name for binding properties, method names, and in object literals.
ReturnStmt is a return statement.
Scope is a function or block scope with a list of variables declared and used.
SwitchStmt is a switch statement.
TemplateExpr is a template literal or member/call expression, super property, or optional chain with template literal.
TemplatePart is a template head or middle.
ThrowStmt is a throw statement.
TryStmt is a try statement.
UnaryExpr is an update or unary expression.
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.
VarDecl is a variable statement or lexical declaration.
WhileStmt is a while iteration statement.
WithStmt is a with statement.
YieldExpr is a yield expression.

# Interfaces

IBinding is a dummy interface for bindings.
IExpr is a dummy interface for expressions.
INode is an interface for AST nodes.
IStmt is a dummy interface for statements.
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.
No description provided by the author

# Type aliases

DeclType specifies the kind of declaration.
OpPrec is the operator precedence.
TokenType determines the type of token, eg.
VarArray is a set of variables in scopes.
VarsByUses is sortable by uses in descending order.