Nashorn parser API provides interfaces to represent ECMAScript source code as abstract syntax trees (AST) and Parser to parse ECMAScript source scripts.
Using parser API user can write Java code to access parse tree representation of ECMAScript source. Script source may be a file, a URL or a String. Unless stated otherwise null argument in methods of this package result in NullPointerException being thrown.
import jdk.nashorn.api.tree.*;
import java.io.File;
// Simple example that prints warning on 'with' statements
public class Main {
public static void main(String[] args) throws Exception {
// Create a new parser instance
Parser parser = Parser.create();
File sourceFile = new File(args[0]);
// Parse given source File using parse method.
// Pass a diagnostic listener to print error messages.
CompilationUnitTree cut = parser.parse(sourceFile,
(d) -> { System.out.println(d); });
if (cut != null) {
// call Tree.accept method passing a SimpleTreeVisitor
cut.accept(new SimpleTreeVisitor<Void, Void>() {
// visit method for 'with' statement
public Void visitWith(WithTree wt, Void v) {
// print warning on 'with' statement
System.out.println("Warning: using 'with' statement!");
return null;
}
}, null);
}
}
}
Interface | Description |
---|---|
ArrayAccessTree |
A tree node for an array access expression.
|
ArrayLiteralTree |
Represents ECMAScript array literal expression.
|
AssignmentTree |
A tree node for an assignment expression.
|
BinaryTree |
A tree node for a binary expression.
|
BlockTree |
A tree node for a statement block.
|
BreakTree |
A tree node for a 'break' statement.
|
CaseTree |
A tree node for a 'case' in a 'switch' statement.
|
CatchTree |
A tree node for a 'catch' block in a 'try' statement.
|
ClassDeclarationTree |
A tree node that represents a class declaration.
|
ClassExpressionTree |
A tree node that represents a class expression.
|
CompilationUnitTree |
Represents the abstract syntax tree for compilation units (source
files)
|
CompoundAssignmentTree |
A tree node for compound assignment operator.
|
ConditionalExpressionTree |
A tree node for the conditional operator ?
|
ConditionalLoopTree |
A mixin for conditional "loop" statements.
|
ContinueTree |
A tree node for a 'continue' statement.
|
DebuggerTree |
A tree node for a 'debugger' statement.
|
Diagnostic |
Interface for diagnostics from tools.
|
DiagnosticListener |
Interface for receiving diagnostics from Nashorn parser.
|
DoWhileLoopTree |
A tree node for a 'do' statement.
|
EmptyStatementTree |
A tree node for an empty (skip) statement.
|
ErroneousTree |
A tree node to stand in for a malformed expression.
|
ExportEntryTree |
A Tree node for export entry in Module information.
|
ExpressionStatementTree |
A tree node for an expression statement.
|
ExpressionTree |
A tree node used as the base class for the different types of
expressions.
|
ForInLoopTree |
A tree node for for..in statement
For example:
|
ForLoopTree |
A tree node for a basic 'for' loop statement.
|
ForOfLoopTree |
A tree node for for..of statement.
|
FunctionCallTree |
A tree node for a function call expression.
|
FunctionDeclarationTree |
A tree node for a function declaration.
|
FunctionExpressionTree |
A tree node for function expressions including arrow functions.
|
GotoTree |
A tree node for a statement that jumps to a target.
|
IdentifierTree |
A tree node for an identifier expression.
|
IfTree |
A tree node for an 'if' statement.
|
ImportEntryTree |
A Tree node for import entry of Module information.
|
InstanceOfTree |
A tree node for an 'instanceof' expression.
|
LabeledStatementTree |
A tree node for a labeled statement.
|
LineMap |
Provides methods to convert between character positions and line numbers
for a compilation unit.
|
LiteralTree |
A tree node for a literal expression.
|
LoopTree |
A mixin for "loop" statements.
|
MemberSelectTree |
A tree node for a member access expression.
|
ModuleTree |
A Tree node for Module information.
|
NewTree |
A tree node to declare a new instance of a class.
|
ObjectLiteralTree |
Represents ECMAScript object literal expression.
|
ParenthesizedTree |
A tree node for a parenthesized expression.
|
Parser |
Represents nashorn ECMAScript parser instance.
|
PropertyTree |
To represent property setting in an object literal tree.
|
RegExpLiteralTree |
Represents regular expression literal in the source code.
|
ReturnTree |
A tree node for a 'return' statement.
|
SpreadTree |
A tree node for spread operator in array elements, function call arguments.
|
StatementTree |
A tree node used as the base class for the different kinds of
statements.
|
SwitchTree |
A tree node for a 'switch' statement.
|
TemplateLiteralTree |
A tree node for template literal strings.
|
ThrowTree |
A tree node for a 'throw' statement.
|
Tree |
Common interface for all nodes in an abstract syntax tree.
|
TreeVisitor<R,P> |
A visitor of trees, in the style of the visitor design pattern.
|
TryTree |
A tree node for a 'try' statement.
|
UnaryTree |
A tree node for postfix and unary expressions.
|
VariableTree |
A tree node for a variable declaration statement.
|
WhileLoopTree |
A tree node for a 'while' loop statement.
|
WithTree |
A tree node for a 'with' statement.
|
YieldTree |
A tree node for yield expressions used in generator functions.
|
Class | Description |
---|---|
SimpleTreeVisitorES5_1<R,P> |
A simple implementation of the TreeVisitor for ECMAScript edition 5.1.
|
SimpleTreeVisitorES6<R,P> |
A simple implementation of the TreeVisitor for ECMAScript edition 6.
|
Enum | Description |
---|---|
Diagnostic.Kind |
Kinds of diagnostics, for example, error or warning.
|
Tree.Kind |
Enumerates all kinds of trees.
|
Exception | Description |
---|---|
UnknownTreeException |
Indicates that an unknown kind of Tree was encountered.
|
Submit a bug or feature
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 2014, 2017, Oracle and/or its affiliates. 500 Oracle Parkway
Redwood Shores, CA 94065 USA. All rights reserved.
DRAFT 9-Ubuntu+0-9b159-1ubuntu1