Permalink
Browse files

Merge pull request #770 from BlaineSch/bug/parserLineFix

Update parser to recognize newlines when PHP does not provide a line
  • Loading branch information...
2 parents 8a8fe88 + d7d8da3 commit 7f6da57e06aadf76c5442a82c920a00d963fb549 @nateabele nateabele committed Dec 28, 2012
Showing with 25 additions and 4 deletions.
  1. +4 −0 analysis/Parser.php
  2. +21 −4 tests/cases/analysis/ParserTest.php
View
@@ -74,6 +74,10 @@ public static function tokenize($code, array $options = array()) {
}
}
$tokens[] = array('id' => $id, 'name' => $name, 'content' => $content, 'line' => $line);
+
+ if ($id === T_WHITESPACE) {
+ $line += count(preg_split('/\r\n|\r|\n/', $content)) - 1;
+ }
}
if ($options['wrap'] && empty($options['include'])) {
@@ -56,7 +56,12 @@ public function testFullTokenization() {
$result = Parser::tokenize('$foo = function() {};');
$this->assertEqual(11, count($result));
- $expected = array('id' => 309, 'name' => 'T_VARIABLE', 'content' => '$foo', 'line' => 1);
+ $expected = array(
+ 'id' => T_VARIABLE,
+ 'name' => 'T_VARIABLE',
+ 'content' => '$foo',
+ 'line' => 1
+ );
$this->assertEqual($expected, $result[0]);
$expected = array('id' => null, 'name' => ';', 'content' => ';', 'line' => 1);
@@ -99,9 +104,9 @@ public function testFilteredTokenization() {
$result = Parser::tokenize($code, array('include' => array('T_IF', 'T_WHILE', 'T_CATCH')));
$expected = array(
- array('id' => 318, 'name' => 'T_WHILE', 'content' => 'while', 'line' => 1),
- array('id' => 301, 'name' => 'T_IF', 'content' => 'if', 'line' => 1),
- array('id' => 338, 'name' => 'T_CATCH', 'content' => 'catch', 'line' => 3)
+ array('id' => T_WHILE, 'name' => 'T_WHILE', 'content' => 'while', 'line' => 1),
+ array('id' => T_IF, 'name' => 'T_IF', 'content' => 'if', 'line' => 1),
+ array('id' => T_CATCH, 'name' => 'T_CATCH', 'content' => 'catch', 'line' => 3)
);
$this->assertEqual($expected, $result);
}
@@ -136,6 +141,18 @@ function ($i) { return join('', $i); },
$expected = array('ClassName', 'method');
$this->assertEqual($expected, $results);
}
+
+ public function testParserGuessesLineBleed() {
+ $code = <<<EOD
+if (false) {
+ return true;
+}
+EOD;
+ $tokens = Parser::tokenize($code);
+ $this->assertIdentical('}', $tokens[13]['content']);
+ $this->assertIdentical(3, $tokens[13]['line']);
+ }
+
}
?>

0 comments on commit 7f6da57

Please sign in to comment.