Subversion Repository Public Repository

Nextrek

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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php

/* vim: set shiftwidth=2 expandtab softtabstop=2: */

namespace Boris;

/**
 * The ShallowParser takes whatever is currently buffered and chunks it into individual statements.
 */
class ShallowParser {
  private $_pairs = array(
    '('   => ')',
    '{'   => '}',
    '['   => ']',
    '"'   => '"',
    "'"   => "'",
    '//'  => "\n",
    '#'   => "\n",
    '/*'  => '*/',
    '<<<' => '_heredoc_special_case_'
  );

  private $_initials;

  public function __construct() {
    $this->_initials   = '/^(' . implode('|', array_map(array($this, 'quote'), array_keys($this->_pairs))) . ')/';
  }

  /**
   * Break the $buffer into chunks, with one for each highest-level construct possible.
   *
   * If the buffer is incomplete, returns an empty array.
   *
   * @param string $buffer
   *
   * @return array
   */
  public function statements($buffer) {
    $result = $this->_createResult($buffer);

    while (strlen($result->buffer) > 0) {
      $this->_resetResult($result);

      if ($result->state == '<<<') {
        if (!$this->_initializeHeredoc($result)) {
          continue;
        }
      }

      $rules = array('_scanEscapedChar', '_scanRegion', '_scanStateEntrant', '_scanWsp', '_scanChar');

      foreach ($rules as $method) {
        if ($this->$method($result)) {
          break;
        }
      }

      if ($result->stop) {
        break;
      }
    }

    if (!empty($result->statements) && trim($result->stmt) === '' && strlen($result->buffer) == 0) {
      $this->_combineStatements($result);
      $this->_prepareForDebug($result);
      return $result->statements;
    }
  }

  public function quote($token) {
    return preg_quote($token, '/');
  }

  // -- Private Methods

  private function _createResult($buffer) {
    $result = new \stdClass();
    $result->buffer     = $buffer;
    $result->stmt       = '';
    $result->state      =  null;
    $result->states     = array();
    $result->statements = array();
    $result->stop       = false;

    return $result;
  }

  private function _resetResult($result) {
    $result->stop       = false;
    $result->state      = end($result->states);
    $result->terminator = $result->state
      ? '/^(.*?' . preg_quote($this->_pairs[$result->state], '/') . ')/s'
      : null
      ;
  }

  private function _combineStatements($result) {
    $combined = array();

    foreach ($result->statements as $scope) {
      if (trim($scope) == ';' || substr(trim($scope), -1) != ';') {
        $combined[] = ((string) array_pop($combined)) . $scope;
      } else {
        $combined[] = $scope;
      }
    }

    $result->statements = $combined;
  }

  private function _prepareForDebug($result) {
    $result->statements []= $this->_prepareDebugStmt(array_pop($result->statements));
  }

  private function _initializeHeredoc($result) {
    if (preg_match('/^([\'"]?)([a-z_][a-z0-9_]*)\\1/i', $result->buffer, $match)) {
      $docId = $match[2];
      $result->stmt .= $match[0];
      $result->buffer = substr($result->buffer, strlen($match[0]));

      $result->terminator = '/^(.*?\n' . $docId . ');?\n/s';

      return true;
    } else {
      return false;
    }
  }

  private function _scanWsp($result) {
    if (preg_match('/^\s+/', $result->buffer, $match)) {
      if (!empty($result->statements) && $result->stmt === '') {
        $result->statements[] = array_pop($result->statements) . $match[0];
      } else {
        $result->stmt .= $match[0];
      }
      $result->buffer = substr($result->buffer, strlen($match[0]));

      return true;
    } else {
      return false;
    }
  }

  private function _scanEscapedChar($result) {
    if (($result->state == '"' || $result->state == "'")
        && preg_match('/^[^' . $result->state . ']*?\\\\./s', $result->buffer, $match)) {

      $result->stmt .= $match[0];
      $result->buffer = substr($result->buffer, strlen($match[0]));

      return true;
    } else {
      return false;
    }
  }

  private function _scanRegion($result) {
    if (in_array($result->state, array('"', "'", '<<<', '//', '#', '/*'))) {
      if (preg_match($result->terminator, $result->buffer, $match)) {
        $result->stmt .= $match[1];
        $result->buffer = substr($result->buffer, strlen($match[1]));
        array_pop($result->states);
      } else {
        $result->stop = true;
      }

      return true;
    } else {
      return false;
    }
  }

  private function _scanStateEntrant($result) {
    if (preg_match($this->_initials, $result->buffer, $match)) {
      $result->stmt .= $match[0];
      $result->buffer = substr($result->buffer, strlen($match[0]));
      $result->states[] = $match[0];

      return true;
    } else {
      return false;
    }
  }

  private function _scanChar($result) {
    $chr = substr($result->buffer, 0, 1);
    $result->stmt .= $chr;
    $result->buffer = substr($result->buffer, 1);
    if ($result->state && $chr == $this->_pairs[$result->state]) {
      array_pop($result->states);
    }

    if (empty($result->states) && ($chr == ';' || $chr == '}')) {
      if (!$this->_isLambda($result->stmt) || $chr == ';') {
        $result->statements[] = $result->stmt;
        $result->stmt = '';
      }
    }

    return true;
  }

  private function _isLambda($input) {
    return preg_match(
      '/^([^=]*?=\s*)?function\s*\([^\)]*\)\s*(use\s*\([^\)]*\)\s*)?\s*\{.*\}\s*;?$/is',
      trim($input)
    );
  }

  private function _isReturnable($input) {
    $input = trim($input);
    if (substr($input, -1) == ';' && substr($input, 0, 1) != '{') {
      return $this->_isLambda($input) || !preg_match(
        '/^(' .
        'echo|print|exit|die|goto|global|include|include_once|require|require_once|list|' .
        'return|do|for|foreach|while|if|function|namespace|class|interface|abstract|switch|' .
        'declare|throw|try|unset' .
        ')\b/i',
        $input
      );
    } else {
      return false;
    }
  }

  private function _prepareDebugStmt($input) {
    if ($this->_isReturnable($input) && !preg_match('/^\s*return/i', $input)) {
      $input = sprintf('return %s', $input);
    }

    return $input;
  }
}

Commits for Nextrek/Aiba_backup/vendor/d11wtq/boris/lib/Boris/ShallowParser.php

Diff revisions: vs.
Revision Author Commited Message
1464 MOliva picture MOliva Tue 13 Oct, 2020 11:16:56 +0000