!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("libcore"),require("libcore-tokenizer")):"function"==typeof define&&define.amd?define(["exports","libcore","libcore-tokenizer"],e):e(t["libcore-parser-lalr"]={},t.libcore,t["libcore-tokenizer"])}(this,function(t,e,r){"use strict";function n(t){var e={};this.stateGen=this.symbolGen=this.reduceGen=0,e[0]={},this.lookup={},this.symbol={},this.start="0",this.states=e,this.ends={},this.exclude={},this.finalized=!1,this.rawStates=[],this.reduceLookup={},this.reducers={},this.debugMode=!0===t,this.setRoot("End"),this.endSymbol=this.generateSymbol("$"),this.endToken="$"}function i(t){return"$"===t||!k.test(t)}function o(t,r,n){var i,o,s,a=e.regex;for(i=-1,o=n.length;o--;){if(s=n[++i],!a(s))throw new Error("Invalid Terminal pattern: "+s);if(!t.registerTerminal(s,r))throw new Error("Invalid Terminal pattern: "+s)}}function s(t,r,n){var o,s,a,u,l,h,c,f,d=e.string,m=e.regex,p=e.array,g=i;for(o=-1,s=n.length;s--;){if(u=n[++o],d(u)||m(u))u=[u];else if(!p(u))throw new Error("Invalid Grammar rule declared in "+r);for(a=u.length,h=[],c={};a--;){if(l=u[a],m(l))t.terminalExist(l)||t.registerTerminal(l),l="/"+l.source+"/",f=!0;else{if(!d(l))throw new Error("Invalid Grammar rule declared in "+r);f=g(l)}h[a]=t.map.generateSymbol(l),f&&(c[a]=!0)}t.registerRule(r,h,c)}}function a(t,e,r){this.id=e,this.registry=t,this.items=r||[],this.end=null,this.tokens=[],this.pointers={}}function u(t){this.name=t}function l(t){for(var e,r,n,i,o,s,l,h,c,f,d,m,p=t.map,g=a,v=t.productions,b=t.closureItems,w=new u,y=1,k=p.augmentedRoot,S=[],I=0;y;)switch(y){case 1:for(e=(i=t.createClosure(v[k]))[1],m=S[I=S.length]=new g(t,I.toString(32),i[0]),r=-1,n=e.length;n--;)i=e[++r],w.push([m,i[1],i[0]]);if(!w.first){y=0;break}y=2;break;case 2:for(d=(i=w.shift())[0],e=i[1],s=i[2],o=(i=t.createClosure(e))[0],h=i[1],l=I=S.length,m=null;I--;)if((i=S[I]).containsItems(o)){m=i;break}if(!m){for(m=S[I=l++]=new g(t,I.toString(32),o),r=-1,n=h.length;n--;)i=h[++r],w.push([m,i[1],i[0]]);for(r=-1,n=o.length;n--;)(i=b[o[++r]]).after||m.setEnd(i)}d.pointTo(s,m),y=w.first?2:0;break;case 0:y=null}for(I=S.length;I--;){for(c=(i=S[I]).id,m=p.createState(c),h=i.tokens,f=i.pointers,r=-1,n=h.length;n--;)s=h[++r],p.createPointer(c,s,f[s].id);(i=i.end)&&p.setReduceState(c,i[1],i[0],i[2])}}function h(t,e){this.tokenizer=e,this.map=t,this.ruleLookup={},this.productions={},this.closureItems={},this.terminals=[],this.terminalLookup={},this.stateTagIdGen=0,this.stateTagId={},this.stateTagIdLookup={}}function c(t,r,n,a,u){var c,f,d,m,p,g=e.string,v=e.array,b=e.regex,w=i,y=o,k=s,S=null,I=!0;for(r.reset(),r.setRoot(t),m=new h(r,n),a.splice(a.length,0,r.lookupSymbol(r.augmentedRoot),[[t,r.lookupSymbol(r.endSymbol)]]),c=-1,f=a.length;f--;)if(d=a[++c],g(d))I=w(d),S=r.generateSymbol(d),d;else{if(!S||!v(d))throw new Error("Invalid item in definitions parameter.");(I?y:k)(m,S,d)}if(l(m),v(u)){for(p=[],c=-1,f=u.length;f--;){if(d=u[++c],b(d))d=m.registerTerminal(d);else{if(!g(d))throw new Error("Invalid [exclude] pattern parameter.");d=r.generateSymbol(d)}p[c]=d}r.setExcludes(p)}return!0}function f(t){this.terminal=!1,this.useType(t)}function d(t){if(!e.object(t))throw new Error("Invalid parser parameter.");this.parser=t,this.reset(),this.start=":start"}function m(t,r){var n=d;if(!e.string(t))throw new Error("Invalid iterator name parameter.");if(!e.method(r)||r!==n&&!(r.prototype instanceof n))throw new Error("Invalid iterator Class parameter.");return I[":"+t]=r,!0}function p(t){var r=I;return e.string(t)&&(t=":"+t)in r?r[t]:null}function g(t,e,i){this.tokenizer=new r,this.map=new n(E),arguments.length&&this.define(t,e,i)}function v(t){E=!1!==t}function b(t,e,r){return new g(t,e,r)}function w(t){var r;if(e.string(t))try{t=JSON.parse(t)}catch(t){throw new Error("Unable to load from invalid json JSON String parameter: "+t.toString())}else if(!e.object(t))throw new Error("Unable to load from invalid json Object parameter.");r=new g;try{r.fromJSON(t)}catch(t){throw new Error(t)}return r}function y(t){return t instanceof g}r=r&&r.hasOwnProperty("default")?r.default:r,n.prototype={stateGen:0,rawStates:null,debugMode:!1,constructor:n,setRoot:function(t){this.root=this.generateSymbol(t),this.augmentedRoot=this.generateSymbol(t+"'")},createState:function(t){var e=this.states;return t in e?e[t]:e[t]={}},createPointer:function(t,e,r){var n=this.createState(t);return n[e]=r,n},generateSymbol:function(t){var e,r=this.lookup,n=this.symbol,i=":"+t;return i in r?r[i]:(e=this.debugMode?"["+t+"]":(++this.symbolGen).toString(36),r[i]=e,n[e]=t,e)},generateReduceId:function(t,e,r){var n,i=this.reduceLookup,o=this.reducers,s=t+":"+e+":"+r;return s in i?i[s]:(n=this.debugMode?"["+t+":"+e+">"+r+"]":(++this.reduceGen).toString(36),i[s]=n,o[n]=[t,e,r],n)},lookupReducer:function(t){var e=this.reducers;return t in e&&e[t]},lookupSymbol:function(t){var e=this.symbol;return t in e&&e[t]},setReduceState:function(t,e,r,n){var i,o=this.ends,s=this.generateReduceId(e,r,n),a=this.reducers;if(t in o){if((i=a[o[t]])[0]!==e||i[1]!==r)throw new Error("Reduce conflict found "+this.lookupSymbol(i[0])+" ! <- "+this.lookupSymbol(e))}else o[t]=s},reset:function(){this.constructor(this.debugMode)},finalize:function(){var t,e,r=this.rawStates;if(!this.finalized&&r){for(this.finalized=!0,t=-1,e=r.length;e--;)r[++t].finalize();r.length=0,delete this.lookup}return this.finalized},setExcludes:function(t){var r,n,i=this.exclude;if(e.array(t))for(r=-1,n=t.length;n--;)i[t[++r]]=!0},importStates:function(t){var r,n,i,o,s,a,u,l,h,c,f,d=e.object,m=e.string;if(!d(t))throw new Error("Invalid Object definition parameter.");if(n=t.states,!d(n))throw new Error('Invalid "states" Object in definition parameter.');if(o=t.root,!m(o))throw new Error('Invalid "root" grammar rule in definition parameter.');if(l=t.augmentedRoot,!m(l))throw new Error('Invalid "augmentedRoot" grammar rule in definition parameter.');if(r=t.start,!(m(r)&&r in n))throw new Error('Invalid "start" state in definition parameter.');if(i=t.ends,!d(i))throw new Error('Invalid "ends" states in definition parameter.');if(u=t.reducers,!d(u))throw new Error('Invalid production "reducers" in definition.');if(a=t.symbol,!d(a))throw new Error('Invalid "symbol" map in definition parameter.');if(h=t.exclude,!e.array(h))throw new Error('Invalid "exclude" token in definition parameter.');for(s={},c=-1,f=h.length;f--;)s[h[++c]]=!0;return this.augmentedRoot=l,this.root=o,this.start=r,this.states=n,this.ends=i,this.reducers=u,this.exclude=s,this.symbol=a,!0},toObject:function(){var t,r=e.contains,n=this.exclude,i=[],o=0;for(t in n)r(n,t)&&(i[o++]=t);return{augmentedRoot:this.augmentedRoot,root:this.root,start:this.start,states:this.states,reducers:this.reducers,ends:this.ends,exclude:i,symbol:this.symbol}},exportStates:function(t){var e=this.toObject();if(!0===t)try{return JSON.stringify(e)}catch(t){return null}return e}};var k=/^([A-Z][a-zA-Z]+(\_?[a-zA-Z0-9])*\'?)$/;a.prototype={constructor:a,containsItems:function(t){var e,r,n,i=this.items,o=i.length;if(t.length===o){r=o;t:for(;r--;){for(e=i[r],n=o;n--;)if(e===t[n])continue t;return!1}return!0}return!1},pointTo:function(t,e){var r=this.tokens,n=this.pointers,i=this.registry.map;if(t in n){if(n[t]!==e)throw new Error("Invalid state target from "+this.id+" -> "+i.lookupSymbol(t)+" -> "+i.lookupSymbol(e))}else n[t]=e,r[r.length]=t},setEnd:function(t){var e=this.end,r=this.registry.map;if(e)throw new Error("There is reduce-reduce conflict in: "+this.id+" when you tried reducing it to `"+r.lookupSymbol(t.production)+"`, currently this state is reduced in `"+r.lookupSymbol(e[1])+"` production.");this.end=[t.params,t.production,t.index]}},u.prototype={constructor:u,first:null,last:null,shift:function(){var t,e=this.first;return e?(this.first=t=e[0],t||(this.last=t),e[1]):null},pop:function(){var t,e=this.last;return e?(this.last=t=e[0],t||(this.first=t),e[1]):null},push:function(t){return t=[null,t],this.last?this.last[0]=t:this.first=t,this.last=t,this}},h.prototype={constructor:h,startRule:null,rules:null,hashState:function(t){var e,r=this.stateTagIdLookup,n=":"+t;return n in r?r[n]:(e=this.map.debugMode?":"+t:(++this.stateTagIdGen).toString(36),r[n]=e,this.stateTagId[e]=t,e)},lookupState:function(t){var e=this.stateTagId;return t in e?e[t]:null},hashLexeme:function(t){var e,r=this.lookup,n=this.symbol,i=":"+t;return i in r?r[i]:(e=this.map.debugMode?"["+t+"]":(++this.symbolGen).toString(36),r[i]=e,n[e]=t,e)},lookupLexeme:function(t){var e=this.lookup;return t in e?e[t]:null},terminalExist:function(t){var r=this.terminalLookup;return e.string(t)?e.contains(r,t):"/"+t.source+"/"in r},registerTerminal:function(t,r){var n,i=this.terminalLookup,o=this.terminals,s=this.map.generateSymbol("/"+t.source+"/");return r||(r=s),!(s in i)&&(i[s]=r,s===r?o[o.length]=r:e.contains(i,r)?(n=i[r])[n.length]=s:(o[o.length]=r,i[r]=[s]),this.tokenizer.define([r,t]),r)},registerRule:function(t,e,r){var n,i,o,s,a=this,u=this.closureItems,l=this.productions,h=this.ruleLookup,c=-1,f=e.length+1,d=null,m=0;t in l||(l[t]=[]),s=(l=l[t]).length+1;for(;f--;){if((n=e.slice(0)).splice(++c,0,"."),i=a.hashState(t+"->"+n.join(" ")),!c){if(i in h)throw new Error("Duplicate Grammar Rule found "+a.lookupState(i)+" in production: "+a.map.lookupSymbol(t));h[i]=t,l[l.length]=i}u[i]=o={id:i,production:t,index:s,before:null,after:null,terminal:!1,token:null},d&&(o.before=d.id,d.after=i),d=o,f?(m++,o.terminal=c in r,o.token=e[c]):o.params=m}},createClosure:function(t){var e,r,n,i,o,s=this.closureItems,a=this.productions,u={},l={},h=[],c=0,f=-1,d=t.length;for(t=t.slice(0);d--;)(e=t[++f])in u?t.splice(f--,1):(u[e]=!0,r=(e=s[e]).token,n=e.terminal,r&&(r in l?(i=h[l[r]][1])[i.length]=e.after:(l[r]=c,h[c++]=[r,[e.after]],n||(o=a[r],t.push.apply(t,o),d+=o.length))));return[t,h]}};var S={terminal:1,nonterminal:2,compound:3,end:4};f.prototype={constructor:f,name:null,rule:null,value:null,reduceCount:0,from:0,to:0,parent:null,first:null,last:null,next:null,previous:null,terminal:!1,useType:function(t){var r=S;this.type=t=e.contains(r,t)?r[t]:r.token,t===S.terminal&&(this.terminal=!0)}};d.prototype={constructor:d,subject:"",returns:!1,current:null,ready:!1,completed:!1,error:null,actions:{":start":{0:":fail",1:":tokenize"},":tokenize":{0:":fail",1:":tokenize",2:":shift",3:":reduce"},":shift":{0:":fail",1:":tokenize"},":reduce":{0:":fail",1:":shift",2:":reduce",3:":success"},":fail":{},":success":{}},":start":function(){var t=this;return t.params=t.nextTokenIndex,1},":tokenize":function(t){var e,r,n,i,o,s=this,a=s.parser,u=a.map,l=u.ends,h=u.states,c=s.pstate,d=a.tokenizer.tokenize(t,s.subject),m=u.endToken;if(d){if(e=d[0],r=d[2],!this.isAcceptableToken(d))return s.params=r,1;if(i=new f("terminal"),o=e,e===m?e=u.endSymbol:o=u.symbol[e],i.name=o,i.symbol=e,i.value=d[1],i.from=t,i.to=r,s.nextTokenIndex=r,s.params=i,n=h[c],e in n)return 2}return s.buffer.length&&c in l?3:(s.params="Invalid token",0)},":shift":function(t){var e=this,r=e.buffer,n=e.parser.map,i=n.states,o=e.pstate,s=t.symbol;return r[r.length]=[o,t],e.pstate=i[o][s],e.current=t,e.params=null,e.returns=s!==n.endSymbol,e.params=e.nextTokenIndex,1},":reduce":function(t){var e,r,n,i,o,s,a=this,u=a.parser.map,l=a.buffer,h=l.length,c=u.ends,d=u.states,m=u.symbol,p=a.pstate,g=u.lookupReducer(c[p]),v=g[0],b=g[1],w=g[2],y=b,k=y-1,S=new f("nonterminal"),I=[],E=m[v];for(S.name=E,S.symbol=v,S.rule=w+":"+E,s=null;y--;)p=(r=l[--h])[0],n=(e=r[1]).from,y===k&&(i=e.to),e.parent=S,s?(s.previous=e,e.next=s):S.last=e,S.first=s=e,I[y]=e.value;return S.value=I,S.from=n,S.to=i,l.length=h,a.current=S,S.reduceCount=b,v===u.augmentedRoot?0===h?(e=S.first,S.useType("end"),S.last=e,S.value=[e.value],S.reduceCount=1,a.params=S,3):(a.params="Failed last reduce",0):(l[h++]=[p,S],a.returns=!0,p=d[p][v],o=d[p],v=t.symbol,a.pstate=p,v in o?1:p in c?2:(a.params="failed reduce! inside :reduce",0))},":success":function(t){var e=this;return e.completed=e.returns=!0,e.current=t,!1},":fail":function(t){var e=this;return e.error=t,e.completed=!0,!1},isAcceptableToken:function(t){return!(t[0]in this.parser.map.exclude)},update:function(t){var e=this,r=e.current;return!e.error&&r&&(r.value=t),this},reset:function(){var t=this.parser;this.nextTokenIndex=0,this.cursor=0,this.buffer=[],this.state=this.start,this.pstate=t.map.start,this.params=null,this.subject||delete this.ready,delete this.complete,delete this.error,delete this.returns,delete this.current},set:function(t){if(!e.string(t))throw new Error("Invalid String subject parameter.");this.reset(),this.subject=t,this.ready=!0},next:function(){var t,r,n,i,o,s=this,a=s.actions,u=e.number,l=s.completed,h=!1;if(!s.ready)throw new Error("Iterator is not yet ready, nothing to Parse.");for(l||delete s.current;!l;){if(t=s.state,r=s.params,!(t in s))throw new Error("No handler found for state "+t);if(n=s[t](r),h=s.returns,delete s.returns,o=s.current,l=s.completed,s.error)break;if(!l){if(!u(n))throw new Error("Invalid result from state handler"+t);if(i=a[t],!(n in i))throw new Error("Invalid result from state handler"+t);s.state=i[n]}if(!0===h)return o}return!(s.error||!l)&&null}};var I={};m("base",d);var E=!1;g.prototype={subject:"",tokenizer:null,map:null,ready:!1,constructor:g,iterator:function(t){var e,r=p;if(arguments.length){if(!(e=r(t)))throw new Error("Invalid iterator name parameter.")}else e=r("base");return new e(this)},define:function(t,r,n){var i,o=e.array;if(o(n)||(n=[]),!e.string(t))throw new Error("Invalid root grammar rule parameter.");if(!o(r))throw new Error("Invalid grammar rules definition parameter");return this.ready=i=c(t,this.map,this.tokenizer,r,n),i},fromJSON:function(t){var r,n=e.object;if(e.string(t))try{t=JSON.parse(t)}catch(t){throw new Error("Invalid JSON String json parameter.")}if(!n(t))throw new Error("Invalid Object json parameter.");if(r=t.tokens,!n(r))throw new Error('Invalid "tokens" property of json parameter.');return this.tokenizer.fromJSON(r),this.map.importStates(t),this},toJSON:function(){return JSON.stringify(this.toObject())},toObject:function(){var t;if(!this.ready)throw new Error("Grammar rules is not yet defined.");return t=this.map.toObject(),t.tokens=this.tokenizer.toObject(),t},parse:function(t,r,n){var i,o,s,a=e.string,u=[],l=0;if(!a(t))throw new Error("Invalid string subject parameter");if(!(n=a(n)?this.iterator(n):this.iterator()))throw new Error("Invalid Iterator parameter.");for(e.object(r)||(r={}),n.set(t),i=n.next();i;i=n.next())u[l++]=i,(o=i.name)in r&&(void 0!==(s=r[o](o,i.value,i))?i.value=s:0!==i.params&&(i.value=null));return!n.error&&u}};var x=Object.freeze({debug:v,Parser:g,define:b,load:w,isParser:y,Iterator:d,registerIterator:m});t.default=x,t.debug=v,t.Parser=g,t.define=b,t.load=w,t.isParser=y,t.Iterator=d,t.registerIterator=m,Object.defineProperty(t,"__esModule",{value:!0})});
//# sourceMappingURL=libcore-parser-lalr.min.js.map
