ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
JsFunctionsScanner.php
Go to the documentation of this file.
1<?php
2
3namespace Gettext\Utils;
4
6{
7 protected $code;
8 protected $status = array();
9
15 public function __construct($code)
16 {
17 $this->code = $code;
18 }
19
23 public function getFunctions()
24 {
25 $length = strlen($this->code);
26 $line = 1;
27 $buffer = '';
28 $functions = array();
29 $bufferFunctions = array();
30 $char = null;
31
32 for ($pos = 0; $pos < $length; ++$pos) {
33 $prev = $char;
34 $char = $this->code[$pos];
35 $next = isset($this->code[$pos]) ? $this->code[$pos] : null;
36
37 switch ($char) {
38 case "\n":
39 ++$line;
40
41 if ($this->status('line-comment')) {
42 $this->upStatus();
43 }
44 break;
45
46 case '/':
47 switch ($this->status()) {
48 case 'simple-quote':
49 case 'double-quote':
50 case 'line-comment':
51 break;
52
53 case 'block-comment':
54 if ($prev === '*') {
55 $this->upStatus();
56 }
57 break;
58
59 default:
60 if ($next === '/') {
61 $this->downStatus('line-comment');
62 } elseif ($next === '*') {
63 $this->downStatus('block-comment');
64 }
65 break;
66 }
67 break;
68
69 case "'":
70 switch ($this->status()) {
71 case 'simple-quote':
72 $this->upStatus();
73 break;
74
75 case 'line-comment':
76 case 'block-comment':
77 case 'double-quote':
78 break;
79
80 default:
81 $this->downStatus('simple-quote');
82 break;
83 }
84 break;
85
86 case '"':
87 switch ($this->status()) {
88 case 'double-quote':
89 $this->upStatus();
90 break;
91
92 case 'line-comment':
93 case 'block-comment':
94 case 'simple-quote':
95 break;
96
97 default:
98 $this->downStatus('double-quote');
99 break;
100 }
101 break;
102
103 case '(':
104 switch ($this->status()) {
105 case 'double-quote':
106 case 'line-comment':
107 case 'block-comment':
108 case 'line-comment':
109 break;
110
111 default:
112 if ($buffer && preg_match('/(\w+)$/', $buffer, $matches)) {
113 $this->downStatus('function');
114 array_unshift($bufferFunctions, array($matches[1], $line, array()));
115 $buffer = '';
116 continue 3;
117 }
118 break;
119 }
120 break;
121
122 case ')':
123 switch ($this->status()) {
124 case 'function':
125 if (($argument = self::prepareArgument($buffer))) {
126 $bufferFunctions[0][2][] = $argument;
127 }
128
129 if (!empty($bufferFunctions)) {
130 $functions[] = array_shift($bufferFunctions);
131 }
132
133 $buffer = '';
134 continue 3;
135 }
136
137 case ',':
138 switch ($this->status()) {
139 case 'function':
140 if (($argument = self::prepareArgument($buffer))) {
141 $bufferFunctions[0][2][] = $argument;
142 }
143
144 $buffer = '';
145 continue 3;
146 }
147 }
148
149 switch ($this->status()) {
150 case 'line-comment':
151 case 'block-comment':
152 break;
153
154 default:
155 $buffer .= $char;
156 break;
157 }
158 }
159
160 return $functions;
161 }
162
170 protected function status($match = null)
171 {
172 $status = isset($this->status[0]) ? $this->status[0] : null;
173
174 if ($match !== null) {
175 return $status === $match;
176 }
177
178 return $status;
179 }
180
186 protected function downStatus($status)
187 {
188 array_unshift($this->status, $status);
189 }
190
196 protected function upStatus()
197 {
198 return array_shift($this->status);
199 }
200
208 protected static function prepareArgument($argument)
209 {
210 if ($argument && ($argument[0] === '"' || $argument[0] === "'")) {
211 if ($argument[0] === '"') {
212 $argument = str_replace('\\"', '"', $argument);
213 } else {
214 $argument = str_replace("\\'", "'", $argument);
215 }
216
217 return substr($argument, 1, -1);
218 }
219 }
220}
An exception for terminatinating execution or to throw for unit testing.
static prepareArgument($argument)
Prepares the arguments found in functions.
downStatus($status)
Add a new status to the stack.
status($match=null)
Get the current context of the scan.
getFunctions()
{Scan and returns the functions and the arguments.array}
upStatus()
Removes and return the current status.