> For the complete documentation index, see [llms.txt](https://php-parser.glayzzle.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://php-parser.glayzzle.com/api/ast.js.md).

# 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 :

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

## 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.

```javascript
  {
    "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
    ]
  }
```

## Nodes hierarchy

* [Location](/api/ast.js.md#location)
* [Position](/api/ast.js.md#position)
* [Node](/api/ast.js.md#node)
  * [Identifier](/api/ast.js.md#identifier)
  * [TraitUse](/api/ast.js.md#traituse)
  * [TraitAlias](/api/ast.js.md#traitalias)
  * [TraitPrecedence](/api/ast.js.md#traitprecedence)
  * [Entry](/api/ast.js.md#entry)
  * [Case](/api/ast.js.md#case)
  * [Label](/api/ast.js.md#label)
  * [Comment](/api/ast.js.md#comment)
    * [CommentLine](/api/ast.js.md#commentline)
    * [CommentBlock](/api/ast.js.md#commentblock)
  * [Error](/api/ast.js.md#error)
  * [Expression](/api/ast.js.md#expression)
    * [Array](/api/ast.js.md#array)
    * [Variable](/api/ast.js.md#variable)
    * [Variadic](/api/ast.js.md#variadic)
    * [ConstRef](/api/ast.js.md#constref)
    * [Yield](/api/ast.js.md#yield)
    * [YieldFrom](/api/ast.js.md#yieldfrom)
    * [Lookup](/api/ast.js.md#lookup)
      * [PropertyLookup](/api/ast.js.md#propertylookup)
      * [StaticLookup](/api/ast.js.md#staticlookup)
      * [OffsetLookup](/api/ast.js.md#offsetlookup)
    * [Operation](/api/ast.js.md#operation)
      * [Pre](/api/ast.js.md#pre)
      * [Post](/api/ast.js.md#post)
      * [Bin](/api/ast.js.md#bin)
      * [Parenthesis](/api/ast.js.md#parenthesis)
      * [Unary](/api/ast.js.md#unary)
      * [Cast](/api/ast.js.md#cast)
    * [Literal](/api/ast.js.md#literal)
      * [Boolean](/api/ast.js.md#boolean)
      * [String](/api/ast.js.md#string)
      * [Number](/api/ast.js.md#number)
      * [Inline](/api/ast.js.md#inline)
      * [Magic](/api/ast.js.md#magic)
      * [Nowdoc](/api/ast.js.md#nowdoc)
      * [Encapsed](/api/ast.js.md#encapsed)
  * [Statement](/api/ast.js.md#statement)
    * [Eval](/api/ast.js.md#eval)
    * [Exit](/api/ast.js.md#exit)
    * [Halt](/api/ast.js.md#halt)
    * [Clone](/api/ast.js.md#clone)
    * [Declare](/api/ast.js.md#declare)
    * [Global](/api/ast.js.md#global)
    * [Static](/api/ast.js.md#static)
    * [Include](/api/ast.js.md#include)
    * [Assign](/api/ast.js.md#assign)
    * [RetIf](/api/ast.js.md#retif)
    * [If](/api/ast.js.md#if)
    * [Do](/api/ast.js.md#do)
    * [While](/api/ast.js.md#while)
    * [For](/api/ast.js.md#for)
    * [Foreach](/api/ast.js.md#foreach)
    * [Switch](/api/ast.js.md#switch)
    * [Goto](/api/ast.js.md#goto)
    * [Silent](/api/ast.js.md#silent)
    * [Try](/api/ast.js.md#try)
    * [Catch](/api/ast.js.md#catch)
    * [Throw](/api/ast.js.md#throw)
    * [Call](/api/ast.js.md#call)
    * [Closure](/api/ast.js.md#closure)
    * [New](/api/ast.js.md#new)
    * [UseGroup](/api/ast.js.md#usegroup)
    * [UseItem](/api/ast.js.md#useitem)
    * [Block](/api/ast.js.md#block)
      * [Program](/api/ast.js.md#program)
      * [Namespace](/api/ast.js.md#namespace)
    * [Sys](/api/ast.js.md#sys)
      * [Echo](/api/ast.js.md#echo)
      * [List](/api/ast.js.md#list)
      * [Print](/api/ast.js.md#print)
      * [Isset](/api/ast.js.md#isset)
      * [Unset](/api/ast.js.md#unset)
      * [Empty](/api/ast.js.md#empty)
    * [Declaration](/api/ast.js.md#declaration)
      * [Class](/api/ast.js.md#class)
      * [Interface](/api/ast.js.md#interface)
      * [Trait](/api/ast.js.md#trait)
      * [Constant](/api/ast.js.md#constant)
        * [ClassConstant](/api/ast.js.md#classconstant)
      * [Function](/api/ast.js.md#function)
        * [Method](/api/ast.js.md#method)
      * [Parameter](/api/ast.js.md#parameter)
      * [Property](/api/ast.js.md#property)

## AST

**Kind**: global class\
**Properties**

| Name          | Type      | Description                                              |
| ------------- | --------- | -------------------------------------------------------- |
| withPositions | `Boolean` | Should locate any node (by default false)                |
| withSource    | `Boolean` | Should extract the node original code (by default false) |

* [AST](/api/ast.js.md#AST)
  * [.swapLocations()](https://php-parser.glayzzle.com/api/pages/-L_3rojFGMhK2rRrTLxA#AST+swapLocations)
  * [.resolvePrecedence()](https://php-parser.glayzzle.com/api/pages/-L_3rojFGMhK2rRrTLxA#AST+resolvePrecedence)
  * [.prepare(kind, parser)](https://php-parser.glayzzle.com/api/pages/-L_3rojFGMhK2rRrTLxA#AST+prepare) ⇒ `function`

### asT.swapLocations()

Change parent node informations after swapping childs

**Kind**: instance method of [`AST`](/api/ast.js.md#AST)<br>

### asT.resolvePrecedence()

Check and fix precence, by default using right

**Kind**: instance method of [`AST`](/api/ast.js.md#AST)<br>

### asT.prepare(kind, parser) ⇒ `function`

Prepares an AST node

**Kind**: instance method of [`AST`](/api/ast.js.md#AST)

| Param  | Type               | Description                                                                   |
| ------ | ------------------ | ----------------------------------------------------------------------------- |
| kind   | `String` \| `null` | Defines the node type (if null, the kind must be passed at the function call) |
| parser | `Parser`           | The parser instance (use for extracting locations)                            |
