@tracespace/parser
Version:
Gerber and NC drill file parser
3 lines (2 loc) • 24.9 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).TracespaceParser={})}(this,(function(e){"use strict";var t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};var r=function(e,t,r){return e(r={path:t,exports:{},require:function(e,t){return function(){throw new Error("Dynamic requires are not currently supported by @rollup/plugin-commonjs")}(null==t&&r.path)}},r.exports),r.exports}((function(e){var r,n;r=t,n=function(){var e=Object.prototype.hasOwnProperty,t=Object.prototype.toString,r="boolean"==typeof(new RegExp).sticky;function n(e){return e&&"[object RegExp]"===t.call(e)}function o(e){return e&&"object"==typeof e&&!n(e)&&!Array.isArray(e)}function i(e){return"("+e+")"}function u(e){return e.length?"(?:"+e.map((function(e){return"(?:"+e+")"})).join("|")+")":"(?!)"}function a(e){if("string"==typeof e)return"(?:"+e.replace(/[-\/\\^$*+?.()|[\]{}]/g,"\\$&")+")";if(n(e)){if(e.ignoreCase)throw new Error("RegExp /i flag not allowed");if(e.global)throw new Error("RegExp /g flag is implied");if(e.sticky)throw new Error("RegExp /y flag is implied");if(e.multiline)throw new Error("RegExp /m flag is implied");return e.source}throw new Error("Not a pattern: "+e)}function l(t,r){if(o(r)||(r={match:r}),r.include)throw new Error("Matching rules cannot also include states");var i={defaultType:t,lineBreaks:!!r.error||!!r.fallback,pop:!1,next:null,push:null,error:!1,fallback:!1,value:null,type:null,shouldThrow:!1};for(var u in r)e.call(r,u)&&(i[u]=r[u]);if("string"==typeof i.type&&t!==i.type)throw new Error("Type transform cannot be a string (type '"+i.type+"' for token '"+t+"')");var a=i.match;return i.match=Array.isArray(a)?a:a?[a]:[],i.match.sort((function(e,t){return n(e)&&n(t)?0:n(t)?-1:n(e)?1:t.length-e.length})),i}function c(e){return Array.isArray(e)?function(e){for(var t=[],r=0;r<e.length;r++){var n=e[r];if(n.include)for(var o=[].concat(n.include),i=0;i<o.length;i++)t.push({include:o[i]});else{if(!n.type)throw new Error("Rule has no type: "+JSON.stringify(n));t.push(l(n.type,n))}}return t}(e):function(e){for(var t=Object.getOwnPropertyNames(e),r=[],n=0;n<t.length;n++){var i=t[n],u=e[i],a=[].concat(u);if("include"!==i){var c=[];a.forEach((function(e){o(e)?(c.length&&r.push(l(i,c)),r.push(l(i,e)),c=[]):c.push(e)})),c.length&&r.push(l(i,c))}else for(var s=0;s<a.length;s++)r.push({include:a[s]})}return r}(e)}var s=l("error",{lineBreaks:!0,shouldThrow:!0});function f(e,t){for(var o=null,l=Object.create(null),c=!0,f=null,p=[],h=[],d=0;d<e.length;d++)e[d].fallback&&(c=!1);for(d=0;d<e.length;d++){var E=e[d];if(E.include)throw new Error("Inheritance is not allowed in stateless lexers");if(E.error||E.fallback){if(o)throw!E.fallback==!o.fallback?new Error("Multiple "+(E.fallback?"fallback":"error")+" rules not allowed (for token '"+E.defaultType+"')"):new Error("fallback and error are mutually exclusive (for token '"+E.defaultType+"')");o=E}var v=E.match.slice();if(c)for(;v.length&&"string"==typeof v[0]&&1===v[0].length;)l[v.shift().charCodeAt(0)]=E;if(E.pop||E.push||E.next){if(!t)throw new Error("State-switching options are not allowed in stateless lexers (for token '"+E.defaultType+"')");if(E.fallback)throw new Error("State-switching options are not allowed on fallback tokens (for token '"+E.defaultType+"')")}if(0!==v.length){c=!1,p.push(E);for(var O=0;O<v.length;O++){var R=v[O];if(n(R))if(null===f)f=R.unicode;else if(f!==R.unicode&&!1===E.fallback)throw new Error("If one rule is /u then all must be")}var y=u(v.map(a)),m=new RegExp(y);if(m.test(""))throw new Error("RegExp matches empty string: "+m);if(new RegExp("|"+y).exec("").length-1>0)throw new Error("RegExp has capture groups: "+m+"\nUse (?: … ) instead");if(!E.lineBreaks&&m.test("\n"))throw new Error("Rule should declare lineBreaks: "+m);h.push(i(y))}}var A=o&&o.fallback,g=r&&!A?"ym":"gm",_=r||A?"":"|";return!0===f&&(g+="u"),{regexp:new RegExp(u(h)+_,g),groups:p,fast:l,error:o||s}}function p(e,t,r){var n=e&&(e.push||e.next);if(n&&!r[n])throw new Error("Missing state '"+n+"' (in token '"+e.defaultType+"' of state '"+t+"')");if(e&&e.pop&&1!=+e.pop)throw new Error("pop must be 1 (in token '"+e.defaultType+"' of state '"+t+"')")}var h=function(e,t){this.startState=t,this.states=e,this.buffer="",this.stack=[],this.reset()};h.prototype.reset=function(e,t){return this.buffer=e||"",this.index=0,this.line=t?t.line:1,this.col=t?t.col:1,this.queuedToken=t?t.queuedToken:null,this.queuedThrow=t?t.queuedThrow:null,this.setState(t?t.state:this.startState),this.stack=t&&t.stack?t.stack.slice():[],this},h.prototype.save=function(){return{line:this.line,col:this.col,state:this.state,stack:this.stack.slice(),queuedToken:this.queuedToken,queuedThrow:this.queuedThrow}},h.prototype.setState=function(e){if(e&&this.state!==e){this.state=e;var t=this.states[e];this.groups=t.groups,this.error=t.error,this.re=t.regexp,this.fast=t.fast}},h.prototype.popState=function(){this.setState(this.stack.pop())},h.prototype.pushState=function(e){this.stack.push(this.state),this.setState(e)};var d=r?function(e,t){return e.exec(t)}:function(e,t){var r=e.exec(t);return 0===r[0].length?null:r};function E(){return this.value}if(h.prototype._getGroup=function(e){for(var t=this.groups.length,r=0;r<t;r++)if(void 0!==e[r+1])return this.groups[r];throw new Error("Cannot find token type for matched text")},h.prototype.next=function(){var e=this.index;if(this.queuedGroup){var t=this._token(this.queuedGroup,this.queuedText,e);return this.queuedGroup=null,this.queuedText="",t}var r=this.buffer;if(e!==r.length){if(u=this.fast[r.charCodeAt(e)])return this._token(u,r.charAt(e),e);var n=this.re;n.lastIndex=e;var o=d(n,r),i=this.error;if(null==o)return this._token(i,r.slice(e,r.length),e);var u=this._getGroup(o),a=o[0];return i.fallback&&o.index!==e?(this.queuedGroup=u,this.queuedText=a,this._token(i,r.slice(e,o.index),e)):this._token(u,a,e)}},h.prototype._token=function(e,t,r){var n=0;if(e.lineBreaks){var o=/\n/g,i=1;if("\n"===t)n=1;else for(;o.exec(t);)n++,i=o.lastIndex}var u={type:"function"==typeof e.type&&e.type(t)||e.defaultType,value:"function"==typeof e.value?e.value(t):t,text:t,toString:E,offset:r,lineBreaks:n,line:this.line,col:this.col},a=t.length;if(this.index+=a,this.line+=n,0!==n?this.col=a-i+1:this.col+=a,e.shouldThrow)throw new Error(this.formatError(u,"invalid syntax"));return e.pop?this.popState():e.push?this.pushState(e.push):e.next&&this.setState(e.next),u},"undefined"!=typeof Symbol&&Symbol.iterator){var v=function(e){this.lexer=e};v.prototype.next=function(){var e=this.lexer.next();return{value:e,done:!e}},v.prototype[Symbol.iterator]=function(){return this},h.prototype[Symbol.iterator]=function(){return new v(this)}}return h.prototype.formatError=function(e,t){if(null==e){var r=this.buffer.slice(this.index);e={text:r,offset:this.index,lineBreaks:-1===r.indexOf("\n")?0:1,line:this.line,col:this.col}}var n=Math.max(0,e.offset-e.col+1),o=e.lineBreaks?e.text.indexOf("\n"):e.text.length,i=this.buffer.substring(n,e.offset+o);return t+=" at line "+e.line+" col "+e.col+":\n\n",t+=" "+i+"\n",t+=" "+Array(e.col).join(" ")+"^"},h.prototype.clone=function(){return new h(this.states,this.state)},h.prototype.has=function(e){return!0},{compile:function(e){var t=f(c(e));return new h({start:t},"start")},states:function(e,t){var r=e.$all?c(e.$all):[];delete e.$all;var n=Object.getOwnPropertyNames(e);t||(t=n[0]);for(var o=Object.create(null),i=0;i<n.length;i++)o[y=n[i]]=c(e[y]).concat(r);for(i=0;i<n.length;i++)for(var u=o[y=n[i]],a=Object.create(null),l=0;l<u.length;l++){var s=u[l];if(s.include){var d=[l,1];if(s.include!==y&&!a[s.include]){a[s.include]=!0;var E=o[s.include];if(!E)throw new Error("Cannot include nonexistent state '"+s.include+"' (in state '"+y+"')");for(var v=0;v<E.length;v++){var O=E[v];-1===u.indexOf(O)&&d.push(O)}}u.splice.apply(u,d),l--}}var R=Object.create(null);for(i=0;i<n.length;i++){var y;R[y=n[i]]=f(o[y],!0)}for(i=0;i<n.length;i++){var m=n[i],A=R[m],g=A.groups;for(l=0;l<g.length;l++)p(g[l],m,R);var _=Object.getOwnPropertyNames(A.fast);for(l=0;l<_.length;l++)p(A.fast[_[l]],m,R)}return new h(R,t)},error:Object.freeze({error:!0}),fallback:Object.freeze({fallback:!0}),keywords:function(e){for(var t=Object.create(null),r=Object.create(null),n=Object.getOwnPropertyNames(e),o=0;o<n.length;o++){var i=n[o],u=e[i];(Array.isArray(u)?u:[u]).forEach((function(e){if((r[e.length]=r[e.length]||[]).push(e),"string"!=typeof e)throw new Error("keyword must be string (in keyword '"+i+"')");t[e]=i}))}function a(e){return JSON.stringify(e)}var l="";for(var c in l+="switch (value.length) {\n",r){var s=r[c];l+="case "+c+":\n",l+="switch (value) {\n",s.forEach((function(e){var r=t[e];l+="case "+a(e)+": return "+a(r)+"\n"})),l+="}\n"}return l+="}\n",Function("value",l)}}},e.exports?e.exports=n():r.moo=n()}));function n(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function o(){return(o=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}function i(e,t){return l(e)||function(e,t){if("undefined"==typeof Symbol||!(Symbol.iterator in Object(e)))return;var r=[],n=!0,o=!1,i=void 0;try{for(var u,a=e[Symbol.iterator]();!(n=(u=a.next()).done)&&(r.push(u.value),!t||r.length!==t);n=!0);}catch(e){o=!0,i=e}finally{try{n||null==a.return||a.return()}finally{if(o)throw i}}return r}(e,t)||s(e,t)||p()}function u(e){return l(e)||c(e)||s(e)||p()}function a(e){return function(e){if(Array.isArray(e))return f(e)}(e)||c(e)||s(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function l(e){if(Array.isArray(e))return e}function c(e){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(e))return Array.from(e)}function s(e,t){if(e){if("string"==typeof e)return f(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?f(e,t):void 0}}function f(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function p(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var h,d="G_CODE",E="D_CODE",v="ASTERISK",O="PERCENT",R="COORD_CHAR",y="NUMBER",m="NEWLINE",A=/^0*/,g=function(e){return e.replace(A,"")},_=function(e){return g(e.slice(1))||"0"},T=(n(h={},"T_CODE",{match:/T\d+/,value:_}),n(h,d,{match:/G\d+/,value:_}),n(h,"M_CODE",{match:/M\d+/,value:_}),n(h,E,{match:/D\d+/,value:_}),n(h,v,"*"),n(h,O,"%"),n(h,"EQUALS","="),n(h,"GERBER_FORMAT",{match:/FS[LTDAI]+/,value:function(e){return e.slice(2)}}),n(h,"GERBER_UNITS",{match:/MO(?:IN|MM)/,value:function(e){return e.slice(2)}}),n(h,"GERBER_TOOL_MACRO",{match:/AM[a-zA-Z_.$][\w.-]*/,value:function(e){return e.slice(2)}}),n(h,"GERBER_TOOL_DEF",{match:/ADD\d+[a-zA-Z_.$][\w.-]*/,value:function(e){return g(e.slice(3))}}),n(h,"GERBER_LOAD_POLARITY",{match:/LP[DC]/,value:function(e){return e.slice(2)}}),n(h,"GERBER_STEP_REPEAT","SR"),n(h,"GERBER_MACRO_VARIABLE",/\$\d+/),n(h,"SEMICOLON",";"),n(h,"DRILL_UNITS",/^(?:METRIC|INCH)/),n(h,"DRILL_ZERO_INCLUSION",{match:/,(?:TZ|LZ)/,value:function(e){return e.slice(1)}}),n(h,R,/[XYIJACFSBHZN]/),n(h,y,/(?:[+-])?[\d.]+/),n(h,"OPERATOR",["x","/","+","-","(",")"]),n(h,"COMMA",","),n(h,"WORD",/[a-zA-Z]+/),n(h,"WHITESPACE",/[ \t]+/),n(h,m,{match:/\r?\n/,lineBreaks:!0}),n(h,"CATCHALL",/[\S]/),n(h,"ERROR",r.error),h);function M(){return r.compile(T)}function C(e,t){return{rule:"TOKEN",type:e,value:t}}function N(e,t){return{rule:"TOKEN",type:e,value:t,negate:!0}}function I(e){return{rule:"MIN_TO_MAX",min:1,max:1,match:e}}function L(e){return{rule:"MIN_TO_MAX",min:0,max:1,match:e}}function w(e){return{rule:"MIN_TO_MAX",min:0,max:1/0,match:e}}function b(e){return{rule:"MIN_TO_MAX",min:1,max:1/0,match:e}}function x(e,t,r){return{rule:"MIN_TO_MAX",min:e,max:t,match:r}}function S(e){return e.reduce((function(t,r,n){var o=e[n-1];return r.type===y&&(null==o?void 0:o.type)===R&&(t[o.value.toLowerCase()]=r.value),t}),{})}function D(e){return e.filter((function(e){return e.type===d})).reduce((function(e,t){return"0"===t.value?"move":"1"===t.value?"line":"2"===t.value?"cwArc":"3"===t.value?"ccwArc":"5"===t.value?"drill":e}),null)}function k(e){return e.map((function(e){return e.value})).join("").trim()}function B(e){var t,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=null!==(t=r.head)&&void 0!==t?t:e[0],o=r.length?e[e.indexOf(n)+r.length-1]:e[e.length-1];return{start:{line:n.line,column:n.col,offset:n.offset},end:{line:o.line,column:o.col,offset:o.offset}}}var P={rules:[I([C("DRILL_UNITS"),C("M_CODE","71"),C("M_CODE","72")]),w([C("COMMA"),C("DRILL_ZERO_INCLUSION"),C(y,/^0{1,8}\.0{1,8}$/)]),C(m)],createNodes:function(e){var t="INCH"===e[0].value||"72"===e[0].value?"in":"mm",r=e.filter((function(e){return"DRILL_ZERO_INCLUSION"===e.type})).reduce((function(e,t){return"LZ"===t.value?"trailing":"TZ"===t.value?"leading":e}),null),n=e.filter((function(e){return e.type===y})).reduce((function(e,t){var r=i(t.value.split("."),2),n=r[0],o=void 0===n?"":n,u=r[1],a=void 0===u?"":u;return[o.length,a.length]}),null),o=[{type:"units",position:B(e.slice(0,2)),units:t}];return(r||n)&&o.push({type:"coordinateFormat",position:B(e.slice(1)),mode:null,format:n,zeroSuppression:r}),o}},G={rules:[C("T_CODE"),x(0,12,[C(R,"C"),C(R,"F"),C(R,"S"),C(R,"B"),C(R,"H"),C(R,"Z"),C(y)]),C(m)],createNodes:function(e){var t=e[0].value,r=B(e),n=S(e.slice(1,-1)).c,o=void 0===n?null:n,i=null!==o?{type:"circle",diameter:Number(o)}:null;return i?[{type:"toolDefinition",hole:null,position:r,shape:i,code:t}]:[{type:"toolChange",position:r,code:t}]}},j={rules:[I([C(d,"0"),C(d,"1"),C(d,"2"),C(d,"3"),C(d,"5")]),C(m)],createNodes:function(e){return[{type:"interpolateMode",position:B(e),mode:D(e)}]}},U={rules:[x(0,2,[C("T_CODE"),C(d,"0"),C(d,"1"),C(d,"2"),C(d,"3"),C(d,"5")]),x(2,8,[C(R),C(y)]),L([C("T_CODE")]),C(m)],createNodes:function(e){var t=e.filter((function(e){return e.type===R||e.type===y})),r=e.find((function(e){return e.type===d})),n=e.find((function(e){return"T_CODE"===e.type})),o=S(t),i=n?n.value:null,u=D(e),a=B(e,{head:t[0],length:t.length+1}),l=B(e,{head:r,length:2}),c=B(e,{head:n,length:2}),s=[{type:"graphic",position:a,graphic:null,coordinates:o}];return u&&s.unshift({type:"interpolateMode",position:l,mode:u}),i&&s.unshift({type:"toolChange",position:c,code:i}),s}},H={rules:[x(2,4,[C(R),C(y)]),C(d,"85"),x(2,4,[C(R),C(y)]),C(m)],createNodes:function(e){var t=e.find((function(e){return e.type===d})),r=t?e.indexOf(t):-1,n=S(e.slice(0,r)),o=S(e.slice(r)),i={};return Object.keys(n).forEach((function(e){return i["".concat(e,"1")]=n[e]})),Object.keys(o).forEach((function(e){return i["".concat(e,"2")]=o[e]})),[{type:"graphic",position:B(e),graphic:"slot",coordinates:i}]}},q={rules:[I([C("M_CODE","30"),C("M_CODE","0")]),C(m)],createNodes:function(e){return[{type:"done",position:B(e)}]}},F=[G,j,U,H,{rules:[C("SEMICOLON"),w([N(m)]),C(m)],createNodes:function(e){return[{type:"comment",comment:k(e.slice(1,-1)),position:B(e)}]}},P,q].map((function(e){return o({},e,{filetype:"drill"})}));function X(e,t,r){null===e&&(e={candidates:r,tokens:[]});var n,o=e.candidates,i=[],u=[].concat(a(e.tokens),[t]);for(n=0;n<o.length;n++){var l=o[n],c=Z(l.rules,u);if("FULL_MATCH"===c)return{candidates:[],tokens:u,nodes:l.createNodes(u),filetype:l.filetype};"PARTIAL_MATCH"===c&&i.push(l)}return{candidates:i,tokens:u}}function Z(e,t){for(var r=0,n=0,o=0;r<e.length&&n<t.length;){var i=e[r];if(V(i,t[n]))"TOKEN"===i.rule||"MIN_TO_MAX"===i.rule&&o>=i.max-1?(r++,n++,o=0):"MIN_TO_MAX"===i.rule&&(n++,o++);else{if(!("MIN_TO_MAX"===i.rule&&o>=i.min))return"NO_MATCH";o=0,r++}}return r<e.length?"PARTIAL_MATCH":"FULL_MATCH"}function V(e,t){if("TOKEN"===e.rule){var r=e.type===t.type,n=null==e.value||"string"==typeof e.value&&e.value===t.value||e.value instanceof RegExp&&e.value.test(t.value),o=r&&n;return e.negate?!o:o}return!!Array.isArray(e.match)&&e.match.some((function(e){return V(e,t)}))}var z={rules:[C(y,"0"),w([N(v)]),C(v)],createNodes:function(e){var t=e.slice(1,-1).map((function(e){return e.text})).join("").trim();return[{type:"macroComment",position:B(e),comment:t}]}},W={rules:[C("GERBER_MACRO_VARIABLE"),C("EQUALS"),b([C(y),C("OPERATOR"),C("GERBER_MACRO_VARIABLE"),C(R,"X")]),C(v)],createNodes:function(e){var t=e[0].value,r=$(e.slice(2,-1));return[{type:"macroVariable",position:B(e),name:t,value:r}]}};function $(e){var t=e.map((function(e){return e.type===R?o({},e,{type:"OPERATOR",value:"x"}):e}));return u();function r(){var e;return null!==(e=t[0])&&void 0!==e?e:null}function n(){var e=t.shift();if(e.type===y)return Number(e.value);if("GERBER_MACRO_VARIABLE"===e.type)return e.value;var r=u();return t.shift(),r}function i(){for(var e=n(),o=r();"OPERATOR"===(null===(i=o)||void 0===i?void 0:i.type)&&("x"===o.value||"/"===o.value);){var i;t.shift(),e={left:e,right:n(),operator:o.value},o=r()}return e}function u(){for(var e=i(),n=r();"OPERATOR"===(null===(o=n)||void 0===o?void 0:o.type)&&("+"===n.value||"-"===n.value)||(null===(u=n)||void 0===u?void 0:u.type)===y;){var o,u,a="+";"OPERATOR"===n.type&&(t.shift(),a=n.value),e={left:e,right:i(),operator:a},n=r()}return e}}var Y=[{rules:[C(y),C("COMMA"),b([C("COMMA"),C(y),C("OPERATOR"),C("GERBER_MACRO_VARIABLE"),C(R,"X")]),C(v)],createNodes:function(e){var t=e[0].value,r=e.slice(2,-1).reduce((function(e,t){var r=e[e.length-1];return"COMMA"!==t.type?r.push(t):e.push([]),e}),[[]]).map($);return[{type:"macroPrimitive",position:B(e),code:t,modifiers:r}]}},W,z];function K(e){var t=null,r=[];return e.forEach((function(e){(t=X(t,e,Y)).nodes&&r.push.apply(r,a(t.nodes)),0===t.candidates.length&&(t=null)})),r}var Q=function(e){if(1===e.length)return{type:"circle",diameter:i(e,1)[0]};if(2===e.length){var t=i(e,2);return{type:"rectangle",xSize:t[0],ySize:t[1]}}return null},J={rules:[I([C("M_CODE","0"),C("M_CODE","2")]),C(v)],createNodes:function(e){return[{type:"done",position:B(e)}]}},ee={rules:[C(d,"4"),w([N(v)]),C(v)],createNodes:function(e){return[{type:"comment",position:B(e),comment:k(e.slice(1,-1))}]}},te={rules:[C(O),C("GERBER_FORMAT"),w([N(R,"X")]),C(R,"X"),C(y),C(R,"Y"),C(y),w([N(v)]),C(v),x(0,2,[C("GERBER_UNITS"),C(v)]),C(O)],createNodes:function(e){var t,r=null,n=null,o=null,i=S(e),u=e.findIndex((function(e){return e.type===v})),a=e.find((function(e){return"GERBER_UNITS"===e.type}));if(e.filter((function(e){return"GERBER_FORMAT"===e.type})).forEach((function(e){e.value.indexOf("T")>=0&&(n="trailing"),e.value.indexOf("L")>=0&&(n="leading"),e.value.indexOf("I")>=0&&(o="incremental"),e.value.indexOf("A")>=0&&(o="absolute")})),i.x===i.y&&2===(null===(t=i.x)||void 0===t?void 0:t.length)){var l=Number(i.x[0]),c=Number(i.x[1]);l&&c&&(r=[l,c])}var s=[{type:"coordinateFormat",position:B(e.slice(1,u+1)),zeroSuppression:n,format:r,mode:o}];return a&&s.push({type:"units",position:B(e.slice(1,-1),{head:a}),units:"MM"===a.value?"mm":"in"}),s}},re={rules:[C(O),C("GERBER_UNITS"),C(v),C(O)],createNodes:function(e){return[{type:"units",position:B(e.slice(1,-1)),units:"MM"===e[1].value?"mm":"in"}]}},ne={rules:[C(O),C("GERBER_TOOL_MACRO"),C(v),w([N(O)]),C(O)],createNodes:function(e){var t=e[1].value;return[{type:"toolMacro",position:B(e.slice(1,-1)),children:K(e.slice(3,-1)),name:t}]}},oe={rules:[C(O),C("GERBER_TOOL_DEF"),w([C("COMMA"),C(y),C(R,"X")]),C(v),C(O)],createNodes:function(e){var t,r=null,n=e[1].value.match(/(\d+)(.+)/),o=i(null!=n?n:[],3),a=o[1],l=void 0===a?"":a,c=o[2],s=void 0===c?"":c,f=e.slice(3,-2).filter((function(e){return e.type===y})).map((function(e){return Number(e.value)}));if("C"===s){var p=u(f),h=p[0],d=p.slice(1);t={type:"circle",diameter:h},r=Q(d)}else if("R"===s||"O"===s){var E=u(f),v=E[0],O=E[1],R=E.slice(2);t={type:"R"===s?"rectangle":"obround",xSize:v,ySize:O},r=Q(R)}else if("P"===s){var m=u(f),A=m[0],g=m[1],_=m[2],T=void 0===_?null:_,M=m.slice(3);t={type:"polygon",diameter:A,vertices:g,rotation:T},r=Q(M)}else t={type:"macroShape",name:s,params:f};return[{type:"toolDefinition",position:B(e.slice(1,-1)),code:l,shape:t,hole:r}]}},ie={rules:[L([C(d,"54")]),C(E),C(v)],createNodes:function(e){var t;return[{type:"toolChange",position:B(e),code:null===(t=e.find((function(e){return e.type===E})))||void 0===t?void 0:t.value}]}},ue=function(e){var t=function(e){return e.filter((function(e){return e.type===E})).reduce((function(e,t){return"1"===t.value?"segment":"2"===t.value?"move":"3"===t.value?"shape":e}),null)}(e),r=S(e),n=D(e),o=[{type:"graphic",position:B(e,{head:n?e[1]:e[0]}),graphic:t,coordinates:r}];if(n){var i=B(e,{head:e[0],length:2});o.unshift({type:"interpolateMode",position:i,mode:n})}return o},ae=[{rules:[L([C(d,"1"),C(d,"2"),C(d,"3")]),x(2,8,[C(R),C(y)]),L([C(E,"1"),C(E,"2"),C(E,"3")]),C(v)],createNodes:ue},{rules:[L([C(d,"1"),C(d,"2"),C(d,"3")]),I([C(E,"1"),C(E,"2"),C(E,"3")]),C(v)],createNodes:ue},{rules:[I([C(d,"1"),C(d,"2"),C(d,"3")]),C(v)],createNodes:function(e){return[{type:"interpolateMode",position:B(e),mode:D(e)}]}},ie,oe,ne,ee,{rules:[I([C(d,"36"),C(d,"37")]),C(v)],createNodes:function(e){return[{type:"regionMode",position:B(e),region:"36"===e[0].value}]}},{rules:[I([C(d,"74"),C(d,"75")]),C(v)],createNodes:function(e){return[{type:"quadrantMode",position:B(e),quadrant:"74"===e[0].value?"single":"multi"}]}},{rules:[C(O),C("GERBER_LOAD_POLARITY"),C(v),C(O)],createNodes:function(e){return[{type:"loadPolarity",position:B(e.slice(1,-1)),polarity:"D"===e[1].value?"dark":"clear"}]}},{rules:[C(O),C("GERBER_STEP_REPEAT"),w([C(R),C(y)]),C(v),C(O)],createNodes:function(e){var t=S(e),r=Object.keys(t).reduce((function(e,r){return e[r]=Number(t[r]),e}),{});return[{type:"stepRepeat",position:B(e.slice(1,-1)),stepRepeat:r}]}},te,re,J,{rules:[C(O),w([N(v)]),C(v),C(O)],createNodes:function(e){return[{type:"unimplemented",position:B(e.slice(1,-1)),value:k(e)}]}}].map((function(e){return o({},e,{filetype:"gerber"})})),le=function(){for(var e=arguments.length,t=new Array(e),r=0;r<e;r++)t[r]=arguments[r];return function(e,r){return X(e,r,t)}}.apply(void 0,a(ae).concat(a(F)));e.ABSOLUTE="absolute",e.ASTERISK=v,e.CATCHALL="CATCHALL",e.CCW_ARC="ccwArc",e.CIRCLE="circle",e.CLEAR="clear",e.COMMA="COMMA",e.COMMENT="comment",e.COORDINATE_FORMAT="coordinateFormat",e.COORD_CHAR=R,e.CW_ARC="cwArc",e.DARK="dark",e.DONE="done",e.DRILL="drill",e.DRILL_UNITS="DRILL_UNITS",e.DRILL_ZERO_INCLUSION="DRILL_ZERO_INCLUSION",e.D_CODE=E,e.EQUALS="EQUALS",e.ERROR="ERROR",e.GERBER="gerber",e.GERBER_FORMAT="GERBER_FORMAT",e.GERBER_LOAD_POLARITY="GERBER_LOAD_POLARITY",e.GERBER_MACRO_VARIABLE="GERBER_MACRO_VARIABLE",e.GERBER_STEP_REPEAT="GERBER_STEP_REPEAT",e.GERBER_TOOL_DEF="GERBER_TOOL_DEF",e.GERBER_TOOL_MACRO="GERBER_TOOL_MACRO",e.GERBER_UNITS="GERBER_UNITS",e.GRAPHIC="graphic",e.G_CODE=d,e.IN="in",e.INCREMENTAL="incremental",e.INTERPOLATE_MODE="interpolateMode",e.LEADING="leading",e.LINE="line",e.LOAD_POLARITY="loadPolarity",e.MACRO_CENTER_LINE="21",e.MACRO_CIRCLE="1",e.MACRO_COMMENT="macroComment",e.MACRO_MOIRE="6",e.MACRO_OUTLINE="4",e.MACRO_POLYGON="5",e.MACRO_PRIMITIVE="macroPrimitive",e.MACRO_SHAPE="macroShape",e.MACRO_THERMAL="7",e.MACRO_VARIABLE="macroVariable",e.MACRO_VECTOR_LINE="20",e.MM="mm",e.MOVE="move",e.MULTI="multi",e.M_CODE="M_CODE",e.NEWLINE=m,e.NUMBER=y,e.OBROUND="obround",e.OPERATOR="OPERATOR",e.PERCENT=O,e.POLYGON="polygon",e.QUADRANT_MODE="quadrantMode",e.RECTANGLE="rectangle",e.REGION_MODE="regionMode",e.ROOT="root",e.SEGMENT="segment",e.SEMICOLON="SEMICOLON",e.SHAPE="shape",e.SINGLE="single",e.SLOT="slot",e.STEP_REPEAT="stepRepeat",e.TOOL_CHANGE="toolChange",e.TOOL_DEFINITION="toolDefinition",e.TOOL_MACRO="toolMacro",e.TRAILING="trailing",e.T_CODE="T_CODE",e.UNIMPLEMENTED="unimplemented",e.UNITS="units",e.WHITESPACE="WHITESPACE",e.WORD="WORD",e.createLexer=M,e.createParser=function(){var e=M(),t={type:"root",filetype:null,done:!1,children:[]},r="",n=0,i=e.save();return{lexer:e,feed:function(u){var l,c=r,s=n,f=null;r="",e.reset("".concat(c).concat(u),i);for(;l=e.next();){var p=o({},l,{offset:l.offset+s});if(r+=l.text,(f=le(f,p)).nodes){var h,d,E,v=f,O=v.nodes,R=v.filetype,y=void 0===R?null:R;r="",n+=null!==(h=e.index)&&void 0!==h?h:0,i=e.save(),(d=t.children).push.apply(d,a(O)),t.filetype=null!==(E=t.filetype)&&void 0!==E?E:y,t.done=t.done||O.some((function(e){return"done"===e.type}))}0===f.candidates.length&&(f=null)}},results:function(){return t}}},Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=parser.min.js.map