| 1 |
AST generation and tokenization |
| 2 |
========== |
| 3 |
|
| 4 |
AST generation |
| 5 |
------------- |
| 6 |
To generate AST (abstract syntax tree) for your JavaScript code just write: |
| 7 |
|
| 8 |
```php |
| 9 |
$source = "var a = 1"; //JavaScript code |
| 10 |
$ast = Peast\Peast::latest($source, $options)->parse(); |
| 11 |
``` |
| 12 |
|
| 13 |
The previous code generates this structure: |
| 14 |
``` |
| 15 |
Peast\Syntax\Node\Program |
| 16 |
getSourceType() => "script" |
| 17 |
getBody() => array( |
| 18 |
Peast\Syntax\Node\VariableDeclaration |
| 19 |
getKind() => "var" |
| 20 |
getDeclarations() => array( |
| 21 |
Peast\Syntax\Node\VariableDeclarator |
| 22 |
getId() => Peast\Syntax\Node\Identifier |
| 23 |
getName() => "a" |
| 24 |
getInit() => Peast\Syntax\Node\NumericLiteral |
| 25 |
getFormat() => "decimal" |
| 26 |
getValue() => 1 |
| 27 |
) |
| 28 |
) |
| 29 |
``` |
| 30 |
|
| 31 |
Tokenization |
| 32 |
------------- |
| 33 |
To tokenize your JavaScript code just write: |
| 34 |
|
| 35 |
```php |
| 36 |
$source = "var a = 1"; //JavaScript code |
| 37 |
$tokens = Peast\Peast::latest($source, $options)->tokenize(); |
| 38 |
``` |
| 39 |
|
| 40 |
This function produces an array of tokens from your code: |
| 41 |
``` |
| 42 |
array( |
| 43 |
Peast\Syntax\Token |
| 44 |
getType() => "Keyword" |
| 45 |
getValue() => "var" |
| 46 |
Peast\Syntax\Token |
| 47 |
getType() => "Identifier" |
| 48 |
getValue() => "a" |
| 49 |
Peast\Syntax\Token |
| 50 |
getType() => "Punctuator" |
| 51 |
getValue() => "=" |
| 52 |
Peast\Syntax\Token |
| 53 |
getType() => "Numeric" |
| 54 |
getValue() => "1" |
| 55 |
) |
| 56 |
``` |
| 57 |
|
| 58 |
EcmaScript version |
| 59 |
------------- |
| 60 |
Peast can parse different versions of EcmaScript, you can choose the version by using the relative method on the main class. |
| 61 |
Available methods are: |
| 62 |
* ```Peast::ES2015(source, options)``` or ```Peast::ES6(source, options)```: parse using EcmaScript 2015 (ES6) syntax |
| 63 |
* ```Peast::ES2016(source, options)``` or ```Peast::ES7(source, options)```: parse using EcmaScript 2016 (ES7) syntax |
| 64 |
* ```Peast::ES2017(source, options)``` or ```Peast::ES8(source, options)```: parse using EcmaScript 2017 (ES8) syntax |
| 65 |
* ```Peast::ES2018(source, options)``` or ```Peast::ES9(source, options)```: parse using EcmaScript 2018 (ES9) syntax |
| 66 |
* ```Peast::ES2019(source, options)``` or ```Peast::ES10(source, options)```: parse using EcmaScript 2019 (ES10) syntax |
| 67 |
* ```Peast::ES2020(source, options)``` or ```Peast::ES11(source, options)```: parse using EcmaScript 2020 (ES11) syntax |
| 68 |
* ```Peast::ES2021(source, options)``` or ```Peast::ES12(source, options)```: parse using EcmaScript 2021 (ES12) syntax |
| 69 |
* ```Peast::ES2022(source, options)``` or ```Peast::ES13(source, options)```: parse using EcmaScript 2022 (ES13) syntax |
| 70 |
* ```Peast::ES2023(source, options)``` or ```Peast::ES14(source, options)```: parse using EcmaScript 2023 (ES14) syntax |
| 71 |
* ```Peast::ES2024(source, options)``` or ```Peast::ES15(source, options)```: parse using EcmaScript 2024 (ES15) syntax |
| 72 |
* ```Peast::ES2025(source, options)``` or ```Peast::ES16(source, options)```: parse using EcmaScript 2025 (ES16) syntax |
| 73 |
* ```Peast::latest(source, options)```: parse using the latest EcmaScript syntax version implemented |
| 74 |
|
| 75 |
Options |
| 76 |
------------- |
| 77 |
|
| 78 |
In the examples above you may have noticed the `$options` parameter. This parameter is an associative array that specifies parsing settings for the parser. Available options are: |
| 79 |
* "sourceType": this can be one of the source type constants defined in the Peast class: |
| 80 |
* `Peast\Peast::SOURCE_TYPE_SCRIPT`: this is the default source type and indicates that the code is a script, this means that `import` and `export` keywords are not parsed |
| 81 |
* `Peast\Peast::SOURCE_TYPE_MODULE`: this indicates that the code is a module and it activates the parsing of `import` and `export` keywords |
| 82 |
* "comments" (from version 1.5): enables comments parsing and attaches the comments to the nodes in the tree. You can get comments attached to nodes using `getLeadingComments` and `getTrailingComments` methods. |
| 83 |
* "jsx" (from version 1.8): enables parsing of JSX syntax. |
| 84 |
* "sourceEncoding": to specify the encoding of the code to parse, if not specified the parser will assume UTF-8. |
| 85 |
* "strictEncoding": if false the parser will handle invalid UTF8 characters in the source code by replacing them with the character defined in the "mbstring.substitute_character" ini setting, otherwise it will throw an exception. (available from version 1.9.4) |
| 86 |
|
| 87 |
Differences from ESTree |
| 88 |
------------- |
| 89 |
|
| 90 |
There is only one big difference from ESTree: parenthesized expressions. This type of expressions have been introduced to let the user know if when an expression is wrapped in round brackets. For example `(a + b)` is a parenthesized expression and generates a ParenthesizedExpression node. |
| 91 |
|
| 92 |
From version 1.3, literals have their own classes: `StringLiteral`, `NumericLiteral`, `BooleanLiteral` and `NullLiteral`. |
| 93 |
|
| 94 |
From version 1.8, when parsing JSX, 2 new token types are emitted: `JSXIdentifier`, that represents a valid JSX identifier, and `JSXText`, that represents text inside JSX elements and fragments. |
| 95 |
|
| 96 |
From version 1.13.7, the new `rawName` property has been added to `Identifiers` nodes. This property reports the raw name of the identifier with unconverted unicode escape sequences. |