-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathTestGenerator.php
223 lines (195 loc) · 6.67 KB
/
TestGenerator.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
namespace SqlParser\Tools;
require_once '../vendor/autoload.php';
use SqlParser\Lexer;
use SqlParser\Parser;
/**
* Used for test generation.
*
* @category Tests
* @package SqlParser
* @subpackage Tools
* @author Dan Ungureanu <udan1107@gmail.com>
* @license http://opensource.org/licenses/GPL-2.0 GNU Public License
*/
class TestGenerator
{
/**
* Generates a test's data.
*
* @param string $query The query to be analyzed.
* @param string $type Test's type (may be `lexer` or `parser`).
*
* @return array
*/
public static function generate($query, $type = 'parser')
{
/**
* Lexer used for tokenizing the query.
*
* @var Lexer $lexer
*/
$lexer = new Lexer($query);
/**
* Parsed used for analyzing the query.
* A new instance of parser is generated only if the test requires.
*
* @var Parser $parser
*/
$parser = ($type === 'parser') ? new Parser($lexer->list) : null;
/**
* Lexer's errors.
*
* @var array $lexerErrors
*/
$lexerErrors = array();
/**
* Parser's errors.
*
* @var array $parserErrors
*/
$parserErrors = array();
// Both the lexer and the parser construct exception for errors.
// Usually, exceptions contain a full stack trace and other details that
// are not required.
// The code below extracts only the relevant information.
// Extracting lexer's errors.
if (!empty($lexer->errors)) {
foreach ($lexer->errors as $err) {
$lexerErrors[] = array($err->getMessage(), $err->ch, $err->pos, $err->getCode());
}
$lexer->errors = array();
}
// Extracting parser's errors.
if (!empty($parser->errors)) {
foreach ($parser->errors as $err) {
$parserErrors[] = array($err->getMessage(), $err->token, $err->getCode());
}
$parser->errors = array();
}
return array(
'query' => $query,
'lexer' => $lexer,
'parser' => $parser,
'errors' => array(
'lexer' => $lexerErrors,
'parser' => $parserErrors
),
);
}
/**
* Builds a test.
*
* Reads the input file, generates the data and writes it back.
*
* @param string $type The type of this test.
* @param string $input The input file.
* @param string $output The output file.
* @param string $debug The debug file.
*
* @return void
*/
public static function build($type, $input, $output, $debug = null)
{
// Support query types: `lexer` / `parser`.
if (!in_array($type, array('lexer', 'parser'))) {
throw new \Exception('Unknown test type (expected `lexer` or `parser`).');
}
/**
* The query that is used to generate the test.
*
* @var string $query
*/
$query = file_get_contents($input);
// There is no point in generating a test without a query.
if (empty($query)) {
throw new \Exception('No input query specified.');
}
$test = static::generate($query, $type);
// Writing test's data.
file_put_contents($output, serialize($test));
// Dumping test's data in human readable format too (if required).
if (!empty($debug)) {
file_put_contents($debug, print_r($test, true));
}
}
/**
* Generates recursively all tests preserving the directory structure.
*
* @param string $input The input directory.
* @param string $output The output directory.
*
* @return void
*/
public static function buildAll($input, $output, $debug = null)
{
$files = scandir($input);
foreach ($files as $file) {
// Skipping current and parent directories.
if (($file === '.') || ($file === '..')) {
continue;
}
// Appending the filename to directories.
$inputFile = $input . '/' . $file;
$outputFile = $output . '/' . $file;
$debugFile = ($debug !== null) ? $debug . '/' . $file : null;
if (is_dir($inputFile)) {
// Creating required directories to maintain the structure.
// Ignoring errors if the folder structure exists already.
if (!is_dir($outputFile)) {
mkdir($outputFile);
}
if (($debug !== null) && (!is_dir($debugFile))) {
mkdir($debugFile);
}
// Generating tests recursively.
static::buildAll($inputFile, $outputFile, $debugFile);
} elseif (substr($inputFile, -3) === '.in') {
// Generating file names by replacing `.in` with `.out` and
// `.debug`.
$outputFile = substr($outputFile, 0, -3) . '.out';
if ($debug !== null) {
$debugFile = substr($debugFile, 0, -3) . '.debug';
}
// Building the test.
if (!file_exists($outputFile)) {
print("Building test for {$inputFile}...\n");
static::build(
strpos($inputFile, 'lex') !== false ? 'lexer' : 'parser',
$inputFile,
$outputFile,
$debugFile
);
} else {
print("Test for {$inputFile} already built!\n");
}
}
}
}
}
// Test generator.
//
// Example of usage:
//
// php TestGenerator.php ../tests/data ../tests/data
//
// Input data must be in the `../tests/data` folder.
// The output will be generated in the same `../tests/data` folder.
//
if (count($argv) >= 3) {
// Extracting directories' name from command line and trimming unnecessary
// slashes at the end.
$input = rtrim($argv[1], '/');
$output = rtrim($argv[2], '/');
$debug = empty($argv[3]) ? null : rtrim($argv[3], '/');
// Checking if all directories are valid.
if (!is_dir($input)) {
throw new \Exception('The input directory does not exist.');
} elseif (!is_dir($output)) {
throw new \Exception('The output directory does not exist.');
} elseif (($debug !== null) && (!is_dir($debug))) {
throw new \Exception('The debug directory does not exist.');
}
// Finally, building the tests.
TestGenerator::buildAll($input, $output, $debug);
}