Test Setup Failed
Push — master ( 2f4b22...8919ea )
by Chris
14:41
created
php/hamle/Grammar/Parser.php 2 patches
Indentation   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -301,14 +301,14 @@
 block discarded – undo
301 301
     private function peg_f1($i) { $return = array();
302 302
                                                   array_walk_recursive($i, function($a) use (&$return) { $return[] = $a; });
303 303
                                                   return $return;
304
-                                         }
304
+                                          }
305 305
     private function peg_f2($text) { return new \Seufert\Hamle\TextNode\StringLit(join('',$text)); }
306 306
     private function peg_f3($body) { return $body; }
307 307
     private function peg_f4($name) {
308 308
             return new \Seufert\Hamle\TextNode\ScopeId(null, null, new \Seufert\Hamle\TextNode\ModelParam($name)); }
309 309
     private function peg_f5($expr, $chain) { if(!$chain) return $expr;
310
-                           $top = array_pop($chain);
311
-                           while($chain) { $top = array_pop($chain)->withChain($top); } return $expr->withChain($top); }
310
+                            $top = array_pop($chain);
311
+                            while($chain) { $top = array_pop($chain)->withChain($top); } return $expr->withChain($top); }
312 312
     private function peg_f6($sub) { return $sub; }
313 313
     private function peg_f7($filter) { return $filter; }
314 314
     private function peg_f8($name) { return new \Seufert\Hamle\TextNode\ModelParam($name, null); }
Please login to merge, or discard this patch.
Braces   +3 added lines, -1 removed lines patch added patch discarded remove patch
@@ -306,7 +306,9 @@
 block discarded – undo
306 306
     private function peg_f3($body) { return $body; }
307 307
     private function peg_f4($name) {
308 308
             return new \Seufert\Hamle\TextNode\ScopeId(null, null, new \Seufert\Hamle\TextNode\ModelParam($name)); }
309
-    private function peg_f5($expr, $chain) { if(!$chain) return $expr;
309
+    private function peg_f5($expr, $chain) { if(!$chain) {
310
+      return $expr;
311
+    }
310 312
                            $top = array_pop($chain);
311 313
                            while($chain) { $top = array_pop($chain)->withChain($top); } return $expr->withChain($top); }
312 314
     private function peg_f6($sub) { return $sub; }
Please login to merge, or discard this patch.
php/hamle/Parse.php 2 patches
Indentation   -275 removed lines patch added patch discarded remove patch
@@ -53,278 +53,3 @@
 block discarded – undo
53 53
    */
54 54
 
55 55
   const REGEX_PARSE_LINE = <<<'ENDREGEX'
56
-  /^(\s*)(?:(?:([a-zA-Z0-9-]*)((?:[\.#!][\w\-\_]+)*)(\[(?:(?:\{\$[^\}]+\})?[^\\\]{]*?(?:\\.)*?(?:{[^\$])*?)+\])?)|([_\/]{1,3})|([\|:\$]\w+)|({?\$[^}]+}?)|)(?: (.*))?$/
57
-  ENDREGEX;
58
-
59
-  /**
60
-   * @var int Current Line Number
61
-   */
62
-  protected int $lineNo = 0;
63
-  /**
64
-   * @var int Total Lines in File
65
-   */
66
-  protected int $lineCount = 0;
67
-
68
-  function __construct()
69
-  {
70
-    $this->init();
71
-  }
72
-
73
-  /**
74
-   * Clear Lines, and Line Number, so if output is
75
-   * called, no output will be produced
76
-   */
77
-  protected function init():void
78
-  {
79
-    $this->lines = [];
80
-    $this->lineNo = 0;
81
-    $this->lineCount = 0;
82
-    $this->root = [];
83
-  }
84
-
85
-  protected function loadLines(string $s):void
86
-  {
87
-    $this->lines = explode("\n", str_replace("\r", '', $s));
88
-    $this->lineCount = count($this->lines);
89
-    $this->lineNo = 0;
90
-  }
91
-
92
-  function parseFilter(ParseFilter $filter):void
93
-  {
94
-    foreach ($this->root as $k => $tag) {
95
-      $this->root[$k] = $filter->filterTag($tag);
96
-    }
97
-  }
98
-
99
-  function parseSnip(string $s):void
100
-  {
101
-    //save root tags
102
-    /** @var Tag[] $roots */
103
-    $roots = $this->root;
104
-    $this->root = [];
105
-    $this->loadLines($s);
106
-    $this->procLines();
107
-    $this->root = array_merge($roots, $this->root);
108
-  }
109
-
110
-  function applySnip():void
111
-  {
112
-    /** @var Tag\Snippet[] $fwdSnip */
113
-    $fwdSnip = [];
114
-    /** @var Tag\Snippet[] $revSnip */
115
-    $revSnip = [];
116
-    /** @var Tag[] $roots */
117
-    $roots = [];
118
-    foreach ($this->root as $snip) {
119
-      if ($snip instanceof Tag\Snippet) {
120
-        if ($snip->getType() == 'append') {
121
-          array_unshift($revSnip, $snip);
122
-        } else {
123
-          $fwdSnip[] = $snip;
124
-        }
125
-      } else {
126
-        $roots[] = $snip;
127
-      }
128
-    }
129
-    foreach ($fwdSnip as $snip) {
130
-      foreach ($roots as $root) {
131
-        $snip->apply($root);
132
-      }
133
-    }
134
-    foreach ($revSnip as $snip) {
135
-      foreach ($roots as $root) {
136
-        $snip->apply($root);
137
-      }
138
-    }
139
-    $this->root = $roots;
140
-  }
141
-
142
-  /**
143
-   * Parse HAMLE template, from a string
144
-   * @param string $s String to parse
145
-   */
146
-  function str($s):void
147
-  {
148
-    $this->init();
149
-    $this->loadLines($s);
150
-    $this->procLines();
151
-  }
152
-
153
-  function procLines():void
154
-  {
155
-    /* @var $heir Tag[] Tag Heirachy Array */
156
-    $heir = [];
157
-    while ($this->lineNo < $this->lineCount) {
158
-      $line = $this->lines[$this->lineNo];
159
-      if (trim($line)) {
160
-        if (preg_match(self::REGEX_PARSE_LINE, $line, $m)) {
161
-          if (false !== strpos($m[1], "\t")) {
162
-            throw new ParseError(
163
-              'Tabs are not supported in templates at this time',
164
-            );
165
-          }
166
-          $indent = strlen($m[1]);
167
-          $tag = isset($m[2]) ? ($tag = $m[2]) : '';
168
-          $classid = isset($m[3]) ? $m[3] : '';
169
-          $params = str_replace(
170
-            ['\[', '\]', '\\&'],
171
-            ['[', ']', '%26'],
172
-            isset($m[4]) ? $m[4] : '',
173
-          );
174
-          $textcode = isset($m[5]) ? $m[5] : '';
175
-          $text = isset($m[8]) ? $m[8] : '';
176
-          $code = isset($m[6]) ? $m[6] : '';
177
-          $i = self::indentLevel($indent);
178
-          unset($m[0]);
179
-          switch (strlen($code) ? $code[0] : ($textcode ? $textcode : '')) {
180
-            case '|': //Control Tag
181
-              if ($code == '|snippet') {
182
-                $hTag = new Tag\Snippet($text);
183
-              } elseif ($code == '|form') {
184
-                $hTag = new Tag\Form($text);
185
-              } elseif ($code == '|formhint') {
186
-                $hTag = new Tag\FormHint();
187
-              } elseif ($code == '|else') {
188
-                $hTag = new Tag\Control(substr($code, 1), $heir[$i - 1]);
189
-                $hTag->setVar($text);
190
-              } else {
191
-                $hTag = new Tag\Control(substr($code, 1));
192
-                $hTag->setVar($text);
193
-              }
194
-              break;
195
-            case ':': //Filter Tag
196
-              $hTag = new Tag\Filter(substr($code, 1));
197
-              $hTag->addContent($text, Text::TOKEN_CODE);
198
-              foreach ($this->consumeBlock($indent) as $l) {
199
-                $hTag->addContent($l, Text::TOKEN_CODE);
200
-              }
201
-              break;
202
-            case '_': //String Tag
203
-            case '__': //Unescape String Tag
204
-            case '___': //Unescape String Tag (with unescaped vars)
205
-              $hTag = new Tag\Text($textcode);
206
-              $hTag->addContent($text);
207
-              break;
208
-            case '___': //Unescape String Tag
209
-              $hTag = new Tag\Text($textcode);
210
-              $hTag->addContent($text);
211
-              break;
212
-            case '/': // HTML Comment
213
-            case '//': // Non Printed Comment
214
-              $hTag = new Tag\Comment($textcode);
215
-              $hTag->addContent($text);
216
-              foreach ($this->consumeBlock($indent) as $l) {
217
-                $hTag->addContent($l, Text::TOKEN_CODE);
218
-              }
219
-              break;
220
-            default:
221
-              $attr = [];
222
-              if (isset($params[0]) && $params[0] == '[') {
223
-                $param = substr($params, 1, -1);
224
-                $param = str_replace(['+', '\\&'], ['%2B', '%26'], $param);
225
-                $attr = $this->parseQueryString($param);
226
-              }
227
-              $class = [];
228
-              $id = '';
229
-              $ref = '';
230
-              preg_match_all('/[#\.!][a-zA-Z0-9\-\_]+/m', $classid, $cid);
231
-              if (isset($cid[0])) {
232
-                foreach ($cid[0] as $s) {
233
-                  if ($s[0] == '#') {
234
-                    $id = substr($s, 1);
235
-                  }
236
-                  if ($s[0] == '.') {
237
-                    $class[] = substr($s, 1);
238
-                  }
239
-                  if ($s[0] == '!') {
240
-                    $ref = substr($s, 1);
241
-                  }
242
-                }
243
-              }
244
-              if ($ref) {
245
-                $hTag = new Tag\DynHtml($tag, $class, $attr, $id, $ref);
246
-              } else {
247
-                $hTag = new Tag\Html($tag, $class, $attr, $id);
248
-              }
249
-              $hTag->addContent($text);
250
-              break;
251
-          }
252
-          $heir[$i] = $hTag;
253
-          if ($i > 0) {
254
-            $heir[$i - 1]->addChild($hTag);
255
-          } else {
256
-            $this->root[] = $hTag;
257
-          }
258
-        } else {
259
-          throw new ParseError(
260
-            "Unable to parse line {$this->lineNo}\n\"$line\"/" .
261
-              preg_last_error(),
262
-          );
263
-        }
264
-      }
265
-      $this->lineNo++;
266
-    }
267
-  }
268
-
269
-  function parseQueryString(string $qs):array
270
-  {
271
-    $out = [];
272
-    foreach (explode('&', $qs) as $s) {
273
-      $kv = explode('=', $s, 2);
274
-      $out[urldecode($kv[0])] = isset($kv[1]) ? urldecode($kv[1]) : null;
275
-    }
276
-    return $out;
277
-  }
278
-
279
-  function output(bool $minify = false):string
280
-  {
281
-    $out = "<?php\nuse Seufert\\Hamle;\n?>";
282
-    foreach ($this->root as $tag) {
283
-      $out .= $tag->render(0, $minify);
284
-    }
285
-    return $out;
286
-  }
287
-
288
-  function consumeBlock(int $indent):array
289
-  {
290
-    $out = [];
291
-    $m = [];
292
-    while (
293
-      $this->lineNo + 1 < $this->lineCount &&
294
-      (!trim($this->lines[$this->lineNo + 1]) ||
295
-        preg_match(
296
-          '/^(\s){' . $indent . '}((\s)+[^\s].*)$/',
297
-          $this->lines[$this->lineNo + 1],
298
-          $m,
299
-        ))
300
-    ) {
301
-      if (trim($this->lines[$this->lineNo + 1])) {
302
-        $out[] = $m[2];
303
-      }
304
-      $this->lineNo++;
305
-    }
306
-    return $out;
307
-  }
308
-
309
-  function indentLevel(int $indent):int
310
-  {
311
-    if (!count($this->indents)) {
312
-      $this->indents = [0 => $indent];
313
-      // Key = indent level, Value = Depth in spaces
314
-      return 0;
315
-    }
316
-    foreach ($this->indents as $k => $v) {
317
-      if ($v == $indent) {
318
-        $this->indents = array_slice($this->indents, 0, 1 + (int) $k );
319
-        return $k;
320
-      }
321
-    }
322
-    $this->indents[] = $indent;
323
-    return max(array_keys($this->indents));
324
-  }
325
-
326
-  function getLineNo():int
327
-  {
328
-    return $this->lineNo;
329
-  }
330
-}
Please login to merge, or discard this patch.
Braces   -275 removed lines patch added patch discarded remove patch
@@ -53,278 +53,3 @@
 block discarded – undo
53 53
    */
54 54
 
55 55
   const REGEX_PARSE_LINE = <<<'ENDREGEX'
56
-  /^(\s*)(?:(?:([a-zA-Z0-9-]*)((?:[\.#!][\w\-\_]+)*)(\[(?:(?:\{\$[^\}]+\})?[^\\\]{]*?(?:\\.)*?(?:{[^\$])*?)+\])?)|([_\/]{1,3})|([\|:\$]\w+)|({?\$[^}]+}?)|)(?: (.*))?$/
57
-  ENDREGEX;
58
-
59
-  /**
60
-   * @var int Current Line Number
61
-   */
62
-  protected int $lineNo = 0;
63
-  /**
64
-   * @var int Total Lines in File
65
-   */
66
-  protected int $lineCount = 0;
67
-
68
-  function __construct()
69
-  {
70
-    $this->init();
71
-  }
72
-
73
-  /**
74
-   * Clear Lines, and Line Number, so if output is
75
-   * called, no output will be produced
76
-   */
77
-  protected function init():void
78
-  {
79
-    $this->lines = [];
80
-    $this->lineNo = 0;
81
-    $this->lineCount = 0;
82
-    $this->root = [];
83
-  }
84
-
85
-  protected function loadLines(string $s):void
86
-  {
87
-    $this->lines = explode("\n", str_replace("\r", '', $s));
88
-    $this->lineCount = count($this->lines);
89
-    $this->lineNo = 0;
90
-  }
91
-
92
-  function parseFilter(ParseFilter $filter):void
93
-  {
94
-    foreach ($this->root as $k => $tag) {
95
-      $this->root[$k] = $filter->filterTag($tag);
96
-    }
97
-  }
98
-
99
-  function parseSnip(string $s):void
100
-  {
101
-    //save root tags
102
-    /** @var Tag[] $roots */
103
-    $roots = $this->root;
104
-    $this->root = [];
105
-    $this->loadLines($s);
106
-    $this->procLines();
107
-    $this->root = array_merge($roots, $this->root);
108
-  }
109
-
110
-  function applySnip():void
111
-  {
112
-    /** @var Tag\Snippet[] $fwdSnip */
113
-    $fwdSnip = [];
114
-    /** @var Tag\Snippet[] $revSnip */
115
-    $revSnip = [];
116
-    /** @var Tag[] $roots */
117
-    $roots = [];
118
-    foreach ($this->root as $snip) {
119
-      if ($snip instanceof Tag\Snippet) {
120
-        if ($snip->getType() == 'append') {
121
-          array_unshift($revSnip, $snip);
122
-        } else {
123
-          $fwdSnip[] = $snip;
124
-        }
125
-      } else {
126
-        $roots[] = $snip;
127
-      }
128
-    }
129
-    foreach ($fwdSnip as $snip) {
130
-      foreach ($roots as $root) {
131
-        $snip->apply($root);
132
-      }
133
-    }
134
-    foreach ($revSnip as $snip) {
135
-      foreach ($roots as $root) {
136
-        $snip->apply($root);
137
-      }
138
-    }
139
-    $this->root = $roots;
140
-  }
141
-
142
-  /**
143
-   * Parse HAMLE template, from a string
144
-   * @param string $s String to parse
145
-   */
146
-  function str($s):void
147
-  {
148
-    $this->init();
149
-    $this->loadLines($s);
150
-    $this->procLines();
151
-  }
152
-
153
-  function procLines():void
154
-  {
155
-    /* @var $heir Tag[] Tag Heirachy Array */
156
-    $heir = [];
157
-    while ($this->lineNo < $this->lineCount) {
158
-      $line = $this->lines[$this->lineNo];
159
-      if (trim($line)) {
160
-        if (preg_match(self::REGEX_PARSE_LINE, $line, $m)) {
161
-          if (false !== strpos($m[1], "\t")) {
162
-            throw new ParseError(
163
-              'Tabs are not supported in templates at this time',
164
-            );
165
-          }
166
-          $indent = strlen($m[1]);
167
-          $tag = isset($m[2]) ? ($tag = $m[2]) : '';
168
-          $classid = isset($m[3]) ? $m[3] : '';
169
-          $params = str_replace(
170
-            ['\[', '\]', '\\&'],
171
-            ['[', ']', '%26'],
172
-            isset($m[4]) ? $m[4] : '',
173
-          );
174
-          $textcode = isset($m[5]) ? $m[5] : '';
175
-          $text = isset($m[8]) ? $m[8] : '';
176
-          $code = isset($m[6]) ? $m[6] : '';
177
-          $i = self::indentLevel($indent);
178
-          unset($m[0]);
179
-          switch (strlen($code) ? $code[0] : ($textcode ? $textcode : '')) {
180
-            case '|': //Control Tag
181
-              if ($code == '|snippet') {
182
-                $hTag = new Tag\Snippet($text);
183
-              } elseif ($code == '|form') {
184
-                $hTag = new Tag\Form($text);
185
-              } elseif ($code == '|formhint') {
186
-                $hTag = new Tag\FormHint();
187
-              } elseif ($code == '|else') {
188
-                $hTag = new Tag\Control(substr($code, 1), $heir[$i - 1]);
189
-                $hTag->setVar($text);
190
-              } else {
191
-                $hTag = new Tag\Control(substr($code, 1));
192
-                $hTag->setVar($text);
193
-              }
194
-              break;
195
-            case ':': //Filter Tag
196
-              $hTag = new Tag\Filter(substr($code, 1));
197
-              $hTag->addContent($text, Text::TOKEN_CODE);
198
-              foreach ($this->consumeBlock($indent) as $l) {
199
-                $hTag->addContent($l, Text::TOKEN_CODE);
200
-              }
201
-              break;
202
-            case '_': //String Tag
203
-            case '__': //Unescape String Tag
204
-            case '___': //Unescape String Tag (with unescaped vars)
205
-              $hTag = new Tag\Text($textcode);
206
-              $hTag->addContent($text);
207
-              break;
208
-            case '___': //Unescape String Tag
209
-              $hTag = new Tag\Text($textcode);
210
-              $hTag->addContent($text);
211
-              break;
212
-            case '/': // HTML Comment
213
-            case '//': // Non Printed Comment
214
-              $hTag = new Tag\Comment($textcode);
215
-              $hTag->addContent($text);
216
-              foreach ($this->consumeBlock($indent) as $l) {
217
-                $hTag->addContent($l, Text::TOKEN_CODE);
218
-              }
219
-              break;
220
-            default:
221
-              $attr = [];
222
-              if (isset($params[0]) && $params[0] == '[') {
223
-                $param = substr($params, 1, -1);
224
-                $param = str_replace(['+', '\\&'], ['%2B', '%26'], $param);
225
-                $attr = $this->parseQueryString($param);
226
-              }
227
-              $class = [];
228
-              $id = '';
229
-              $ref = '';
230
-              preg_match_all('/[#\.!][a-zA-Z0-9\-\_]+/m', $classid, $cid);
231
-              if (isset($cid[0])) {
232
-                foreach ($cid[0] as $s) {
233
-                  if ($s[0] == '#') {
234
-                    $id = substr($s, 1);
235
-                  }
236
-                  if ($s[0] == '.') {
237
-                    $class[] = substr($s, 1);
238
-                  }
239
-                  if ($s[0] == '!') {
240
-                    $ref = substr($s, 1);
241
-                  }
242
-                }
243
-              }
244
-              if ($ref) {
245
-                $hTag = new Tag\DynHtml($tag, $class, $attr, $id, $ref);
246
-              } else {
247
-                $hTag = new Tag\Html($tag, $class, $attr, $id);
248
-              }
249
-              $hTag->addContent($text);
250
-              break;
251
-          }
252
-          $heir[$i] = $hTag;
253
-          if ($i > 0) {
254
-            $heir[$i - 1]->addChild($hTag);
255
-          } else {
256
-            $this->root[] = $hTag;
257
-          }
258
-        } else {
259
-          throw new ParseError(
260
-            "Unable to parse line {$this->lineNo}\n\"$line\"/" .
261
-              preg_last_error(),
262
-          );
263
-        }
264
-      }
265
-      $this->lineNo++;
266
-    }
267
-  }
268
-
269
-  function parseQueryString(string $qs):array
270
-  {
271
-    $out = [];
272
-    foreach (explode('&', $qs) as $s) {
273
-      $kv = explode('=', $s, 2);
274
-      $out[urldecode($kv[0])] = isset($kv[1]) ? urldecode($kv[1]) : null;
275
-    }
276
-    return $out;
277
-  }
278
-
279
-  function output(bool $minify = false):string
280
-  {
281
-    $out = "<?php\nuse Seufert\\Hamle;\n?>";
282
-    foreach ($this->root as $tag) {
283
-      $out .= $tag->render(0, $minify);
284
-    }
285
-    return $out;
286
-  }
287
-
288
-  function consumeBlock(int $indent):array
289
-  {
290
-    $out = [];
291
-    $m = [];
292
-    while (
293
-      $this->lineNo + 1 < $this->lineCount &&
294
-      (!trim($this->lines[$this->lineNo + 1]) ||
295
-        preg_match(
296
-          '/^(\s){' . $indent . '}((\s)+[^\s].*)$/',
297
-          $this->lines[$this->lineNo + 1],
298
-          $m,
299
-        ))
300
-    ) {
301
-      if (trim($this->lines[$this->lineNo + 1])) {
302
-        $out[] = $m[2];
303
-      }
304
-      $this->lineNo++;
305
-    }
306
-    return $out;
307
-  }
308
-
309
-  function indentLevel(int $indent):int
310
-  {
311
-    if (!count($this->indents)) {
312
-      $this->indents = [0 => $indent];
313
-      // Key = indent level, Value = Depth in spaces
314
-      return 0;
315
-    }
316
-    foreach ($this->indents as $k => $v) {
317
-      if ($v == $indent) {
318
-        $this->indents = array_slice($this->indents, 0, 1 + (int) $k );
319
-        return $k;
320
-      }
321
-    }
322
-    $this->indents[] = $indent;
323
-    return max(array_keys($this->indents));
324
-  }
325
-
326
-  function getLineNo():int
327
-  {
328
-    return $this->lineNo;
329
-  }
330
-}
Please login to merge, or discard this patch.