message-compiler.node.mjs 56 KB

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