AstNode

abstract class AstNode(pos: Int = -1, len: Int = 1) : Node, Comparable<AstNode>

Base class for AST node types. The AST represents the physical source code, which makes it useful for IDEs and pretty-printers; the parser must not rewrite the tree when producing it.

The hierarchy sits on top of the older Node class, which was designed for code generation and keeps its children in a weakly typed linked list. AstNode is a strongly typed facade over that list with named accessors, plus position, length and parent.

All offset fields in subclasses are relative to the parent. During parsing the positions are absolute; adding a node to its parent rewrites them to be relative, so by the time a visitor sees the tree all offsets are relative.

Inheritors

Constructors

Link copied to clipboard
constructor(pos: Int = -1, len: Int = 1)

Types

Link copied to clipboard
object Companion
Link copied to clipboard

Sorts nodes by relative start position, so it only compares siblings.

Properties

Link copied to clipboard

The absolute document position, computed by adding this node's relative position to the relative positions of all its parents.

Link copied to clipboard

The AstRoot at the top of this node's parent chain, or null if there is none.

Link copied to clipboard
open var bigInt: KBigInt?

Only valid when type == Token.BIGINT.

Link copied to clipboard
open val column: Int

The column where this node is defined in source, one-based. -1 means it was never initialized. May be overridden by subclasses.

Link copied to clipboard

Only valid when type == Token.NUMBER.

Link copied to clipboard

The innermost enclosing function, or null. The search begins with the parent.

Link copied to clipboard

The innermost enclosing Scope, or null. The search begins with the parent. This is not the same as the defining scope for a Name.

Link copied to clipboard
Link copied to clipboard

Comments on the same line as the statement, for example the trailing comment in if (x == 2) // note.

Link copied to clipboard

The JsDoc comment string attached to this node, or null.

Link copied to clipboard

The JsDoc Comment attached to this node, or null.

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
var length: Int

Number of characters spanned by this node in the source text.

Link copied to clipboard
open override val lineno: Int

The line number recorded for this node. If none was recorded, searches the parent chain and returns -1 when nothing is found.

Link copied to clipboard

The node parent, or null. Setting it adjusts this node's start position to be relative to the new parent.

Link copied to clipboard

Relative position in the parent.

Link copied to clipboard
open var scope: Scope?

Only valid when the node has String context.

Link copied to clipboard

Only valid when the node has String context.

Link copied to clipboard
open var type: Int

The node type, e.g. Token.NAME.

Functions

Link copied to clipboard
fun addChild(kid: AstNode)

Adds a child to the end of the block. Sets the child's parent to this node, rewrites the child position to be relative, and grows this node to include the child.

Link copied to clipboard
fun addChildAfter(newChild: Node, node: Node)

Add newChild after node.

Link copied to clipboard
fun addChildBefore(newChild: Node, node: Node)

Add newChild before node.

Link copied to clipboard
fun addChildrenToBack(children: Node)
Link copied to clipboard
fun addChildrenToFront(children: Node)
Link copied to clipboard
fun addChildToBack(child: Node)
Link copied to clipboard
fun addChildToFront(child: Node)
Link copied to clipboard
open operator override fun compareTo(other: AstNode): Int

Sorts by start position, then by length, then arbitrarily by hash code. Lets Comment and error nodes be mixed into a sorted collection of other AST nodes.

Link copied to clipboard
open fun debugPrint(): String

A debugging representation of the parse tree starting at this node. Each line reads abs-pos name position length [identifier].

Link copied to clipboard
fun depth(): Int

The depth of this node. The root is depth 0, its children depth 1, and so on.

Link copied to clipboard
fun getChildBefore(child: Node): Node?
Link copied to clipboard
fun getExistingIntProp(propType: Int): Int
Link copied to clipboard
fun getIntProp(propType: Int, defaultValue: Int): Int
Link copied to clipboard
fun getProp(propType: Int): Any?
Link copied to clipboard
Link copied to clipboard

Checks that every return usage in a function body is consistent with strict mode. See the END_* flags for what the analysis reports.

Link copied to clipboard
open override fun hasSideEffects(): Boolean
Link copied to clipboard
open operator override fun iterator(): MutableIterator<Node>
Link copied to clipboard
fun labelId(): Int
fun labelId(labelId: Int)
Link copied to clipboard
fun makeIndent(indent: Int): String

Constructs an indentation string of indent steps.

Link copied to clipboard
fun putIntProp(propType: Int, prop: Int)
Link copied to clipboard
fun putProp(propType: Int, prop: Any?)
Link copied to clipboard
fun removeChild(child: Node)
Link copied to clipboard
Link copied to clipboard
fun removeProp(propType: Int)
Link copied to clipboard
fun replaceChild(child: Node, newChild: Node)
Link copied to clipboard
fun replaceChildAfter(prevChild: Node, newChild: Node)
Link copied to clipboard

Recursively unlabel every TARGET or YIELD node in the tree. Used only for inlining finally blocks where jsr instructions used to be.

Link copied to clipboard
fun setBounds(position: Int, end: Int)

Sets the start and end positions; the length becomes end - position.

Link copied to clipboard
fun setLineColumnNumber(lineno: Int, column: Int)
Link copied to clipboard
fun setRelative(parentPosition: Int)

Makes this node's position relative to a parent. The current position is assumed to be absolute and is decremented by parentPosition.

Link copied to clipboard

A short, descriptive name for the node, such as "ArrayComprehension".

Link copied to clipboard
abstract fun toSource(depth: Int = 0): String

Emits source code for this node. The caller recurses into children, incrementing depth as appropriate.

Link copied to clipboard
open override fun toString(): String
Link copied to clipboard
Link copied to clipboard
abstract fun visit(visitor: NodeVisitor)

Visits this node and its children in an arbitrary order.