token-parser.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import { BadOptionError, BadStemError, MaskedValueError } from '../errors.js';
  2. import { isNumberingSystem } from '../types/skeleton.js';
  3. import { isUnit } from '../types/unit.js';
  4. import { validOptions } from './options.js';
  5. import { parsePrecisionBlueprint } from './parse-precision-blueprint.js';
  6. /** @internal */
  7. export class TokenParser {
  8. constructor(onError) {
  9. this.skeleton = {};
  10. this.onError = onError;
  11. }
  12. badOption(stem, opt) {
  13. this.onError(new BadOptionError(stem, opt));
  14. }
  15. assertEmpty(key) {
  16. const prev = this.skeleton[key];
  17. if (prev)
  18. this.onError(new MaskedValueError(key, prev));
  19. }
  20. parseToken(stem, options) {
  21. if (!validOptions(stem, options, this.onError))
  22. return;
  23. const option = options[0];
  24. const res = this.skeleton;
  25. switch (stem) {
  26. // notation
  27. case 'compact-short':
  28. case 'compact-long':
  29. case 'notation-simple':
  30. this.assertEmpty('notation');
  31. res.notation = { style: stem };
  32. break;
  33. case 'scientific':
  34. case 'engineering': {
  35. let expDigits = null;
  36. let expSign = undefined;
  37. for (const opt of options) {
  38. switch (opt) {
  39. case 'sign-auto':
  40. case 'sign-always':
  41. case 'sign-never':
  42. case 'sign-accounting':
  43. case 'sign-accounting-always':
  44. case 'sign-except-zero':
  45. case 'sign-accounting-except-zero':
  46. expSign = opt;
  47. break;
  48. default:
  49. if (/^\+e+$/.test(opt))
  50. expDigits = opt.length - 1;
  51. else {
  52. this.badOption(stem, opt);
  53. }
  54. }
  55. }
  56. this.assertEmpty('notation');
  57. const source = options.join('/');
  58. res.notation =
  59. expDigits && expSign
  60. ? { style: stem, source, expDigits, expSign }
  61. : expDigits
  62. ? { style: stem, source, expDigits }
  63. : expSign
  64. ? { style: stem, source, expSign }
  65. : { style: stem, source };
  66. break;
  67. }
  68. // unit
  69. case 'percent':
  70. case 'permille':
  71. case 'base-unit':
  72. this.assertEmpty('unit');
  73. res.unit = { style: stem };
  74. break;
  75. case 'currency':
  76. if (/^[A-Z]{3}$/.test(option)) {
  77. this.assertEmpty('unit');
  78. res.unit = { style: stem, currency: option };
  79. }
  80. else
  81. this.badOption(stem, option);
  82. break;
  83. case 'measure-unit': {
  84. if (isUnit(option)) {
  85. this.assertEmpty('unit');
  86. res.unit = { style: stem, unit: option };
  87. }
  88. else
  89. this.badOption(stem, option);
  90. break;
  91. }
  92. // unitPer
  93. case 'per-measure-unit': {
  94. if (isUnit(option)) {
  95. this.assertEmpty('unitPer');
  96. res.unitPer = option;
  97. }
  98. else
  99. this.badOption(stem, option);
  100. break;
  101. }
  102. // unitWidth
  103. case 'unit-width-narrow':
  104. case 'unit-width-short':
  105. case 'unit-width-full-name':
  106. case 'unit-width-iso-code':
  107. case 'unit-width-hidden':
  108. this.assertEmpty('unitWidth');
  109. res.unitWidth = stem;
  110. break;
  111. // precision
  112. case 'precision-integer':
  113. case 'precision-unlimited':
  114. case 'precision-currency-cash':
  115. this.assertEmpty('precision');
  116. res.precision = { style: stem };
  117. break;
  118. case 'precision-currency-standard':
  119. this.assertEmpty('precision');
  120. if (option === 'w') {
  121. res.precision = { style: stem, trailingZero: 'stripIfInteger' };
  122. }
  123. else {
  124. res.precision = { style: stem };
  125. }
  126. break;
  127. case 'precision-increment': {
  128. const increment = Number(option);
  129. if (increment > 0) {
  130. this.assertEmpty('precision');
  131. res.precision = { style: stem, increment };
  132. }
  133. else
  134. this.badOption(stem, option);
  135. break;
  136. }
  137. // roundingMode
  138. case 'rounding-mode-ceiling':
  139. case 'rounding-mode-floor':
  140. case 'rounding-mode-down':
  141. case 'rounding-mode-up':
  142. case 'rounding-mode-half-even':
  143. case 'rounding-mode-half-odd':
  144. case 'rounding-mode-half-ceiling':
  145. case 'rounding-mode-half-floor':
  146. case 'rounding-mode-half-down':
  147. case 'rounding-mode-half-up':
  148. case 'rounding-mode-unnecessary':
  149. this.assertEmpty('roundingMode');
  150. res.roundingMode = stem;
  151. break;
  152. // integerWidth
  153. case 'integer-width': {
  154. if (/^\+0*$/.test(option)) {
  155. this.assertEmpty('integerWidth');
  156. res.integerWidth = { source: option, min: option.length - 1 };
  157. }
  158. else {
  159. const m = option.match(/^#*(0*)$/);
  160. if (m) {
  161. this.assertEmpty('integerWidth');
  162. res.integerWidth = {
  163. source: option,
  164. min: m[1].length,
  165. max: m[0].length
  166. };
  167. }
  168. else
  169. this.badOption(stem, option);
  170. }
  171. break;
  172. }
  173. // scale
  174. case 'scale': {
  175. const scale = Number(option);
  176. if (scale > 0) {
  177. this.assertEmpty('scale');
  178. res.scale = scale;
  179. }
  180. else
  181. this.badOption(stem, option);
  182. break;
  183. }
  184. // group
  185. case 'group-off':
  186. case 'group-min2':
  187. case 'group-auto':
  188. case 'group-on-aligned':
  189. case 'group-thousands':
  190. this.assertEmpty('group');
  191. res.group = stem;
  192. break;
  193. // numberingSystem
  194. case 'latin':
  195. this.assertEmpty('numberingSystem');
  196. res.numberingSystem = 'latn';
  197. break;
  198. case 'numbering-system': {
  199. if (isNumberingSystem(option)) {
  200. this.assertEmpty('numberingSystem');
  201. res.numberingSystem = option;
  202. }
  203. else
  204. this.badOption(stem, option);
  205. break;
  206. }
  207. // sign
  208. case 'sign-auto':
  209. case 'sign-always':
  210. case 'sign-never':
  211. case 'sign-accounting':
  212. case 'sign-accounting-always':
  213. case 'sign-except-zero':
  214. case 'sign-accounting-except-zero':
  215. this.assertEmpty('sign');
  216. res.sign = stem;
  217. break;
  218. // decimal
  219. case 'decimal-auto':
  220. case 'decimal-always':
  221. this.assertEmpty('decimal');
  222. res.decimal = stem;
  223. break;
  224. // precision blueprint
  225. default: {
  226. const precision = parsePrecisionBlueprint(stem, options, this.onError);
  227. if (precision) {
  228. this.assertEmpty('precision');
  229. res.precision = precision;
  230. }
  231. else {
  232. this.onError(new BadStemError(stem));
  233. }
  234. }
  235. }
  236. }
  237. }