Class Node

java.lang.Object
io.karatelabs.parser.Node

public class Node extends Object
A node in a parsed tree.

Parsed trees are shared across threads, so treat a Node as read-only once parsing has returned. A Suite parses karate-config.js once and every scenario — running in parallel — evaluates that one AST, and the interpreter only ever reads: no node.x = assignment exists on any evaluation path. The mutators below (add(io.karatelabs.parser.Node), addFirst(io.karatelabs.parser.Node), removeFirst()) belong to tree construction, and the safety of the sharing rests on nobody calling them afterwards. That is a convention, not an invariant this class enforces.

One field already bends it: getText() caches into a non-volatile cachedText on first ask, which under sharing is a data race. It is benign — every racing thread computes the same value, and a String's final fields make it safe to publish that way — but it is the reason "a Node is read-only once parsed" is a description of intent rather than a fact. A lazily assigned field of any other type would not be benign. An array or a mutable object published by a plain write can be seen non-null with its contents not yet visible; if you add one here, make it volatile and read it once into a local.

  • Field Details

    • type

      public final NodeType type
    • token

      public final Token token
    • async

      public boolean async
      The async modifier, for the node types that can carry one — FN_EXPR (a declaration / expression / object-literal method / class method) and FN_ARROW_EXPR. It is a field rather than a child token because async is a contextual keyword that the lexer emits as a plain IDENT: an extra leading child would shift every positional lookup the interpreter does on those two node types, and a synthesized method has no token of its own to carry the marker. Written by the parser during tree construction only — same read-only-once-parsed convention as the children, and a boolean written before publication is safe to share (see the class javadoc).
    • slot

      public short slot
    • meta

      public volatile Object meta
  • Constructor Details

    • Node

      public Node(NodeType type)
    • Node

      public Node(Token token)
  • Method Details

    • getParent

      public Node getParent()
    • isToken

      public boolean isToken()
    • isEof

      public boolean isEof()
    • getFirstToken

      public Token getFirstToken()
    • getLastToken

      public Token getLastToken()
    • toStringError

      public String toStringError(String message)
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • findFirstChild

      public Node findFirstChild(NodeType type)
    • findAll

      public List<Node> findAll(NodeType type)
    • findFirstChild

      public Node findFirstChild(TokenType token)
    • findParent

      public Node findParent(NodeType type)
    • findImmediateChildren

      public List<Node> findImmediateChildren(NodeType type)
    • findChildren

      public List<Node> findChildren(TokenType token)
    • getText

      public String getText()
    • getTextIncludingWhitespace

      public String getTextIncludingWhitespace()
    • removeFirst

      public Node removeFirst()
    • addFirst

      public void addFirst(Node child)
    • add

      public void add(Node child)
    • getFirst

      public Node getFirst()
    • getLast

      public Node getLast()
    • get

      public Node get(int index)
    • size

      public int size()
    • isEmpty

      public boolean isEmpty()