Commit 912bee4a authored by RobertPlummer's avatar RobertPlummer

[NEW] Added a np state

[NEW] Created a on the fly php and js conversion script for jison language parser, needs a server side js component to run on a development system like node.js, but that is needed to run Jison anyway
[NEW] Pushed most of the functions that have to do with handling syntax/returning objects to their own libs, it is just easier that way
parent 7b0dd415
......@@ -2338,6 +2338,7 @@ lib/parser/WikiParser.jison -text
lib/parser/WikiParser.js -text
lib/parser/WikiParser.php -text
lib/parser/WikiParserTester.html -text
lib/parser/jison_tiki.js -text
lib/parser/parserlib.js -text
lib/parser/parserlib.php -text
lib/payment/behavior/cancel_cart_order.php -text
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
var fs = require('fs');
var sys = require('sys')
var exec = require('child_process').exec;
function puts(error, stdout, stderr) {
sys.puts(stdout)
}
exec("jison WikiParser.jison", function() {
var WikiParser = require('./WikiParser.js');
var symbols = JSON.stringify(WikiParser.parser.symbols_);
var terminals = JSON.stringify(WikiParser.parser.terminals_);
var productions = JSON.stringify(WikiParser.parser.productions_);
var table = JSON.stringify(WikiParser.parser.table);
var defaultActions = JSON.stringify(WikiParser.parser.defaultActions);
//turn regex into string
var rules = [];
for(var i = 0; i < WikiParser.parser.lexer.rules.length; i++) {
rules.push(WikiParser.parser.lexer.rules[i].toString());
}
rules = JSON.stringify(rules);
rules = rules.substring(1, rules.length - 1);
var conditions = JSON.stringify(WikiParser.parser.lexer.conditions);
var parserPerformAction = WikiParser.parser.performAction.toString();
var lexerPerformAction = WikiParser.parser.lexer.performAction.toString();
function jsToPhpGen(str, stripKey) {
str = str.replace(new RegExp('[\[]', 'g'), "array(");
str = str.replace(new RegExp('\]', 'g'), ")");
str = str.replace(new RegExp('[\{]', 'g'), "array(");
str = str.replace(new RegExp('[\}]', 'g'), ")");
str = str.replace(new RegExp('[:]', 'g'), "=>");
str = str.replace('$accept', 'accept');
str = str.replace('$end', 'end');
if (stripKey) {
str = str.replace(new RegExp(',"', 'g'), ',');
str = str.replace(new RegExp('"=>', 'g'), '=>');
str = str.replace(new RegExp('[\(]"', 'g'), '(');
}
return str;
}
function jsFnToPhpGen(str) {
str = str.split('{');
str.shift();
str = str.join('{');
str = str.split('}');
str.pop();
str = str.join('}');
return str;
}
function jsPerformActionToPhp(str) {
str = jsFnToPhpGen(str);
str = str.replace("var $0 = $$.length - 1;", '');
str = str.replace("var YYSTATE=YY_START", '');
str = str.replace(new RegExp('[$]0', 'g'), '$O');
str = str.replace(new RegExp('[$][$]', 'g'), '$S');
str = str.replace(new RegExp('parserlib[.]', 'g'), 'ParserLib::');
str = str.replace(new RegExp('this[.][$]', 'g'), '$thisS');
str = str.replace(new RegExp('yystate', 'g'), '$yystate');
str = str.replace(new RegExp('this[.]yy[.]', 'g'), '$this->yy->');
str = str.replace(new RegExp('this[.]', 'g'), '$this->');
str = str.replace(new RegExp('yy[_][.]yytext', 'g'), '$yy_->yytext');
str = str.replace(new RegExp('yy[.]', 'g'), '$yy->');
str = str.replace(new RegExp('\][.]', 'g'), ']->');
str = str.replace(new RegExp('\[\]', 'g'), 'array()');
str = str.replace(new RegExp('default[:][;]', 'g'), '');
str = str.replace(/(\d)\n/g, function(){
return arguments[1] + ';\n';
});
return str;
}
var parserRaw = fs.readFileSync("./parser.php", "utf8");
parserRaw = parserRaw.replace('"<@@SYMBOLS@@>"', jsToPhpGen(symbols));
parserRaw = parserRaw.replace('"<@@TERMINALS@@>"', jsToPhpGen(terminals, true));
parserRaw = parserRaw.replace('"<@@PRODUCTIONS@@>"', jsToPhpGen(productions));
parserRaw = parserRaw.replace('"<@@TABLE@@>"', jsToPhpGen(table));
parserRaw = parserRaw.replace('"<@@DEFAULT_ACTIONS@@>"', jsToPhpGen(defaultActions));
parserRaw = parserRaw.replace('"<@@RULES@@>"', 'array(' + rules + ')');
parserRaw = parserRaw.replace('"<@@CONDITIONS@@>"', jsToPhpGen(conditions));
parserRaw = parserRaw.replace('"<@@PARSER_PERFORM_ACTION@@>"', jsPerformActionToPhp(parserPerformAction));
parserRaw = parserRaw.replace('"<@@LEXER_PERFORM_ACTION@@>"', jsPerformActionToPhp(lexerPerformAction));
fs.writeFile("WikiParser.php", parserRaw, function(err) {
if (err) {
console.log("Something went bad");
} else {
console.log("Success writing new parser files WikiParser.js & WikiParser.php.");
console.log("Please Note: The php version of the jison parser is only an ATTEMPTED conversion, that being said:");
console.log("PLEASE TEST FILES BEFORE COMMITTING!");
}
});
});
\ No newline at end of file
......@@ -6,7 +6,7 @@ var parserlib = {
function Parser () {
this.lexer = new Lexer();
this.yy = {cmd: parserlib};
this.yy = {};
}
Parser.prototype = WikiParser;
......@@ -37,71 +37,102 @@ var parserlib = {
}
return '';
},
make_bold: function ($html) {
return "<strong>" + $html + "</strong>";
bold: function ($content) {
return "<strong>" + $content + "</strong>";
},
make_box: function ($html) {
return "<div style='border: solid 1px black;'>" + $html + "</div>";
box: function ($content) {
return "<div style='border: solid 1px black;'>" + $content + "</div>";
},
make_center: function ($html) {
return "<center>" + $html + "</center>";
center: function ($content) {
return "<center>" + $content + "</center>";
},
make_colortext: function ($color, $html) {
colortext: function ($content) {
var $text = $content.split(':');
var $color = $text[0];
var $html = $text[1];
return "<span style='color: #" + $color + ";'>" + $html + "</span>";
},
make_italics: function ($html) {
return "<i>" + $html + "</i>";
italics: function ($content) {
return "<i>" + $content + "</i>";
},
make_header1: function ($html) {
return "<h1>" + $html + "</h1>";
header1: function ($content) {
return "<h1>" + $content + "</h1>";
},
make_header2: function ($html) {
return "<h2>" + $html + "</h2>";
header2: function ($content) {
return "<h2>" + $content + "</h2>";
},
make_header3: function ($html) {
return "<h3>" + $html + "</h3>";
header3: function ($content) {
return "<h3>" + $content + "</h3>";
},
make_header4: function ($html) {
return "<h4>" + $html + "</h4>";
header4: function ($content) {
return "<h4>" + $content + "</h4>";
},
make_header5: function ($html) {
return "<h5>" + $html + "</h5>";
header5: function ($content) {
return "<h5>" + $content + "</h5>";
},
make_header6: function ($html) {
return "<h6>" + $html + "</h6>";
header6: function ($content) {
return "<h6>" + $content + "</h6>";
},
make_hr: function () {
hr: function () {
return "<hr />";
},
make_link: function ($href, $html) {
return "<a href='" + $href + "'>" + $html + "</a>";
link: function ($content) {
var $link = this.split(':', $content);
var $href = $content;
if (this.match(/\|/, $content)) {
$href = $link[0];
$content = $link[1];
}
return "<a href='" + $href + "'>" + $content + "</a>";
},
make_smile: function ($smile) { //this needs more tlc too
smile: function ($smile) { //this needs more tlc too
return "<img src='img/smiles/icon_" + $smile + ".gif' alt='" + $smile + "' />";
},
make_strikethrough: function ($html) {
return "<span style='text-decoration: line-through;'>" + $html + "</span>";
strikethrough: function ($content) {
return "<span style='text-decoration: line-through;'>" + $content + "</span>";
},
table: function ($content) {
var $tableContents = '';
var $rows = this.split('<br />', $content);
for(var $i = 0; $i < this.size($rows); $i++) {
$row = '';
$cells = this.split('|', $rows[$i]);
for(var $j = 0; $j < this.size($cells); $j++) {
$row += this.table_td($cells[$j]);
}
$tableContents += this.table_tr($row);
}
return "<table style='width: 100%;'>" + $tableContents + "</table>";
},
make_table: function ($html) {
return "<table style='width: 100%;'>" + $html + "</table>";
table_tr: function ($content) {
return "<tr>" + $content + "</tr>";
},
make_table_tr: function ($html) {
return "<tr>" + $html + "</tr>";
table_td: function ($content) {
return "<td>" + $content + "</td>";
},
make_table_td: function ($html) {
return "<td>" + $html + "</td>";
titlebar: function ($content) {
return "<div class='titlebar'>" + $content + "</div>";
},
make_titlebar: function ($html) {
return "<div class='titlebar'>" + $html + "</div>";
underscore: function ($content) {
return "<u>" + $content + "</u>";
},
make_underscore: function ($html) {
return "<u>" + $html + "</u>";
wikilink: function ($content) {
var $wikilink = this.split('|', $content);
var $href = $content;
if (this.match('/\|/', $content)) {
$href = $wikilink[0];
$content = $wikilink[1];
}
return "<a href='" + $href + "'>" + $content + "</a>";
},
make_wikilink: function ($href, $html) {
return "<a href='" + $href + "'>" + $html + "</a>";
html: function ($content) { //this needs some ajax tlc
return $content;
},
html: function ($html) { //this needs some ajax tlc
return $html;
formatContent: function ($content) {
return $content.replace(/\n/g, '<br />');
},
substring: function($val, $left, $right) {
return $val.substring($left, $val.length + $right);
......@@ -114,5 +145,56 @@ var parserlib = {
},
split: function ($delimiter, $string) {
return $string.split($delimiter);
},
join: function () {
var result = '';
for(var i = 0; i < arguments.length; i++) {
if (arguments[i]) result += arguments[i];
}
return result;
},
size: function($array) {
if (!$array) $array = [];
return $array.length;
},
pop: function($array) {
if (!$array) $array = [];
$array.pop();
return $array;
},
push: function ($array, $val) {
if (!$array) $array = [];
$array.push($val);
return $array;
},
shift: function($array) {
if (!$array) $array = [];
$array.shift();
return $array;
},
// start state handlers
stackPlugin: function ($yytext, $pluginStack) {
var $pluginName = this.match(/^\{([A-Z]+)/, $yytext);
var $pluginArgs = this.match(/[(].*?[)]/, $yytext);
return this.push($pluginStack, {
name: $pluginName[1],
args: $pluginArgs,
body: ''
});
},
inlinePlugin: function ($yytext) {
var $pluginName = this.match(/^\{([a-z]+)/, $yytext);
var $pluginArgs = this.match(/[ ].*?[}]|[/}]/, $yytext);
return {
name: $pluginName[1],
args: $pluginArgs,
body: ''
};
},
npState: function (npState, ifTrue, ifFalse) {
return (npState ? ifTrue : ifFalse);
}
//end state handlers
};
\ No newline at end of file
......@@ -31,22 +31,21 @@ class ParserLib extends TikiDb_Bridge
function __construct() {
include_once "WikiParser.php";
$this->parser = new WikiParser;
//private methods
$this->parser->yy->cmd = $this;
$this->parser = new WikiParser();
}
function plugin($pluginDetails) {
$name = $pluginDetails->name;
$body = $pluginDetails->body;
$args = $this->plugin_split_args($pluginDetails->args);
$pluginBodyParser = new WikiParser;
//nested parsing!
$parserlib = new ParserLib;
$pluginOutput = $pluginBodyParser->parse(
$this->plugin_execute($name, $body, $args)
$parserlib->plugin_execute(
$pluginDetails->name,
$pluginDetails->body,
$parserlib->plugin_split_args(
$pluginDetails->args
)
)
);
return $pluginOutput;
......@@ -57,93 +56,125 @@ class ParserLib extends TikiDb_Bridge
}
//Wiki Syntax Objects Parsing Start
function make_bold($html) {
return "<strong>" . $html . "</strong>";
function bold($content) {
return "<strong>" . $content . "</strong>";
}
function make_box($html) {
return "<div style='border: solid 1px black;'>" . $html . "</div>";
function box($content) {
return "<div style='border: solid 1px black;'>" . $content . "</div>";
}
function make_center($html) {
return "<center>" . $html . "</center>";
function center($content) {
return "<center>" . $content . "</center>";
}
function make_colortext($color, $html) {
return "<span style='color: #" . $color . ";'>" . $html . "</span>";
function colortext($content) {
$text = ParserLib::split(':', $content);
$color = $text[0];
$content = $text[1];
return "<span style='color: #" . $color . ";'>" . $content . "</span>";
}
function make_italics($html) {
return "<i>" . $html . "</i>";
function italics($content) {
return "<i>" . $content . "</i>";
}
function make_header1($html) {
return "<h1>" . $html . "</h1>";
function header1($content) {
return "<h1>" . $content . "</h1>";
}
function make_header2($html) {
return "<h2>" . $html . "</h2>";
function header2($content) {
return "<h2>" . $content . "</h2>";
}
function make_header3($html) {
return "<h3>" . $html . "</h3>";
function header3($content) {
return "<h3>" . $content . "</h3>";
}
function make_header4($html) {
return "<h4>" . $html . "</h4>";
function header4($content) {
return "<h4>" . $content . "</h4>";
}
function make_header5($html) {
return "<h5>" . $html . "</h5>";
function header5($content) {
return "<h5>" . $content . "</h5>";
}
function make_header6($html) {
return "<h6>" . $html . "</h6>";
function header6($content) {
return "<h6>" . $content . "</h6>";
}
function make_hr() {
function hr() {
return "<hr />";
}
function make_link($href, $html) {
return "<a href='" . $href . "'>" . $html . "</a>";
function link($content) {
$link = ParserLib::split(':', $content);
$href = $content;
if (ParserLib::match('/\|/', $content)) {
$href = $link[0];
$content = $link[1];
}
return "<a href='" . $href . "'>" . $content . "</a>";
}
function make_smile($smile) { //this needs more tlc too
function smile($smile) { //this needs more tlc too
return "<img src='img/smiles/icon_" . $smile . ".gif' alt='" . $smile . "' />";
}
function make_strikethrough($html) {
return "<span style='text-decoration: line-through;'>" . $html . "</span>";
function strikethrough($content) {
return "<span style='text-decoration: line-through;'>" . $content . "</span>";
}
function make_table($html) {
return "<table style='width: 100%;'>" . $html . "</table>";
function table($content) {
$tableContents = '';
$rows = ParserLib::split('<br />', $content);
for($i = 0; $i < count($rows); $i++) {
$row = '';
$cells = ParserLib::split('|', $rows[$i]);
for($j = 0; $j < count($cells); $j++) {
$row .= ParserLib::table_td($cells[$j]);
}
$tableContents .= ParserLib::table_tr($row);
}
return "<table style='width: 100%;'>" . $tableContents . "</table>";
}
function make_table_tr($html) {
return "<tr>" . $html . "</tr>";
function table_tr($content) {
return "<tr>" . $content . "</tr>";
}
function make_table_td($html) {
return "<td>" . $html . "</td>";
function table_td($content) {
return "<td>" . $content . "</td>";
}
function make_titlebar($html) {
return "<div class='titlebar'>" . $html . "</div>";
function titlebar($content) {
return "<div class='titlebar'>" . $content . "</div>";
}
function make_underscore($html) {
return "<u>" . $html . "</u>";
function underscore($content) {
return "<u>" . $content . "</u>";
}
function make_wikilink($href, $html) {
return "<a href='" . $href . "'>" . $html . "</a>";
function wikilink($content) {
$wikilink = ParserLib::split('|', $content);
$href = $content;
if (ParserLib::match('/\|/', $content)) {
$href = $wikilink[0];
$content = $wikilink[1];
}
return "<a href='" . $href . "'>" . $content . "</a>";
}
//Wiki Syntax Objects Parsing End
function html($html) {
return $html;
function html($content) {
return $content;
}
function formatContent($content) {
return nl2br($content);
}
//unified functions used inside parser
......@@ -152,7 +183,8 @@ class ParserLib extends TikiDb_Bridge
}
function match($pattern, $subject) {
return preg_match($pattern, $subject);
preg_match($pattern, $subject, $match);
return (!empty($match[1]) ? $match[1] : false);
}
function replace($search, $replace, $subject) {
......@@ -163,6 +195,62 @@ class ParserLib extends TikiDb_Bridge
return explode($delimiter, $string);
}
function join() {
$array = func_get_args();
return implode($array, '');
}
function size($array) {
if (empty($array)) $array = array();
return count($array);
}
function pop($array) {
if (empty($array)) $array = array();
array_pop($array);
return $array;
}
function push($array, $val) {
if (empty($array)) $array = array();
array_push($array, $val);
return $array;
}
function shift($array) {
if (empty($array)) $array = array();
array_shift($array);
return $array;
}
// start state handlers
function stackPlugin($yytext, $pluginStack) {
$pluginName = ParserLib::match('/^\{([A-Z]+)/', $yytext);
$pluginArgs = ParserLib::match('/[(].*?[)]/', $yytext);
return ParserLib::push($pluginStack, (object)array(
name=> $pluginName,
args=> $pluginArgs,
body=> ''
));
}
function inlinePlugin($yytext) {
$pluginName = ParserLib::match('/^\{([a-z]+)/', $yytext);
$pluginArgs = ParserLib::split(' ', $yytext);
$pluginArgs = ParserLib::shift($pluginArgs);
return (object)array(
name=> $pluginName,
args=> implode(' ', $pluginArgs),
body=> ''
);
}
function npState($npState, $ifTrue, $ifFalse) {
return ($npState == true ? $ifTrue : $ifFalse);
}
//end state handlers
//NEED MIGRATION
//Below here methods that need updating to new WikiParser.php that were integrated from tikilib
//*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment