Skip to content

Commit

Permalink
optimize lexer code for speed smarty-php/smarty#311
Browse files Browse the repository at this point in the history
  • Loading branch information
uwetews committed Nov 6, 2016
1 parent 04496b4 commit cd37883
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions LexerGenerator/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,10 @@ public function doFirstMatch($rules, $statename, $ruleindex)
if (!isset($this->yy_global_pattern' . $ruleindex . ')) {
$this->yy_global_pattern' . $ruleindex . ' = "' . $pattern . 'isS";
}
if (' . $this->counter . ' >= strlen(' . $this->input . ')) {
if (!isset($this->dataLenght)) {
$this->dataLenght = strlen(' . $this->input . ');
}
if (' . $this->counter . ' >= $this->dataLenght) {
return false; // end of input
}
');
Expand All @@ -359,13 +362,8 @@ public function doFirstMatch($rules, $statename, $ruleindex)
if (preg_match($this->yy_global_pattern' . $ruleindex . ',' . $this->input . ', $yymatches, null, ' .
$this->counter .
')) {
$yysubmatches = $yymatches;
if (strlen($yysubmatches[0]) < 200) {
$yymatches = preg_grep("/(.|\s)+/", $yysubmatches);
} else {
$yymatches = array_filter($yymatches, \'strlen\');
}
if (empty($yymatches)) {
$yymatches = array_filter($yymatches);
if (empty($yymatches)) {
throw new Exception(\'Error: lexing failed because a rule matched\' .
\' an empty string. Input "\' . substr(' . $this->input . ',
' . $this->counter . ', 5) . \'... state ' . $statename . '\');
Expand All @@ -386,7 +384,7 @@ public function doFirstMatch($rules, $statename, $ruleindex)
} elseif ($r === false) {
' . $this->counter . ' += strlen(' . $this->value . ');
' . $this->line . ' += substr_count(' . $this->value . ', "\n");
if (' . $this->counter . ' >= strlen(' . $this->input . ')) {
if (' . $this->counter . ' >= $this->dataLenght) {
return false; // end of input
}
// skip this token
Expand Down Expand Up @@ -435,7 +433,7 @@ public function doFirstMatch($rules, $statename, $ruleindex)
} elseif ($r === false) {
' . $this->counter . ' += strlen(' . $this->value . ');
' . $this->line . ' += substr_count(' . $this->value . ', "\n");
if (' . $this->counter . ' >= strlen(' . $this->input . ')) {
if (' . $this->counter . ' >= $this->dataLenght) {
return false; // end of input
}
// skip this token
Expand Down

0 comments on commit cd37883

Please sign in to comment.