@@ -13,123 +13,123 @@ |
||
13 | 13 | abstract class AbstractPreprocessor |
14 | 14 | { |
15 | 15 | |
16 | - private $defines = array(); |
|
17 | - private $stack = array(); |
|
18 | - |
|
19 | - public function __construct() |
|
20 | - { |
|
21 | - $this->resetDefines(); |
|
22 | - } |
|
23 | - |
|
24 | - public function resetDefines() |
|
25 | - { |
|
26 | - $this->defines = array(); |
|
27 | - } |
|
28 | - |
|
29 | - public function addDefines(array $defines) |
|
30 | - { |
|
31 | - $this->defines = array_merge($this->defines, $defines); |
|
32 | - } |
|
33 | - |
|
34 | - public function define($name, $value = 1) |
|
35 | - { |
|
36 | - $this->defines[$name] = $value; |
|
37 | - } |
|
38 | - |
|
39 | - public function undefine($name) |
|
40 | - { |
|
41 | - unset($this->defines[$name]); |
|
42 | - } |
|
43 | - |
|
44 | - protected function getState() |
|
45 | - { |
|
46 | - return empty($this->stack) || end($this->stack); |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * Get the first word from a string and remove it from the string. |
|
51 | - * |
|
52 | - * @param string $data |
|
53 | - * @return boolean|string |
|
54 | - */ |
|
55 | - private static function wordShift(&$data) |
|
56 | - { |
|
57 | - if (preg_match('~^(\S+)\s*(.*)$~', $data, $matches) === 1) { |
|
58 | - $data = $matches[2]; |
|
59 | - return $matches[1]; |
|
60 | - } |
|
61 | - return false; |
|
62 | - } |
|
63 | - |
|
64 | - protected function handle($command, $expression) |
|
65 | - { |
|
66 | - switch (strtolower($command)) { |
|
67 | - case 'if': |
|
68 | - $name = self::wordShift($expression); |
|
69 | - $state = $this->getState(); |
|
70 | - if (empty($expression)) { |
|
71 | - $this->stack[] = $state && !empty($this->defines[$name]); |
|
72 | - } else { |
|
73 | - $this->stack[] = $state && isset($this->defines[$name]) && $this->defines[$name] == $expression; |
|
74 | - } |
|
75 | - break; |
|
76 | - |
|
77 | - case 'ifdef': |
|
78 | - $this->stack[] = $this->getState() && isset($this->defines[$expression]); |
|
79 | - break; |
|
80 | - |
|
81 | - case 'ifndef': |
|
82 | - $this->stack[] = $this->getState() && !isset($this->defines[$expression]); |
|
83 | - break; |
|
84 | - |
|
85 | - case 'else': |
|
86 | - $state = $this->getState(); |
|
87 | - array_pop($this->stack); |
|
88 | - $this->stack[] = !$state; |
|
89 | - break; |
|
90 | - |
|
91 | - case 'elif': |
|
92 | - $name = self::wordShift($expression); |
|
93 | - $state = $this->getState(); |
|
94 | - array_pop($this->stack); |
|
95 | - if (empty($expression)) { |
|
96 | - $this->stack[] = !$state && !empty($this->defines[$name]); |
|
97 | - } else { |
|
98 | - $this->stack[] = !$state && isset($this->defines[$name]) && $this->defines[$name] == $expression; |
|
99 | - } |
|
100 | - break; |
|
101 | - |
|
102 | - case 'define': |
|
103 | - $name = self::wordShift($expression); |
|
104 | - $this->defines[$name] = $expression; |
|
105 | - break; |
|
106 | - |
|
107 | - case 'undef': |
|
108 | - unset($this->defines[$expression]); |
|
109 | - break; |
|
110 | - |
|
111 | - case 'endif': |
|
112 | - array_pop($this->stack); |
|
113 | - break; |
|
114 | - |
|
115 | - default: |
|
116 | - return false; |
|
117 | - } |
|
118 | - |
|
119 | - return true; |
|
120 | - } |
|
121 | - |
|
122 | - public function preprocess($content) |
|
123 | - { |
|
124 | - $this->stack = array(); |
|
125 | - |
|
126 | - return $this->parseContent($content); |
|
127 | - } |
|
128 | - |
|
129 | - public function preprocessFile($filename) |
|
130 | - { |
|
131 | - return $this->preprocess(file_get_contents($filename)); |
|
132 | - } |
|
133 | - |
|
134 | - abstract protected function parseContent($content); |
|
16 | + private $defines = array(); |
|
17 | + private $stack = array(); |
|
18 | + |
|
19 | + public function __construct() |
|
20 | + { |
|
21 | + $this->resetDefines(); |
|
22 | + } |
|
23 | + |
|
24 | + public function resetDefines() |
|
25 | + { |
|
26 | + $this->defines = array(); |
|
27 | + } |
|
28 | + |
|
29 | + public function addDefines(array $defines) |
|
30 | + { |
|
31 | + $this->defines = array_merge($this->defines, $defines); |
|
32 | + } |
|
33 | + |
|
34 | + public function define($name, $value = 1) |
|
35 | + { |
|
36 | + $this->defines[$name] = $value; |
|
37 | + } |
|
38 | + |
|
39 | + public function undefine($name) |
|
40 | + { |
|
41 | + unset($this->defines[$name]); |
|
42 | + } |
|
43 | + |
|
44 | + protected function getState() |
|
45 | + { |
|
46 | + return empty($this->stack) || end($this->stack); |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * Get the first word from a string and remove it from the string. |
|
51 | + * |
|
52 | + * @param string $data |
|
53 | + * @return boolean|string |
|
54 | + */ |
|
55 | + private static function wordShift(&$data) |
|
56 | + { |
|
57 | + if (preg_match('~^(\S+)\s*(.*)$~', $data, $matches) === 1) { |
|
58 | + $data = $matches[2]; |
|
59 | + return $matches[1]; |
|
60 | + } |
|
61 | + return false; |
|
62 | + } |
|
63 | + |
|
64 | + protected function handle($command, $expression) |
|
65 | + { |
|
66 | + switch (strtolower($command)) { |
|
67 | + case 'if': |
|
68 | + $name = self::wordShift($expression); |
|
69 | + $state = $this->getState(); |
|
70 | + if (empty($expression)) { |
|
71 | + $this->stack[] = $state && !empty($this->defines[$name]); |
|
72 | + } else { |
|
73 | + $this->stack[] = $state && isset($this->defines[$name]) && $this->defines[$name] == $expression; |
|
74 | + } |
|
75 | + break; |
|
76 | + |
|
77 | + case 'ifdef': |
|
78 | + $this->stack[] = $this->getState() && isset($this->defines[$expression]); |
|
79 | + break; |
|
80 | + |
|
81 | + case 'ifndef': |
|
82 | + $this->stack[] = $this->getState() && !isset($this->defines[$expression]); |
|
83 | + break; |
|
84 | + |
|
85 | + case 'else': |
|
86 | + $state = $this->getState(); |
|
87 | + array_pop($this->stack); |
|
88 | + $this->stack[] = !$state; |
|
89 | + break; |
|
90 | + |
|
91 | + case 'elif': |
|
92 | + $name = self::wordShift($expression); |
|
93 | + $state = $this->getState(); |
|
94 | + array_pop($this->stack); |
|
95 | + if (empty($expression)) { |
|
96 | + $this->stack[] = !$state && !empty($this->defines[$name]); |
|
97 | + } else { |
|
98 | + $this->stack[] = !$state && isset($this->defines[$name]) && $this->defines[$name] == $expression; |
|
99 | + } |
|
100 | + break; |
|
101 | + |
|
102 | + case 'define': |
|
103 | + $name = self::wordShift($expression); |
|
104 | + $this->defines[$name] = $expression; |
|
105 | + break; |
|
106 | + |
|
107 | + case 'undef': |
|
108 | + unset($this->defines[$expression]); |
|
109 | + break; |
|
110 | + |
|
111 | + case 'endif': |
|
112 | + array_pop($this->stack); |
|
113 | + break; |
|
114 | + |
|
115 | + default: |
|
116 | + return false; |
|
117 | + } |
|
118 | + |
|
119 | + return true; |
|
120 | + } |
|
121 | + |
|
122 | + public function preprocess($content) |
|
123 | + { |
|
124 | + $this->stack = array(); |
|
125 | + |
|
126 | + return $this->parseContent($content); |
|
127 | + } |
|
128 | + |
|
129 | + public function preprocessFile($filename) |
|
130 | + { |
|
131 | + return $this->preprocess(file_get_contents($filename)); |
|
132 | + } |
|
133 | + |
|
134 | + abstract protected function parseContent($content); |
|
135 | 135 | } |
@@ -18,72 +18,72 @@ |
||
18 | 18 | class Preprocessor extends AbstractPreprocessor |
19 | 19 | { |
20 | 20 | |
21 | - private $prefix = 'rest'; |
|
21 | + private $prefix = 'rest'; |
|
22 | 22 | |
23 | - public function __construct($prefix = null) |
|
24 | - { |
|
25 | - parent::__construct(); |
|
26 | - if (!empty($prefix)) { |
|
27 | - $this->prefix = $prefix; |
|
28 | - } |
|
29 | - } |
|
23 | + public function __construct($prefix = null) |
|
24 | + { |
|
25 | + parent::__construct(); |
|
26 | + if (!empty($prefix)) { |
|
27 | + $this->prefix = $prefix; |
|
28 | + } |
|
29 | + } |
|
30 | 30 | |
31 | - public function getPrefix() |
|
32 | - { |
|
33 | - return $this->prefix; |
|
34 | - } |
|
31 | + public function getPrefix() |
|
32 | + { |
|
33 | + return $this->prefix; |
|
34 | + } |
|
35 | 35 | |
36 | - public function setPrefix($prefix) |
|
37 | - { |
|
38 | - $this->prefix = $prefix; |
|
39 | - } |
|
36 | + public function setPrefix($prefix) |
|
37 | + { |
|
38 | + $this->prefix = $prefix; |
|
39 | + } |
|
40 | 40 | |
41 | - protected function parseContent($content) |
|
42 | - { |
|
43 | - $pattern = '/@' . preg_quote($this->getPrefix()) . '\\\\([a-z]+)\\s*(.*)$/'; |
|
41 | + protected function parseContent($content) |
|
42 | + { |
|
43 | + $pattern = '/@' . preg_quote($this->getPrefix()) . '\\\\([a-z]+)\\s*(.*)$/'; |
|
44 | 44 | |
45 | - $output_file = ''; |
|
45 | + $output_file = ''; |
|
46 | 46 | |
47 | - foreach (token_get_all($content) as $token) { |
|
48 | - $output = ''; |
|
47 | + foreach (token_get_all($content) as $token) { |
|
48 | + $output = ''; |
|
49 | 49 | |
50 | - if (is_array($token)) { |
|
51 | - switch ($token[0]) { |
|
52 | - case T_DOC_COMMENT: |
|
53 | - case T_COMMENT: |
|
54 | - foreach (preg_split('/(\\R)/m', $token[1], -1, PREG_SPLIT_DELIM_CAPTURE) as $index => $line) { |
|
55 | - if ($index % 2) { |
|
56 | - $output .= $line; |
|
57 | - } else { |
|
58 | - $match = array(); |
|
59 | - if (preg_match($pattern, $line, $match) === 1) { |
|
60 | - if (!$this->handle($match[1], $match[2]) && $this->getState()) { |
|
61 | - $output .= $line; |
|
62 | - } else { |
|
63 | - $output .= str_replace('@' . $this->getPrefix() . '\\', '@!' . $this->getPrefix() . '\\', $line); |
|
64 | - } |
|
65 | - } else { |
|
66 | - $output .= $line; |
|
67 | - } |
|
68 | - } |
|
69 | - } |
|
70 | - break; |
|
50 | + if (is_array($token)) { |
|
51 | + switch ($token[0]) { |
|
52 | + case T_DOC_COMMENT: |
|
53 | + case T_COMMENT: |
|
54 | + foreach (preg_split('/(\\R)/m', $token[1], -1, PREG_SPLIT_DELIM_CAPTURE) as $index => $line) { |
|
55 | + if ($index % 2) { |
|
56 | + $output .= $line; |
|
57 | + } else { |
|
58 | + $match = array(); |
|
59 | + if (preg_match($pattern, $line, $match) === 1) { |
|
60 | + if (!$this->handle($match[1], $match[2]) && $this->getState()) { |
|
61 | + $output .= $line; |
|
62 | + } else { |
|
63 | + $output .= str_replace('@' . $this->getPrefix() . '\\', '@!' . $this->getPrefix() . '\\', $line); |
|
64 | + } |
|
65 | + } else { |
|
66 | + $output .= $line; |
|
67 | + } |
|
68 | + } |
|
69 | + } |
|
70 | + break; |
|
71 | 71 | |
72 | - default: |
|
73 | - $output .= $token[1]; |
|
74 | - } |
|
75 | - } else { |
|
76 | - $output .= $token; |
|
77 | - } |
|
72 | + default: |
|
73 | + $output .= $token[1]; |
|
74 | + } |
|
75 | + } else { |
|
76 | + $output .= $token; |
|
77 | + } |
|
78 | 78 | |
79 | - if ($this->getState()) { |
|
80 | - $output_file .= $output; |
|
81 | - } else { |
|
82 | - $output_file .= '/* ' . $output . ' */'; |
|
83 | - } |
|
84 | - } |
|
79 | + if ($this->getState()) { |
|
80 | + $output_file .= $output; |
|
81 | + } else { |
|
82 | + $output_file .= '/* ' . $output . ' */'; |
|
83 | + } |
|
84 | + } |
|
85 | 85 | |
86 | - return $output_file; |
|
87 | - } |
|
86 | + return $output_file; |
|
87 | + } |
|
88 | 88 | |
89 | 89 | } |
@@ -15,120 +15,120 @@ |
||
15 | 15 | class ParserClass extends AbstractEntity |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * @var string |
|
20 | - */ |
|
21 | - public $name = null; |
|
22 | - |
|
23 | - /** |
|
24 | - * @var ParserFunction[] |
|
25 | - */ |
|
26 | - public $Methods = array(); |
|
27 | - |
|
28 | - /** |
|
29 | - * @var string |
|
30 | - */ |
|
31 | - public $extends = null; |
|
32 | - |
|
33 | - /** |
|
34 | - * @var string[] |
|
35 | - */ |
|
36 | - public $implements = array(); |
|
37 | - private $lastStatements = null; |
|
38 | - |
|
39 | - public function __construct(Parser $Parser, &$tokens, $Statements) |
|
40 | - { |
|
41 | - if ($Statements) { |
|
42 | - $this->Statements = array_merge($this->Statements, $Statements); |
|
43 | - } |
|
44 | - |
|
45 | - $depth = 0; |
|
46 | - |
|
47 | - $mode = T_CLASS; |
|
48 | - |
|
49 | - $token = current($tokens); |
|
50 | - while ($token) { |
|
51 | - switch ($token[0]) { |
|
52 | - case T_STRING: |
|
53 | - switch ($mode) { |
|
54 | - case T_CLASS: |
|
55 | - $this->name = $token[1]; |
|
56 | - $mode = null; |
|
57 | - break; |
|
58 | - |
|
59 | - case T_EXTENDS: |
|
60 | - $Parser->queueClass($token[1]); |
|
61 | - $this->extends = $token[1]; |
|
62 | - $mode = null; |
|
63 | - break; |
|
64 | - |
|
65 | - case T_IMPLEMENTS: |
|
66 | - $Parser->queueClass($token[1]); |
|
67 | - $this->implements[] = $token[1]; |
|
68 | - break; |
|
69 | - } |
|
70 | - break; |
|
71 | - |
|
72 | - case '{': |
|
73 | - case T_CURLY_OPEN: |
|
74 | - case T_DOLLAR_OPEN_CURLY_BRACES: |
|
75 | - case T_STRING_VARNAME: |
|
76 | - $mode = null; |
|
77 | - ++$depth; |
|
78 | - break; |
|
79 | - |
|
80 | - case '}': |
|
81 | - --$depth; |
|
82 | - if ($depth == 0) { |
|
83 | - if ($this->lastStatements) { |
|
84 | - $this->Statements = array_merge($this->Statements, $this->lastStatements); |
|
85 | - $this->lastStatements = null; |
|
86 | - } |
|
87 | - return; |
|
88 | - } |
|
89 | - break; |
|
90 | - |
|
91 | - case T_FUNCTION: |
|
92 | - $Method = new ParserFunction($Parser, $tokens, $this->lastStatements); |
|
93 | - $this->Methods[strtolower($Method->name)] = $Method; |
|
94 | - $this->lastStatements = null; |
|
95 | - break; |
|
96 | - |
|
97 | - case T_EXTENDS: |
|
98 | - $mode = T_EXTENDS; |
|
99 | - break; |
|
100 | - |
|
101 | - case T_IMPLEMENTS: |
|
102 | - $mode = T_IMPLEMENTS; |
|
103 | - break; |
|
104 | - |
|
105 | - case T_COMMENT: |
|
106 | - if ($this->lastStatements) { |
|
107 | - $this->Statements = array_merge($this->Statements, $this->lastStatements); |
|
108 | - $this->lastStatements = null; |
|
109 | - } |
|
110 | - $Statements = $Parser->tokenToStatements($token); |
|
111 | - $Parser->queueClassesFromComments($Statements); |
|
112 | - $this->Statements = array_merge($this->Statements, $Statements); |
|
113 | - break; |
|
114 | - |
|
115 | - case T_DOC_COMMENT: |
|
116 | - if ($this->lastStatements) { |
|
117 | - $this->Statements = array_merge($this->Statements, $this->lastStatements); |
|
118 | - } |
|
119 | - $Statements = $Parser->tokenToStatements($token); |
|
120 | - $Parser->queueClassesFromComments($Statements); |
|
121 | - $this->lastStatements = $Statements; |
|
122 | - break; |
|
123 | - } |
|
124 | - |
|
125 | - $token = next($tokens); |
|
126 | - } |
|
127 | - |
|
128 | - if ($this->lastStatements) { |
|
129 | - $this->Statements = array_merge($this->Statements, $this->lastStatements); |
|
130 | - $this->lastStatements = null; |
|
131 | - } |
|
132 | - } |
|
18 | + /** |
|
19 | + * @var string |
|
20 | + */ |
|
21 | + public $name = null; |
|
22 | + |
|
23 | + /** |
|
24 | + * @var ParserFunction[] |
|
25 | + */ |
|
26 | + public $Methods = array(); |
|
27 | + |
|
28 | + /** |
|
29 | + * @var string |
|
30 | + */ |
|
31 | + public $extends = null; |
|
32 | + |
|
33 | + /** |
|
34 | + * @var string[] |
|
35 | + */ |
|
36 | + public $implements = array(); |
|
37 | + private $lastStatements = null; |
|
38 | + |
|
39 | + public function __construct(Parser $Parser, &$tokens, $Statements) |
|
40 | + { |
|
41 | + if ($Statements) { |
|
42 | + $this->Statements = array_merge($this->Statements, $Statements); |
|
43 | + } |
|
44 | + |
|
45 | + $depth = 0; |
|
46 | + |
|
47 | + $mode = T_CLASS; |
|
48 | + |
|
49 | + $token = current($tokens); |
|
50 | + while ($token) { |
|
51 | + switch ($token[0]) { |
|
52 | + case T_STRING: |
|
53 | + switch ($mode) { |
|
54 | + case T_CLASS: |
|
55 | + $this->name = $token[1]; |
|
56 | + $mode = null; |
|
57 | + break; |
|
58 | + |
|
59 | + case T_EXTENDS: |
|
60 | + $Parser->queueClass($token[1]); |
|
61 | + $this->extends = $token[1]; |
|
62 | + $mode = null; |
|
63 | + break; |
|
64 | + |
|
65 | + case T_IMPLEMENTS: |
|
66 | + $Parser->queueClass($token[1]); |
|
67 | + $this->implements[] = $token[1]; |
|
68 | + break; |
|
69 | + } |
|
70 | + break; |
|
71 | + |
|
72 | + case '{': |
|
73 | + case T_CURLY_OPEN: |
|
74 | + case T_DOLLAR_OPEN_CURLY_BRACES: |
|
75 | + case T_STRING_VARNAME: |
|
76 | + $mode = null; |
|
77 | + ++$depth; |
|
78 | + break; |
|
79 | + |
|
80 | + case '}': |
|
81 | + --$depth; |
|
82 | + if ($depth == 0) { |
|
83 | + if ($this->lastStatements) { |
|
84 | + $this->Statements = array_merge($this->Statements, $this->lastStatements); |
|
85 | + $this->lastStatements = null; |
|
86 | + } |
|
87 | + return; |
|
88 | + } |
|
89 | + break; |
|
90 | + |
|
91 | + case T_FUNCTION: |
|
92 | + $Method = new ParserFunction($Parser, $tokens, $this->lastStatements); |
|
93 | + $this->Methods[strtolower($Method->name)] = $Method; |
|
94 | + $this->lastStatements = null; |
|
95 | + break; |
|
96 | + |
|
97 | + case T_EXTENDS: |
|
98 | + $mode = T_EXTENDS; |
|
99 | + break; |
|
100 | + |
|
101 | + case T_IMPLEMENTS: |
|
102 | + $mode = T_IMPLEMENTS; |
|
103 | + break; |
|
104 | + |
|
105 | + case T_COMMENT: |
|
106 | + if ($this->lastStatements) { |
|
107 | + $this->Statements = array_merge($this->Statements, $this->lastStatements); |
|
108 | + $this->lastStatements = null; |
|
109 | + } |
|
110 | + $Statements = $Parser->tokenToStatements($token); |
|
111 | + $Parser->queueClassesFromComments($Statements); |
|
112 | + $this->Statements = array_merge($this->Statements, $Statements); |
|
113 | + break; |
|
114 | + |
|
115 | + case T_DOC_COMMENT: |
|
116 | + if ($this->lastStatements) { |
|
117 | + $this->Statements = array_merge($this->Statements, $this->lastStatements); |
|
118 | + } |
|
119 | + $Statements = $Parser->tokenToStatements($token); |
|
120 | + $Parser->queueClassesFromComments($Statements); |
|
121 | + $this->lastStatements = $Statements; |
|
122 | + break; |
|
123 | + } |
|
124 | + |
|
125 | + $token = next($tokens); |
|
126 | + } |
|
127 | + |
|
128 | + if ($this->lastStatements) { |
|
129 | + $this->Statements = array_merge($this->Statements, $this->lastStatements); |
|
130 | + $this->lastStatements = null; |
|
131 | + } |
|
132 | + } |
|
133 | 133 | |
134 | 134 | } |
@@ -15,30 +15,30 @@ |
||
15 | 15 | class AbstractEntity |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * @var Statement[] |
|
20 | - */ |
|
21 | - public $Statements = array(); |
|
22 | - |
|
23 | - /** |
|
24 | - * Returns true if a statement with the specified command exists. |
|
25 | - * @param string $command |
|
26 | - * @return boolean |
|
27 | - */ |
|
28 | - public function hasCommand($command) |
|
29 | - { |
|
30 | - foreach ($this->Statements as $Statement) { |
|
31 | - if ($Statement->getCommand() === $command) { |
|
32 | - return true; |
|
33 | - } |
|
34 | - } |
|
35 | - |
|
36 | - return false; |
|
37 | - } |
|
38 | - |
|
39 | - public function getStatements() |
|
40 | - { |
|
41 | - return $this->Statements; |
|
42 | - } |
|
18 | + /** |
|
19 | + * @var Statement[] |
|
20 | + */ |
|
21 | + public $Statements = array(); |
|
22 | + |
|
23 | + /** |
|
24 | + * Returns true if a statement with the specified command exists. |
|
25 | + * @param string $command |
|
26 | + * @return boolean |
|
27 | + */ |
|
28 | + public function hasCommand($command) |
|
29 | + { |
|
30 | + foreach ($this->Statements as $Statement) { |
|
31 | + if ($Statement->getCommand() === $command) { |
|
32 | + return true; |
|
33 | + } |
|
34 | + } |
|
35 | + |
|
36 | + return false; |
|
37 | + } |
|
38 | + |
|
39 | + public function getStatements() |
|
40 | + { |
|
41 | + return $this->Statements; |
|
42 | + } |
|
43 | 43 | |
44 | 44 | } |
@@ -16,77 +16,77 @@ |
||
16 | 16 | class ParserFunction extends AbstractEntity |
17 | 17 | { |
18 | 18 | |
19 | - public $name = null; |
|
20 | - private $lastStatements = null; |
|
19 | + public $name = null; |
|
20 | + private $lastStatements = null; |
|
21 | 21 | |
22 | - public function __construct(Parser $Parser, &$tokens, $Statements) |
|
23 | - { |
|
24 | - if ($Statements) { |
|
25 | - $this->Statements = array_merge($this->Statements, $Statements); |
|
26 | - } |
|
22 | + public function __construct(Parser $Parser, &$tokens, $Statements) |
|
23 | + { |
|
24 | + if ($Statements) { |
|
25 | + $this->Statements = array_merge($this->Statements, $Statements); |
|
26 | + } |
|
27 | 27 | |
28 | - $depth = 0; |
|
28 | + $depth = 0; |
|
29 | 29 | |
30 | - $token = current($tokens); |
|
31 | - while ($token) { |
|
32 | - switch ($token[0]) { |
|
33 | - case T_STRING: |
|
34 | - if (empty($this->name)) { |
|
35 | - $this->name = $token[1]; |
|
36 | - } |
|
37 | - break; |
|
30 | + $token = current($tokens); |
|
31 | + while ($token) { |
|
32 | + switch ($token[0]) { |
|
33 | + case T_STRING: |
|
34 | + if (empty($this->name)) { |
|
35 | + $this->name = $token[1]; |
|
36 | + } |
|
37 | + break; |
|
38 | 38 | |
39 | - case '{': |
|
40 | - case T_CURLY_OPEN: |
|
41 | - case T_DOLLAR_OPEN_CURLY_BRACES: |
|
42 | - case T_STRING_VARNAME: |
|
43 | - ++$depth; |
|
44 | - break; |
|
39 | + case '{': |
|
40 | + case T_CURLY_OPEN: |
|
41 | + case T_DOLLAR_OPEN_CURLY_BRACES: |
|
42 | + case T_STRING_VARNAME: |
|
43 | + ++$depth; |
|
44 | + break; |
|
45 | 45 | |
46 | - case '}': |
|
47 | - --$depth; |
|
48 | - if ($depth == 0) { |
|
49 | - if ($this->lastStatements) { |
|
50 | - $this->Statements = array_merge($this->Statements, $this->lastStatements); |
|
51 | - $this->lastStatements = null; |
|
52 | - } |
|
53 | - return; |
|
54 | - } |
|
55 | - break; |
|
46 | + case '}': |
|
47 | + --$depth; |
|
48 | + if ($depth == 0) { |
|
49 | + if ($this->lastStatements) { |
|
50 | + $this->Statements = array_merge($this->Statements, $this->lastStatements); |
|
51 | + $this->lastStatements = null; |
|
52 | + } |
|
53 | + return; |
|
54 | + } |
|
55 | + break; |
|
56 | 56 | |
57 | - case T_COMMENT: |
|
58 | - if ($this->lastStatements) { |
|
59 | - $this->Statements = array_merge($this->Statements, $this->lastStatements); |
|
60 | - $this->lastStatements = null; |
|
61 | - } |
|
62 | - $Statements = $Parser->tokenToStatements($token); |
|
63 | - $Parser->queueClassesFromComments($Statements); |
|
64 | - $this->Statements = array_merge($this->Statements, $Statements); |
|
65 | - break; |
|
57 | + case T_COMMENT: |
|
58 | + if ($this->lastStatements) { |
|
59 | + $this->Statements = array_merge($this->Statements, $this->lastStatements); |
|
60 | + $this->lastStatements = null; |
|
61 | + } |
|
62 | + $Statements = $Parser->tokenToStatements($token); |
|
63 | + $Parser->queueClassesFromComments($Statements); |
|
64 | + $this->Statements = array_merge($this->Statements, $Statements); |
|
65 | + break; |
|
66 | 66 | |
67 | - case T_DOC_COMMENT: |
|
68 | - if ($this->lastStatements) { |
|
69 | - $this->Statements = array_merge($this->Statements, $this->lastStatements); |
|
70 | - } |
|
71 | - $Statements = $Parser->tokenToStatements($token); |
|
72 | - $Parser->queueClassesFromComments($Statements); |
|
73 | - $this->lastStatements = $Statements; |
|
74 | - break; |
|
75 | - } |
|
67 | + case T_DOC_COMMENT: |
|
68 | + if ($this->lastStatements) { |
|
69 | + $this->Statements = array_merge($this->Statements, $this->lastStatements); |
|
70 | + } |
|
71 | + $Statements = $Parser->tokenToStatements($token); |
|
72 | + $Parser->queueClassesFromComments($Statements); |
|
73 | + $this->lastStatements = $Statements; |
|
74 | + break; |
|
75 | + } |
|
76 | 76 | |
77 | - $token = next($tokens); |
|
78 | - } |
|
77 | + $token = next($tokens); |
|
78 | + } |
|
79 | 79 | |
80 | - if ($this->lastStatements) { |
|
81 | - $this->Statements = array_merge($this->Statements, $this->lastStatements); |
|
82 | - $this->lastStatements = null; |
|
83 | - } |
|
84 | - } |
|
80 | + if ($this->lastStatements) { |
|
81 | + $this->Statements = array_merge($this->Statements, $this->lastStatements); |
|
82 | + $this->lastStatements = null; |
|
83 | + } |
|
84 | + } |
|
85 | 85 | |
86 | - public function getStatements() |
|
87 | - { |
|
88 | - // inherit |
|
89 | - return $this->Statements; |
|
90 | - } |
|
86 | + public function getStatements() |
|
87 | + { |
|
88 | + // inherit |
|
89 | + return $this->Statements; |
|
90 | + } |
|
91 | 91 | |
92 | 92 | } |
@@ -13,5 +13,5 @@ |
||
13 | 13 | interface IParser |
14 | 14 | { |
15 | 15 | |
16 | - public function parse($file, array $dirs = array()); |
|
16 | + public function parse($file, array $dirs = array()); |
|
17 | 17 | } |
@@ -18,30 +18,30 @@ |
||
18 | 18 | class Preprocessor extends AbstractPreprocessor |
19 | 19 | { |
20 | 20 | |
21 | - protected function parseContent($content) |
|
22 | - { |
|
23 | - $pattern = '/\\s*([a-z]+)\\s*(.*)\\s*/'; |
|
24 | - |
|
25 | - $output = ''; |
|
26 | - |
|
27 | - foreach (preg_split('/(\\R)/m', $content, null, PREG_SPLIT_DELIM_CAPTURE) as $index => $line) { |
|
28 | - if ($index % 2) { |
|
29 | - $output .= $line; |
|
30 | - } else { |
|
31 | - $match = array(); |
|
32 | - if (preg_match($pattern, $line, $match) === 1) { |
|
33 | - if (!$this->handle($match[1], $match[2]) && $this->getState()) { |
|
34 | - $output .= $line; |
|
35 | - } else { |
|
36 | - $output .= ''; |
|
37 | - } |
|
38 | - } else { |
|
39 | - $output .= $line; |
|
40 | - } |
|
41 | - } |
|
42 | - } |
|
43 | - |
|
44 | - return $output; |
|
45 | - } |
|
21 | + protected function parseContent($content) |
|
22 | + { |
|
23 | + $pattern = '/\\s*([a-z]+)\\s*(.*)\\s*/'; |
|
24 | + |
|
25 | + $output = ''; |
|
26 | + |
|
27 | + foreach (preg_split('/(\\R)/m', $content, null, PREG_SPLIT_DELIM_CAPTURE) as $index => $line) { |
|
28 | + if ($index % 2) { |
|
29 | + $output .= $line; |
|
30 | + } else { |
|
31 | + $match = array(); |
|
32 | + if (preg_match($pattern, $line, $match) === 1) { |
|
33 | + if (!$this->handle($match[1], $match[2]) && $this->getState()) { |
|
34 | + $output .= $line; |
|
35 | + } else { |
|
36 | + $output .= ''; |
|
37 | + } |
|
38 | + } else { |
|
39 | + $output .= $line; |
|
40 | + } |
|
41 | + } |
|
42 | + } |
|
43 | + |
|
44 | + return $output; |
|
45 | + } |
|
46 | 46 | |
47 | 47 | } |
@@ -17,99 +17,99 @@ |
||
17 | 17 | class Parser implements IParser |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * List of directories to scan for class files referenced in the parsed |
|
22 | - * command statements. |
|
23 | - * |
|
24 | - * @var string[] |
|
25 | - */ |
|
26 | - protected $common_dirs = []; |
|
20 | + /** |
|
21 | + * List of directories to scan for class files referenced in the parsed |
|
22 | + * command statements. |
|
23 | + * |
|
24 | + * @var string[] |
|
25 | + */ |
|
26 | + protected $common_dirs = []; |
|
27 | 27 | |
28 | - /** |
|
29 | - * @var AbstractPreprocessor |
|
30 | - */ |
|
31 | - private $Preprocessor; |
|
28 | + /** |
|
29 | + * @var AbstractPreprocessor |
|
30 | + */ |
|
31 | + private $Preprocessor; |
|
32 | 32 | |
33 | - /** |
|
34 | - * Create a new text parser and set directories to scan for referenced |
|
35 | - * class files. |
|
36 | - * |
|
37 | - * @param string[] $dirs |
|
38 | - */ |
|
39 | - public function __construct(array $dirs = []) |
|
40 | - { |
|
41 | - foreach ($dirs as $dir) { |
|
42 | - $this->common_dirs[] = realpath($dir); |
|
43 | - } |
|
33 | + /** |
|
34 | + * Create a new text parser and set directories to scan for referenced |
|
35 | + * class files. |
|
36 | + * |
|
37 | + * @param string[] $dirs |
|
38 | + */ |
|
39 | + public function __construct(array $dirs = []) |
|
40 | + { |
|
41 | + foreach ($dirs as $dir) { |
|
42 | + $this->common_dirs[] = realpath($dir); |
|
43 | + } |
|
44 | 44 | |
45 | - $this->Preprocessor = new Preprocessor(); |
|
46 | - } |
|
45 | + $this->Preprocessor = new Preprocessor(); |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Add additional directories to scan for referenced class files. |
|
50 | - * |
|
51 | - * @param string[] $dirs |
|
52 | - */ |
|
53 | - public function addDirs(array $dirs) |
|
54 | - { |
|
55 | - foreach ($dirs as $dir) { |
|
56 | - $this->common_dirs[] = realpath($dir); |
|
57 | - } |
|
58 | - } |
|
48 | + /** |
|
49 | + * Add additional directories to scan for referenced class files. |
|
50 | + * |
|
51 | + * @param string[] $dirs |
|
52 | + */ |
|
53 | + public function addDirs(array $dirs) |
|
54 | + { |
|
55 | + foreach ($dirs as $dir) { |
|
56 | + $this->common_dirs[] = realpath($dir); |
|
57 | + } |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * Parse a text file |
|
62 | - * |
|
63 | - * @param string $file |
|
64 | - * @param string[] $dirs |
|
65 | - * @param string[] $defines |
|
66 | - * @return Statement[] |
|
67 | - */ |
|
68 | - public function parse($file, array $dirs = [], array $defines = []) |
|
69 | - { |
|
70 | - return $this->parseText(file_get_contents(realpath($file)), $dirs); |
|
71 | - } |
|
60 | + /** |
|
61 | + * Parse a text file |
|
62 | + * |
|
63 | + * @param string $file |
|
64 | + * @param string[] $dirs |
|
65 | + * @param string[] $defines |
|
66 | + * @return Statement[] |
|
67 | + */ |
|
68 | + public function parse($file, array $dirs = [], array $defines = []) |
|
69 | + { |
|
70 | + return $this->parseText(file_get_contents(realpath($file)), $dirs); |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Parse plain text |
|
75 | - * |
|
76 | - * @param string $text |
|
77 | - * @param string[] $dirs |
|
78 | - * @param string[] $defines |
|
79 | - * @return Statement[] |
|
80 | - */ |
|
81 | - public function parseText($text, array $dirs = [], array $defines = []): array |
|
82 | - { |
|
83 | - $this->Preprocessor->resetDefines(); |
|
84 | - $this->Preprocessor->addDefines($defines); |
|
85 | - $text = $this->Preprocessor->preprocess($text); |
|
73 | + /** |
|
74 | + * Parse plain text |
|
75 | + * |
|
76 | + * @param string $text |
|
77 | + * @param string[] $dirs |
|
78 | + * @param string[] $defines |
|
79 | + * @return Statement[] |
|
80 | + */ |
|
81 | + public function parseText($text, array $dirs = [], array $defines = []): array |
|
82 | + { |
|
83 | + $this->Preprocessor->resetDefines(); |
|
84 | + $this->Preprocessor->addDefines($defines); |
|
85 | + $text = $this->Preprocessor->preprocess($text); |
|
86 | 86 | |
87 | - $Statements = []; |
|
87 | + $Statements = []; |
|
88 | 88 | |
89 | - foreach (preg_split('/\\R/m', $text) as $line => $data) { |
|
90 | - $data = trim($data); |
|
91 | - $command = self::wordShift($data); |
|
92 | - if (!empty($command)) { |
|
93 | - $Statements[] = new Statement($command, $data, null, $line); |
|
94 | - } |
|
95 | - } |
|
89 | + foreach (preg_split('/\\R/m', $text) as $line => $data) { |
|
90 | + $data = trim($data); |
|
91 | + $command = self::wordShift($data); |
|
92 | + if (!empty($command)) { |
|
93 | + $Statements[] = new Statement($command, $data, null, $line); |
|
94 | + } |
|
95 | + } |
|
96 | 96 | |
97 | - return $Statements; |
|
98 | - } |
|
97 | + return $Statements; |
|
98 | + } |
|
99 | 99 | |
100 | - /** |
|
101 | - * Get the first word from a string and remove it from the string. |
|
102 | - * |
|
103 | - * @param string $data |
|
104 | - * @return boolean|string |
|
105 | - */ |
|
106 | - private static function wordShift(&$data) |
|
107 | - { |
|
108 | - if (preg_match('~^(\S+)\s*(.*)$~', $data, $matches) === 1) { |
|
109 | - $data = $matches[2]; |
|
110 | - return $matches[1]; |
|
111 | - } |
|
112 | - return false; |
|
113 | - } |
|
100 | + /** |
|
101 | + * Get the first word from a string and remove it from the string. |
|
102 | + * |
|
103 | + * @param string $data |
|
104 | + * @return boolean|string |
|
105 | + */ |
|
106 | + private static function wordShift(&$data) |
|
107 | + { |
|
108 | + if (preg_match('~^(\S+)\s*(.*)$~', $data, $matches) === 1) { |
|
109 | + $data = $matches[2]; |
|
110 | + return $matches[1]; |
|
111 | + } |
|
112 | + return false; |
|
113 | + } |
|
114 | 114 | |
115 | 115 | } |
@@ -15,55 +15,55 @@ |
||
15 | 15 | class TypeRegistry |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * Map of format-name => class-name |
|
20 | - * |
|
21 | - * @var array |
|
22 | - */ |
|
23 | - private $formats = array(); |
|
18 | + /** |
|
19 | + * Map of format-name => class-name |
|
20 | + * |
|
21 | + * @var array |
|
22 | + */ |
|
23 | + private $formats = array(); |
|
24 | 24 | |
25 | - /** |
|
26 | - * Add a type name from classname |
|
27 | - * |
|
28 | - * @param string $classname |
|
29 | - */ |
|
30 | - public function add(string $classname): void |
|
31 | - { |
|
32 | - if (is_subclass_of($classname, ICustomType::class)) { |
|
33 | - foreach ($classname::getFormats() as $format) { |
|
34 | - $this->formats[$format] = $classname; |
|
35 | - } |
|
36 | - } |
|
37 | - } |
|
25 | + /** |
|
26 | + * Add a type name from classname |
|
27 | + * |
|
28 | + * @param string $classname |
|
29 | + */ |
|
30 | + public function add(string $classname): void |
|
31 | + { |
|
32 | + if (is_subclass_of($classname, ICustomType::class)) { |
|
33 | + foreach ($classname::getFormats() as $format) { |
|
34 | + $this->formats[$format] = $classname; |
|
35 | + } |
|
36 | + } |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * Remove type format by explicitly nulling it (disables it) |
|
41 | - * |
|
42 | - * @param string $name |
|
43 | - */ |
|
44 | - public function remove($name) |
|
45 | - { |
|
46 | - $this->formats[$name] = null; |
|
47 | - } |
|
39 | + /** |
|
40 | + * Remove type format by explicitly nulling it (disables it) |
|
41 | + * |
|
42 | + * @param string $name |
|
43 | + */ |
|
44 | + public function remove($name) |
|
45 | + { |
|
46 | + $this->formats[$name] = null; |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Is a type format known? |
|
51 | - * |
|
52 | - * @return bool |
|
53 | - */ |
|
54 | - public function has($name) |
|
55 | - { |
|
56 | - return !empty($this->formats[$name]); |
|
57 | - } |
|
49 | + /** |
|
50 | + * Is a type format known? |
|
51 | + * |
|
52 | + * @return bool |
|
53 | + */ |
|
54 | + public function has($name) |
|
55 | + { |
|
56 | + return !empty($this->formats[$name]); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Get the format class name |
|
61 | - * |
|
62 | - * @return null|string |
|
63 | - */ |
|
64 | - public function get($name) |
|
65 | - { |
|
66 | - return !empty($this->formats[$name]) ? $this->formats[$name] : null; |
|
67 | - } |
|
59 | + /** |
|
60 | + * Get the format class name |
|
61 | + * |
|
62 | + * @return null|string |
|
63 | + */ |
|
64 | + public function get($name) |
|
65 | + { |
|
66 | + return !empty($this->formats[$name]) ? $this->formats[$name] : null; |
|
67 | + } |
|
68 | 68 | |
69 | 69 | } |