readMappings.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const ALPHABET =
  7. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  8. const CONTINUATION_BIT = 0x20;
  9. const END_SEGMENT_BIT = 0x40;
  10. const NEXT_LINE = END_SEGMENT_BIT | 0x01;
  11. const INVALID = END_SEGMENT_BIT | 0x02;
  12. const DATA_MASK = 0x1f;
  13. const ccToValue = new Uint8Array("z".charCodeAt(0) + 1);
  14. ccToValue.fill(INVALID);
  15. for (let i = 0; i < ALPHABET.length; i++) {
  16. ccToValue[ALPHABET.charCodeAt(i)] = i;
  17. }
  18. ccToValue[",".charCodeAt(0)] = END_SEGMENT_BIT;
  19. ccToValue[";".charCodeAt(0)] = NEXT_LINE;
  20. const ccMax = ccToValue.length - 1;
  21. /** @typedef {(generatedLine: number, generatedColumn: number, sourceIndex: number, originalLine: number, originalColumn: number, nameIndex: number) => void} OnMapping */
  22. /**
  23. * @param {string} mappings the mappings string
  24. * @param {OnMapping} onMapping called for each mapping
  25. * @returns {void}
  26. */
  27. const readMappings = (mappings, onMapping) => {
  28. // generatedColumn, [sourceIndex, originalLine, originalColumn, [nameIndex]]
  29. const currentData = new Int32Array([0, 0, 1, 0, 0]);
  30. let currentDataPos = 0;
  31. // currentValue will include a sign bit at bit 0
  32. let currentValue = 0;
  33. let currentValuePos = 0;
  34. let generatedLine = 1;
  35. let generatedColumn = -1;
  36. for (let i = 0; i < mappings.length; i++) {
  37. const cc = mappings.charCodeAt(i);
  38. if (cc > ccMax) continue;
  39. const value = ccToValue[cc];
  40. if ((value & END_SEGMENT_BIT) !== 0) {
  41. // End current segment
  42. if (currentData[0] > generatedColumn) {
  43. if (currentDataPos === 1) {
  44. onMapping(generatedLine, currentData[0], -1, -1, -1, -1);
  45. } else if (currentDataPos === 4) {
  46. onMapping(
  47. generatedLine,
  48. currentData[0],
  49. currentData[1],
  50. currentData[2],
  51. currentData[3],
  52. -1,
  53. );
  54. } else if (currentDataPos === 5) {
  55. onMapping(
  56. generatedLine,
  57. currentData[0],
  58. currentData[1],
  59. currentData[2],
  60. currentData[3],
  61. currentData[4],
  62. );
  63. }
  64. // Direct typed-array index is faster here than destructuring,
  65. // which would invoke the Int32Array iterator protocol.
  66. // eslint-disable-next-line prefer-destructuring
  67. generatedColumn = currentData[0];
  68. }
  69. currentDataPos = 0;
  70. if (value === NEXT_LINE) {
  71. // Start new line
  72. generatedLine++;
  73. currentData[0] = 0;
  74. generatedColumn = -1;
  75. }
  76. } else if ((value & CONTINUATION_BIT) === 0) {
  77. // last sextet
  78. currentValue |= value << currentValuePos;
  79. const finalValue =
  80. currentValue & 1 ? -(currentValue >> 1) : currentValue >> 1;
  81. currentData[currentDataPos++] += finalValue;
  82. currentValuePos = 0;
  83. currentValue = 0;
  84. } else {
  85. currentValue |= (value & DATA_MASK) << currentValuePos;
  86. currentValuePos += 5;
  87. }
  88. }
  89. // End current segment
  90. if (currentDataPos === 1) {
  91. onMapping(generatedLine, currentData[0], -1, -1, -1, -1);
  92. } else if (currentDataPos === 4) {
  93. onMapping(
  94. generatedLine,
  95. currentData[0],
  96. currentData[1],
  97. currentData[2],
  98. currentData[3],
  99. -1,
  100. );
  101. } else if (currentDataPos === 5) {
  102. onMapping(
  103. generatedLine,
  104. currentData[0],
  105. currentData[1],
  106. currentData[2],
  107. currentData[3],
  108. currentData[4],
  109. );
  110. }
  111. };
  112. module.exports = readMappings;