AST | php-parser (original) (raw)

  1. API

AST

You can see the AST as a DOM document, the source code of the program beeing the graphical text output, and the DOM beeing a representation of it.

The main node is the program node, and it's structured like this :

  {
    "kind": "program",
    "children": [
      // array of nodes
    ]
  }

Every node has a common structure enabling you to scan them and act accordingly.

NOTE : This structure depends also on what options you enable.

  {
    "kind": "node name",
    "loc": {
      ""
    },
    // the location node
    "loc": {
      "source": "original source code of the node",
      "start": {
        "line": 1, // 1 based
        "column": 0, // 0 based
        "offset": 0 // offset from the source code
      },
      "end": {
        // same structure as start
      }
    },
    "leadingComments": [
      // array of comments nodes
    ]
  }

Kind: global classProperties

Should locate any node (by default false)

Should extract the node original code (by default false)

Change parent node informations after swapping childs

Check and fix precence, by default using right

asT.prepare(kind, parser) ⇒ function

Prepares an AST node

Defines the node type (if null, the kind must be passed at the function call)

The parser instance (use for extracting locations)