logica
Version:
a compile-to-javascript predicate logic language
396 lines (387 loc) • 17.6 kB
JavaScript
/* Jison generated parser */
var parser = (function(){
var parser = {trace: function trace() { },
yy: {},
symbols_: {"error":2,"Program":3,"Operation":4,"EOF":5,"BOOLEAN":6,"(":7,"Complement":8,"BoolOperand":9,")":10,"Combinator":11,"BoolOperands":12,"Value":13,"Operator":14,"Operands":15,"Comparator":16,"Operand":17,"NOT":18,"AND":19,"OR":20,"EQ":21,"GT":22,"LT":23,"GTE":24,"LTE":25,"IN":26,"SYMBOL":27,"Boolean":28,"Literal":29,"NUMBER":30,"STRING":31,"NULL":32,"$accept":0,"$end":1},
terminals_: {2:"error",5:"EOF",6:"BOOLEAN",7:"(",10:")",18:"NOT",19:"AND",20:"OR",21:"EQ",22:"GT",23:"LT",24:"GTE",25:"LTE",26:"IN",27:"SYMBOL",30:"NUMBER",31:"STRING",32:"NULL"},
productions_: [0,[3,2],[3,2],[4,4],[4,4],[4,5],[4,3],[4,5],[15,2],[15,1],[8,1],[11,1],[11,1],[16,1],[16,1],[16,1],[16,1],[16,1],[16,1],[14,1],[14,1],[28,1],[29,1],[29,1],[29,1],[29,1],[13,1],[13,1],[12,2],[12,1],[9,1],[9,1],[9,1],[17,1],[17,1]],
performAction: function anonymous(yytext,yyleng,yylineno,yy,yystate,$$,_$) {
var $0 = $$.length - 1;
switch (yystate) {
case 1: yy.AST = this.$;
break;
case 2: yy.AST = {type: 'BOOLEAN', val: this.$ }
break;
case 3:this.$ = {type: 'COMBINATION', combinator:$$[$0-2], operands: [$$[$0-1]]};
break;
case 4:this.$ = {type: 'COMBINATION', combinator:$$[$0-2], operands: $$[$0-1]};
break;
case 5:this.$ = {type: 'OPERATION', headOperand: $$[$0-3], operator:$$[$0-2], tailOperands: $$[$0-1]};
break;
case 7:this.$ = {type: 'OPERATION', headOperand: $$[$0-2], operator:$$[$0-3], tailOperands: $$[$0-1]};
break;
case 8:this.$ = [$$[$0-1], $$[$0]];
break;
case 9:this.$ = [$$[$0]];
break;
case 10:this.$ = 'NOT';
break;
case 11:this.$ = 'AND';
break;
case 12:this.$ = 'OR';
break;
case 13:this.$ = 'EQ';
break;
case 14:this.$ = 'GT';
break;
case 15:this.$ = 'LT';
break;
case 16:this.$ = 'GTE';
break;
case 17:this.$ = 'LTE';
break;
case 18:this.$ = 'IN';
break;
case 21:this.$ = {type: 'BOOLEAN', val: JSON.parse($$[$0])};
break;
case 22:this.$ = {type: 'NUMBER', val: $$[$0]};
break;
case 23:this.$ = {type: 'STRING', val: $$[$0]};
break;
case 25:this.$ = {type: 'NULL', val: null};
break;
case 27:this.$ = {type: 'SYMBOL', val: $$[$0]};
break;
case 28:this.$ = [$$[$0-1], $$[$0]];
break;
case 29:this.$ = [$$[$0]];
break;
case 30:this.$ = {type: 'SYMBOL', val: $$[$0]};
break;
}
},
table: [{3:1,4:2,6:[1,3],7:[1,4]},{1:[3]},{5:[1,5]},{5:[1,6]},{6:[1,26],8:7,11:8,13:9,16:10,18:[1,11],19:[1,12],20:[1,13],21:[1,16],22:[1,17],23:[1,18],24:[1,19],25:[1,20],26:[1,21],27:[1,15],28:24,29:14,30:[1,22],31:[1,23],32:[1,25]},{1:[2,1]},{1:[2,2]},{4:30,6:[1,26],7:[1,4],9:27,27:[1,28],28:29},{4:30,6:[1,26],7:[1,4],9:32,12:31,27:[1,28],28:29},{10:[1,34],14:33,16:35,21:[1,16],22:[1,17],23:[1,18],24:[1,19],25:[1,20],26:[1,21],27:[1,36]},{4:39,6:[1,26],7:[1,4],13:38,17:37,27:[1,15],28:24,29:14,30:[1,22],31:[1,23],32:[1,25]},{6:[2,10],7:[2,10],27:[2,10]},{6:[2,11],7:[2,11],27:[2,11]},{6:[2,12],7:[2,12],27:[2,12]},{6:[2,26],7:[2,26],10:[2,26],21:[2,26],22:[2,26],23:[2,26],24:[2,26],25:[2,26],26:[2,26],27:[2,26],30:[2,26],31:[2,26],32:[2,26]},{6:[2,27],7:[2,27],10:[2,27],21:[2,27],22:[2,27],23:[2,27],24:[2,27],25:[2,27],26:[2,27],27:[2,27],30:[2,27],31:[2,27],32:[2,27]},{6:[2,13],7:[2,13],27:[2,13],30:[2,13],31:[2,13],32:[2,13]},{6:[2,14],7:[2,14],27:[2,14],30:[2,14],31:[2,14],32:[2,14]},{6:[2,15],7:[2,15],27:[2,15],30:[2,15],31:[2,15],32:[2,15]},{6:[2,16],7:[2,16],27:[2,16],30:[2,16],31:[2,16],32:[2,16]},{6:[2,17],7:[2,17],27:[2,17],30:[2,17],31:[2,17],32:[2,17]},{6:[2,18],7:[2,18],27:[2,18],30:[2,18],31:[2,18],32:[2,18]},{6:[2,22],7:[2,22],10:[2,22],21:[2,22],22:[2,22],23:[2,22],24:[2,22],25:[2,22],26:[2,22],27:[2,22],30:[2,22],31:[2,22],32:[2,22]},{6:[2,23],7:[2,23],10:[2,23],21:[2,23],22:[2,23],23:[2,23],24:[2,23],25:[2,23],26:[2,23],27:[2,23],30:[2,23],31:[2,23],32:[2,23]},{6:[2,24],7:[2,24],10:[2,24],21:[2,24],22:[2,24],23:[2,24],24:[2,24],25:[2,24],26:[2,24],27:[2,24],30:[2,24],31:[2,24],32:[2,24]},{6:[2,25],7:[2,25],10:[2,25],21:[2,25],22:[2,25],23:[2,25],24:[2,25],25:[2,25],26:[2,25],27:[2,25],30:[2,25],31:[2,25],32:[2,25]},{6:[2,21],7:[2,21],10:[2,21],21:[2,21],22:[2,21],23:[2,21],24:[2,21],25:[2,21],26:[2,21],27:[2,21],30:[2,21],31:[2,21],32:[2,21]},{10:[1,40]},{6:[2,30],7:[2,30],10:[2,30],27:[2,30]},{6:[2,31],7:[2,31],10:[2,31],27:[2,31]},{6:[2,32],7:[2,32],10:[2,32],27:[2,32]},{4:30,6:[1,26],7:[1,4],9:42,10:[1,41],27:[1,28],28:29},{6:[2,29],7:[2,29],10:[2,29],27:[2,29]},{4:39,6:[1,26],7:[1,4],13:38,15:43,17:44,27:[1,15],28:24,29:14,30:[1,22],31:[1,23],32:[1,25]},{5:[2,6],6:[2,6],7:[2,6],10:[2,6],27:[2,6],30:[2,6],31:[2,6],32:[2,6]},{6:[2,19],7:[2,19],27:[2,19],30:[2,19],31:[2,19],32:[2,19]},{6:[2,20],7:[2,20],27:[2,20],30:[2,20],31:[2,20],32:[2,20]},{4:39,6:[1,26],7:[1,4],13:38,15:45,17:44,27:[1,15],28:24,29:14,30:[1,22],31:[1,23],32:[1,25]},{6:[2,33],7:[2,33],10:[2,33],27:[2,33],30:[2,33],31:[2,33],32:[2,33]},{6:[2,34],7:[2,34],10:[2,34],27:[2,34],30:[2,34],31:[2,34],32:[2,34]},{5:[2,3],6:[2,3],7:[2,3],10:[2,3],27:[2,3],30:[2,3],31:[2,3],32:[2,3]},{5:[2,4],6:[2,4],7:[2,4],10:[2,4],27:[2,4],30:[2,4],31:[2,4],32:[2,4]},{6:[2,28],7:[2,28],10:[2,28],27:[2,28]},{4:39,6:[1,26],7:[1,4],10:[1,46],13:38,17:47,27:[1,15],28:24,29:14,30:[1,22],31:[1,23],32:[1,25]},{6:[2,9],7:[2,9],10:[2,9],27:[2,9],30:[2,9],31:[2,9],32:[2,9]},{4:39,6:[1,26],7:[1,4],10:[1,48],13:38,17:47,27:[1,15],28:24,29:14,30:[1,22],31:[1,23],32:[1,25]},{5:[2,5],6:[2,5],7:[2,5],10:[2,5],27:[2,5],30:[2,5],31:[2,5],32:[2,5]},{6:[2,8],7:[2,8],10:[2,8],27:[2,8],30:[2,8],31:[2,8],32:[2,8]},{5:[2,7],6:[2,7],7:[2,7],10:[2,7],27:[2,7],30:[2,7],31:[2,7],32:[2,7]}],
defaultActions: {5:[2,1],6:[2,2]},
parseError: function parseError(str, hash) {
throw new Error(str);
},
parse: function parse(input) {
var self = this, stack = [0], vstack = [null], lstack = [], table = this.table, yytext = "", yylineno = 0, yyleng = 0, recovering = 0, TERROR = 2, EOF = 1;
this.lexer.setInput(input);
this.lexer.yy = this.yy;
this.yy.lexer = this.lexer;
this.yy.parser = this;
if (typeof this.lexer.yylloc == "undefined")
this.lexer.yylloc = {};
var yyloc = this.lexer.yylloc;
lstack.push(yyloc);
var ranges = this.lexer.options && this.lexer.options.ranges;
if (typeof this.yy.parseError === "function")
this.parseError = this.yy.parseError;
function popStack(n) {
stack.length = stack.length - 2 * n;
vstack.length = vstack.length - n;
lstack.length = lstack.length - n;
}
function lex() {
var token;
token = self.lexer.lex() || 1;
if (typeof token !== "number") {
token = self.symbols_[token] || token;
}
return token;
}
var symbol, preErrorSymbol, state, action, a, r, yyval = {}, p, len, newState, expected;
while (true) {
state = stack[stack.length - 1];
if (this.defaultActions[state]) {
action = this.defaultActions[state];
} else {
if (symbol === null || typeof symbol == "undefined") {
symbol = lex();
}
action = table[state] && table[state][symbol];
}
if (typeof action === "undefined" || !action.length || !action[0]) {
var errStr = "";
if (!recovering) {
expected = [];
for (p in table[state])
if (this.terminals_[p] && p > 2) {
expected.push("'" + this.terminals_[p] + "'");
}
if (this.lexer.showPosition) {
errStr = "Parse error on line " + (yylineno + 1) + ":\n" + this.lexer.showPosition() + "\nExpecting " + expected.join(", ") + ", got '" + (this.terminals_[symbol] || symbol) + "'";
} else {
errStr = "Parse error on line " + (yylineno + 1) + ": Unexpected " + (symbol == 1?"end of input":"'" + (this.terminals_[symbol] || symbol) + "'");
}
this.parseError(errStr, {text: this.lexer.match, token: this.terminals_[symbol] || symbol, line: this.lexer.yylineno, loc: yyloc, expected: expected});
}
}
if (action[0] instanceof Array && action.length > 1) {
throw new Error("Parse Error: multiple actions possible at state: " + state + ", token: " + symbol);
}
switch (action[0]) {
case 1:
stack.push(symbol);
vstack.push(this.lexer.yytext);
lstack.push(this.lexer.yylloc);
stack.push(action[1]);
symbol = null;
if (!preErrorSymbol) {
yyleng = this.lexer.yyleng;
yytext = this.lexer.yytext;
yylineno = this.lexer.yylineno;
yyloc = this.lexer.yylloc;
if (recovering > 0)
recovering--;
} else {
symbol = preErrorSymbol;
preErrorSymbol = null;
}
break;
case 2:
len = this.productions_[action[1]][1];
yyval.$ = vstack[vstack.length - len];
yyval._$ = {first_line: lstack[lstack.length - (len || 1)].first_line, last_line: lstack[lstack.length - 1].last_line, first_column: lstack[lstack.length - (len || 1)].first_column, last_column: lstack[lstack.length - 1].last_column};
if (ranges) {
yyval._$.range = [lstack[lstack.length - (len || 1)].range[0], lstack[lstack.length - 1].range[1]];
}
r = this.performAction.call(yyval, yytext, yyleng, yylineno, this.yy, action[1], vstack, lstack);
if (typeof r !== "undefined") {
return r;
}
if (len) {
stack = stack.slice(0, -1 * len * 2);
vstack = vstack.slice(0, -1 * len);
lstack = lstack.slice(0, -1 * len);
}
stack.push(this.productions_[action[1]][0]);
vstack.push(yyval.$);
lstack.push(yyval._$);
newState = table[stack[stack.length - 2]][stack[stack.length - 1]];
stack.push(newState);
break;
case 3:
return true;
}
}
return true;
}
};
/* Jison generated lexer */
var lexer = (function(){
var lexer = ({EOF:1,
parseError:function parseError(str, hash) {
if (this.yy.parser) {
this.yy.parser.parseError(str, hash);
} else {
throw new Error(str);
}
},
setInput:function (input) {
this._input = input;
this._more = this._less = this.done = false;
this.yylineno = this.yyleng = 0;
this.yytext = this.matched = this.match = '';
this.conditionStack = ['INITIAL'];
this.yylloc = {first_line:1,first_column:0,last_line:1,last_column:0};
if (this.options.ranges) this.yylloc.range = [0,0];
this.offset = 0;
return this;
},
input:function () {
var ch = this._input[0];
this.yytext += ch;
this.yyleng++;
this.offset++;
this.match += ch;
this.matched += ch;
var lines = ch.match(/(?:\r\n?|\n).*/g);
if (lines) {
this.yylineno++;
this.yylloc.last_line++;
} else {
this.yylloc.last_column++;
}
if (this.options.ranges) this.yylloc.range[1]++;
this._input = this._input.slice(1);
return ch;
},
unput:function (ch) {
var len = ch.length;
var lines = ch.split(/(?:\r\n?|\n)/g);
this._input = ch + this._input;
this.yytext = this.yytext.substr(0, this.yytext.length-len-1);
//this.yyleng -= len;
this.offset -= len;
var oldLines = this.match.split(/(?:\r\n?|\n)/g);
this.match = this.match.substr(0, this.match.length-1);
this.matched = this.matched.substr(0, this.matched.length-1);
if (lines.length-1) this.yylineno -= lines.length-1;
var r = this.yylloc.range;
this.yylloc = {first_line: this.yylloc.first_line,
last_line: this.yylineno+1,
first_column: this.yylloc.first_column,
last_column: lines ?
(lines.length === oldLines.length ? this.yylloc.first_column : 0) + oldLines[oldLines.length - lines.length].length - lines[0].length:
this.yylloc.first_column - len
};
if (this.options.ranges) {
this.yylloc.range = [r[0], r[0] + this.yyleng - len];
}
return this;
},
more:function () {
this._more = true;
return this;
},
less:function (n) {
this.unput(this.match.slice(n));
},
pastInput:function () {
var past = this.matched.substr(0, this.matched.length - this.match.length);
return (past.length > 20 ? '...':'') + past.substr(-20).replace(/\n/g, "");
},
upcomingInput:function () {
var next = this.match;
if (next.length < 20) {
next += this._input.substr(0, 20-next.length);
}
return (next.substr(0,20)+(next.length > 20 ? '...':'')).replace(/\n/g, "");
},
showPosition:function () {
var pre = this.pastInput();
var c = new Array(pre.length + 1).join("-");
return pre + this.upcomingInput() + "\n" + c+"^";
},
next:function () {
if (this.done) {
return this.EOF;
}
if (!this._input) this.done = true;
var token,
match,
tempMatch,
index,
col,
lines;
if (!this._more) {
this.yytext = '';
this.match = '';
}
var rules = this._currentRules();
for (var i=0;i < rules.length; i++) {
tempMatch = this._input.match(this.rules[rules[i]]);
if (tempMatch && (!match || tempMatch[0].length > match[0].length)) {
match = tempMatch;
index = i;
if (!this.options.flex) break;
}
}
if (match) {
lines = match[0].match(/(?:\r\n?|\n).*/g);
if (lines) this.yylineno += lines.length;
this.yylloc = {first_line: this.yylloc.last_line,
last_line: this.yylineno+1,
first_column: this.yylloc.last_column,
last_column: lines ? lines[lines.length-1].length-lines[lines.length-1].match(/\r?\n?/)[0].length : this.yylloc.last_column + match[0].length};
this.yytext += match[0];
this.match += match[0];
this.matches = match;
this.yyleng = this.yytext.length;
if (this.options.ranges) {
this.yylloc.range = [this.offset, this.offset += this.yyleng];
}
this._more = false;
this._input = this._input.slice(match[0].length);
this.matched += match[0];
token = this.performAction.call(this, this.yy, this, rules[index],this.conditionStack[this.conditionStack.length-1]);
if (this.done && this._input) this.done = false;
if (token) return token;
else return;
}
if (this._input === "") {
return this.EOF;
} else {
return this.parseError('Lexical error on line '+(this.yylineno+1)+'. Unrecognized text.\n'+this.showPosition(),
{text: "", token: null, line: this.yylineno});
}
},
lex:function lex() {
var r = this.next();
if (typeof r !== 'undefined') {
return r;
} else {
return this.lex();
}
},
begin:function begin(condition) {
this.conditionStack.push(condition);
},
popState:function popState() {
return this.conditionStack.pop();
},
_currentRules:function _currentRules() {
return this.conditions[this.conditionStack[this.conditionStack.length-1]].rules;
},
topState:function () {
return this.conditionStack[this.conditionStack.length-2];
},
pushState:function begin(condition) {
this.begin(condition);
}});
lexer.options = {};
lexer.performAction = function anonymous(yy,yy_,$avoiding_name_collisions,YY_START) {
var YYSTATE=YY_START
switch($avoiding_name_collisions) {
case 0:/* skip comment */
break;
case 1:/* commas and whitespace are delimitors */
break;
case 2:return 7;
break;
case 3:return 10;
break;
case 4:return 19;
break;
case 5:return 20;
break;
case 6:return 18;
break;
case 7:return 25;
break;
case 8:return 24;
break;
case 9:return 22;
break;
case 10:return 23;
break;
case 11:return 21;
break;
case 12:return 26;
break;
case 13:return 31;
break;
case 14:return 30;
break;
case 15:return 6;
break;
case 16:return 32;
break;
case 17:return 27;
break;
case 18:return 5;
break;
}
};
lexer.rules = [/^(?:#[^\n]*)/,/^(?:,\s*|\s+)/,/^(?:\()/,/^(?:\))/,/^(?:AND|and\b)/,/^(?:OR|or\b)/,/^(?:NOT|not\b)/,/^(?:<=|LTE|lte\b)/,/^(?:>=|GTE|gte\b)/,/^(?:>|GT|gt\b)/,/^(?:<|LT|lt\b)/,/^(?:=|EQ|eq\b)/,/^(?:IN|in\b)/,/^(?:'.*?'|".*?")/,/^(?:[0-9]+(\.[0-9]+)?\b)/,/^(?:true|TRUE|false|FALSE\b)/,/^(?:NULL|null\b)/,/^(?:[a-zA-Z]+[a-zA-Z0-9]*)/,/^(?:$)/];
lexer.conditions = {"INITIAL":{"rules":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18],"inclusive":true}};
return lexer;})()
parser.lexer = lexer;
function Parser () { this.yy = {}; }Parser.prototype = parser;parser.Parser = Parser;
return new Parser;
})();
module.exports.parser = parser;
module.exports.Parser = parser.Parser;
module.exports.parse = function () { return parser.parse.apply(parser, arguments); }