1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
const GRAMMAR_FILE = './php5.y'; |
4
|
|
|
|
5
|
|
|
const LIB = '(?(DEFINE) |
6
|
|
|
(?<singleQuotedString>\'[^\\\\\']*+(?:\\\\.[^\\\\\']*+)*+\') |
7
|
|
|
(?<doubleQuotedString>"[^\\\\"]*+(?:\\\\.[^\\\\"]*+)*+") |
8
|
|
|
(?<string>(?&singleQuotedString)|(?&doubleQuotedString)) |
9
|
|
|
(?<comment>/\*[^*]*+(?:\*(?!/)[^*]*+)*+\*/) |
10
|
|
|
(?<code>\{[^\'"/{}]*+(?:(?:(?&string)|(?&comment)|(?&code)|/)[^\'"/{}]*+)*+}) |
11
|
|
|
)'; |
12
|
|
|
|
13
|
|
|
const RULE_BLOCK = '(?<name>[a-z_]++):(?<rules>[^\'"/{};]*+(?:(?:(?&string)|(?&comment)|(?&code)|/|})[^\'"/{};]*+)*+);'; |
14
|
|
|
|
15
|
|
|
$usedTerminals = array_flip(array( |
16
|
|
|
'T_VARIABLE', 'T_STRING', 'T_INLINE_HTML', 'T_ENCAPSED_AND_WHITESPACE', |
17
|
|
|
'T_LNUMBER', 'T_DNUMBER', 'T_CONSTANT_ENCAPSED_STRING', 'T_STRING_VARNAME', 'T_NUM_STRING' |
18
|
|
|
)); |
19
|
|
|
$unusedNonterminals = array_flip(array( |
20
|
|
|
'case_separator', 'optional_comma' |
21
|
|
|
)); |
22
|
|
|
|
23
|
|
|
function regex($regex) { |
24
|
|
|
return '~' . LIB . '(?:' . str_replace('~', '\~', $regex) . ')~'; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
function magicSplit($regex, $string) { |
28
|
|
|
$pieces = preg_split(regex('(?:(?&string)|(?&comment)|(?&code))(*SKIP)(*FAIL)|' . $regex), $string); |
29
|
|
|
|
30
|
|
|
foreach ($pieces as &$piece) { |
31
|
|
|
$piece = trim($piece); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return array_filter($pieces); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
echo '<pre>'; |
38
|
|
|
|
39
|
|
|
//////////////////// |
40
|
|
|
//////////////////// |
41
|
|
|
//////////////////// |
42
|
|
|
|
43
|
|
|
list($defs, $ruleBlocks) = magicSplit('%%', file_get_contents(GRAMMAR_FILE)); |
44
|
|
|
|
45
|
|
|
if ('' !== trim(preg_replace(regex(RULE_BLOCK), '', $ruleBlocks))) { |
46
|
|
|
die('Not all rule blocks were properly recognized!'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
preg_match_all(regex(RULE_BLOCK), $ruleBlocks, $ruleBlocksMatches, PREG_SET_ORDER); |
50
|
|
|
foreach ($ruleBlocksMatches as $match) { |
|
|
|
|
51
|
|
|
$ruleBlockName = $match['name']; |
52
|
|
|
$rules = magicSplit('\|', $match['rules']); |
53
|
|
|
|
54
|
|
|
foreach ($rules as &$rule) { |
55
|
|
|
$parts = magicSplit('\s+', $rule); |
56
|
|
|
$usedParts = array(); |
57
|
|
|
|
58
|
|
|
foreach ($parts as $part) { |
59
|
|
|
if ('{' === $part[0]) { |
60
|
|
|
preg_match_all('~\$([0-9]+)~', $part, $backReferencesMatches, PREG_SET_ORDER); |
61
|
|
|
foreach ($backReferencesMatches as $match) { |
|
|
|
|
62
|
|
|
$usedParts[$match[1]] = true; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$i = 1; |
68
|
|
|
foreach ($parts as &$part) { |
69
|
|
|
if ('/' === $part[0]) { |
70
|
|
|
continue; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (isset($usedParts[$i])) { |
74
|
|
|
if ('\'' === $part[0] || '{' === $part[0] |
75
|
|
|
|| (ctype_upper($part[0]) && !isset($usedTerminals[$part])) |
76
|
|
|
|| (ctype_lower($part[0]) && isset($unusedNonterminals[$part])) |
77
|
|
|
) { |
78
|
|
|
$part = '<span style="background-color: red; color: white;">' . $part . '</span>'; |
79
|
|
|
} else { |
80
|
|
|
$part = '<strong><em>' . $part . '</em></strong>'; |
81
|
|
|
} |
82
|
|
|
} elseif ((ctype_upper($part[0]) && isset($usedTerminals[$part])) |
83
|
|
|
|| (ctype_lower($part[0]) && !isset($unusedNonterminals[$part])) |
84
|
|
|
|
85
|
|
|
) { |
86
|
|
|
$part = '<span style="background-color: blue; color: white;">' . $part . '</span>'; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
++$i; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$rule = implode(' ', $parts); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
echo $ruleBlockName, ':', "\n", ' ', implode("\n" . ' | ', $rules), "\n", ';', "\n\n"; |
96
|
|
|
} |
97
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.