Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ports/php/example/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
test: compile
node filter.js example
php test.php

compile:
node ../php.js filter.jison
1 change: 1 addition & 0 deletions ports/php/example/example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'Hello world' LIKE ['helo','123','3453']
280 changes: 280 additions & 0 deletions ports/php/example/filter.jison
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@

/* description: Parses end executes mathematical expressions with strings. */

/* lexical grammar */
%lex
%%

\s+ /* skip whitespace */
[0-9]+("."[0-9]+)?\b return 'NUMBER';
"*" return '*';
"/" return '/';
"-" return '-';
"+" return '+';
"^" return '^';
"!" return '!';
"%" return '%';
"(" return '(';
")" return ')';
"[" return '[';
"]" return ']';
"," return ',';
"PI" return 'PI';
"E" return 'E';
"SUM" return 'SUM';
"LIKE" return 'LIKE';
\"([^"]*)\" %{
//js
yytext = yytext.substr(1,yyleng-2);
return 'STRING';
/*php
$yytext = substr($yytext, 1, strlen($yytext) - 2);
return 'STRING';
*/
%}
\'([^']*)\' %{
//js
yytext = yytext.substr(1,yyleng-2);
return 'STRING';
/*php
$yytext = substr($yytext, 1, strlen($yytext) - 2);
return 'STRING';
*/
%}
'AND' return 'AND';
'OR' return 'OR';
'NOT' return 'NOT';
'>' return '>';
'<' return '<';
'=' return '=';
'<>' return '<>';
'>=' return '>=';
'=>' return '>=';
'<=' return '=<';
'=<' return '=<';
'true' return 'true';
'false' return 'false';
<<EOF>> return 'EOF';
. return 'INVALID';

/lex

/* operator associations and precedence */

%right ','
%left 'OR',
%left 'AND'
%left 'NOT'
%left '>','<','=<','>=', '<>','='
%left '+' '-'
%left '*' '/'
%left '^'
%right '!'
%right '%'
%left UMINUS

%start expressions

%% /* language grammar */
expressions
: e EOF
{
//js
typeof console !== 'undefined' ? console.log($1) : print($1);
return $1;
/*php
return $1;
*/
}
;

sub_list
: sub_list ',' e
{
//js
var A = $1;
A.push($3);
$$ = A;
/*php
$a = $1;
array_push($a, $3);
$$ = $a;
*/
}
| e
{
//js
$$ = [ $1 ];
//php $$ = array($1);
}
;

list
: '[' sub_list ']'
{
$$ = $2;
}
;

e
: e '+' e
{
$$ = ( $1 + $3 );
}
| e '-' e
{
$$ = ($1 - $3);
}
| e '*' e
{
$$ = ( $1*$3 );
}

| e '/' e
{
$$ = ( $1 /$3 );
}
| e '^' e
{
//js
$$ = Math.pow($1, $3);
//php $$ = pow( $1, $3);
}
| e '!'
{
//js
$$ = (function fact (n) { return n==0 ? 1 : fact(n-1) * n })($1);
/*php
$f = function($n) use (&$f) { return ($n<1) ? 1 : ($f($n-1) * $n); };
$$ = $f((int) $1);
*/
}
| e '%'
{
$$ = $1/100;
}
| '-' e %prec UMINUS
{
$$ = -$2;
}
| '(' e ')'
{
//js
$$ = $2;
//php $$ = $2;
}
| NUMBER
{
//js
$$ = Number(yytext);
//php $$ = $1;
}
| STRING
{
//js
$$ = yytext;
//php $$ = $1;
}
| E
{
//js
$$ = Math.E;
//php $$ = exp(1);
}
| PI
{
//js
$$ = Math.PI;
//php $$ = 3.141592653589793;
}
| 'SUM' list
{
//js
var sum = 0;
$2.forEach(function(item) { sum+= Number(item); });
$$ = sum;
/*php
$sum = 0;
foreach($2 as $value) {
$sum += $value;
}
$$ = $sum;
*/
}
| e 'LIKE' list
{
//js
var flag = false;
var escape = function(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
$3.forEach(function(item) {
var regexp = new RegExp(escape(item).replace('%', '(.*)'),'gi');
flag = flag || regexp.test($1);
});
$$ = flag;
/*php
// TODO: Синхронизировать метод с JS
$flag = false;
$template = $1;
foreach($3 as $item) {
$flag = $flag || (mb_stripos($template, $item,0, 'utf-8') !== false);
if ($flag) break;
}
$$ = $flag;
*/
}
| e 'AND' e
{
//js
$$ = ($1!=false) && ($3!=false);
//php $$ = ($1 !== false) && ($3 !== false);
}
| e 'OR' e
{
//js
$$ = ($1!=false) || ($3!=false);
/*php
$$ = ($1 !== false) || ($3 !== false);
*/
}
| 'NOT' e
{
//js
$$ = !($2!=false) ;
//php $$ = ! ($2 !== false) ;
}
| 'true'
{
$$ = true;
}
| 'false'
{
$$ = false;
}
| e '>' e
{
$$ = ( $1 > $3 );
}
| e '<' e
{
$$ = ( $1 < $3 );
}
| e '>=' e
{
$$ = ( $1 >= $3 );
}
| e '=<' e
{
$$ = ( $1 <= $3 );
}
| e '=' e
{
$$ = ( $1 == $3 );
}
| e '<>' e
{
$$ = ( $1 != $3 );
}
;

7 changes: 7 additions & 0 deletions ports/php/example/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
require_once 'filter.php';

$parser = new jison\filter();
$text = file_get_contents('example');
var_export($parser->parse($text));
echo "\n";
6 changes: 3 additions & 3 deletions ports/php/php.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ exec("jison " + process.argv[2], function (error) {
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('[$][$](\\[(.+?)\\])', 'g'), '$$s$1->value()');
str = str.replace(new RegExp('default[:][;]', 'g'), '');
str = str.replace(new RegExp('this[.][$]', 'g'), '$thisS');
str = str.replace(new RegExp('this[-][>]', 'g'), '$this->');
Expand Down Expand Up @@ -112,7 +112,7 @@ exec("jison " + process.argv[2], function (error) {

console.log(option);

var parserRaw = fs.readFileSync(__dirname + "/template.php", "utf8");
var parserRaw = fs.readFileSync(process.argv[3] ? process.argv[3] : (__dirname + "/template.php"), "utf8");

function parserInject() {
var result = '\n';
Expand Down Expand Up @@ -221,7 +221,7 @@ exec("jison " + process.argv[2], function (error) {
this.conditions = [];

for (var i in rules) {
this.rules.push('\t\t\t\t\t' + i + '=>"/' + rules[i].substring(1, rules[i].length - 1).replace(/"/g, '\\"') + '/"');
this.rules.push('\t\t\t\t\t' + i + '=>"' + rules[i].replace(/"/g, '\\"') + 'u"');
}

result += '\t\t\t$this->rules = array(\n\t\t\t\t\n' + this.rules.join(',\n') + '\n\t\t\t\t);\n\n';
Expand Down
2 changes: 1 addition & 1 deletion ports/php/readme
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Description: A jison wrapper that first processes a jison file and then injects

DIRECTIONS:
1. After you've downloaded jison & node.js, navigate in command line interface to the folder that contains the ".jison" file that you want to process
2. Process the ".jison" file like this "nodejs /location_of_jison/ports/php/php.js my_jison_file.jison"
2. Process the ".jison" file like this "nodejs /location_of_jison/ports/php/php.js my_jison_file.jison [path_to_template.php]"

CONFIGURATION:
Configuration takes advantage of the commenting in javascript so as not to conflict with.
Expand Down
Loading