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