-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.php
More file actions
164 lines (142 loc) · 3.04 KB
/
token.php
File metadata and controls
164 lines (142 loc) · 3.04 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
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
<?php
//$source = file_get_contents('example.php');
$source = file_get_contents('Main.php');
$tokens = token_get_all($source);
$functions = array();
$hasFunc = false;
$stack = array();
$name = '|';
$current_stack_count = 0;
foreach ($tokens as $token) {
if (is_string($token)) {
// simple 1-character token
//echo "token: $token \n";
if($token == '{')
{
$stack[] = $name;
$c = count($stack);
//echo "{ $c: $name \n";
}
else if($token == '}')
{
$c = count($stack);
$nm = array_pop($stack);
//echo "} $c: '$nm' current_stack_count: $current_stack_count \n";
if(($c - 1) == $current_stack_count)
{
if($nm == $name)
{
$name = '|';
}
else
{
//echo "ERROR: $name \n";
exit;
}
}
}
} else {
// token array
list($id, $text) = $token;
switch ($id) {
case T_COMMENT:
case T_DOC_COMMENT:
// no action on comments
break;
default:
// anything else -> output "as is"
//echo "id: $id \n";
if(empty(trim($text)) ) continue;
//echo "$text \n";
if($text == '{')
{
$stack[] = $name;
$c = count($stack);
//echo "{ $c: $name \n";
}
else if($text == '}')
{
$c = count($stack);
$nm = array_pop($stack);
//echo "} $c: '$nm' current_stack_count: $current_stack_count \n";
if(($c - 1) == $current_stack_count)
{
if($nm == $name)
{
$name = '|';
}
else
{
//echo "ERROR: $name \n";
exit;
}
}
}
else if($text == 'function' && $name == '|')
{
$hasFunc = true;
$current_stack_count = count($stack);
}
else
{
if($hasFunc == true )
{
$name = $text;
$hasFunc = false;
// $functions[$name] = array();
//echo "----------------------------FUNCTION :$name ".count($stack)." \n";
}
else if($name != '')
{
//echo "----------------------------TOKEN :$name : '$text' ".count($stack)." \n";
$functions[$name][$text] = array();
}
}
break;
}
}
}
$funcs = array_keys($functions);
$result = array();
foreach($functions as $k=>$tokens)
{
$result[$k] = array();
foreach($tokens as $n=>$t)
{
if(in_array($n, $funcs))
{
//echo "delete $k $t\n";
// unset($functions[$k][$n]);
$result[$k][$n] = '';
}
}
}
//var_dump($result);
foreach($result as $k=>$tokens)
{
foreach($tokens as $n=>$t)
{
//echo "$k :$n \n";
$result[$k][$n] = $result[$n];
}
}
foreach($result as $k=>$tokens)
{
foreach($tokens as $n=>$t)
{
//echo "$k :$n \n";
foreach($t as $tk=>$tv)
{
$result[$k][$n][$tk] = $result[$tk];
}
}
}
echo json_encode($result); exit;
//var_dump($functions);
{
echo " \n$k \n";
foreach($tokens as $n=>$t)
{
echo " $t \n";
}
}