message-compiler.global.js 59 KB

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