prototxt-parser
Version:
a prototxt parser for js based on parsimmon
1 lines • 11.1 kB
JavaScript
!function(n){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=n();else if("function"==typeof define&&define.amd)define([],n);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).prototxtParser=n()}}(function(){var n={exports:{}};!function(t,r){"object"==typeof n.exports?n.exports=r():"object"==typeof n.exports?n.exports.Parsimmon=r():t.Parsimmon=r()}(this,function(){return function(n){function t(e){if(r[e])return r[e].exports;var u=r[e]={i:e,l:!1,exports:{}};return n[e].call(u.exports,u,u.exports,t),u.l=!0,u.exports}var r={};return t.m=n,t.c=r,t.i=function(n){return n},t.d=function(n,r,e){t.o(n,r)||Object.defineProperty(n,r,{configurable:!1,enumerable:!0,get:e})},t.n=function(n){var r=n&&n.__esModule?function(){return n.default}:function(){return n};return t.d(r,"a",r),r},t.o=function(n,t){return Object.prototype.hasOwnProperty.call(n,t)},t.p="",t(t.s=0)}([function(n,t,r){"use strict";function e(n){if(!(this instanceof e))return new e(n);this._=n}function u(n){return n instanceof e}function i(n){return"[object Array]"==={}.toString.call(n)}function o(n,t){return{status:!0,index:n,value:t,furthest:-1,expected:[]}}function a(n,t){return{status:!1,index:-1,value:null,furthest:n,expected:[t]}}function f(n,t){if(!t)return n;if(n.furthest>t.furthest)return n;var r=n.furthest===t.furthest?function(n,t){var r=n.length,e=t.length;if(0===r)return t;if(0===e)return n;for(var u={},i=0;i<r;i++)u[n[i]]=!0;for(var o=0;o<e;o++)u[t[o]]=!0;var a=[];for(var f in u)u.hasOwnProperty(f)&&a.push(f);return a.sort(),a}(n.expected,t.expected):t.expected;return{status:n.status,index:n.index,value:n.value,furthest:t.furthest,expected:r}}function c(n,t){var r=n.slice(0,t).split("\n");return{offset:t,line:r.length,column:r[r.length-1].length+1}}function s(n){if(!u(n))throw new Error("not a parser: "+n)}function l(n){if("number"!=typeof n)throw new Error("not a number: "+n)}function p(n){if("function"!=typeof n)throw new Error("not a function: "+n)}function h(n){if("string"!=typeof n)throw new Error("not a string: "+n)}function d(n,t){return"expected "+function(n){return 1===n.length?n[0]:"one of "+n.join(", ")}(t.expected)+function(n,r){var e=t.index,u=e.offset;if(u===n.length)return", got the end of the input";var i=u>0?"'...":"'",o=n.length-u>12?"...'":"'";return" at line "+e.line+" column "+e.column+", got "+i+n.slice(u,u+12)+o}(n)}function v(n){var t=""+n;return t.slice(t.lastIndexOf("/")+1)}function g(){for(var n=[].slice.call(arguments),t=n.length,r=0;r<t;r+=1)s(n[r]);return e(function(r,e){for(var u,i=new Array(t),a=0;a<t;a+=1){if(!(u=f(n[a]._(r,e),u)).status)return u;i[a]=u.value,e=u.index}return f(o(e,i),u)})}function m(){var n=[].slice.call(arguments);if(0===n.length)throw new Error("seqMap needs at least one argument");var t=n.pop();return p(t),g.apply(null,n).map(function(n){return t.apply(null,n)})}function x(){var n=[].slice.call(arguments),t=n.length;if(0===t)return O("zero alternates");for(var r=0;r<t;r+=1)s(n[r]);return e(function(t,r){for(var e,u=0;u<n.length;u+=1)if((e=f(n[u]._(t,r),e)).status)return e;return e})}function y(n,t){return w(n,t).or(E([]))}function w(n,t){s(n),s(t);var r=t.then(n).many();return n.chain(function(n){return r.map(function(t){return[n].concat(t)})})}function b(n){h(n);var t="'"+n+"'";return e(function(r,e){var u=e+n.length,i=r.slice(e,u);return i===n?o(u,i):a(e,t)})}function _(n,t){(function(n){if(!(n instanceof RegExp))throw new Error("not a regexp: "+n);for(var t=v(n),r=0;r<t.length;r++){var e=t.charAt(r);if("i"!==e&&"m"!==e&&"u"!==e)throw new Error('unsupported regexp flag "'+e+'": '+n)}})(n),arguments.length>=2?l(t):t=0;var r=function(n){return RegExp("^(?:"+n.source+")",v(n))}(n),u=""+n;return e(function(n,e){var i=r.exec(n.slice(e));if(i){if(0<=t&&t<=i.length){var f=i[0],c=i[t];return o(e+f.length,c)}return a(e,"valid match group (0 to "+i.length+") in "+u)}return a(e,u)})}function E(n){return e(function(t,r){return o(r,n)})}function O(n){return e(function(t,r){return a(r,n)})}function P(n){if(u(n))return e(function(t,r){var e=n._(t,r);return e.index=r,e.value="",e});if("string"==typeof n)return P(b(n));if(n instanceof RegExp)return P(_(n));throw new Error("not a string, regexp, or parser: "+n)}function k(n){return s(n),e(function(t,r){var e=n._(t,r),u=t.slice(r,e.index);return e.status?a(r,'not "'+u+'"'):o(r,null)})}function j(n){return p(n),e(function(t,r){var e=t.charAt(r);return r<t.length&&n(e)?o(r+1,e):a(r,"a character matching "+n)})}function A(n,t){arguments.length<2&&(t=n,n=void 0);var r=e(function(n,e){return r._=t()._,r._(n,e)});return n?r.desc(n):r}function q(){return O("fantasy-land/empty")}var z=e.prototype;z.parse=function(n){if("string"!=typeof n)throw new Error(".parse must be called with a string as its argument");var t=this.skip(F)._(n,0);return t.status?{status:!0,value:t.value}:{status:!1,index:c(n,t.furthest),expected:t.expected}},z.tryParse=function(n){var t=this.parse(n);if(t.status)return t.value;var r=d(n,t),e=new Error(r);throw e.type="ParsimmonError",e.result=t,e},z.or=function(n){return x(this,n)},z.trim=function(n){return this.wrap(n,n)},z.wrap=function(n,t){return m(n,this,t,function(n,t){return t})},z.thru=function(n){return n(this)},z.then=function(n){return s(n),g(this,n).map(function(n){return n[1]})},z.many=function(){var n=this;return e(function(t,r){for(var e=[],u=void 0;;){if(!(u=f(n._(t,r),u)).status)return f(o(r,e),u);r=u.index,e.push(u.value)}})},z.tie=function(){return this.map(function(n){!function(n){if(!i(n))throw new Error("not an array: "+n)}(n);for(var t="",r=0;r<n.length;r++)h(n[r]),t+=n[r];return t})},z.times=function(n,t){var r=this;return arguments.length<2&&(t=n),l(n),l(t),e(function(e,u){for(var i=[],a=void 0,c=void 0,s=0;s<n;s+=1){if(a=r._(e,u),c=f(a,c),!a.status)return c;u=a.index,i.push(a.value)}for(;s<t&&(a=r._(e,u),c=f(a,c),a.status);s+=1)u=a.index,i.push(a.value);return f(o(u,i),c)})},z.result=function(n){return this.map(function(){return n})},z.atMost=function(n){return this.times(0,n)},z.atLeast=function(n){return m(this.times(n),this.many(),function(n,t){return n.concat(t)})},z.map=function(n){p(n);var t=this;return e(function(r,e){var u=t._(r,e);return u.status?f(o(u.index,n(u.value)),u):u})},z.skip=function(n){return g(this,n).map(function(n){return n[0]})},z.mark=function(){return m(S,this,S,function(n,t,r){return{start:n,value:t,end:r}})},z.node=function(n){return m(S,this,S,function(t,r,e){return{name:n,value:r,start:t,end:e}})},z.sepBy=function(n){return y(this,n)},z.sepBy1=function(n){return w(this,n)},z.lookahead=function(n){return this.skip(P(n))},z.notFollowedBy=function(n){return this.skip(k(n))},z.desc=function(n){var t=this;return e(function(r,e){var u=t._(r,e);return u.status||(u.expected=[n]),u})},z.fallback=function(n){return this.or(E(n))},z.ap=function(n){return m(n,this,function(n,t){return n(t)})},z.chain=function(n){var t=this;return e(function(r,e){var u=t._(r,e);return u.status?f(n(u.value)._(r,u.index),u):u})},z.concat=z.or,z.empty=q,z.of=E,z["fantasy-land/ap"]=z.ap,z["fantasy-land/chain"]=z.chain,z["fantasy-land/concat"]=z.concat,z["fantasy-land/empty"]=z.empty,z["fantasy-land/of"]=z.of,z["fantasy-land/map"]=z.map;var S=e(function(n,t){return o(t,c(n,t))}),B=e(function(n,t){return t>=n.length?a(t,"any character"):o(t+1,n.charAt(t))}),M=e(function(n,t){return o(n.length,n.slice(t))}),F=e(function(n,t){return t<n.length?a(t,"EOF"):o(t,null)}),Q=_(/[0-9]/).desc("a digit"),L=_(/[0-9]*/).desc("optional digits"),R=_(/[a-z]/i).desc("a letter"),W=_(/[a-z]*/i).desc("optional letters"),C=_(/\s*/).desc("optional whitespace"),I=_(/\s+/).desc("whitespace");e.all=M,e.alt=x,e.any=B,e.createLanguage=function(n){var t={};for(var r in n)({}).hasOwnProperty.call(n,r)&&function(r){t[r]=A(function(){return n[r](t)})}(r);return t},e.custom=function(n){return e(n(o,a))},e.digit=Q,e.digits=L,e.empty=q,e.eof=F,e.fail=O,e.formatError=d,e.index=S,e.isParser=u,e.lazy=A,e.letter=R,e.letters=W,e.lookahead=P,e.makeFailure=a,e.makeSuccess=o,e.noneOf=function(n){return j(function(t){return n.indexOf(t)<0})},e.notFollowedBy=k,e.of=E,e.oneOf=function(n){return j(function(t){return n.indexOf(t)>=0})},e.optWhitespace=C,e.Parser=e,e.range=function(n,t){return j(function(r){return n<=r&&r<=t}).desc(n+"-"+t)},e.regex=_,e.regexp=_,e.sepBy=y,e.sepBy1=w,e.seq=g,e.seqMap=m,e.seqObj=function(){for(var n={},t=0,r=[].slice.call(arguments),a=r.length,c=0;c<a;c+=1){var s=r[c];if(!u(s)){if(i(s)&&2===s.length&&"string"==typeof s[0]&&u(s[1])){var l=s[0];if(n[l])throw new Error("seqObj: duplicate key "+l);n[l]=!0,t++;continue}throw new Error("seqObj arguments must be parsers or [string, parser] array pairs.")}}if(0===t)throw new Error("seqObj expects at least one named parser, found zero");return e(function(n,t){for(var e,u={},c=0;c<a;c+=1){var s,l;if(i(r[c])?(s=r[c][0],l=r[c][1]):(s=null,l=r[c]),!(e=f(l._(n,t),e)).status)return e;s&&(u[s]=e.value),t=e.index}return f(o(t,u),e)})},e.string=b,e.succeed=E,e.takeWhile=function(n){return p(n),e(function(t,r){for(var e=r;e<t.length&&n(t.charAt(e));)e++;return o(e,t.slice(r,e))})},e.test=j,e.whitespace=I,e["fantasy-land/empty"]=q,e["fantasy-land/of"]=E,n.exports=e}])}),n=n.exports;var t={};Object.defineProperty(t,"__esModule",{value:!0});var r=function(n){return n.filter(function(n){return Array.isArray(n)})},e=function(n){return n.reduce(function(n,t){var r=t[0],e=t[1];return n[r]=n.hasOwnProperty(r)?[].concat(n[r],e):e,n},{})},u=function(n){var t={b:"\b",f:"\f",n:"\n",r:"\r",t:"\t"};return n.replace(/\\(u[0-9a-fA-F]{4}|[^u])/,function(n,r){var e=r.charAt(0),u=r.slice(1);return"u"===e?String.fromCharCode(parseInt(u,16)):t.hasOwnProperty(e)?t[e]:n})},i=n.regexp(/\s*/m),o=function(n){return n.skip(i)},a=function(t){return n.string(t).thru(o)},f=n.createLanguage({value:function(t){return n.alt(t.number,t.null,t.true,t.false,t.string,t.identifier).thru(function(n){return i.then(n)}).desc("value")},identifier:function(){return o(n.regexp(/[a-zA-Z_-][a-zA-Z0-9_+-]*/).desc("identifier"))},lbrace:function(){return a("{")},rbrace:function(){return a("}")},colon:function(){return a(":")},null:function(){return a("null").result(null)},true:function(){return a("true").result(!0)},false:function(){return a("false").result(!1)},comment:function(){return n.regexp(/\s*#\s*(.*)/m,1)},doubleQuotedString:function(){return n.regexp(/"((?:\\.|.)*?)"/,1)},singleQuotedString:function(){return n.regexp(/'((?:\\.|.)*?)'/,1)},string:function(t){return o(n.alt(t.doubleQuotedString,t.singleQuotedString)).map(u).desc("string")},number:function(){return o(n.regexp(/-?(0|[1-9][0-9]*)([.][0-9]+)?([eE][+-]?[0-9]+)?/)).map(Number).desc("number")},pair:function(t){return n.seq(t.identifier.skip(t.colon),t.value)},message:function(t){return n.seq(t.identifier,t.colon.times(0,1).then(t.lbrace).then(t.exp).skip(t.rbrace))},exp:function(t){return n.alt(t.pair,t.message,t.comment).trim(n.optWhitespace).many().map(r).map(e)}});t.parse=function(n){return f.exp.tryParse(n)};var c={};return Object.defineProperty(c,"__esModule",{value:!0}),c.parse=t.parse,c});