Skip to content

Instantly share code, notes, and snippets.

@dalen
Created July 12, 2014 13:57
Show Gist options
  • Save dalen/d59ef0893180e9beecf2 to your computer and use it in GitHub Desktop.
Save dalen/d59ef0893180e9beecf2 to your computer and use it in GitHub Desktop.
puppetdbquery javascript
parseQuery = function () {
parser.yy.nodeQuery = true;
apiQuery = parser.parse(document.getElementById('puppetdbquery').value);
document.getElementById('apiquery').value = JSON.stringify(apiQuery);
};
/*! node-puppetdbquery 2014-05-20 */
var parser=function(){function capitalize(a){return a.charAt(0).toUpperCase()+a.slice(1).toLowerCase()}function capitalizeClass(a){return a.split("::").map(capitalize).join("::")}function comparisonOp(a,b,c,d,e){return"."==b[0]?[a,b.slice(1),c]:d?[a,["parameter",b],c]:e?[a,["fact",b],c]:["in","certname",["extract","certname",["select-facts",["and",["=","name",b],[a,"value",c]]]]]}function regExpEscape(a){return String(a).replace(/([-()\[\]{}+?*.$\^|,:#<!\\])/g,"\\$1").replace(/\x08/g,"\\x08")}function Parser(){this.yy={}}var parser={trace:function(){},yy:{},symbols_:{error:2,query:3,expression:4,EOF:5,literal_expression:6,"~":7,"!~":8,"=":9,"!=":10,">":11,">=":12,"<":13,"<=":14,not:15,and:16,or:17,"(":18,")":19,resource_expression:20,"boolean":21,string:22,number:23,Boolean:24,Number:25,String:26,resource_type_title:27,resource_parameters:28,resource_exported:29,"@@":30,"[":31,"]":32,resource_param_start:33,resource_param_end:34,"{":35,"}":36,$accept:0,$end:1},terminals_:{2:"error",5:"EOF",7:"~",8:"!~",9:"=",10:"!=",11:">",12:">=",13:"<",14:"<=",15:"not",16:"and",17:"or",18:"(",19:")",24:"Boolean",25:"Number",26:"String",30:"@@",31:"[",32:"]",35:"{",36:"}"},productions_:[0,[3,2],[3,1],[4,3],[4,3],[4,3],[4,3],[4,3],[4,3],[4,3],[4,3],[4,1],[4,2],[4,3],[4,3],[4,3],[4,1],[6,1],[6,1],[6,1],[21,1],[23,1],[22,1],[20,1],[20,2],[20,2],[20,3],[29,1],[27,4],[27,5],[28,3],[33,1],[34,1]],performAction:function(a,b,c,d,e,f){var g=f.length-1;switch(e){case 1:return f[g-1];case 2:return null;case 3:this.$=comparisonOp("~",f[g-2],f[g],d.inResource,d.nodeQuery);break;case 4:this.$=["not",comparisonOp("~",f[g-2],f[g],d.inResource,d.nodeQuery)];break;case 5:this.$=comparisonOp("=",f[g-2],f[g],d.inResource,d.nodeQuery);break;case 6:this.$=["not",comparisonOp("=",f[g-2],f[g],d.inResource,d.nodeQuery)];break;case 7:this.$=comparisonOp(">",f[g-2],f[g],d.inResource,d.nodeQuery);break;case 8:this.$=comparisonOp(">=",f[g-2],f[g],d.inResource,d.nodeQuery);break;case 9:this.$=comparisonOp("<",f[g-2],f[g],d.inResource,d.nodeQuery);break;case 10:this.$=comparisonOp("<=",f[g-2],f[g],d.inResource,d.nodeQuery);break;case 11:this.$=["~",d.nodeQuery?"name":"certname",regExpEscape(f[g])];break;case 12:this.$=["not",f[g]];break;case 13:this.$=["and",f[g-2],f[g]];break;case 14:this.$=["or",f[g-2],f[g]];break;case 15:this.$=f[g-1];break;case 16:this.$=["in",d.nodeQuery?"name":"certname",["extract","certname",["select-resources",f[g]]]];break;case 20:this.$="true"===a?!0:!1;break;case 21:this.$=Number(a);break;case 22:this.$=a;break;case 23:this.$=f[g].concat([["=","exported",!1]]);break;case 24:this.$=f[g-1].concat([["=","exported",!1],f[g]]);break;case 25:this.$=f[g].concat([["=","exported",!0]]);break;case 26:this.$=f[g-1].concat([["=","exported",!0],f[g]]);break;case 28:this.$=["and",["=","type",capitalize(f[g-3])],["=","title","class"===f[g-3].toLowerCase()?capitalizeClass(f[g-1]):f[g-1]]];break;case 29:this.$=["and",["=","type",capitalize(f[g-4])],["~","title","class"===f[g-4].toLowerCase()?capitalizeClass(f[g-1]):f[g-1]]];break;case 30:this.$=f[g-1];break;case 31:d.inResource=!0;break;case 32:d.inResource=!1}},table:[{3:1,4:2,5:[1,3],6:4,15:[1,5],18:[1,6],20:7,21:8,22:9,23:10,24:[1,13],25:[1,15],26:[1,14],27:11,29:12,30:[1,16]},{1:[3]},{5:[1,17],16:[1,18],17:[1,19]},{1:[2,2]},{5:[2,11],7:[1,20],8:[1,21],9:[1,22],10:[1,23],11:[1,24],12:[1,25],13:[1,26],14:[1,27],16:[2,11],17:[2,11],19:[2,11],36:[2,11]},{4:28,6:4,15:[1,5],18:[1,6],20:7,21:8,22:9,23:10,24:[1,13],25:[1,15],26:[1,14],27:11,29:12,30:[1,16]},{4:29,6:4,15:[1,5],18:[1,6],20:7,21:8,22:9,23:10,24:[1,13],25:[1,15],26:[1,14],27:11,29:12,30:[1,16]},{5:[2,16],16:[2,16],17:[2,16],19:[2,16],36:[2,16]},{5:[2,17],7:[2,17],8:[2,17],9:[2,17],10:[2,17],11:[2,17],12:[2,17],13:[2,17],14:[2,17],16:[2,17],17:[2,17],19:[2,17],36:[2,17]},{5:[2,18],7:[2,18],8:[2,18],9:[2,18],10:[2,18],11:[2,18],12:[2,18],13:[2,18],14:[2,18],16:[2,18],17:[2,18],19:[2,18],31:[1,30],36:[2,18]},{5:[2,19],7:[2,19],8:[2,19],9:[2,19],10:[2,19],11:[2,19],12:[2,19],13:[2,19],14:[2,19],16:[2,19],17:[2,19],19:[2,19],36:[2,19]},{5:[2,23],16:[2,23],17:[2,23],19:[2,23],28:31,33:32,35:[1,33],36:[2,23]},{22:35,26:[1,14],27:34},{5:[2,20],7:[2,20],8:[2,20],9:[2,20],10:[2,20],11:[2,20],12:[2,20],13:[2,20],14:[2,20],16:[2,20],17:[2,20],19:[2,20],36:[2,20]},{5:[2,22],7:[2,22],8:[2,22],9:[2,22],10:[2,22],11:[2,22],12:[2,22],13:[2,22],14:[2,22],16:[2,22],17:[2,22],19:[2,22],31:[2,22],32:[2,22],36:[2,22]},{5:[2,21],7:[2,21],8:[2,21],9:[2,21],10:[2,21],11:[2,21],12:[2,21],13:[2,21],14:[2,21],16:[2,21],17:[2,21],19:[2,21],36:[2,21]},{26:[2,27]},{1:[2,1]},{4:36,6:4,15:[1,5],18:[1,6],20:7,21:8,22:9,23:10,24:[1,13],25:[1,15],26:[1,14],27:11,29:12,30:[1,16]},{4:37,6:4,15:[1,5],18:[1,6],20:7,21:8,22:9,23:10,24:[1,13],25:[1,15],26:[1,14],27:11,29:12,30:[1,16]},{6:38,21:8,22:39,23:10,24:[1,13],25:[1,15],26:[1,14]},{6:40,21:8,22:39,23:10,24:[1,13],25:[1,15],26:[1,14]},{6:41,21:8,22:39,23:10,24:[1,13],25:[1,15],26:[1,14]},{6:42,21:8,22:39,23:10,24:[1,13],25:[1,15],26:[1,14]},{6:43,21:8,22:39,23:10,24:[1,13],25:[1,15],26:[1,14]},{6:44,21:8,22:39,23:10,24:[1,13],25:[1,15],26:[1,14]},{6:45,21:8,22:39,23:10,24:[1,13],25:[1,15],26:[1,14]},{6:46,21:8,22:39,23:10,24:[1,13],25:[1,15],26:[1,14]},{5:[2,12],16:[2,12],17:[2,12],19:[2,12],36:[2,12]},{16:[1,18],17:[1,19],19:[1,47]},{7:[1,49],22:48,26:[1,14]},{5:[2,24],16:[2,24],17:[2,24],19:[2,24],36:[2,24]},{4:50,6:4,15:[1,5],18:[1,6],20:7,21:8,22:9,23:10,24:[1,13],25:[1,15],26:[1,14],27:11,29:12,30:[1,16]},{15:[2,31],18:[2,31],24:[2,31],25:[2,31],26:[2,31],30:[2,31]},{5:[2,25],16:[2,25],17:[2,25],19:[2,25],28:51,33:32,35:[1,33],36:[2,25]},{31:[1,30]},{5:[2,13],16:[2,13],17:[2,13],19:[2,13],36:[2,13]},{5:[2,14],16:[1,18],17:[2,14],19:[2,14],36:[2,14]},{5:[2,3],16:[2,3],17:[2,3],19:[2,3],36:[2,3]},{5:[2,18],16:[2,18],17:[2,18],19:[2,18],36:[2,18]},{5:[2,4],16:[2,4],17:[2,4],19:[2,4],36:[2,4]},{5:[2,5],16:[2,5],17:[2,5],19:[2,5],36:[2,5]},{5:[2,6],16:[2,6],17:[2,6],19:[2,6],36:[2,6]},{5:[2,7],16:[2,7],17:[2,7],19:[2,7],36:[2,7]},{5:[2,8],16:[2,8],17:[2,8],19:[2,8],36:[2,8]},{5:[2,9],16:[2,9],17:[2,9],19:[2,9],36:[2,9]},{5:[2,10],16:[2,10],17:[2,10],19:[2,10],36:[2,10]},{5:[2,15],16:[2,15],17:[2,15],19:[2,15],36:[2,15]},{32:[1,52]},{22:53,26:[1,14]},{16:[1,18],17:[1,19],34:54,36:[1,55]},{5:[2,26],16:[2,26],17:[2,26],19:[2,26],36:[2,26]},{5:[2,28],16:[2,28],17:[2,28],19:[2,28],35:[2,28],36:[2,28]},{32:[1,56]},{5:[2,30],16:[2,30],17:[2,30],19:[2,30],36:[2,30]},{5:[2,32],16:[2,32],17:[2,32],19:[2,32],36:[2,32]},{5:[2,29],16:[2,29],17:[2,29],19:[2,29],35:[2,29],36:[2,29]}],defaultActions:{3:[2,2],16:[2,27],17:[2,1]},parseError:function(a,b){if(!b.recoverable)throw new Error(a);this.trace(a)},parse:function(a){function b(){var a;return a=c.lexer.lex()||m,"number"!=typeof a&&(a=c.symbols_[a]||a),a}var c=this,d=[0],e=[null],f=[],g=this.table,h="",i=0,j=0,k=0,l=2,m=1,n=f.slice.call(arguments,1);this.lexer.setInput(a),this.lexer.yy=this.yy,this.yy.lexer=this.lexer,this.yy.parser=this,"undefined"==typeof this.lexer.yylloc&&(this.lexer.yylloc={});var o=this.lexer.yylloc;f.push(o);var p=this.lexer.options&&this.lexer.options.ranges;this.parseError="function"==typeof this.yy.parseError?this.yy.parseError:Object.getPrototypeOf(this).parseError;for(var q,r,s,t,u,v,w,x,y,z={};;){if(s=d[d.length-1],this.defaultActions[s]?t=this.defaultActions[s]:((null===q||"undefined"==typeof q)&&(q=b()),t=g[s]&&g[s][q]),"undefined"==typeof t||!t.length||!t[0]){var A="";y=[];for(v in g[s])this.terminals_[v]&&v>l&&y.push("'"+this.terminals_[v]+"'");A=this.lexer.showPosition?"Parse error on line "+(i+1)+":\n"+this.lexer.showPosition()+"\nExpecting "+y.join(", ")+", got '"+(this.terminals_[q]||q)+"'":"Parse error on line "+(i+1)+": Unexpected "+(q==m?"end of input":"'"+(this.terminals_[q]||q)+"'"),this.parseError(A,{text:this.lexer.match,token:this.terminals_[q]||q,line:this.lexer.yylineno,loc:o,expected:y})}if(t[0]instanceof Array&&t.length>1)throw new Error("Parse Error: multiple actions possible at state: "+s+", token: "+q);switch(t[0]){case 1:d.push(q),e.push(this.lexer.yytext),f.push(this.lexer.yylloc),d.push(t[1]),q=null,r?(q=r,r=null):(j=this.lexer.yyleng,h=this.lexer.yytext,i=this.lexer.yylineno,o=this.lexer.yylloc,k>0&&k--);break;case 2:if(w=this.productions_[t[1]][1],z.$=e[e.length-w],z._$={first_line:f[f.length-(w||1)].first_line,last_line:f[f.length-1].last_line,first_column:f[f.length-(w||1)].first_column,last_column:f[f.length-1].last_column},p&&(z._$.range=[f[f.length-(w||1)].range[0],f[f.length-1].range[1]]),u=this.performAction.apply(z,[h,j,i,this.yy,t[1],e,f].concat(n)),"undefined"!=typeof u)return u;w&&(d=d.slice(0,-1*w*2),e=e.slice(0,-1*w),f=f.slice(0,-1*w)),d.push(this.productions_[t[1]][0]),e.push(z.$),f.push(z._$),x=g[d[d.length-2]][d[d.length-1]],d.push(x);break;case 3:return!0}}return!0}},lexer=function(){var lexer={EOF:1,parseError:function(a,b){if(!this.yy.parser)throw new Error(a);this.yy.parser.parseError(a,b)},setInput:function(a){return this._input=a,this._more=this._backtrack=this.done=!1,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},this.options.ranges&&(this.yylloc.range=[0,0]),this.offset=0,this},input:function(){var a=this._input[0];this.yytext+=a,this.yyleng++,this.offset++,this.match+=a,this.matched+=a;var b=a.match(/(?:\r\n?|\n).*/g);return b?(this.yylineno++,this.yylloc.last_line++):this.yylloc.last_column++,this.options.ranges&&this.yylloc.range[1]++,this._input=this._input.slice(1),a},unput:function(a){var b=a.length,c=a.split(/(?:\r\n?|\n)/g);this._input=a+this._input,this.yytext=this.yytext.substr(0,this.yytext.length-b-1),this.offset-=b;var d=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),c.length-1&&(this.yylineno-=c.length-1);var e=this.yylloc.range;return this.yylloc={first_line:this.yylloc.first_line,last_line:this.yylineno+1,first_column:this.yylloc.first_column,last_column:c?(c.length===d.length?this.yylloc.first_column:0)+d[d.length-c.length].length-c[0].length:this.yylloc.first_column-b},this.options.ranges&&(this.yylloc.range=[e[0],e[0]+this.yyleng-b]),this.yyleng=this.yytext.length,this},more:function(){return this._more=!0,this},reject:function(){return this.options.backtrack_lexer?(this._backtrack=!0,this):this.parseError("Lexical error on line "+(this.yylineno+1)+". You can only invoke reject() in the lexer when the lexer is of the backtracking persuasion (options.backtrack_lexer = true).\n"+this.showPosition(),{text:"",token:null,line:this.yylineno})},less:function(a){this.unput(this.match.slice(a))},pastInput:function(){var a=this.matched.substr(0,this.matched.length-this.match.length);return(a.length>20?"...":"")+a.substr(-20).replace(/\n/g,"")},upcomingInput:function(){var a=this.match;return a.length<20&&(a+=this._input.substr(0,20-a.length)),(a.substr(0,20)+(a.length>20?"...":"")).replace(/\n/g,"")},showPosition:function(){var a=this.pastInput(),b=new Array(a.length+1).join("-");return a+this.upcomingInput()+"\n"+b+"^"},test_match:function(a,b){var c,d,e;if(this.options.backtrack_lexer&&(e={yylineno:this.yylineno,yylloc:{first_line:this.yylloc.first_line,last_line:this.last_line,first_column:this.yylloc.first_column,last_column:this.yylloc.last_column},yytext:this.yytext,match:this.match,matches:this.matches,matched:this.matched,yyleng:this.yyleng,offset:this.offset,_more:this._more,_input:this._input,yy:this.yy,conditionStack:this.conditionStack.slice(0),done:this.done},this.options.ranges&&(e.yylloc.range=this.yylloc.range.slice(0))),d=a[0].match(/(?:\r\n?|\n).*/g),d&&(this.yylineno+=d.length),this.yylloc={first_line:this.yylloc.last_line,last_line:this.yylineno+1,first_column:this.yylloc.last_column,last_column:d?d[d.length-1].length-d[d.length-1].match(/\r?\n?/)[0].length:this.yylloc.last_column+a[0].length},this.yytext+=a[0],this.match+=a[0],this.matches=a,this.yyleng=this.yytext.length,this.options.ranges&&(this.yylloc.range=[this.offset,this.offset+=this.yyleng]),this._more=!1,this._backtrack=!1,this._input=this._input.slice(a[0].length),this.matched+=a[0],c=this.performAction.call(this,this.yy,this,b,this.conditionStack[this.conditionStack.length-1]),this.done&&this._input&&(this.done=!1),c)return c;if(this._backtrack){for(var f in e)this[f]=e[f];return!1}return!1},next:function(){if(this.done)return this.EOF;this._input||(this.done=!0);var a,b,c,d;this._more||(this.yytext="",this.match="");for(var e=this._currentRules(),f=0;f<e.length;f++)if(c=this._input.match(this.rules[e[f]]),c&&(!b||c[0].length>b[0].length)){if(b=c,d=f,this.options.backtrack_lexer){if(a=this.test_match(c,e[f]),a!==!1)return a;if(this._backtrack){b=!1;continue}return!1}if(!this.options.flex)break}return b?(a=this.test_match(b,e[d]),a!==!1?a:!1):""===this._input?this.EOF:this.parseError("Lexical error on line "+(this.yylineno+1)+". Unrecognized text.\n"+this.showPosition(),{text:"",token:null,line:this.yylineno})},lex:function(){var a=this.next();return a?a:this.lex()},begin:function(a){this.conditionStack.push(a)},popState:function(){var a=this.conditionStack.length-1;return a>0?this.conditionStack.pop():this.conditionStack[0]},_currentRules:function(){return this.conditionStack.length&&this.conditionStack[this.conditionStack.length-1]?this.conditions[this.conditionStack[this.conditionStack.length-1]].rules:this.conditions.INITIAL.rules},topState:function(a){return a=this.conditionStack.length-1-Math.abs(a||0),a>=0?this.conditionStack[a]:"INITIAL"},pushState:function(a){this.begin(a)},stateStackSize:function(){return this.conditionStack.length},options:{},performAction:function anonymous(yy,yy_,$avoiding_name_collisions,YY_START){var YYSTATE=YY_START;switch($avoiding_name_collisions){case 0:break;case 1:return 18;case 2:return 19;case 3:return 31;case 4:return 32;case 5:return 35;case 6:return 36;case 7:return 9;case 8:return 10;case 9:return 7;case 10:return 13;case 11:return 14;case 12:return 11;case 13:return 12;case 14:return 15;case 15:return 16;case 16:return 17;case 17:return 24;case 18:return 24;case 19:return 25;case 20:return yy_.yytext=eval(yy_.yytext),26;case 21:return yy_.yytext=eval(yy_.yytext),26;case 22:return 26;case 23:return 30;case 24:return 5}},rules:[/^(?:\s+)/,/^(?:\()/,/^(?:\))/,/^(?:\[)/,/^(?:\])/,/^(?:\{)/,/^(?:\})/,/^(?:=)/,/^(?:!=)/,/^(?:~)/,/^(?:<)/,/^(?:<=)/,/^(?:>)/,/^(?:>=)/,/^(?:not\b)/,/^(?:and\b)/,/^(?:or\b)/,/^(?:true\b)/,/^(?:false\b)/,/^(?:-?\d+(\.\d+)?)/,/^(?:"(\\.|[^\\"])*")/,/^(?:'(\\.|[^\\'])*')/,/^(?:[-\w_\\.:]+)/,/^(?:@@)/,/^(?:$)/],conditions:{INITIAL:{rules:[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24],inclusive:!0}}};return lexer}();return parser.lexer=lexer,Parser.prototype=parser,parser.Parser=Parser,new Parser}();"undefined"!=typeof require&&"undefined"!=typeof exports&&(exports.parser=parser,exports.Parser=parser.Parser,exports.parse=function(){return parser.parse.apply(parser,arguments)},exports.main=function(a){a[1]||(console.log("Usage: "+a[0]+" FILE"),process.exit(1));var b=require("fs").readFileSync(require("path").normalize(a[1]),"utf8");return exports.parser.parse(b)},"undefined"!=typeof module&&require.main===module&&exports.main(process.argv.slice(1)));
<html>
<head>
</head>
<body>
<input type="text" id="puppetdbquery" onChange="parseQuery()" size="100"><br>
<input type="text" id="apiquery" size="100">
<script src="puppetdbquery.min.js"></script>
<script src="puppetboard-dbquery.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment