Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Removed need to explicitly end lines with backslashes inside matched …

…parens or curlies.
  • Loading branch information...
commit 45aa5fe986fc614ded46f53d8b705db8eb3bd2e7 1 parent f4e0c58
Justin Hileman authored

Showing 1 changed file with 32 additions and 6 deletions. Show diff stats Hide diff stats

  1. +32 6 PHP/Repl.php
38 PHP/Repl.php
@@ -154,9 +154,11 @@ public function run(array $scope = array())
154 154 */
155 155 private function read()
156 156 {
157   - $code = '';
158   - $done = false;
159   - $lines = 0;
  157 + $code = '';
  158 + $done = false;
  159 + $lines = 0;
  160 + $curleys = 0;
  161 + $parens = 0;
160 162 static $shifted;
161 163 if (!$shifted) {
162 164 // throw away argv[0]
@@ -179,13 +181,37 @@ private function read()
179 181 return false;
180 182 }
181 183
  184 + $done = true;
182 185 $line = trim($line);
183 186 // If the last char is a backslash, remove it and
184 187 // accumulate more lines.
185 188 if (substr($line, -1) == '\\') {
186   - $line = substr($line, 0, strlen($line) - 1);
187   - } else {
188   - $done = true;
  189 + $line = trim(substr($line, 0, strlen($line) - 1));
  190 + $done = false;
  191 + }
  192 +
  193 + // check for curleys and parens, keep accumulating lines.
  194 + $tokens = token_get_all("<?php {$line}");
  195 + foreach ($tokens as $t) {
  196 + switch ($t) {
  197 + case '{':
  198 + ++$curleys;
  199 + break;
  200 + case '}':
  201 + --$curleys;
  202 + break;
  203 + case '(':
  204 + ++$parens;
  205 + break;
  206 + case ')':
  207 + --$parens;
  208 + break;
  209 + }
  210 + }
  211 + if ($curleys > 0) {
  212 + $done = false;
  213 + } else if ($parens > 0) {
  214 + $done = false;
189 215 }
190 216 $code .= $line;
191 217 $lines++;

0 comments on commit 45aa5fe

Please sign in to comment.
Something went wrong with that request. Please try again.