@intlify/message-compiler-edge
Version:
@intlify/message-compiler
7 lines (6 loc) • 16.2 kB
JavaScript
/*!
* message-compiler v9.3.0-beta.14
* (c) 2023 kazuya kawaguchi
* Released under the MIT License.
*/
const assign=Object.assign,isString=e=>"string"==typeof e,CompileErrorCodes={EXPECTED_TOKEN:1,INVALID_TOKEN_IN_PLACEHOLDER:2,UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER:3,UNKNOWN_ESCAPE_SEQUENCE:4,INVALID_UNICODE_ESCAPE_SEQUENCE:5,UNBALANCED_CLOSING_BRACE:6,UNTERMINATED_CLOSING_BRACE:7,EMPTY_PLACEHOLDER:8,NOT_ALLOW_NEST_PLACEHOLDER:9,INVALID_LINKED_FORMAT:10,MUST_HAVE_MESSAGES_IN_PLURAL:11,UNEXPECTED_EMPTY_LINKED_MODIFIER:12,UNEXPECTED_EMPTY_LINKED_KEY:13,UNEXPECTED_LEXICAL_ANALYSIS:14,__EXTEND_POINT__:15},errorMessages={[CompileErrorCodes.EXPECTED_TOKEN]:"Expected token: '{0}'",[CompileErrorCodes.INVALID_TOKEN_IN_PLACEHOLDER]:"Invalid token in placeholder: '{0}'",[CompileErrorCodes.UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER]:"Unterminated single quote in placeholder",[CompileErrorCodes.UNKNOWN_ESCAPE_SEQUENCE]:"Unknown escape sequence: \\{0}",[CompileErrorCodes.INVALID_UNICODE_ESCAPE_SEQUENCE]:"Invalid unicode escape sequence: {0}",[CompileErrorCodes.UNBALANCED_CLOSING_BRACE]:"Unbalanced closing brace",[CompileErrorCodes.UNTERMINATED_CLOSING_BRACE]:"Unterminated closing brace",[CompileErrorCodes.EMPTY_PLACEHOLDER]:"Empty placeholder",[CompileErrorCodes.NOT_ALLOW_NEST_PLACEHOLDER]:"Not allowed nest placeholder",[CompileErrorCodes.INVALID_LINKED_FORMAT]:"Invalid linked format",[CompileErrorCodes.MUST_HAVE_MESSAGES_IN_PLURAL]:"Plural must have messages",[CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_MODIFIER]:"Unexpected empty linked modifier",[CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_KEY]:"Unexpected empty linked key",[CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS]:"Unexpected lexical analysis in token: '{0}'"};function createCompileError(e,t,n={}){const{domain:r,messages:o,args:c}=n,s=new SyntaxError(String(e));return s.code=e,t&&(s.location=t),s.domain=r,s}function defaultOnError(e){throw e}const LocationStub={start:{line:1,column:1,offset:0},end:{line:1,column:1,offset:0}};function createPosition(e,t,n){return{line:e,column:t,offset:n}}function createLocation(e,t,n){const r={start:e,end:t};return null!=n&&(r.source=n),r}const CHAR_SP=" ",CHAR_CR="\r",CHAR_LF="\n",CHAR_LS=String.fromCharCode(8232),CHAR_PS=String.fromCharCode(8233);function createScanner(e){const t=e;let n=0,r=1,o=1,c=0;const s=e=>"\r"===t[e]&&"\n"===t[e+1],u=e=>t[e]===CHAR_PS,a=e=>t[e]===CHAR_LS,i=e=>s(e)||(e=>"\n"===t[e])(e)||u(e)||a(e),l=e=>s(e)||u(e)||a(e)?"\n":t[e];function f(){return c=0,i(n)&&(r++,o=0),s(n)&&n++,n++,o++,t[n]}return{index:()=>n,line:()=>r,column:()=>o,peekOffset:()=>c,charAt:l,currentChar:()=>l(n),currentPeek:()=>l(n+c),next:f,peek:function(){return s(n+c)&&c++,c++,t[n+c]},reset:function(){n=0,r=1,o=1,c=0},resetPeek:function(e=0){c=e},skipToPeek:function(){const e=n+c;for(;e!==n;)f();c=0}}}const EOF=void 0,LITERAL_DELIMITER="'",ERROR_DOMAIN$1="tokenizer";function createTokenizer(e,t={}){const n=!1!==t.location,r=createScanner(e),o=()=>r.index(),c=()=>createPosition(r.line(),r.column(),r.index()),s=c(),u=o(),a={currentType:14,offset:u,startLoc:s,endLoc:s,lastType:14,lastOffset:u,lastStartLoc:s,lastEndLoc:s,braceNest:0,inLinked:!1,text:""},i=()=>a,{onError:l}=t;function f(e,t,r){e.endLoc=c(),e.currentType=t;const o={type:t};return n&&(o.loc=createLocation(e.startLoc,e.endLoc)),null!=r&&(o.value=r),o}const d=e=>f(e,14);function p(e,t){return e.currentChar()===t?(e.next(),t):(c(),"")}function E(e){let t="";for(;" "===e.currentPeek()||"\n"===e.currentPeek();)t+=e.currentPeek(),e.peek();return t}function C(e){const t=E(e);return e.skipToPeek(),t}function k(e){if(e===EOF)return!1;const t=e.charCodeAt(0);return t>=97&&t<=122||t>=65&&t<=90||95===t}function L(e,t){const{currentType:n}=t;if(2!==n)return!1;E(e);const r=function(e){if(e===EOF)return!1;const t=e.charCodeAt(0);return t>=48&&t<=57}("-"===e.currentPeek()?e.peek():e.currentPeek());return e.resetPeek(),r}function N(e){E(e);const t="|"===e.currentPeek();return e.resetPeek(),t}function h(e,t=!0){const n=(t=!1,r="",o=!1)=>{const c=e.currentPeek();return"{"===c?"%"!==r&&t:"@"!==c&&c?"%"===c?(e.peek(),n(t,"%",!0)):"|"===c?!("%"!==r&&!o)||!(" "===r||"\n"===r):" "===c?(e.peek(),n(!0," ",o)):"\n"!==c||(e.peek(),n(!0,"\n",o)):"%"===r||t},r=n();return t&&e.resetPeek(),r}function T(e,t){const n=e.currentChar();return n===EOF?EOF:t(n)?(e.next(),n):null}function _(e){return T(e,(e=>{const t=e.charCodeAt(0);return t>=97&&t<=122||t>=65&&t<=90||t>=48&&t<=57||95===t||36===t}))}function m(e){return T(e,(e=>{const t=e.charCodeAt(0);return t>=48&&t<=57}))}function P(e){return T(e,(e=>{const t=e.charCodeAt(0);return t>=48&&t<=57||t>=65&&t<=70||t>=97&&t<=102}))}function S(e){let t="",n="";for(;t=m(e);)n+=t;return n}function O(e){let t="";for(;;){const n=e.currentChar();if("{"===n||"}"===n||"@"===n||"|"===n||!n)break;if("%"===n){if(!h(e))break;t+=n,e.next()}else if(" "===n||"\n"===n)if(h(e))t+=n,e.next();else{if(N(e))break;t+=n,e.next()}else t+=n,e.next()}return t}function A(e){const t=e.currentChar();switch(t){case"\\":case"'":return e.next(),`\\${t}`;case"u":return x(e,t,4);case"U":return x(e,t,6);default:return c(),""}}function x(e,t,n){p(e,t);let r="";for(let t=0;t<n;t++){const t=P(e);if(!t){c(),e.currentChar();break}r+=t}return`\\${t}${r}`}function y(e){C(e);const t=p(e,"|");return C(e),t}function g(e,t){let n=null;switch(e.currentChar()){case"{":return t.braceNest>=1&&c(),e.next(),n=f(t,2,"{"),C(e),t.braceNest++,n;case"}":return t.braceNest>0&&2===t.currentType&&c(),e.next(),n=f(t,3,"}"),t.braceNest--,t.braceNest>0&&C(e),t.inLinked&&0===t.braceNest&&(t.inLinked=!1),n;case"@":return t.braceNest>0&&c(),n=I(e,t)||d(t),t.braceNest=0,n;default:let r=!0,o=!0,s=!0;if(N(e))return t.braceNest>0&&c(),n=f(t,1,y(e)),t.braceNest=0,t.inLinked=!1,n;if(t.braceNest>0&&(5===t.currentType||6===t.currentType||7===t.currentType))return c(),t.braceNest=0,b(e,t);if(r=function(e,t){const{currentType:n}=t;if(2!==n)return!1;E(e);const r=k(e.currentPeek());return e.resetPeek(),r}(e,t))return n=f(t,5,function(e){C(e);let t="",n="";for(;t=_(e);)n+=t;return e.currentChar()===EOF&&c(),n}(e)),C(e),n;if(o=L(e,t))return n=f(t,6,function(e){C(e);let t="";return"-"===e.currentChar()?(e.next(),t+=`-${S(e)}`):t+=S(e),e.currentChar()===EOF&&c(),t}(e)),C(e),n;if(s=function(e,t){const{currentType:n}=t;if(2!==n)return!1;E(e);const r="'"===e.currentPeek();return e.resetPeek(),r}(e,t))return n=f(t,7,function(e){C(e),p(e,"'");let t="",n="";const r=e=>"'"!==e&&"\n"!==e;for(;t=T(e,r);)n+="\\"===t?A(e):t;const o=e.currentChar();return"\n"===o||o===EOF?(c(),"\n"===o&&(e.next(),p(e,"'")),n):(p(e,"'"),n)}(e)),C(e),n;if(!r&&!o&&!s)return n=f(t,13,function(e){C(e);let t="",n="";const r=e=>"{"!==e&&"}"!==e&&" "!==e&&"\n"!==e;for(;t=T(e,r);)n+=t;return n}(e)),c(),n.value,C(e),n}return n}function I(e,t){const{currentType:n}=t;let r=null;const o=e.currentChar();switch(8!==n&&9!==n&&12!==n&&10!==n||"\n"!==o&&" "!==o||c(),o){case"@":return e.next(),r=f(t,8,"@"),t.inLinked=!0,r;case".":return C(e),e.next(),f(t,9,".");case":":return C(e),e.next(),f(t,10,":");default:return N(e)?(r=f(t,1,y(e)),t.braceNest=0,t.inLinked=!1,r):function(e,t){const{currentType:n}=t;if(8!==n)return!1;E(e);const r="."===e.currentPeek();return e.resetPeek(),r}(e,t)||function(e,t){const{currentType:n}=t;if(8!==n&&12!==n)return!1;E(e);const r=":"===e.currentPeek();return e.resetPeek(),r}(e,t)?(C(e),I(e,t)):function(e,t){const{currentType:n}=t;if(9!==n)return!1;E(e);const r=k(e.currentPeek());return e.resetPeek(),r}(e,t)?(C(e),f(t,12,function(e){let t="",n="";for(;t=_(e);)n+=t;return n}(e))):function(e,t){const{currentType:n}=t;if(10!==n)return!1;const r=()=>{const t=e.currentPeek();return"{"===t?k(e.peek()):!("@"===t||"%"===t||"|"===t||":"===t||"."===t||" "===t||!t)&&("\n"===t?(e.peek(),r()):k(t))},o=r();return e.resetPeek(),o}(e,t)?(C(e),"{"===o?g(e,t)||r:f(t,11,function(e){const t=(n=!1,r)=>{const o=e.currentChar();return"{"!==o&&"%"!==o&&"@"!==o&&"|"!==o&&o?" "===o?r:"\n"===o?(r+=o,e.next(),t(n,r)):(r+=o,e.next(),t(!0,r)):r};return t(!1,"")}(e))):(8===n&&c(),t.braceNest=0,t.inLinked=!1,b(e,t))}}function b(e,t){let n={type:14};if(t.braceNest>0)return g(e,t)||d(t);if(t.inLinked)return I(e,t)||d(t);switch(e.currentChar()){case"{":return g(e,t)||d(t);case"}":return c(),e.next(),f(t,3,"}");case"@":return I(e,t)||d(t);default:if(N(e))return n=f(t,1,y(e)),t.braceNest=0,t.inLinked=!1,n;const{isModulo:r,hasSpace:o}=function(e){const t=E(e),n="%"===e.currentPeek()&&"{"===e.peek();return e.resetPeek(),{isModulo:n,hasSpace:t.length>0}}(e);if(r)return o?f(t,0,O(e)):f(t,4,function(e){C(e);return"%"!==e.currentChar()&&c(),e.next(),"%"}(e));if(h(e))return f(t,0,O(e))}return n}return{nextToken:function(){const{currentType:e,offset:t,startLoc:n,endLoc:s}=a;return a.lastType=e,a.lastOffset=t,a.lastStartLoc=n,a.lastEndLoc=s,a.offset=o(),a.startLoc=c(),r.currentChar()===EOF?f(a,14):b(r,a)},currentOffset:o,currentPosition:c,context:i}}const ERROR_DOMAIN="parser",KNOWN_ESCAPES=/(?:\\\\|\\'|\\u([0-9a-fA-F]{4})|\\U([0-9a-fA-F]{6}))/g;function fromEscapeSequence(e,t,n){switch(e){case"\\\\":return"\\";case"\\'":return"'";default:{const e=parseInt(t||n,16);return e<=55295||e>=57344?String.fromCodePoint(e):"�"}}}function createParser(e={}){const t=!1!==e.location,{onError:n}=e;function r(e,n,r){const o={type:e,start:n,end:n};return t&&(o.loc={start:r,end:r}),o}function o(e,n,r,o){e.end=n,o&&(e.type=o),t&&e.loc&&(e.loc.end=r)}function c(e,t){const n=e.context(),c=r(3,n.offset,n.startLoc);return c.value=t,o(c,e.currentOffset(),e.currentPosition()),c}function s(e,t){const n=e.context(),{lastOffset:c,lastStartLoc:s}=n,u=r(5,c,s);return u.index=parseInt(t,10),e.nextToken(),o(u,e.currentOffset(),e.currentPosition()),u}function u(e,t){const n=e.context(),{lastOffset:c,lastStartLoc:s}=n,u=r(4,c,s);return u.key=t,e.nextToken(),o(u,e.currentOffset(),e.currentPosition()),u}function a(e,t){const n=e.context(),{lastOffset:c,lastStartLoc:s}=n,u=r(9,c,s);return u.value=t.replace(KNOWN_ESCAPES,fromEscapeSequence),e.nextToken(),o(u,e.currentOffset(),e.currentPosition()),u}function i(e){const t=e.context(),n=r(6,t.offset,t.startLoc);let c=e.nextToken();if(9===c.type){const t=function(e){const t=e.nextToken(),n=e.context(),{lastOffset:c,lastStartLoc:s}=n,u=r(8,c,s);return 12!==t.type?(n.lastStartLoc,u.value="",o(u,c,s),{nextConsumeToken:t,node:u}):(null==t.value&&(n.lastStartLoc,getTokenCaption(t)),u.value=t.value||"",o(u,e.currentOffset(),e.currentPosition()),{node:u})}(e);n.modifier=t.node,c=t.nextConsumeToken||e.nextToken()}switch(10!==c.type&&(t.lastStartLoc,getTokenCaption(c)),c=e.nextToken(),2===c.type&&(c=e.nextToken()),c.type){case 11:null==c.value&&(t.lastStartLoc,getTokenCaption(c)),n.key=function(e,t){const n=e.context(),c=r(7,n.offset,n.startLoc);return c.value=t,o(c,e.currentOffset(),e.currentPosition()),c}(e,c.value||"");break;case 5:null==c.value&&(t.lastStartLoc,getTokenCaption(c)),n.key=u(e,c.value||"");break;case 6:null==c.value&&(t.lastStartLoc,getTokenCaption(c)),n.key=s(e,c.value||"");break;case 7:null==c.value&&(t.lastStartLoc,getTokenCaption(c)),n.key=a(e,c.value||"");break;default:t.lastStartLoc;const i=e.context(),l=r(7,i.offset,i.startLoc);return l.value="",o(l,i.offset,i.startLoc),n.key=l,o(n,i.offset,i.startLoc),{nextConsumeToken:c,node:n}}return o(n,e.currentOffset(),e.currentPosition()),{node:n}}function l(e){const t=e.context(),n=r(2,1===t.currentType?e.currentOffset():t.offset,1===t.currentType?t.endLoc:t.startLoc);n.items=[];let l=null;do{const r=l||e.nextToken();switch(l=null,r.type){case 0:null==r.value&&(t.lastStartLoc,getTokenCaption(r)),n.items.push(c(e,r.value||""));break;case 6:null==r.value&&(t.lastStartLoc,getTokenCaption(r)),n.items.push(s(e,r.value||""));break;case 5:null==r.value&&(t.lastStartLoc,getTokenCaption(r)),n.items.push(u(e,r.value||""));break;case 7:null==r.value&&(t.lastStartLoc,getTokenCaption(r)),n.items.push(a(e,r.value||""));break;case 8:const o=i(e);n.items.push(o.node),l=o.nextConsumeToken||null}}while(14!==t.currentType&&1!==t.currentType);return o(n,1===t.currentType?t.lastOffset:e.currentOffset(),1===t.currentType?t.lastEndLoc:e.currentPosition()),n}function f(e){const t=e.context(),{offset:n,startLoc:c}=t,s=l(e);return 14===t.currentType?s:function(e,t,n,c){const s=e.context();let u=0===c.items.length;const a=r(1,t,n);a.cases=[],a.cases.push(c);do{const t=l(e);u||(u=0===t.items.length),a.cases.push(t)}while(14!==s.currentType);return o(a,e.currentOffset(),e.currentPosition()),a}(e,n,c,s)}return{parse:function(n){const c=createTokenizer(n,assign({},e)),s=c.context(),u=r(0,s.offset,s.startLoc);return t&&u.loc&&(u.loc.source=n),u.body=f(c),14!==s.currentType&&(s.lastStartLoc,n[s.offset]),o(u,c.currentOffset(),c.currentPosition()),u}}}function getTokenCaption(e){if(14===e.type)return"EOF";const t=(e.value||"").replace(/\r?\n/gu,"\\n");return t.length>10?t.slice(0,9)+"…":t}function createTransformer(e,t={}){const n={ast:e,helpers:new Set};return{context:()=>n,helper:e=>(n.helpers.add(e),e)}}function traverseNodes(e,t){for(let n=0;n<e.length;n++)traverseNode(e[n],t)}function traverseNode(e,t){switch(e.type){case 1:traverseNodes(e.cases,t),t.helper("plural");break;case 2:traverseNodes(e.items,t);break;case 6:traverseNode(e.key,t),t.helper("linked"),t.helper("type");break;case 5:t.helper("interpolate"),t.helper("list");break;case 4:t.helper("interpolate"),t.helper("named")}}function transform(e,t={}){const n=createTransformer(e);n.helper("normalize"),e.body&&traverseNode(e.body,n);const r=n.context();e.helpers=Array.from(r.helpers)}function createCodeGenerator(e,t){const{sourceMap:n,filename:r,breakLineCode:o,needIndent:c}=t,s={source:e.loc.source,filename:r,code:"",column:1,line:1,offset:0,map:void 0,breakLineCode:o,needIndent:c,indentLevel:0};function u(e,t){s.code+=e}function a(e,t=!0){const n=t?o:"";u(c?n+" ".repeat(e):n)}return{context:()=>s,push:u,indent:function(e=!0){const t=++s.indentLevel;e&&a(t)},deindent:function(e=!0){const t=--s.indentLevel;e&&a(t)},newline:function(){a(s.indentLevel)},helper:e=>`_${e}`,needIndent:()=>s.needIndent}}function generateLinkedNode(e,t){const{helper:n}=e;e.push(`${n("linked")}(`),generateNode(e,t.key),t.modifier?(e.push(", "),generateNode(e,t.modifier),e.push(", _type")):e.push(", undefined, _type"),e.push(")")}function generateMessageNode(e,t){const{helper:n,needIndent:r}=e;e.push(`${n("normalize")}([`),e.indent(r());const o=t.items.length;for(let n=0;n<o&&(generateNode(e,t.items[n]),n!==o-1);n++)e.push(", ");e.deindent(r()),e.push("])")}function generatePluralNode(e,t){const{helper:n,needIndent:r}=e;if(t.cases.length>1){e.push(`${n("plural")}([`),e.indent(r());const o=t.cases.length;for(let n=0;n<o&&(generateNode(e,t.cases[n]),n!==o-1);n++)e.push(", ");e.deindent(r()),e.push("])")}}function generateResource(e,t){t.body?generateNode(e,t.body):e.push("null")}function generateNode(e,t){const{helper:n}=e;switch(t.type){case 0:generateResource(e,t);break;case 1:generatePluralNode(e,t);break;case 2:generateMessageNode(e,t);break;case 6:generateLinkedNode(e,t);break;case 8:case 7:case 9:case 3:e.push(JSON.stringify(t.value),t);break;case 5:e.push(`${n("interpolate")}(${n("list")}(${t.index}))`,t);break;case 4:e.push(`${n("interpolate")}(${n("named")}(${JSON.stringify(t.key)}))`,t)}}const generate=(e,t={})=>{const n=isString(t.mode)?t.mode:"normal",r=isString(t.filename)?t.filename:"message.intl",o=!!t.sourceMap,c=null!=t.breakLineCode?t.breakLineCode:"arrow"===n?";":"\n",s=t.needIndent?t.needIndent:"arrow"!==n,u=e.helpers||[],a=createCodeGenerator(e,{mode:n,filename:r,sourceMap:o,breakLineCode:c,needIndent:s});a.push("normal"===n?"function __msg__ (ctx) {":"(ctx) => {"),a.indent(s),u.length>0&&(a.push(`const { ${u.map((e=>`${e}: _${e}`)).join(", ")} } = ctx`),a.newline()),a.push("return "),generateNode(a,e),a.deindent(s),a.push("}");const{code:i,map:l}=a.context();return{ast:e,code:i,map:l?l.toJSON():void 0}};function baseCompile(e,t={}){const n=assign({},t),r=createParser(n).parse(e);return transform(r,n),generate(r,n)}export{CompileErrorCodes,ERROR_DOMAIN,LocationStub,baseCompile,createCompileError,createLocation,createParser,createPosition,defaultOnError,errorMessages};