This repository has been archived by the owner on Jul 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
make.php
115 lines (88 loc) · 2.17 KB
/
make.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
<?php
define('ROOT', realpath(dirname(__FILE__)).'/');
// Disable output.
ini_set('implicit_flush', false);
function init()
{
$arg = array_slice($_SERVER['argv'], 1);
$arg = $arg ? $arg[0] : '';
if (in_array($arg, array('?', 'help')))
{
exit("make.php\n");
}
make();
}
function make()
{
require 'vendor/ParserGenerator/ParserGenerator.php';
$source = 'grammar';
$target = 'src/CoffeeScript/Parser.php';
// Lemon takes arguments on the command line.
$_SERVER['argv'] = $argv = array('-s', ROOT.$source.'.y');
echo "Attempting to build \"{$target}\" from \"{$source}.y\".\n";
echo "This could take a few minutes...\n";
// The -q flag doesn't seem to work but we can catch the output in a
// buffer (only want to display the errors).
ob_start();
$lemon = new PHP_ParserGenerator;
$lemon->main();
$reply = explode("\n", ob_get_contents());
ob_end_clean();
$errors = array();
$conflicts = 0;
foreach ($reply as $i => $line)
{
// Errors are prefixed with the grammar file path.
if (strpos($line, $argv[1]) === 0)
{
$errors[] = str_replace($argv[1], basename($argv[1]), $line);
}
if ($i === count($reply) - 2)
{
if (preg_match('/^(\d+).+/', $line, $m))
{
$conflicts = intval($m[1]);
}
}
}
if ($errors)
{
exit(implode("\n", $errors));
}
// Build was a success!
if ( ! file_exists(ROOT.$target) || @unlink(ROOT.$target))
{
$content = file_get_contents(ROOT.$source.'.php');
// Add namespace declaration.
$content = strtr($content, array(
'<?php' =>
"<?php\n"
. "namespace CoffeeScript;\n"
. "use \ArrayAccess as ArrayAccess;\n"
. "Init::init();\n"
));
// Write.
file_put_contents(ROOT.$target, $content);
echo "Success!\n";
// Clean up.
unlink(ROOT.$source.'.php');
if ($conflicts)
{
echo "{$conflicts} parsing conflicts occurred (see {$source}.out).\n";
}
else
{
unlink(ROOT.$source.'.out');
}
exit(0);
}
else
{
// Bad permissions.
echo "Failed!\n";
echo "Couldn't remove {$target}. Check your permissions.\n";
exit(1);
}
}
init();
?>