message-compiler.esm-browser.js 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  1. /*!
  2. * message-compiler v11.1.3
  3. * (c) 2025 kazuya kawaguchi
  4. * Released under the MIT License.
  5. */
  6. const LOCATION_STUB = {
  7. start: { line: 1, column: 1, offset: 0 },
  8. end: { line: 1, column: 1, offset: 0 }
  9. };
  10. function createPosition(line, column, offset) {
  11. return { line, column, offset };
  12. }
  13. function createLocation(start, end, source) {
  14. const loc = { start, end };
  15. if (source != null) {
  16. loc.source = source;
  17. }
  18. return loc;
  19. }
  20. /**
  21. * Original Utilities
  22. * written by kazuya kawaguchi
  23. */
  24. const RE_ARGS = /\{([0-9a-zA-Z]+)\}/g;
  25. /* eslint-disable */
  26. function format(message, ...args) {
  27. if (args.length === 1 && isObject(args[0])) {
  28. args = args[0];
  29. }
  30. if (!args || !args.hasOwnProperty) {
  31. args = {};
  32. }
  33. return message.replace(RE_ARGS, (match, identifier) => {
  34. return args.hasOwnProperty(identifier) ? args[identifier] : '';
  35. });
  36. }
  37. const assign = Object.assign;
  38. const isString = (val) => typeof val === 'string';
  39. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  40. const isObject = (val) => val !== null && typeof val === 'object';
  41. function join(items, separator = '') {
  42. return items.reduce((str, item, index) => (index === 0 ? str + item : str + separator + item), '');
  43. }
  44. const CompileErrorCodes = {
  45. // tokenizer error codes
  46. EXPECTED_TOKEN: 1,
  47. INVALID_TOKEN_IN_PLACEHOLDER: 2,
  48. UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER: 3,
  49. UNKNOWN_ESCAPE_SEQUENCE: 4,
  50. INVALID_UNICODE_ESCAPE_SEQUENCE: 5,
  51. UNBALANCED_CLOSING_BRACE: 6,
  52. UNTERMINATED_CLOSING_BRACE: 7,
  53. EMPTY_PLACEHOLDER: 8,
  54. NOT_ALLOW_NEST_PLACEHOLDER: 9,
  55. INVALID_LINKED_FORMAT: 10,
  56. // parser error codes
  57. MUST_HAVE_MESSAGES_IN_PLURAL: 11,
  58. UNEXPECTED_EMPTY_LINKED_MODIFIER: 12,
  59. UNEXPECTED_EMPTY_LINKED_KEY: 13,
  60. UNEXPECTED_LEXICAL_ANALYSIS: 14,
  61. // generator error codes
  62. UNHANDLED_CODEGEN_NODE_TYPE: 15,
  63. // minifier error codes
  64. UNHANDLED_MINIFIER_NODE_TYPE: 16
  65. };
  66. // Special value for higher-order compilers to pick up the last code
  67. // to avoid collision of error codes.
  68. // This should always be kept as the last item.
  69. const COMPILE_ERROR_CODES_EXTEND_POINT = 17;
  70. /** @internal */
  71. const errorMessages = {
  72. // tokenizer error messages
  73. [CompileErrorCodes.EXPECTED_TOKEN]: `Expected token: '{0}'`,
  74. [CompileErrorCodes.INVALID_TOKEN_IN_PLACEHOLDER]: `Invalid token in placeholder: '{0}'`,
  75. [CompileErrorCodes.UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER]: `Unterminated single quote in placeholder`,
  76. [CompileErrorCodes.UNKNOWN_ESCAPE_SEQUENCE]: `Unknown escape sequence: \\{0}`,
  77. [CompileErrorCodes.INVALID_UNICODE_ESCAPE_SEQUENCE]: `Invalid unicode escape sequence: {0}`,
  78. [CompileErrorCodes.UNBALANCED_CLOSING_BRACE]: `Unbalanced closing brace`,
  79. [CompileErrorCodes.UNTERMINATED_CLOSING_BRACE]: `Unterminated closing brace`,
  80. [CompileErrorCodes.EMPTY_PLACEHOLDER]: `Empty placeholder`,
  81. [CompileErrorCodes.NOT_ALLOW_NEST_PLACEHOLDER]: `Not allowed nest placeholder`,
  82. [CompileErrorCodes.INVALID_LINKED_FORMAT]: `Invalid linked format`,
  83. // parser error messages
  84. [CompileErrorCodes.MUST_HAVE_MESSAGES_IN_PLURAL]: `Plural must have messages`,
  85. [CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_MODIFIER]: `Unexpected empty linked modifier`,
  86. [CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_KEY]: `Unexpected empty linked key`,
  87. [CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS]: `Unexpected lexical analysis in token: '{0}'`,
  88. // generator error messages
  89. [CompileErrorCodes.UNHANDLED_CODEGEN_NODE_TYPE]: `unhandled codegen node type: '{0}'`,
  90. // minimizer error messages
  91. [CompileErrorCodes.UNHANDLED_MINIFIER_NODE_TYPE]: `unhandled mimifier node type: '{0}'`
  92. };
  93. function createCompileError(code, loc, options = {}) {
  94. const { domain, messages, args } = options;
  95. const msg = format((messages || errorMessages)[code] || '', ...(args || []))
  96. ;
  97. const error = new SyntaxError(String(msg));
  98. error.code = code;
  99. if (loc) {
  100. error.location = loc;
  101. }
  102. error.domain = domain;
  103. return error;
  104. }
  105. /** @internal */
  106. function defaultOnError(error) {
  107. throw error;
  108. }
  109. // eslint-disable-next-line no-useless-escape
  110. const RE_HTML_TAG = /<\/?[\w\s="/.':;#-\/]+>/;
  111. const detectHtmlTag = (source) => RE_HTML_TAG.test(source);
  112. const CHAR_SP = ' ';
  113. const CHAR_CR = '\r';
  114. const CHAR_LF = '\n';
  115. const CHAR_LS = String.fromCharCode(0x2028);
  116. const CHAR_PS = String.fromCharCode(0x2029);
  117. function createScanner(str) {
  118. const _buf = str;
  119. let _index = 0;
  120. let _line = 1;
  121. let _column = 1;
  122. let _peekOffset = 0;
  123. const isCRLF = (index) => _buf[index] === CHAR_CR && _buf[index + 1] === CHAR_LF;
  124. const isLF = (index) => _buf[index] === CHAR_LF;
  125. const isPS = (index) => _buf[index] === CHAR_PS;
  126. const isLS = (index) => _buf[index] === CHAR_LS;
  127. const isLineEnd = (index) => isCRLF(index) || isLF(index) || isPS(index) || isLS(index);
  128. const index = () => _index;
  129. const line = () => _line;
  130. const column = () => _column;
  131. const peekOffset = () => _peekOffset;
  132. const charAt = (offset) => isCRLF(offset) || isPS(offset) || isLS(offset) ? CHAR_LF : _buf[offset];
  133. const currentChar = () => charAt(_index);
  134. const currentPeek = () => charAt(_index + _peekOffset);
  135. function next() {
  136. _peekOffset = 0;
  137. if (isLineEnd(_index)) {
  138. _line++;
  139. _column = 0;
  140. }
  141. if (isCRLF(_index)) {
  142. _index++;
  143. }
  144. _index++;
  145. _column++;
  146. return _buf[_index];
  147. }
  148. function peek() {
  149. if (isCRLF(_index + _peekOffset)) {
  150. _peekOffset++;
  151. }
  152. _peekOffset++;
  153. return _buf[_index + _peekOffset];
  154. }
  155. function reset() {
  156. _index = 0;
  157. _line = 1;
  158. _column = 1;
  159. _peekOffset = 0;
  160. }
  161. function resetPeek(offset = 0) {
  162. _peekOffset = offset;
  163. }
  164. function skipToPeek() {
  165. const target = _index + _peekOffset;
  166. while (target !== _index) {
  167. next();
  168. }
  169. _peekOffset = 0;
  170. }
  171. return {
  172. index,
  173. line,
  174. column,
  175. peekOffset,
  176. charAt,
  177. currentChar,
  178. currentPeek,
  179. next,
  180. peek,
  181. reset,
  182. resetPeek,
  183. skipToPeek
  184. };
  185. }
  186. const EOF = undefined;
  187. const DOT = '.';
  188. const LITERAL_DELIMITER = "'";
  189. const ERROR_DOMAIN$3 = 'tokenizer';
  190. function createTokenizer(source, options = {}) {
  191. const location = options.location !== false;
  192. const _scnr = createScanner(source);
  193. const currentOffset = () => _scnr.index();
  194. const currentPosition = () => createPosition(_scnr.line(), _scnr.column(), _scnr.index());
  195. const _initLoc = currentPosition();
  196. const _initOffset = currentOffset();
  197. const _context = {
  198. currentType: 13 /* TokenTypes.EOF */,
  199. offset: _initOffset,
  200. startLoc: _initLoc,
  201. endLoc: _initLoc,
  202. lastType: 13 /* TokenTypes.EOF */,
  203. lastOffset: _initOffset,
  204. lastStartLoc: _initLoc,
  205. lastEndLoc: _initLoc,
  206. braceNest: 0,
  207. inLinked: false,
  208. text: ''
  209. };
  210. const context = () => _context;
  211. const { onError } = options;
  212. function emitError(code, pos, offset, ...args) {
  213. const ctx = context();
  214. pos.column += offset;
  215. pos.offset += offset;
  216. if (onError) {
  217. const loc = location ? createLocation(ctx.startLoc, pos) : null;
  218. const err = createCompileError(code, loc, {
  219. domain: ERROR_DOMAIN$3,
  220. args
  221. });
  222. onError(err);
  223. }
  224. }
  225. function getToken(context, type, value) {
  226. context.endLoc = currentPosition();
  227. context.currentType = type;
  228. const token = { type };
  229. if (location) {
  230. token.loc = createLocation(context.startLoc, context.endLoc);
  231. }
  232. if (value != null) {
  233. token.value = value;
  234. }
  235. return token;
  236. }
  237. const getEndToken = (context) => getToken(context, 13 /* TokenTypes.EOF */);
  238. function eat(scnr, ch) {
  239. if (scnr.currentChar() === ch) {
  240. scnr.next();
  241. return ch;
  242. }
  243. else {
  244. emitError(CompileErrorCodes.EXPECTED_TOKEN, currentPosition(), 0, ch);
  245. return '';
  246. }
  247. }
  248. function peekSpaces(scnr) {
  249. let buf = '';
  250. while (scnr.currentPeek() === CHAR_SP || scnr.currentPeek() === CHAR_LF) {
  251. buf += scnr.currentPeek();
  252. scnr.peek();
  253. }
  254. return buf;
  255. }
  256. function skipSpaces(scnr) {
  257. const buf = peekSpaces(scnr);
  258. scnr.skipToPeek();
  259. return buf;
  260. }
  261. function isIdentifierStart(ch) {
  262. if (ch === EOF) {
  263. return false;
  264. }
  265. const cc = ch.charCodeAt(0);
  266. return ((cc >= 97 && cc <= 122) || // a-z
  267. (cc >= 65 && cc <= 90) || // A-Z
  268. cc === 95 // _
  269. );
  270. }
  271. function isNumberStart(ch) {
  272. if (ch === EOF) {
  273. return false;
  274. }
  275. const cc = ch.charCodeAt(0);
  276. return cc >= 48 && cc <= 57; // 0-9
  277. }
  278. function isNamedIdentifierStart(scnr, context) {
  279. const { currentType } = context;
  280. if (currentType !== 2 /* TokenTypes.BraceLeft */) {
  281. return false;
  282. }
  283. peekSpaces(scnr);
  284. const ret = isIdentifierStart(scnr.currentPeek());
  285. scnr.resetPeek();
  286. return ret;
  287. }
  288. function isListIdentifierStart(scnr, context) {
  289. const { currentType } = context;
  290. if (currentType !== 2 /* TokenTypes.BraceLeft */) {
  291. return false;
  292. }
  293. peekSpaces(scnr);
  294. const ch = scnr.currentPeek() === '-' ? scnr.peek() : scnr.currentPeek();
  295. const ret = isNumberStart(ch);
  296. scnr.resetPeek();
  297. return ret;
  298. }
  299. function isLiteralStart(scnr, context) {
  300. const { currentType } = context;
  301. if (currentType !== 2 /* TokenTypes.BraceLeft */) {
  302. return false;
  303. }
  304. peekSpaces(scnr);
  305. const ret = scnr.currentPeek() === LITERAL_DELIMITER;
  306. scnr.resetPeek();
  307. return ret;
  308. }
  309. function isLinkedDotStart(scnr, context) {
  310. const { currentType } = context;
  311. if (currentType !== 7 /* TokenTypes.LinkedAlias */) {
  312. return false;
  313. }
  314. peekSpaces(scnr);
  315. const ret = scnr.currentPeek() === "." /* TokenChars.LinkedDot */;
  316. scnr.resetPeek();
  317. return ret;
  318. }
  319. function isLinkedModifierStart(scnr, context) {
  320. const { currentType } = context;
  321. if (currentType !== 8 /* TokenTypes.LinkedDot */) {
  322. return false;
  323. }
  324. peekSpaces(scnr);
  325. const ret = isIdentifierStart(scnr.currentPeek());
  326. scnr.resetPeek();
  327. return ret;
  328. }
  329. function isLinkedDelimiterStart(scnr, context) {
  330. const { currentType } = context;
  331. if (!(currentType === 7 /* TokenTypes.LinkedAlias */ ||
  332. currentType === 11 /* TokenTypes.LinkedModifier */)) {
  333. return false;
  334. }
  335. peekSpaces(scnr);
  336. const ret = scnr.currentPeek() === ":" /* TokenChars.LinkedDelimiter */;
  337. scnr.resetPeek();
  338. return ret;
  339. }
  340. function isLinkedReferStart(scnr, context) {
  341. const { currentType } = context;
  342. if (currentType !== 9 /* TokenTypes.LinkedDelimiter */) {
  343. return false;
  344. }
  345. const fn = () => {
  346. const ch = scnr.currentPeek();
  347. if (ch === "{" /* TokenChars.BraceLeft */) {
  348. return isIdentifierStart(scnr.peek());
  349. }
  350. else if (ch === "@" /* TokenChars.LinkedAlias */ ||
  351. ch === "|" /* TokenChars.Pipe */ ||
  352. ch === ":" /* TokenChars.LinkedDelimiter */ ||
  353. ch === "." /* TokenChars.LinkedDot */ ||
  354. ch === CHAR_SP ||
  355. !ch) {
  356. return false;
  357. }
  358. else if (ch === CHAR_LF) {
  359. scnr.peek();
  360. return fn();
  361. }
  362. else {
  363. // other characters
  364. return isTextStart(scnr, false);
  365. }
  366. };
  367. const ret = fn();
  368. scnr.resetPeek();
  369. return ret;
  370. }
  371. function isPluralStart(scnr) {
  372. peekSpaces(scnr);
  373. const ret = scnr.currentPeek() === "|" /* TokenChars.Pipe */;
  374. scnr.resetPeek();
  375. return ret;
  376. }
  377. function isTextStart(scnr, reset = true) {
  378. const fn = (hasSpace = false, prev = '') => {
  379. const ch = scnr.currentPeek();
  380. if (ch === "{" /* TokenChars.BraceLeft */) {
  381. return hasSpace;
  382. }
  383. else if (ch === "@" /* TokenChars.LinkedAlias */ || !ch) {
  384. return hasSpace;
  385. }
  386. else if (ch === "|" /* TokenChars.Pipe */) {
  387. return !(prev === CHAR_SP || prev === CHAR_LF);
  388. }
  389. else if (ch === CHAR_SP) {
  390. scnr.peek();
  391. return fn(true, CHAR_SP);
  392. }
  393. else if (ch === CHAR_LF) {
  394. scnr.peek();
  395. return fn(true, CHAR_LF);
  396. }
  397. else {
  398. return true;
  399. }
  400. };
  401. const ret = fn();
  402. reset && scnr.resetPeek();
  403. return ret;
  404. }
  405. function takeChar(scnr, fn) {
  406. const ch = scnr.currentChar();
  407. if (ch === EOF) {
  408. return EOF;
  409. }
  410. if (fn(ch)) {
  411. scnr.next();
  412. return ch;
  413. }
  414. return null;
  415. }
  416. function isIdentifier(ch) {
  417. const cc = ch.charCodeAt(0);
  418. return ((cc >= 97 && cc <= 122) || // a-z
  419. (cc >= 65 && cc <= 90) || // A-Z
  420. (cc >= 48 && cc <= 57) || // 0-9
  421. cc === 95 || // _
  422. cc === 36 // $
  423. );
  424. }
  425. function takeIdentifierChar(scnr) {
  426. return takeChar(scnr, isIdentifier);
  427. }
  428. function isNamedIdentifier(ch) {
  429. const cc = ch.charCodeAt(0);
  430. return ((cc >= 97 && cc <= 122) || // a-z
  431. (cc >= 65 && cc <= 90) || // A-Z
  432. (cc >= 48 && cc <= 57) || // 0-9
  433. cc === 95 || // _
  434. cc === 36 || // $
  435. cc === 45 // -
  436. );
  437. }
  438. function takeNamedIdentifierChar(scnr) {
  439. return takeChar(scnr, isNamedIdentifier);
  440. }
  441. function isDigit(ch) {
  442. const cc = ch.charCodeAt(0);
  443. return cc >= 48 && cc <= 57; // 0-9
  444. }
  445. function takeDigit(scnr) {
  446. return takeChar(scnr, isDigit);
  447. }
  448. function isHexDigit(ch) {
  449. const cc = ch.charCodeAt(0);
  450. return ((cc >= 48 && cc <= 57) || // 0-9
  451. (cc >= 65 && cc <= 70) || // A-F
  452. (cc >= 97 && cc <= 102)); // a-f
  453. }
  454. function takeHexDigit(scnr) {
  455. return takeChar(scnr, isHexDigit);
  456. }
  457. function getDigits(scnr) {
  458. let ch = '';
  459. let num = '';
  460. while ((ch = takeDigit(scnr))) {
  461. num += ch;
  462. }
  463. return num;
  464. }
  465. function readText(scnr) {
  466. let buf = '';
  467. while (true) {
  468. const ch = scnr.currentChar();
  469. if (ch === "{" /* TokenChars.BraceLeft */ ||
  470. ch === "}" /* TokenChars.BraceRight */ ||
  471. ch === "@" /* TokenChars.LinkedAlias */ ||
  472. ch === "|" /* TokenChars.Pipe */ ||
  473. !ch) {
  474. break;
  475. }
  476. else if (ch === CHAR_SP || ch === CHAR_LF) {
  477. if (isTextStart(scnr)) {
  478. buf += ch;
  479. scnr.next();
  480. }
  481. else if (isPluralStart(scnr)) {
  482. break;
  483. }
  484. else {
  485. buf += ch;
  486. scnr.next();
  487. }
  488. }
  489. else {
  490. buf += ch;
  491. scnr.next();
  492. }
  493. }
  494. return buf;
  495. }
  496. function readNamedIdentifier(scnr) {
  497. skipSpaces(scnr);
  498. let ch = '';
  499. let name = '';
  500. while ((ch = takeNamedIdentifierChar(scnr))) {
  501. name += ch;
  502. }
  503. if (scnr.currentChar() === EOF) {
  504. emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
  505. }
  506. return name;
  507. }
  508. function readListIdentifier(scnr) {
  509. skipSpaces(scnr);
  510. let value = '';
  511. if (scnr.currentChar() === '-') {
  512. scnr.next();
  513. value += `-${getDigits(scnr)}`;
  514. }
  515. else {
  516. value += getDigits(scnr);
  517. }
  518. if (scnr.currentChar() === EOF) {
  519. emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
  520. }
  521. return value;
  522. }
  523. function isLiteral(ch) {
  524. return ch !== LITERAL_DELIMITER && ch !== CHAR_LF;
  525. }
  526. function readLiteral(scnr) {
  527. skipSpaces(scnr);
  528. // eslint-disable-next-line no-useless-escape
  529. eat(scnr, `\'`);
  530. let ch = '';
  531. let literal = '';
  532. while ((ch = takeChar(scnr, isLiteral))) {
  533. if (ch === '\\') {
  534. literal += readEscapeSequence(scnr);
  535. }
  536. else {
  537. literal += ch;
  538. }
  539. }
  540. const current = scnr.currentChar();
  541. if (current === CHAR_LF || current === EOF) {
  542. emitError(CompileErrorCodes.UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER, currentPosition(), 0);
  543. // TODO: Is it correct really?
  544. if (current === CHAR_LF) {
  545. scnr.next();
  546. // eslint-disable-next-line no-useless-escape
  547. eat(scnr, `\'`);
  548. }
  549. return literal;
  550. }
  551. // eslint-disable-next-line no-useless-escape
  552. eat(scnr, `\'`);
  553. return literal;
  554. }
  555. function readEscapeSequence(scnr) {
  556. const ch = scnr.currentChar();
  557. switch (ch) {
  558. case '\\':
  559. case `\'`: // eslint-disable-line no-useless-escape
  560. scnr.next();
  561. return `\\${ch}`;
  562. case 'u':
  563. return readUnicodeEscapeSequence(scnr, ch, 4);
  564. case 'U':
  565. return readUnicodeEscapeSequence(scnr, ch, 6);
  566. default:
  567. emitError(CompileErrorCodes.UNKNOWN_ESCAPE_SEQUENCE, currentPosition(), 0, ch);
  568. return '';
  569. }
  570. }
  571. function readUnicodeEscapeSequence(scnr, unicode, digits) {
  572. eat(scnr, unicode);
  573. let sequence = '';
  574. for (let i = 0; i < digits; i++) {
  575. const ch = takeHexDigit(scnr);
  576. if (!ch) {
  577. emitError(CompileErrorCodes.INVALID_UNICODE_ESCAPE_SEQUENCE, currentPosition(), 0, `\\${unicode}${sequence}${scnr.currentChar()}`);
  578. break;
  579. }
  580. sequence += ch;
  581. }
  582. return `\\${unicode}${sequence}`;
  583. }
  584. function isInvalidIdentifier(ch) {
  585. return (ch !== "{" /* TokenChars.BraceLeft */ &&
  586. ch !== "}" /* TokenChars.BraceRight */ &&
  587. ch !== CHAR_SP &&
  588. ch !== CHAR_LF);
  589. }
  590. function readInvalidIdentifier(scnr) {
  591. skipSpaces(scnr);
  592. let ch = '';
  593. let identifiers = '';
  594. while ((ch = takeChar(scnr, isInvalidIdentifier))) {
  595. identifiers += ch;
  596. }
  597. return identifiers;
  598. }
  599. function readLinkedModifier(scnr) {
  600. let ch = '';
  601. let name = '';
  602. while ((ch = takeIdentifierChar(scnr))) {
  603. name += ch;
  604. }
  605. return name;
  606. }
  607. function readLinkedRefer(scnr) {
  608. const fn = (buf) => {
  609. const ch = scnr.currentChar();
  610. if (ch === "{" /* TokenChars.BraceLeft */ ||
  611. ch === "@" /* TokenChars.LinkedAlias */ ||
  612. ch === "|" /* TokenChars.Pipe */ ||
  613. ch === "(" /* TokenChars.ParenLeft */ ||
  614. ch === ")" /* TokenChars.ParenRight */ ||
  615. !ch) {
  616. return buf;
  617. }
  618. else if (ch === CHAR_SP) {
  619. return buf;
  620. }
  621. else if (ch === CHAR_LF || ch === DOT) {
  622. buf += ch;
  623. scnr.next();
  624. return fn(buf);
  625. }
  626. else {
  627. buf += ch;
  628. scnr.next();
  629. return fn(buf);
  630. }
  631. };
  632. return fn('');
  633. }
  634. function readPlural(scnr) {
  635. skipSpaces(scnr);
  636. const plural = eat(scnr, "|" /* TokenChars.Pipe */);
  637. skipSpaces(scnr);
  638. return plural;
  639. }
  640. // TODO: We need refactoring of token parsing ...
  641. function readTokenInPlaceholder(scnr, context) {
  642. let token = null;
  643. const ch = scnr.currentChar();
  644. switch (ch) {
  645. case "{" /* TokenChars.BraceLeft */:
  646. if (context.braceNest >= 1) {
  647. emitError(CompileErrorCodes.NOT_ALLOW_NEST_PLACEHOLDER, currentPosition(), 0);
  648. }
  649. scnr.next();
  650. token = getToken(context, 2 /* TokenTypes.BraceLeft */, "{" /* TokenChars.BraceLeft */);
  651. skipSpaces(scnr);
  652. context.braceNest++;
  653. return token;
  654. case "}" /* TokenChars.BraceRight */:
  655. if (context.braceNest > 0 &&
  656. context.currentType === 2 /* TokenTypes.BraceLeft */) {
  657. emitError(CompileErrorCodes.EMPTY_PLACEHOLDER, currentPosition(), 0);
  658. }
  659. scnr.next();
  660. token = getToken(context, 3 /* TokenTypes.BraceRight */, "}" /* TokenChars.BraceRight */);
  661. context.braceNest--;
  662. context.braceNest > 0 && skipSpaces(scnr);
  663. if (context.inLinked && context.braceNest === 0) {
  664. context.inLinked = false;
  665. }
  666. return token;
  667. case "@" /* TokenChars.LinkedAlias */:
  668. if (context.braceNest > 0) {
  669. emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
  670. }
  671. token = readTokenInLinked(scnr, context) || getEndToken(context);
  672. context.braceNest = 0;
  673. return token;
  674. default: {
  675. let validNamedIdentifier = true;
  676. let validListIdentifier = true;
  677. let validLiteral = true;
  678. if (isPluralStart(scnr)) {
  679. if (context.braceNest > 0) {
  680. emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
  681. }
  682. token = getToken(context, 1 /* TokenTypes.Pipe */, readPlural(scnr));
  683. // reset
  684. context.braceNest = 0;
  685. context.inLinked = false;
  686. return token;
  687. }
  688. if (context.braceNest > 0 &&
  689. (context.currentType === 4 /* TokenTypes.Named */ ||
  690. context.currentType === 5 /* TokenTypes.List */ ||
  691. context.currentType === 6 /* TokenTypes.Literal */)) {
  692. emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
  693. context.braceNest = 0;
  694. return readToken(scnr, context);
  695. }
  696. if ((validNamedIdentifier = isNamedIdentifierStart(scnr, context))) {
  697. token = getToken(context, 4 /* TokenTypes.Named */, readNamedIdentifier(scnr));
  698. skipSpaces(scnr);
  699. return token;
  700. }
  701. if ((validListIdentifier = isListIdentifierStart(scnr, context))) {
  702. token = getToken(context, 5 /* TokenTypes.List */, readListIdentifier(scnr));
  703. skipSpaces(scnr);
  704. return token;
  705. }
  706. if ((validLiteral = isLiteralStart(scnr, context))) {
  707. token = getToken(context, 6 /* TokenTypes.Literal */, readLiteral(scnr));
  708. skipSpaces(scnr);
  709. return token;
  710. }
  711. if (!validNamedIdentifier && !validListIdentifier && !validLiteral) {
  712. // TODO: we should be re-designed invalid cases, when we will extend message syntax near the future ...
  713. token = getToken(context, 12 /* TokenTypes.InvalidPlace */, readInvalidIdentifier(scnr));
  714. emitError(CompileErrorCodes.INVALID_TOKEN_IN_PLACEHOLDER, currentPosition(), 0, token.value);
  715. skipSpaces(scnr);
  716. return token;
  717. }
  718. break;
  719. }
  720. }
  721. return token;
  722. }
  723. // TODO: We need refactoring of token parsing ...
  724. function readTokenInLinked(scnr, context) {
  725. const { currentType } = context;
  726. let token = null;
  727. const ch = scnr.currentChar();
  728. if ((currentType === 7 /* TokenTypes.LinkedAlias */ ||
  729. currentType === 8 /* TokenTypes.LinkedDot */ ||
  730. currentType === 11 /* TokenTypes.LinkedModifier */ ||
  731. currentType === 9 /* TokenTypes.LinkedDelimiter */) &&
  732. (ch === CHAR_LF || ch === CHAR_SP)) {
  733. emitError(CompileErrorCodes.INVALID_LINKED_FORMAT, currentPosition(), 0);
  734. }
  735. switch (ch) {
  736. case "@" /* TokenChars.LinkedAlias */:
  737. scnr.next();
  738. token = getToken(context, 7 /* TokenTypes.LinkedAlias */, "@" /* TokenChars.LinkedAlias */);
  739. context.inLinked = true;
  740. return token;
  741. case "." /* TokenChars.LinkedDot */:
  742. skipSpaces(scnr);
  743. scnr.next();
  744. return getToken(context, 8 /* TokenTypes.LinkedDot */, "." /* TokenChars.LinkedDot */);
  745. case ":" /* TokenChars.LinkedDelimiter */:
  746. skipSpaces(scnr);
  747. scnr.next();
  748. return getToken(context, 9 /* TokenTypes.LinkedDelimiter */, ":" /* TokenChars.LinkedDelimiter */);
  749. default:
  750. if (isPluralStart(scnr)) {
  751. token = getToken(context, 1 /* TokenTypes.Pipe */, readPlural(scnr));
  752. // reset
  753. context.braceNest = 0;
  754. context.inLinked = false;
  755. return token;
  756. }
  757. if (isLinkedDotStart(scnr, context) ||
  758. isLinkedDelimiterStart(scnr, context)) {
  759. skipSpaces(scnr);
  760. return readTokenInLinked(scnr, context);
  761. }
  762. if (isLinkedModifierStart(scnr, context)) {
  763. skipSpaces(scnr);
  764. return getToken(context, 11 /* TokenTypes.LinkedModifier */, readLinkedModifier(scnr));
  765. }
  766. if (isLinkedReferStart(scnr, context)) {
  767. skipSpaces(scnr);
  768. if (ch === "{" /* TokenChars.BraceLeft */) {
  769. // scan the placeholder
  770. return readTokenInPlaceholder(scnr, context) || token;
  771. }
  772. else {
  773. return getToken(context, 10 /* TokenTypes.LinkedKey */, readLinkedRefer(scnr));
  774. }
  775. }
  776. if (currentType === 7 /* TokenTypes.LinkedAlias */) {
  777. emitError(CompileErrorCodes.INVALID_LINKED_FORMAT, currentPosition(), 0);
  778. }
  779. context.braceNest = 0;
  780. context.inLinked = false;
  781. return readToken(scnr, context);
  782. }
  783. }
  784. // TODO: We need refactoring of token parsing ...
  785. function readToken(scnr, context) {
  786. let token = { type: 13 /* TokenTypes.EOF */ };
  787. if (context.braceNest > 0) {
  788. return readTokenInPlaceholder(scnr, context) || getEndToken(context);
  789. }
  790. if (context.inLinked) {
  791. return readTokenInLinked(scnr, context) || getEndToken(context);
  792. }
  793. const ch = scnr.currentChar();
  794. switch (ch) {
  795. case "{" /* TokenChars.BraceLeft */:
  796. return readTokenInPlaceholder(scnr, context) || getEndToken(context);
  797. case "}" /* TokenChars.BraceRight */:
  798. emitError(CompileErrorCodes.UNBALANCED_CLOSING_BRACE, currentPosition(), 0);
  799. scnr.next();
  800. return getToken(context, 3 /* TokenTypes.BraceRight */, "}" /* TokenChars.BraceRight */);
  801. case "@" /* TokenChars.LinkedAlias */:
  802. return readTokenInLinked(scnr, context) || getEndToken(context);
  803. default: {
  804. if (isPluralStart(scnr)) {
  805. token = getToken(context, 1 /* TokenTypes.Pipe */, readPlural(scnr));
  806. // reset
  807. context.braceNest = 0;
  808. context.inLinked = false;
  809. return token;
  810. }
  811. if (isTextStart(scnr)) {
  812. return getToken(context, 0 /* TokenTypes.Text */, readText(scnr));
  813. }
  814. break;
  815. }
  816. }
  817. return token;
  818. }
  819. function nextToken() {
  820. const { currentType, offset, startLoc, endLoc } = _context;
  821. _context.lastType = currentType;
  822. _context.lastOffset = offset;
  823. _context.lastStartLoc = startLoc;
  824. _context.lastEndLoc = endLoc;
  825. _context.offset = currentOffset();
  826. _context.startLoc = currentPosition();
  827. if (_scnr.currentChar() === EOF) {
  828. return getToken(_context, 13 /* TokenTypes.EOF */);
  829. }
  830. return readToken(_scnr, _context);
  831. }
  832. return {
  833. nextToken,
  834. currentOffset,
  835. currentPosition,
  836. context
  837. };
  838. }
  839. const ERROR_DOMAIN$2 = 'parser';
  840. // Backslash backslash, backslash quote, uHHHH, UHHHHHH.
  841. const KNOWN_ESCAPES = /(?:\\\\|\\'|\\u([0-9a-fA-F]{4})|\\U([0-9a-fA-F]{6}))/g;
  842. function fromEscapeSequence(match, codePoint4, codePoint6) {
  843. switch (match) {
  844. case `\\\\`:
  845. return `\\`;
  846. // eslint-disable-next-line no-useless-escape
  847. case `\\\'`:
  848. // eslint-disable-next-line no-useless-escape
  849. return `\'`;
  850. default: {
  851. const codePoint = parseInt(codePoint4 || codePoint6, 16);
  852. if (codePoint <= 0xd7ff || codePoint >= 0xe000) {
  853. return String.fromCodePoint(codePoint);
  854. }
  855. // invalid ...
  856. // Replace them with U+FFFD REPLACEMENT CHARACTER.
  857. return '�';
  858. }
  859. }
  860. }
  861. function createParser(options = {}) {
  862. const location = options.location !== false;
  863. const { onError } = options;
  864. function emitError(tokenzer, code, start, offset, ...args) {
  865. const end = tokenzer.currentPosition();
  866. end.offset += offset;
  867. end.column += offset;
  868. if (onError) {
  869. const loc = location ? createLocation(start, end) : null;
  870. const err = createCompileError(code, loc, {
  871. domain: ERROR_DOMAIN$2,
  872. args
  873. });
  874. onError(err);
  875. }
  876. }
  877. function startNode(type, offset, loc) {
  878. const node = { type };
  879. if (location) {
  880. node.start = offset;
  881. node.end = offset;
  882. node.loc = { start: loc, end: loc };
  883. }
  884. return node;
  885. }
  886. function endNode(node, offset, pos, type) {
  887. if (location) {
  888. node.end = offset;
  889. if (node.loc) {
  890. node.loc.end = pos;
  891. }
  892. }
  893. }
  894. function parseText(tokenizer, value) {
  895. const context = tokenizer.context();
  896. const node = startNode(3 /* NodeTypes.Text */, context.offset, context.startLoc);
  897. node.value = value;
  898. endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
  899. return node;
  900. }
  901. function parseList(tokenizer, index) {
  902. const context = tokenizer.context();
  903. const { lastOffset: offset, lastStartLoc: loc } = context; // get brace left loc
  904. const node = startNode(5 /* NodeTypes.List */, offset, loc);
  905. node.index = parseInt(index, 10);
  906. tokenizer.nextToken(); // skip brach right
  907. endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
  908. return node;
  909. }
  910. function parseNamed(tokenizer, key) {
  911. const context = tokenizer.context();
  912. const { lastOffset: offset, lastStartLoc: loc } = context; // get brace left loc
  913. const node = startNode(4 /* NodeTypes.Named */, offset, loc);
  914. node.key = key;
  915. tokenizer.nextToken(); // skip brach right
  916. endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
  917. return node;
  918. }
  919. function parseLiteral(tokenizer, value) {
  920. const context = tokenizer.context();
  921. const { lastOffset: offset, lastStartLoc: loc } = context; // get brace left loc
  922. const node = startNode(9 /* NodeTypes.Literal */, offset, loc);
  923. node.value = value.replace(KNOWN_ESCAPES, fromEscapeSequence);
  924. tokenizer.nextToken(); // skip brach right
  925. endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
  926. return node;
  927. }
  928. function parseLinkedModifier(tokenizer) {
  929. const token = tokenizer.nextToken();
  930. const context = tokenizer.context();
  931. const { lastOffset: offset, lastStartLoc: loc } = context; // get linked dot loc
  932. const node = startNode(8 /* NodeTypes.LinkedModifier */, offset, loc);
  933. if (token.type !== 11 /* TokenTypes.LinkedModifier */) {
  934. // empty modifier
  935. emitError(tokenizer, CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_MODIFIER, context.lastStartLoc, 0);
  936. node.value = '';
  937. endNode(node, offset, loc);
  938. return {
  939. nextConsumeToken: token,
  940. node
  941. };
  942. }
  943. // check token
  944. if (token.value == null) {
  945. emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
  946. }
  947. node.value = token.value || '';
  948. endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
  949. return {
  950. node
  951. };
  952. }
  953. function parseLinkedKey(tokenizer, value) {
  954. const context = tokenizer.context();
  955. const node = startNode(7 /* NodeTypes.LinkedKey */, context.offset, context.startLoc);
  956. node.value = value;
  957. endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
  958. return node;
  959. }
  960. function parseLinked(tokenizer) {
  961. const context = tokenizer.context();
  962. const linkedNode = startNode(6 /* NodeTypes.Linked */, context.offset, context.startLoc);
  963. let token = tokenizer.nextToken();
  964. if (token.type === 8 /* TokenTypes.LinkedDot */) {
  965. const parsed = parseLinkedModifier(tokenizer);
  966. linkedNode.modifier = parsed.node;
  967. token = parsed.nextConsumeToken || tokenizer.nextToken();
  968. }
  969. // asset check token
  970. if (token.type !== 9 /* TokenTypes.LinkedDelimiter */) {
  971. emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
  972. }
  973. token = tokenizer.nextToken();
  974. // skip brace left
  975. if (token.type === 2 /* TokenTypes.BraceLeft */) {
  976. token = tokenizer.nextToken();
  977. }
  978. switch (token.type) {
  979. case 10 /* TokenTypes.LinkedKey */:
  980. if (token.value == null) {
  981. emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
  982. }
  983. linkedNode.key = parseLinkedKey(tokenizer, token.value || '');
  984. break;
  985. case 4 /* TokenTypes.Named */:
  986. if (token.value == null) {
  987. emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
  988. }
  989. linkedNode.key = parseNamed(tokenizer, token.value || '');
  990. break;
  991. case 5 /* TokenTypes.List */:
  992. if (token.value == null) {
  993. emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
  994. }
  995. linkedNode.key = parseList(tokenizer, token.value || '');
  996. break;
  997. case 6 /* TokenTypes.Literal */:
  998. if (token.value == null) {
  999. emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
  1000. }
  1001. linkedNode.key = parseLiteral(tokenizer, token.value || '');
  1002. break;
  1003. default: {
  1004. // empty key
  1005. emitError(tokenizer, CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_KEY, context.lastStartLoc, 0);
  1006. const nextContext = tokenizer.context();
  1007. const emptyLinkedKeyNode = startNode(7 /* NodeTypes.LinkedKey */, nextContext.offset, nextContext.startLoc);
  1008. emptyLinkedKeyNode.value = '';
  1009. endNode(emptyLinkedKeyNode, nextContext.offset, nextContext.startLoc);
  1010. linkedNode.key = emptyLinkedKeyNode;
  1011. endNode(linkedNode, nextContext.offset, nextContext.startLoc);
  1012. return {
  1013. nextConsumeToken: token,
  1014. node: linkedNode
  1015. };
  1016. }
  1017. }
  1018. endNode(linkedNode, tokenizer.currentOffset(), tokenizer.currentPosition());
  1019. return {
  1020. node: linkedNode
  1021. };
  1022. }
  1023. function parseMessage(tokenizer) {
  1024. const context = tokenizer.context();
  1025. const startOffset = context.currentType === 1 /* TokenTypes.Pipe */
  1026. ? tokenizer.currentOffset()
  1027. : context.offset;
  1028. const startLoc = context.currentType === 1 /* TokenTypes.Pipe */
  1029. ? context.endLoc
  1030. : context.startLoc;
  1031. const node = startNode(2 /* NodeTypes.Message */, startOffset, startLoc);
  1032. node.items = [];
  1033. let nextToken = null;
  1034. do {
  1035. const token = nextToken || tokenizer.nextToken();
  1036. nextToken = null;
  1037. switch (token.type) {
  1038. case 0 /* TokenTypes.Text */:
  1039. if (token.value == null) {
  1040. emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
  1041. }
  1042. node.items.push(parseText(tokenizer, token.value || ''));
  1043. break;
  1044. case 5 /* TokenTypes.List */:
  1045. if (token.value == null) {
  1046. emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
  1047. }
  1048. node.items.push(parseList(tokenizer, token.value || ''));
  1049. break;
  1050. case 4 /* TokenTypes.Named */:
  1051. if (token.value == null) {
  1052. emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
  1053. }
  1054. node.items.push(parseNamed(tokenizer, token.value || ''));
  1055. break;
  1056. case 6 /* TokenTypes.Literal */:
  1057. if (token.value == null) {
  1058. emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
  1059. }
  1060. node.items.push(parseLiteral(tokenizer, token.value || ''));
  1061. break;
  1062. case 7 /* TokenTypes.LinkedAlias */: {
  1063. const parsed = parseLinked(tokenizer);
  1064. node.items.push(parsed.node);
  1065. nextToken = parsed.nextConsumeToken || null;
  1066. break;
  1067. }
  1068. }
  1069. } while (context.currentType !== 13 /* TokenTypes.EOF */ &&
  1070. context.currentType !== 1 /* TokenTypes.Pipe */);
  1071. // adjust message node loc
  1072. const endOffset = context.currentType === 1 /* TokenTypes.Pipe */
  1073. ? context.lastOffset
  1074. : tokenizer.currentOffset();
  1075. const endLoc = context.currentType === 1 /* TokenTypes.Pipe */
  1076. ? context.lastEndLoc
  1077. : tokenizer.currentPosition();
  1078. endNode(node, endOffset, endLoc);
  1079. return node;
  1080. }
  1081. function parsePlural(tokenizer, offset, loc, msgNode) {
  1082. const context = tokenizer.context();
  1083. let hasEmptyMessage = msgNode.items.length === 0;
  1084. const node = startNode(1 /* NodeTypes.Plural */, offset, loc);
  1085. node.cases = [];
  1086. node.cases.push(msgNode);
  1087. do {
  1088. const msg = parseMessage(tokenizer);
  1089. if (!hasEmptyMessage) {
  1090. hasEmptyMessage = msg.items.length === 0;
  1091. }
  1092. node.cases.push(msg);
  1093. } while (context.currentType !== 13 /* TokenTypes.EOF */);
  1094. if (hasEmptyMessage) {
  1095. emitError(tokenizer, CompileErrorCodes.MUST_HAVE_MESSAGES_IN_PLURAL, loc, 0);
  1096. }
  1097. endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
  1098. return node;
  1099. }
  1100. function parseResource(tokenizer) {
  1101. const context = tokenizer.context();
  1102. const { offset, startLoc } = context;
  1103. const msgNode = parseMessage(tokenizer);
  1104. if (context.currentType === 13 /* TokenTypes.EOF */) {
  1105. return msgNode;
  1106. }
  1107. else {
  1108. return parsePlural(tokenizer, offset, startLoc, msgNode);
  1109. }
  1110. }
  1111. function parse(source) {
  1112. const tokenizer = createTokenizer(source, assign({}, options));
  1113. const context = tokenizer.context();
  1114. const node = startNode(0 /* NodeTypes.Resource */, context.offset, context.startLoc);
  1115. if (location && node.loc) {
  1116. node.loc.source = source;
  1117. }
  1118. node.body = parseResource(tokenizer);
  1119. if (options.onCacheKey) {
  1120. node.cacheKey = options.onCacheKey(source);
  1121. }
  1122. // assert whether achieved to EOF
  1123. if (context.currentType !== 13 /* TokenTypes.EOF */) {
  1124. emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, source[context.offset] || '');
  1125. }
  1126. endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
  1127. return node;
  1128. }
  1129. return { parse };
  1130. }
  1131. function getTokenCaption(token) {
  1132. if (token.type === 13 /* TokenTypes.EOF */) {
  1133. return 'EOF';
  1134. }
  1135. const name = (token.value || '').replace(/\r?\n/gu, '\\n');
  1136. return name.length > 10 ? name.slice(0, 9) + '…' : name;
  1137. }
  1138. function createTransformer(ast, options = {} // eslint-disable-line
  1139. ) {
  1140. const _context = {
  1141. ast,
  1142. helpers: new Set()
  1143. };
  1144. const context = () => _context;
  1145. const helper = (name) => {
  1146. _context.helpers.add(name);
  1147. return name;
  1148. };
  1149. return { context, helper };
  1150. }
  1151. function traverseNodes(nodes, transformer) {
  1152. for (let i = 0; i < nodes.length; i++) {
  1153. traverseNode(nodes[i], transformer);
  1154. }
  1155. }
  1156. function traverseNode(node, transformer) {
  1157. // TODO: if we need pre-hook of transform, should be implemented to here
  1158. switch (node.type) {
  1159. case 1 /* NodeTypes.Plural */:
  1160. traverseNodes(node.cases, transformer);
  1161. transformer.helper("plural" /* HelperNameMap.PLURAL */);
  1162. break;
  1163. case 2 /* NodeTypes.Message */:
  1164. traverseNodes(node.items, transformer);
  1165. break;
  1166. case 6 /* NodeTypes.Linked */: {
  1167. const linked = node;
  1168. traverseNode(linked.key, transformer);
  1169. transformer.helper("linked" /* HelperNameMap.LINKED */);
  1170. transformer.helper("type" /* HelperNameMap.TYPE */);
  1171. break;
  1172. }
  1173. case 5 /* NodeTypes.List */:
  1174. transformer.helper("interpolate" /* HelperNameMap.INTERPOLATE */);
  1175. transformer.helper("list" /* HelperNameMap.LIST */);
  1176. break;
  1177. case 4 /* NodeTypes.Named */:
  1178. transformer.helper("interpolate" /* HelperNameMap.INTERPOLATE */);
  1179. transformer.helper("named" /* HelperNameMap.NAMED */);
  1180. break;
  1181. }
  1182. // TODO: if we need post-hook of transform, should be implemented to here
  1183. }
  1184. // transform AST
  1185. function transform(ast, options = {} // eslint-disable-line
  1186. ) {
  1187. const transformer = createTransformer(ast);
  1188. transformer.helper("normalize" /* HelperNameMap.NORMALIZE */);
  1189. // traverse
  1190. ast.body && traverseNode(ast.body, transformer);
  1191. // set meta information
  1192. const context = transformer.context();
  1193. ast.helpers = Array.from(context.helpers);
  1194. }
  1195. function optimize(ast) {
  1196. const body = ast.body;
  1197. if (body.type === 2 /* NodeTypes.Message */) {
  1198. optimizeMessageNode(body);
  1199. }
  1200. else {
  1201. body.cases.forEach(c => optimizeMessageNode(c));
  1202. }
  1203. return ast;
  1204. }
  1205. function optimizeMessageNode(message) {
  1206. if (message.items.length === 1) {
  1207. const item = message.items[0];
  1208. if (item.type === 3 /* NodeTypes.Text */ || item.type === 9 /* NodeTypes.Literal */) {
  1209. message.static = item.value;
  1210. delete item.value; // optimization for size
  1211. }
  1212. }
  1213. else {
  1214. const values = [];
  1215. for (let i = 0; i < message.items.length; i++) {
  1216. const item = message.items[i];
  1217. if (!(item.type === 3 /* NodeTypes.Text */ || item.type === 9 /* NodeTypes.Literal */)) {
  1218. break;
  1219. }
  1220. if (item.value == null) {
  1221. break;
  1222. }
  1223. values.push(item.value);
  1224. }
  1225. if (values.length === message.items.length) {
  1226. message.static = join(values);
  1227. for (let i = 0; i < message.items.length; i++) {
  1228. const item = message.items[i];
  1229. if (item.type === 3 /* NodeTypes.Text */ || item.type === 9 /* NodeTypes.Literal */) {
  1230. delete item.value; // optimization for size
  1231. }
  1232. }
  1233. }
  1234. }
  1235. }
  1236. const ERROR_DOMAIN$1 = 'minifier';
  1237. /* eslint-disable @typescript-eslint/no-explicit-any */
  1238. function minify(node) {
  1239. node.t = node.type;
  1240. switch (node.type) {
  1241. case 0 /* NodeTypes.Resource */: {
  1242. const resource = node;
  1243. minify(resource.body);
  1244. resource.b = resource.body;
  1245. delete resource.body;
  1246. break;
  1247. }
  1248. case 1 /* NodeTypes.Plural */: {
  1249. const plural = node;
  1250. const cases = plural.cases;
  1251. for (let i = 0; i < cases.length; i++) {
  1252. minify(cases[i]);
  1253. }
  1254. plural.c = cases;
  1255. delete plural.cases;
  1256. break;
  1257. }
  1258. case 2 /* NodeTypes.Message */: {
  1259. const message = node;
  1260. const items = message.items;
  1261. for (let i = 0; i < items.length; i++) {
  1262. minify(items[i]);
  1263. }
  1264. message.i = items;
  1265. delete message.items;
  1266. if (message.static) {
  1267. message.s = message.static;
  1268. delete message.static;
  1269. }
  1270. break;
  1271. }
  1272. case 3 /* NodeTypes.Text */:
  1273. case 9 /* NodeTypes.Literal */:
  1274. case 8 /* NodeTypes.LinkedModifier */:
  1275. case 7 /* NodeTypes.LinkedKey */: {
  1276. const valueNode = node;
  1277. if (valueNode.value) {
  1278. valueNode.v = valueNode.value;
  1279. delete valueNode.value;
  1280. }
  1281. break;
  1282. }
  1283. case 6 /* NodeTypes.Linked */: {
  1284. const linked = node;
  1285. minify(linked.key);
  1286. linked.k = linked.key;
  1287. delete linked.key;
  1288. if (linked.modifier) {
  1289. minify(linked.modifier);
  1290. linked.m = linked.modifier;
  1291. delete linked.modifier;
  1292. }
  1293. break;
  1294. }
  1295. case 5 /* NodeTypes.List */: {
  1296. const list = node;
  1297. list.i = list.index;
  1298. delete list.index;
  1299. break;
  1300. }
  1301. case 4 /* NodeTypes.Named */: {
  1302. const named = node;
  1303. named.k = named.key;
  1304. delete named.key;
  1305. break;
  1306. }
  1307. default:
  1308. {
  1309. throw createCompileError(CompileErrorCodes.UNHANDLED_MINIFIER_NODE_TYPE, null, {
  1310. domain: ERROR_DOMAIN$1,
  1311. args: [node.type]
  1312. });
  1313. }
  1314. }
  1315. delete node.type;
  1316. }
  1317. /* eslint-enable @typescript-eslint/no-explicit-any */
  1318. // eslint-disable-next-line @typescript-eslint/triple-slash-reference
  1319. /// <reference types="source-map-js" />
  1320. const ERROR_DOMAIN = 'parser';
  1321. function createCodeGenerator(ast, options) {
  1322. const { sourceMap, filename, breakLineCode, needIndent: _needIndent } = options;
  1323. const location = options.location !== false;
  1324. const _context = {
  1325. filename,
  1326. code: '',
  1327. column: 1,
  1328. line: 1,
  1329. offset: 0,
  1330. map: undefined,
  1331. breakLineCode,
  1332. needIndent: _needIndent,
  1333. indentLevel: 0
  1334. };
  1335. if (location && ast.loc) {
  1336. _context.source = ast.loc.source;
  1337. }
  1338. const context = () => _context;
  1339. function push(code, node) {
  1340. _context.code += code;
  1341. }
  1342. function _newline(n, withBreakLine = true) {
  1343. const _breakLineCode = withBreakLine ? breakLineCode : '';
  1344. push(_needIndent ? _breakLineCode + ` `.repeat(n) : _breakLineCode);
  1345. }
  1346. function indent(withNewLine = true) {
  1347. const level = ++_context.indentLevel;
  1348. withNewLine && _newline(level);
  1349. }
  1350. function deindent(withNewLine = true) {
  1351. const level = --_context.indentLevel;
  1352. withNewLine && _newline(level);
  1353. }
  1354. function newline() {
  1355. _newline(_context.indentLevel);
  1356. }
  1357. const helper = (key) => `_${key}`;
  1358. const needIndent = () => _context.needIndent;
  1359. return {
  1360. context,
  1361. push,
  1362. indent,
  1363. deindent,
  1364. newline,
  1365. helper,
  1366. needIndent
  1367. };
  1368. }
  1369. function generateLinkedNode(generator, node) {
  1370. const { helper } = generator;
  1371. generator.push(`${helper("linked" /* HelperNameMap.LINKED */)}(`);
  1372. generateNode(generator, node.key);
  1373. if (node.modifier) {
  1374. generator.push(`, `);
  1375. generateNode(generator, node.modifier);
  1376. generator.push(`, _type`);
  1377. }
  1378. else {
  1379. generator.push(`, undefined, _type`);
  1380. }
  1381. generator.push(`)`);
  1382. }
  1383. function generateMessageNode(generator, node) {
  1384. const { helper, needIndent } = generator;
  1385. generator.push(`${helper("normalize" /* HelperNameMap.NORMALIZE */)}([`);
  1386. generator.indent(needIndent());
  1387. const length = node.items.length;
  1388. for (let i = 0; i < length; i++) {
  1389. generateNode(generator, node.items[i]);
  1390. if (i === length - 1) {
  1391. break;
  1392. }
  1393. generator.push(', ');
  1394. }
  1395. generator.deindent(needIndent());
  1396. generator.push('])');
  1397. }
  1398. function generatePluralNode(generator, node) {
  1399. const { helper, needIndent } = generator;
  1400. if (node.cases.length > 1) {
  1401. generator.push(`${helper("plural" /* HelperNameMap.PLURAL */)}([`);
  1402. generator.indent(needIndent());
  1403. const length = node.cases.length;
  1404. for (let i = 0; i < length; i++) {
  1405. generateNode(generator, node.cases[i]);
  1406. if (i === length - 1) {
  1407. break;
  1408. }
  1409. generator.push(', ');
  1410. }
  1411. generator.deindent(needIndent());
  1412. generator.push(`])`);
  1413. }
  1414. }
  1415. function generateResource(generator, node) {
  1416. if (node.body) {
  1417. generateNode(generator, node.body);
  1418. }
  1419. else {
  1420. generator.push('null');
  1421. }
  1422. }
  1423. function generateNode(generator, node) {
  1424. const { helper } = generator;
  1425. switch (node.type) {
  1426. case 0 /* NodeTypes.Resource */:
  1427. generateResource(generator, node);
  1428. break;
  1429. case 1 /* NodeTypes.Plural */:
  1430. generatePluralNode(generator, node);
  1431. break;
  1432. case 2 /* NodeTypes.Message */:
  1433. generateMessageNode(generator, node);
  1434. break;
  1435. case 6 /* NodeTypes.Linked */:
  1436. generateLinkedNode(generator, node);
  1437. break;
  1438. case 8 /* NodeTypes.LinkedModifier */:
  1439. generator.push(JSON.stringify(node.value), node);
  1440. break;
  1441. case 7 /* NodeTypes.LinkedKey */:
  1442. generator.push(JSON.stringify(node.value), node);
  1443. break;
  1444. case 5 /* NodeTypes.List */:
  1445. generator.push(`${helper("interpolate" /* HelperNameMap.INTERPOLATE */)}(${helper("list" /* HelperNameMap.LIST */)}(${node.index}))`, node);
  1446. break;
  1447. case 4 /* NodeTypes.Named */:
  1448. generator.push(`${helper("interpolate" /* HelperNameMap.INTERPOLATE */)}(${helper("named" /* HelperNameMap.NAMED */)}(${JSON.stringify(node.key)}))`, node);
  1449. break;
  1450. case 9 /* NodeTypes.Literal */:
  1451. generator.push(JSON.stringify(node.value), node);
  1452. break;
  1453. case 3 /* NodeTypes.Text */:
  1454. generator.push(JSON.stringify(node.value), node);
  1455. break;
  1456. default:
  1457. {
  1458. throw createCompileError(CompileErrorCodes.UNHANDLED_CODEGEN_NODE_TYPE, null, {
  1459. domain: ERROR_DOMAIN,
  1460. args: [node.type]
  1461. });
  1462. }
  1463. }
  1464. }
  1465. // generate code from AST
  1466. const generate = (ast, options = {}) => {
  1467. const mode = isString(options.mode) ? options.mode : 'normal';
  1468. const filename = isString(options.filename)
  1469. ? options.filename
  1470. : 'message.intl';
  1471. const sourceMap = !!options.sourceMap;
  1472. // prettier-ignore
  1473. const breakLineCode = options.breakLineCode != null
  1474. ? options.breakLineCode
  1475. : mode === 'arrow'
  1476. ? ';'
  1477. : '\n';
  1478. const needIndent = options.needIndent ? options.needIndent : mode !== 'arrow';
  1479. const helpers = ast.helpers || [];
  1480. const generator = createCodeGenerator(ast, {
  1481. mode,
  1482. filename,
  1483. sourceMap,
  1484. breakLineCode,
  1485. needIndent
  1486. });
  1487. generator.push(mode === 'normal' ? `function __msg__ (ctx) {` : `(ctx) => {`);
  1488. generator.indent(needIndent);
  1489. if (helpers.length > 0) {
  1490. generator.push(`const { ${join(helpers.map(s => `${s}: _${s}`), ', ')} } = ctx`);
  1491. generator.newline();
  1492. }
  1493. generator.push(`return `);
  1494. generateNode(generator, ast);
  1495. generator.deindent(needIndent);
  1496. generator.push(`}`);
  1497. delete ast.helpers;
  1498. const { code, map } = generator.context();
  1499. return {
  1500. ast,
  1501. code,
  1502. map: map ? map.toJSON() : undefined // eslint-disable-line @typescript-eslint/no-explicit-any
  1503. };
  1504. };
  1505. function baseCompile(source, options = {}) {
  1506. const assignedOptions = assign({}, options);
  1507. const jit = !!assignedOptions.jit;
  1508. const enalbeMinify = !!assignedOptions.minify;
  1509. const enambeOptimize = assignedOptions.optimize == null ? true : assignedOptions.optimize;
  1510. // parse source codes
  1511. const parser = createParser(assignedOptions);
  1512. const ast = parser.parse(source);
  1513. if (!jit) {
  1514. // transform ASTs
  1515. transform(ast, assignedOptions);
  1516. // generate javascript codes
  1517. return generate(ast, assignedOptions);
  1518. }
  1519. else {
  1520. // optimize ASTs
  1521. enambeOptimize && optimize(ast);
  1522. // minimize ASTs
  1523. enalbeMinify && minify(ast);
  1524. // In JIT mode, no ast transform, no code generation.
  1525. return { ast, code: '' };
  1526. }
  1527. }
  1528. export { COMPILE_ERROR_CODES_EXTEND_POINT, CompileErrorCodes, ERROR_DOMAIN$2 as ERROR_DOMAIN, LOCATION_STUB, baseCompile, createCompileError, createLocation, createParser, createPosition, defaultOnError, detectHtmlTag, errorMessages };