-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload.php
129 lines (96 loc) · 2.21 KB
/
load.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
<?php
require 'api/ext.php';
require 'api/timing.php';
//Render any errors/debug in plain text.
header('content-type: text/plain');
startTiming();
$GRAMMAR_FILE = 'paprika-grammar/index.grammar';
//Final output, $result:
$result = '';
//Check the grammar manifest exists
if (!file_exists($GRAMMAR_FILE)){
echo "Can't find the grammar manifest; should be '$GRAMMAR_FILE'.\n";
die;
}
//Open the grammar manifest
$man = fopen($GRAMMAR_FILE , 'r');
$category = '';
$catGrammar = array();
$grammar = array();
while (!feof($man)){
$thisFile = trim(fgets($man));
if ($thisFile == ''){
continue;
}
if (!file_exists($thisFile)){
echo "Can't find a linked grammar file, '$thisFile'.\n";
die;
}
loadGrammar($thisFile);
}
// === Save grammar to PHP array file ===
$grammarString = '<?php $grammar = ' . var_export($grammar, true) . ' ?>';
$outFile = fopen('grammar.php','w');
fwrite($outFile, $grammarString);
fclose($outFile);
// === Debug info for the admin ===
echo "\n\n=====FINAL GRAMMAR=====\n\n";
echo $grammarString;
echo "\n\n==========END==========\n";
$totalTime = stopTiming();
echo " $totalTime secs\n=======================\n";
function loadGrammar($thisFile){
global $category;
global $catGrammar;
global $grammar;
echo "Loading $thisFile...\n";
$h = fopen($thisFile , 'r');
//Read through grammar
while (!feof($h)){
$line = fgets($h);
if (ignoreLine($line)){
continue;
}
if (lineIsCategory($line)){
// We've come across a new category
// So write the last category to grammar (if we've done one)
if ($category > ''){
commitCategory();
}
$category = substr($line, 1);
$catGrammar = [];
}
else{
//It's a member
$member = trim($line);
array_push($catGrammar, $member);
}
}
//Commit the final category
commitCategory();
//Debug?
if (from_request('debug')){
var_dump($grammar);
}
$category='';
$catGrammar=[];
echo "...Done\n\n";
}
function commitCategory(){
global $category;
global $catGrammar;
global $grammar;
$category = trim($category);
echo " - Set $category\n";
$grammar[$category] = $catGrammar;
}
function ignoreLine($l){
return (
$l[0] == '#'
|| trim($l) <= ""
);
}
function lineIsCategory($l){
return $l[0] == '*';
}
?>