-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpolygen.php
More file actions
executable file
·75 lines (62 loc) · 1.89 KB
/
polygen.php
File metadata and controls
executable file
·75 lines (62 loc) · 1.89 KB
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
#!/usr/bin/env php
<?php
// ⣀⡀ ⢀⡀ ⡇ ⡀⢀ ⢀⡀ ⢀⡀ ⣀⡀
// ⡧⠜ ⠣⠜ ⠣ ⣑⡺ ⣑⡺ ⠣⠭ ⠇⠸
use GuzzleHttp\Stream\Stream;
use Polygen\Language\Document;
use Polygen\Language\Exceptions\StaticCheckException;
use Polygen\Polygen;
if (file_exists($autoloadFile = __DIR__ . '/vendor/autoload.php')
|| file_exists($autoloadFile = __DIR__ . '/../autoload.php')
|| file_exists($autoloadFile = __DIR__ . '/../../autoload.php')
) {
include_once($autoloadFile);
} else {
throw new \Exception("Could not locate autoload.php. __DIR__ is " . __DIR__);
}
require_once(__DIR__ . '/includes/cli.php');
// Execution starts here
$options = getopt(
implode([
OPT_LABEL, OPT_REQUIRED_VALUE,
OPT_DESTINATION, OPT_REQUIRED_VALUE,
OPT_START_SYMBOL, OPT_REQUIRED_VALUE,
OPT_ITERATE, OPT_REQUIRED_VALUE,
OPT_LABEL, OPT_REQUIRED_VALUE,
OPT_HELP,
]),
[
OPT_SEED . OPT_REQUIRED_VALUE,
OPT_INFO . OPT_REQUIRED_VALUE,
OPT_HELP_LONG,
]
);
$path = $argv[$argc - 1];
$polygen = new Polygen();
if ($argc === 1 || isset($options[OPT_HELP]) || isset($options[OPT_HELP_LONG])) {
echo $polygen->generate($polygen->getDocument(Stream::factory(HELP_GRAMMAR), 'S'));
exit(0);
}
$options += [
OPT_START_SYMBOL => Document::START,
OPT_SEED => null,
OPT_ITERATE => 1,
OPT_LABEL => null,
OPT_INFO => null,
];
$context = build_context($options);
validate_file_path($path);
try {
$document = $polygen->getDocument(load_grammar($path), $options[OPT_START_SYMBOL]);
} catch (StaticCheckException $e) {
echo "Static Check error(s) found:\n";
$i = 0;
foreach ($e->getErrors()->getErrors() as $error) {
$i++;
echo "{$i}) {$error->getMessage()}\n";
}
}
for ($i = 0; $i < $options[OPT_ITERATE]; $i++) {
echo $polygen->generate($document, $context);
echo PHP_EOL;
}