| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file is part of the Peast package |
| 4 |
* |
| 5 |
* (c) Marco Marchiò <[email protected]> |
| 6 |
* |
| 7 |
* For the full copyright and license information refer to the LICENSE file |
| 8 |
* distributed with this source code |
| 9 |
*/ |
| 10 |
namespace Peast; |
| 11 |
|
| 12 |
/** |
| 13 |
* Main class of Peast library. |
| 14 |
* Every function of this class takes two arguments: |
| 15 |
* - The source code to parse |
| 16 |
* - The options array that is an associative array of parser settings. |
| 17 |
* Available options are: |
| 18 |
* - "sourceType": one of the source type constants declared in this class. |
| 19 |
* This option tells the parser to parse the source in script or module |
| 20 |
* mode. If this option is not provided the parser will work in script |
| 21 |
* mode. |
| 22 |
* - "sourceEncoding": the encoding of the source. If not specified the |
| 23 |
* parser will assume UTF-8. |
| 24 |
* - "strictEncoding": if false the parser will handle invalid UTF8 |
| 25 |
* characters in the source code by replacing them with the character |
| 26 |
* defined in the "mbstring.substitute_character" ini setting, otherwise |
| 27 |
* it will throw an exception. |
| 28 |
* - "comments": if true it enables comments parsing. |
| 29 |
* - "jsx": if true it enables parsing of JSX syntax. |
| 30 |
* |
| 31 |
* @method static Syntax\Parser ES2015(string $source, array $options = array()) |
| 32 |
* Returns a parser instance with ES2015 features for the given source. See Peast |
| 33 |
* class documentation to understand the function arguments. |
| 34 |
* |
| 35 |
* @method static Syntax\Parser ES6(string $source, array $options = array()) |
| 36 |
* Returns a parser instance with ES2015 features for the given source. See Peast |
| 37 |
* class documentation to understand function arguments. |
| 38 |
* |
| 39 |
* @method static Syntax\Parser ES2016(string $source, array $options = array()) |
| 40 |
* Returns a parser instance with ES2016 features for the given source. See Peast |
| 41 |
* class documentation to understand function arguments. |
| 42 |
* |
| 43 |
* @method static Syntax\Parser ES7(string $source, array $options = array()) |
| 44 |
* Returns a parser instance with ES2016 features for the given source. See Peast |
| 45 |
* class documentation to understand function arguments. |
| 46 |
* |
| 47 |
* @method static Syntax\Parser ES2017(string $source, array $options = array()) |
| 48 |
* Returns a parser instance with ES2017 features for the given source. See Peast |
| 49 |
* class documentation to understand function arguments. |
| 50 |
* |
| 51 |
* @method static Syntax\Parser ES8(string $source, array $options = array()) |
| 52 |
* Returns a parser instance with ES2017 features for the given source. See Peast |
| 53 |
* class documentation to understand function arguments. |
| 54 |
* |
| 55 |
* @method static Syntax\Parser ES2018(string $source, array $options = array()) |
| 56 |
* Returns a parser instance with ES2018 features for the given source. See Peast |
| 57 |
* class documentation to understand function arguments. |
| 58 |
* |
| 59 |
* @method static Syntax\Parser ES9(string $source, array $options = array()) |
| 60 |
* Returns a parser instance with ES2018 features for the given source. See Peast |
| 61 |
* class documentation to understand function arguments. |
| 62 |
* |
| 63 |
* @method static Syntax\Parser ES2019(string $source, array $options = array()) |
| 64 |
* Returns a parser instance with ES2019 features for the given source. See Peast |
| 65 |
* class documentation to understand function arguments. |
| 66 |
* |
| 67 |
* @method static Syntax\Parser ES10(string $source, array $options = array()) |
| 68 |
* Returns a parser instance with ES2019 features for the given source. See Peast |
| 69 |
* class documentation to understand function arguments. |
| 70 |
* |
| 71 |
* @method static Syntax\Parser ES2020(string $source, array $options = array()) |
| 72 |
* Returns a parser instance with ES2020 features for the given source. See Peast |
| 73 |
* class documentation to understand function arguments. |
| 74 |
* |
| 75 |
* @method static Syntax\Parser ES11(string $source, array $options = array()) |
| 76 |
* Returns a parser instance with ES2020 features for the given source. See Peast |
| 77 |
* class documentation to understand function arguments. |
| 78 |
* |
| 79 |
* @method static Syntax\Parser ES2021(string $source, array $options = array()) |
| 80 |
* Returns a parser instance with ES2021 features for the given source. See Peast |
| 81 |
* class documentation to understand function arguments. |
| 82 |
* |
| 83 |
* @method static Syntax\Parser ES12(string $source, array $options = array()) |
| 84 |
* Returns a parser instance with ES2021 features for the given source. See Peast |
| 85 |
* class documentation to understand function arguments. |
| 86 |
* |
| 87 |
* @method static Syntax\Parser ES2022(string $source, array $options = array()) |
| 88 |
* Returns a parser instance with ES2022 features for the given source. See Peast |
| 89 |
* class documentation to understand function arguments. |
| 90 |
* |
| 91 |
* @method static Syntax\Parser ES13(string $source, array $options = array()) |
| 92 |
* Returns a parser instance with ES2022 features for the given source. See Peast |
| 93 |
* class documentation to understand function arguments. |
| 94 |
* |
| 95 |
* @method static Syntax\Parser ES2023(string $source, array $options = array()) |
| 96 |
* Returns a parser instance with ES2023 features for the given source. See Peast |
| 97 |
* class documentation to understand function arguments. |
| 98 |
* |
| 99 |
* @method static Syntax\Parser ES14(string $source, array $options = array()) |
| 100 |
* Returns a parser instance with ES2023 features for the given source. See Peast |
| 101 |
* class documentation to understand function arguments. |
| 102 |
* |
| 103 |
* @method static Syntax\Parser ES2024(string $source, array $options = array()) |
| 104 |
* Returns a parser instance with ES2024 features for the given source. See Peast |
| 105 |
* class documentation to understand function arguments. |
| 106 |
* |
| 107 |
* @method static Syntax\Parser ES15(string $source, array $options = array()) |
| 108 |
* Returns a parser instance with ES2024 features for the given source. See Peast |
| 109 |
* class documentation to understand function arguments. |
| 110 |
* |
| 111 |
* @method static Syntax\Parser ES2025(string $source, array $options = array()) |
| 112 |
* Returns a parser instance with ES2025 features for the given source. See Peast |
| 113 |
* class documentation to understand function arguments. |
| 114 |
* |
| 115 |
* @method static Syntax\Parser ES16(string $source, array $options = array()) |
| 116 |
* Returns a parser instance with ES2025 features for the given source. See Peast |
| 117 |
* class documentation to understand function arguments. |
| 118 |
* |
| 119 |
* @method static Syntax\Parser latest(string $source, array $options = array()) |
| 120 |
* Returns an instance of the latest parser version for the given source. See |
| 121 |
* Peast class documentation to understand function arguments. |
| 122 |
* |
| 123 |
* @author Marco Marchiò <[email protected]> |
| 124 |
*/ |
| 125 |
class Peast |
| 126 |
{ |
| 127 |
//Source type constants |
| 128 |
/** |
| 129 |
* This source type indicates that the source is a script and import |
| 130 |
* and export keywords are not parsed. |
| 131 |
*/ |
| 132 |
const SOURCE_TYPE_SCRIPT = "script"; |
| 133 |
|
| 134 |
/** |
| 135 |
* This source type indicates that the source is a module, this enables |
| 136 |
* the parsing of import and export keywords. |
| 137 |
*/ |
| 138 |
const SOURCE_TYPE_MODULE = "module"; |
| 139 |
|
| 140 |
/** |
| 141 |
* Valid versions and aliases |
| 142 |
* |
| 143 |
* @var array |
| 144 |
*/ |
| 145 |
static protected $versions = array( |
| 146 |
"ES6" => "ES2015", |
| 147 |
"ES7" => "ES2016", |
| 148 |
"ES8" => "ES2017", |
| 149 |
"ES9" => "ES2018", |
| 150 |
"ES10" => "ES2019", |
| 151 |
"ES11" => "ES2020", |
| 152 |
"ES12" => "ES2021", |
| 153 |
"ES13" => "ES2022", |
| 154 |
"ES14" => "ES2023", |
| 155 |
"ES15" => "ES2024", |
| 156 |
"ES16" => "ES2025" |
| 157 |
); |
| 158 |
|
| 159 |
/** |
| 160 |
* Magic method that exposes all the functions to access parser with |
| 161 |
* specific features |
| 162 |
* |
| 163 |
* @param string $version Parser version |
| 164 |
* @param array $args Parser arguments |
| 165 |
* |
| 166 |
* @return Syntax\Parser |
| 167 |
* |
| 168 |
* @throws \Exception |
| 169 |
*/ |
| 170 |
public static function __callStatic($version, $args) |
| 171 |
{ |
| 172 |
$source = $args[0]; |
| 173 |
$options = isset($args[1]) ? $args[1] : array(); |
| 174 |
|
| 175 |
if (!in_array($version, self::$versions)) { |
| 176 |
if ($version === "latest") { |
| 177 |
$version = end(self::$versions); |
| 178 |
} elseif (isset(self::$versions[$version])) { |
| 179 |
$version = self::$versions[$version]; |
| 180 |
} else { |
| 181 |
throw new \Exception("Invalid version $version"); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
$featuresClass = "\\Peast\\Syntax\\$version\\Features"; |
| 186 |
return new Syntax\Parser( |
| 187 |
$source, new $featuresClass, $options |
| 188 |
); |
| 189 |
} |
| 190 |
} |