Skip to content

Commit

Permalink
Merge pull request #113 from robertleeplummerjr/master
Browse files Browse the repository at this point in the history
Updating php port to latest
  • Loading branch information
zaach committed Sep 10, 2012
2 parents bdc9422 + ab02e2a commit 746a829
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 18 deletions.
3 changes: 3 additions & 0 deletions ports/php/jison.js
Expand Up @@ -37,6 +37,7 @@ exec("jison " + process.argv[2], function(error) {
rules = rules.substring(1, rules.length - 1); rules = rules.substring(1, rules.length - 1);


var conditions = Parser.parser.lexer.conditions; var conditions = Parser.parser.lexer.conditions;
var options = JSON.stringify(Parser.parser.lexer.options);
var parserPerformAction = Parser.parser.performAction.toString(); var parserPerformAction = Parser.parser.performAction.toString();
var lexerPerformAction = Parser.parser.lexer.performAction.toString(); var lexerPerformAction = Parser.parser.lexer.performAction.toString();


Expand Down Expand Up @@ -126,6 +127,8 @@ exec("jison " + process.argv[2], function(error) {
.replace('"<@@RULES@@>"', 'array(' + rules + ')') .replace('"<@@RULES@@>"', 'array(' + rules + ')')
.replace('"<@@CONDITIONS@@>"', "json_decode('" + JSON.stringify(conditions) + "', true)") .replace('"<@@CONDITIONS@@>"', "json_decode('" + JSON.stringify(conditions) + "', true)")


.replace('"<@@OPTIONS@@>"', "json_decode('" + (options) + "', true)")

.replace('"<@@PARSER_PERFORM_ACTION@@>";', jsPerformActionToPhp(parserPerformAction)) .replace('"<@@PARSER_PERFORM_ACTION@@>";', jsPerformActionToPhp(parserPerformAction))
.replace('"<@@LEXER_PERFORM_ACTION@@>";', jsPerformActionToPhp(lexerPerformAction)); .replace('"<@@LEXER_PERFORM_ACTION@@>";', jsPerformActionToPhp(lexerPerformAction));


Expand Down
83 changes: 65 additions & 18 deletions ports/php/template.php
Expand Up @@ -8,7 +8,7 @@ class Parser
var $productions_ = array(); var $productions_ = array();
var $table = array(); var $table = array();
var $defaultActions = array(); var $defaultActions = array();
var $version = '0.3.6'; var $version = '0.3.12';
var $debug = false; var $debug = false;


function __construct() function __construct()
Expand Down Expand Up @@ -267,7 +267,12 @@ function parse($input)
var $yytext = ""; var $yytext = "";
var $match = ""; var $match = "";
var $matched = ""; var $matched = "";
var $yyloc = array(); var $yyloc = array(
"first_line"=> 1,
"first_column"=> 0,
"last_line"=> 1,
"last_column"=> 0
);
var $conditionsStack = array(); var $conditionsStack = array();
var $conditionStackCount = 0; var $conditionStackCount = 0;
var $rules = array(); var $rules = array();
Expand All @@ -277,6 +282,7 @@ function parse($input)
var $more; var $more;
var $_input; var $_input;
var $options; var $options;
var $offset;


function setInput($input) function setInput($input)
{ {
Expand All @@ -285,30 +291,68 @@ function setInput($input)
$this->yylineno = $this->yyleng = 0; $this->yylineno = $this->yyleng = 0;
$this->yytext = $this->matched = $this->match = ''; $this->yytext = $this->matched = $this->match = '';
$this->conditionStack = array('INITIAL'); $this->conditionStack = array('INITIAL');
$this->yyloc = array( $this->yyloc["first_line"] = 1;
"first_line"=> 1, $this->yyloc["first_column"] = 0;
"first_column"=> 0, $this->yyloc["last_line"] = 1;
"last_line"=> 1, $this->yyloc["last_column"] = 0;
"last_column"=> 0 if ($this->options->ranges) {
); $this->yyloc['range'] = array(0,0);
}
$this->offset = 0;
return $this;
} }


function input() function input()
{ {
$ch = $this->_input[0]; $ch = $this->_input[0];
$this->yytext .= $ch; $this->yytext .= $ch;
$this->yyleng++; $this->yyleng++;
$this->offset++;
$this->match .= $ch; $this->match .= $ch;
$this->matched .= $ch; $this->matched .= $ch;
$lines = preg_match("/\n/", $ch); $lines = preg_match("/(?:\r\n?|\n).*/", $ch);
if (count($lines) > 0) $this->yylineno++; if (count($lines) > 0) {
array_slice($this->_input, 1); $this->yylineno++;
$this->yyloc['last_line']++;
} else {
$this->yyloc['last_column']++;
}
if ($this->options->ranges) $this->yyloc['range'][1]++;

$this->_input = array_slice($this->_input, 1);
return $ch; return $ch;
} }


function unput($ch) function unput($ch)
{ {
$len = strlen($ch);
$lines = explode("/(?:\r\n?|\n)/", $ch);
$linesCount = count($lines);

$this->_input = $ch . $this->_input; $this->_input = $ch . $this->_input;
$this->yytext = substr($this->yytext, 0, $len - 1);
//$this->yylen -= $len;
$this->offset -= $len;
$oldLines = explode("/(?:\r\n?|\n)/", $this->match);
$oldLinesCount = count($oldLines);
$this->match = substr($this->match, 0, strlen($this->match) - 1);
$this->matched = substr($this->matched, 0, strlen($this->matched) - 1);

if (($linesCount - 1) > 0) $this->yylineno -= $linesCount - 1;
$r = $this->yyloc['range'];
$oldLinesLength = (isset($oldLines[$oldLinesCount - $linesCount]) ? strlen($oldLines[$oldLinesCount - $linesCount]) : 0);

$this->yyloc["first_line"] = $this->yyloc["first_line"];
$this->yyloc["last_line"] = $this->yylineno + 1;
$this->yyloc["first_column"] = $this->yyloc['first_column'];
$this->yyloc["last_column"] = (empty($lines) ?
($linesCount == $oldLinesCount ? $this->yyloc['first_column'] : 0) + $oldLinesLength :
$this->yyloc['first_column'] - $len);

if (isset($this->options->ranges)) {
$this->yyloc['range'] = array($r[0], $r[0] + $this->yyleng - $len);
}

return $this; return $this;
} }


Expand Down Expand Up @@ -369,16 +413,19 @@ function next()
$matchCount = strlen($match[0]); $matchCount = strlen($match[0]);
$lineCount = preg_match("/\n.*/", $match[0], $lines); $lineCount = preg_match("/\n.*/", $match[0], $lines);


if ($lineCount > 1) $this->yylineno += $lineCount; $this->yylineno += $lineCount;
$this->yyloc = array( $this->yyloc["first_line"] = $this->yyloc['last_line'];
"first_line"=> $this->yyloc['last_line'], $this->yyloc["last_line"] = $this->yylineno + 1;
"last_line"=> $this->yylineno + 1, $this->yyloc["first_column"] = $this->yyloc['last_column'];
"first_column"=> $this->yyloc['last_column'], $this->yyloc["last_column"] = $lines ? count($lines[$lineCount - 1]) - 1 : $this->yyloc['last_column'] + $matchCount;
"last_column"=> $lines ? count($lines[$lineCount - 1]) - 1 : $this->yyloc['last_column'] + $matchCount
);
$this->yytext .= $match[0]; $this->yytext .= $match[0];
$this->match .= $match[0]; $this->match .= $match[0];
$this->matches = $match;
$this->yyleng = strlen($this->yytext); $this->yyleng = strlen($this->yytext);
if ($this->options.ranges) {
$this->yyloc['range'] = array($this->offset, $this->offset += $this->yyleng);
}
$this->more = false; $this->more = false;
$this->_input = substr($this->_input, $matchCount, strlen($this->_input)); $this->_input = substr($this->_input, $matchCount, strlen($this->_input));
$this->matched .= $match[0]; $this->matched .= $match[0];
Expand Down

0 comments on commit 746a829

Please sign in to comment.