import{Number as t,Ident as s,Dimension as i,WhiteSpace as e,Comment as h}from"../../tokenizer/types.mjs";import{isDigit as n}from"../../tokenizer/char-code-definitions.mjs";import"../../tokenizer/TokenStream.mjs";const o=43,r=45,l=110,a=!0;function c(t,s){let i=this.tokenStart+t;const e=this.charCodeAt(i);for(e!==o&&e!==r||(s&&this.error("Number sign is not allowed"),i++);i<this.tokenEnd;i++)n(this.charCodeAt(i))||this.error("Integer is expected",i)}function k(t){return c.call(this,0,t)}function u(t,s){if(!this.cmpChar(this.tokenStart+t,s)){let i="";switch(s){case l:i="N is expected";break;case r:i="HyphenMinus is expected"}this.error(i,this.tokenStart+t)}}function p(){let s=0,i=0,n=this.tokenType;for(;n===e||n===h;)n=this.lookupType(++s);if(n!==t){if(!this.isDelim(o,s)&&!this.isDelim(r,s))return null;i=this.isDelim(o,s)?o:r;do{n=this.lookupType(++s)}while(n===e||n===h);n!==t&&(this.skip(s),k.call(this,a))}return s>0&&this.skip(s),0===i&&(n=this.charCodeAt(this.tokenStart),n!==o&&n!==r&&this.error("Number sign is expected")),k.call(this,0!==i),i===r?"-"+this.consume(t):this.consume(t)}const d="AnPlusB",f={a:[String,null],b:[String,null]};function m(){const e=this.tokenStart;let h=null,d=null;if(this.tokenType===t)k.call(this,false),d=this.consume(t);else if(this.tokenType===s&&this.cmpChar(this.tokenStart,r))switch(h="-1",u.call(this,1,l),this.tokenEnd-this.tokenStart){case 2:this.next(),d=p.call(this);break;case 3:u.call(this,2,r),this.next(),this.skipSC(),k.call(this,a),d="-"+this.consume(t);break;default:u.call(this,2,r),c.call(this,3,a),this.next(),d=this.substrToCursor(e+2)}else if(this.tokenType===s||this.isDelim(o)&&this.lookupType(1)===s){let s=0;switch(h="1",this.isDelim(o)&&(s=1,this.next()),u.call(this,0,l),this.tokenEnd-this.tokenStart){case 1:this.next(),d=p.call(this);break;case 2:u.call(this,1,r),this.next(),this.skipSC(),k.call(this,a),d="-"+this.consume(t);break;default:u.call(this,1,r),c.call(this,2,a),this.next(),d=this.substrToCursor(e+s+1)}}else if(this.tokenType===i){const s=this.charCodeAt(this.tokenStart),i=s===o||s===r;let f=this.tokenStart+i;for(;f<this.tokenEnd&&n(this.charCodeAt(f));f++);f===this.tokenStart+i&&this.error("Integer is expected",this.tokenStart+i),u.call(this,f-this.tokenStart,l),h=this.substring(e,f),f+1===this.tokenEnd?(this.next(),d=p.call(this)):(u.call(this,f-this.tokenStart+1,r),f+2===this.tokenEnd?(this.next(),this.skipSC(),k.call(this,a),d="-"+this.consume(t)):(c.call(this,f-this.tokenStart+2,a),this.next(),d=this.substrToCursor(f+1)))}else this.error();return null!==h&&h.charCodeAt(0)===o&&(h=h.substr(1)),null!==d&&d.charCodeAt(0)===o&&(d=d.substr(1)),{type:"AnPlusB",loc:this.getLocation(e,this.tokenStart),a:h,b:d}}function S(t){if(t.a){const s=("+1"===t.a||"1"===t.a?"n":"-1"===t.a&&"-n")||t.a+"n";if(t.b){const i="-"===t.b[0]||"+"===t.b[0]?t.b:"+"+t.b;this.tokenize(s+i)}else this.tokenize(s)}else this.tokenize(t.b)}export{S as generate,d as name,m as parse,f as structure};