-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllpg.php
executable file
·117 lines (88 loc) · 2.98 KB
/
llpg.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
#!/usr/bin/php
<?php
/*
* llpg command line utility.
* MIT licence, © Ege Madra, October 2013 .
*/
function help()
{
global $argv;
echo
"LL Parser Generator for PHP.
Usage: {$argv[0]} ...options... grammar_file
If both -p and -j options are missing it will be considered a dry run and there
will be no output file. Useful for validating your grammar.
Example: {$argv[0]} -p './myparser.php' -j 'myparserdata.json' -t 1 'my.grammar'
This would attempt produce both versions of the parser for the the grammar
defined in file 'my.grammar'. If succeeds, the driverless one will be formatted
and it will have the trim level of 1.
Options:
-p parser_file: parser_file is the path to a php file that llpg will output.
This is a single php file that is able to parse the grammar defined in
grammar_file. Has no default value.
-j json_file: json_file is the path to a json file that llpg will output to
be used by the driving version of the generator. Has no default value.
-t trim_level: trim_level is an integer that can be 0, 1 or 2. Please see
the documentation for the meaning of it. If left unspecified it is defaulted to
0.
-f: Must be used with -p option. If set, generated parser code in the
parser_filewill be formatted. Generator is much faster when it doesn't have to
format the php code.
-h: Prints this text.
-v: Prints version info.
";
exit();
}
function exitError($msg,$dontExit=false)
{
$fh = fopen('php://stderr','a');
fwrite($fh,"$msg\n\n");
fclose($fh);
if (!$dontExit) exit();
}
$pathToLLPG=pathinfo(__FILE__,PATHINFO_DIRNAME).DIRECTORY_SEPARATOR.'LLPG.class.php';
if (!is_readable($pathToLLPG)) exitError("Error: '$pathToLLPG' does not exist or not readable.");
require_once $pathToLLPG;
$opts=getopt("p:j:t:fvh");
if (isset($opts['h'])) help();
if (isset($opts['v'])) exit("LLPG -v ".LLPG::getVersion().".\n\n");
//http://php.net/manual/en/function.getopt.php
$parameters = array(
'f' => 'format',
'p:' => 'parser_file:',
'j:' => 'json_file:',
't:' => 'trim_level:',
);
$options = getopt(implode('', array_keys($parameters)), $parameters);
$pruneargv = array();
foreach ($options as $option => $value) {
foreach ($argv as $key => $chunk) {
$regex = '/^'. (isset($option[1]) ? '--' : '-') . $option . '/';
if ($chunk == $value && $argv[$key-1][0] == '-' || preg_match($regex, $chunk)) {
array_push($pruneargv, $key);
}
}
}
while ($key = array_pop($pruneargv)) unset($argv[$key]);
if (sizeof($argv)!==2) exitError("Error: Illegal or wrong number of options. Please type \"{$argv[0]} -h\" for help.");
foreach ($opts as $key=>$val)
$$key=$val;
$format=isset($options['f']);
$grammarFile=end($argv);
$trimLevel=isset($t) ? $t : 0;
$parser=new LLPG();
try{
$parser->parseFile($grammarFile, $trimLevel);
if (isset($p))
$parser->outputParser($p, $format);
if (isset($j))
$parser->outputJSON($j);
}
catch (LLPGException $e)
{
exitError($e->getMessage());
}
$warnings=$parser->getWarnings();
if (sizeof($warnings))
foreach ($warnings as $w)
exitError($w,true);