yari-javascript-parser

Fault-tolerant JavaScript parser producing a typed, fully-located AST. Supports the full ES2022+ language: functions, classes, ES6 modules (import/export), async/await, destructuring, template literals, generators, optional chaining, nullish coalescing, and more. Never throws on malformed input. Built for speed — it stays fast even on very large scripts, which it was designed to handle.

Performance. The parser was engineered for throughput: it parses large, real-world bundles and minified files quickly, with linear scaling and low allocation overhead, so big scripts stay fast.

Installation

Add the dependency to your build file. Replace VERSION with the desired release.

// Gradle (Groovy DSL)
implementation 'com.easyparsingapi:yari-javascript-parser:VERSION'

// Maven
<dependency>
    <groupId>com.easyparsingapi</groupId>
    <artifactId>yari-javascript-parser</artifactId>
    <version>VERSION</version>
</dependency>

JavascriptParser — Entry Points

All methods are static. JavascriptParser is not instantiable.

JavascriptParser com.easyparsingapi.yari.parser.javascript.parser
parse(String) → Javascript Parse a complete JavaScript program
parse(List<Token>) → Javascript Parse a pre-tokenised program
parseUnit(String) → AstResult<Javascript> Parse and return node + token list
parseUnit(List<Token>) → AstResult<Javascript> Parse pre-tokenised, return with token list
parseExpression(String) → AstResult<Javascript> Parse a single expression
parseExpression(List<Token>) → AstResult<Javascript> Parse an expression from tokens

Javascript — Root AST Node

Root of a parsed JavaScript program. Implements AstUnit and JavascriptProcedure.

Javascript com.easyparsingapi.yari.parser.javascript.ast
getNodes() → List<JavascriptNode> Top-level statements
astComments() → List<AstComment> All comments in the parsed program
astCommentsOf(AstNode, Position…) → List<AstComment> Comments associated with a given node at the specified position

Declarations

Declaration nodes represent named definitions — variables, functions, classes, and ES6 module imports and exports.

Declaration nodes com.easyparsingapi.yari.parser.javascript.ast
VariableDeclaration var/let/const declaration. getType() → Type ('var', 'let', 'const'), getVariableDeclarations() → List<Variable>
FunctionDeclaration Function declaration. getName() → Identifier, getSignature() → Signature, getProcedure() → Procedure, isGenerator() → boolean, isAsynchronous() → boolean
ClassDeclaration Class declaration with optional extends. getClassName() → Identifier, isExtended() → boolean, getExtendedName() → JavascriptNode, getProperties() → List<JavascriptNode>
ArrowFunction Arrow function expression. getSignature() → JavascriptSignature, getProcedure() → JavascriptProcedure, isAsynchronous() → boolean
Import import … from '…' statement. getModuleName() → Literal, getDefaultName() → Identifier, getImportBlock() → ImportBlock
Export export declaration. isDefault() → boolean, getDefinition() → JavascriptNode
MethodDeclaration / ClassMethodDeclaration / Getter / Setter Class member declarations

Expressions

Expression nodes represent values, operators, and compound constructs that can appear wherever a value is expected.

Expression nodes com.easyparsingapi.yari.parser.javascript.ast
Identifier A name. getValue() → String
Literal A literal value (number, string, boolean, regexp…). getType() → Literal.Type, getValue() → String
LiteralTemplate A template literal. getElements() → List<JavascriptNode>
Infix Binary expression. getLeftOperand(), getOperator(), getRightOperand()
Prefix / Decrement / Increment Prefix/postfix unary operators
Ternary Conditional expression. getCondition(), getIfPart(), getElsePart()
Assignment Assignment expression, with optional chaining via Assignment.Chaining
FunctionCall Function invocation. getName() → JavascriptNode, getSignature() → Signature
QualifiedExpression Member access chain (a.b.c, a?.b). getQualifiers(), walk(Consumer<Handler>)
NewClass / New new expressions
ObjectDeclaration Object literal {…}
ArrayDeclaration Array literal […]
Spread Spread expression …expr
Await / Yield async/generator primitives
Sequence / Parenthesis Comma sequence and parenthesised expressions

Statements

Statement nodes represent control-flow and structural constructs that appear in a procedure body or at the top level of a program.

Statement nodes com.easyparsingapi.yari.parser.javascript.ast
If If/else statement. getIfBlock() → IfBlock, getElseIfBlocks() → List<ElseIfBlock>, getElseBlock() → ElseBlock
For For, for-in, and for-of loops
While While loop
DoWhile Do-while loop
Switch Switch statement. getSwitchExpression() → SwitchExpression, getSwitchProcedure() → SwitchProcedure (holds SwitchCase and DefaultCase clauses)
Try / Catch / Finally Try-catch-finally block
Return Return statement. getReturnValue() → JavascriptNode
Throw Throw statement. getThrownValue() → JavascriptNode
Break Break statement, with optional label
Continue Continue statement, with optional label
Label Labelled statement
Debugger Debugger statement
UseStrict The 'use strict' directive
EmptyStatement A lone semicolon
BlockProcedure A bare block statement {…}

Comments & Source Locations

Every AST node carries an exact SourceLocation with start/end offsets, lines, and columns. Comments are preserved as first-class AstComment nodes and can be queried relative to any AST node.

AstResult<Javascript> result = JavascriptParser.parseUnit(source);
Javascript js = result.unit();

// All comments
List<AstComment> all = js.astComments();

// Comments before each top-level node
js.getNodes().forEach(node ->
    js.astCommentsOf(node, AstUnit.Position.before)
      .forEach(c -> System.out.println(((JavascriptComment) c).getComment())));

// Source substring for a node
js.astStream()
  .filter(n -> n instanceof FunctionDeclaration)
  .forEach(n -> System.out.println(
      result.substring(n.getSourceLocation())));

Error Recovery

The parser emits a JavascriptError node and resumes whenever it encounters unrecognisable input. The enclosing procedure body is still parsed to completion, so you always get the maximum number of recognisable nodes from a broken or partially-malformed source.

For the full code-level reference, see the README on GitHub and Javadoc under yari-javascript-parser/src/main/java/.