readMappings.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. {
  15. ccToValue.fill(INVALID);
  16. for (let i = 0; i < ALPHABET.length; i++) {
  17. ccToValue[ALPHABET.charCodeAt(i)] = i;
  18. }
  19. ccToValue[",".charCodeAt(0)] = END_SEGMENT_BIT;
  20. ccToValue[";".charCodeAt(0)] = NEXT_LINE;
  21. }
  22. const ccMax = ccToValue.length - 1;
  23. /** @typedef {function(number, number, number, number, number, number): void} OnMapping */
  24. /**
  25. * @param {string} mappings the mappings string
  26. * @param {OnMapping} onMapping called for each mapping
  27. * @returns {void}
  28. */
  29. const readMappings = (mappings, onMapping) => {
  30. // generatedColumn, [sourceIndex, originalLine, orignalColumn, [nameIndex]]
  31. const currentData = new Uint32Array([0, 0, 1, 0, 0]);
  32. let currentDataPos = 0;
  33. // currentValue will include a sign bit at bit 0
  34. let currentValue = 0;
  35. let currentValuePos = 0;
  36. let generatedLine = 1;
  37. let generatedColumn = -1;
  38. for (let i = 0; i < mappings.length; i++) {
  39. const cc = mappings.charCodeAt(i);
  40. if (cc > ccMax) continue;
  41. const value = ccToValue[cc];
  42. if ((value & END_SEGMENT_BIT) !== 0) {
  43. // End current segment
  44. if (currentData[0] > generatedColumn) {
  45. if (currentDataPos === 1) {
  46. onMapping(generatedLine, currentData[0], -1, -1, -1, -1);
  47. } else if (currentDataPos === 4) {
  48. onMapping(
  49. generatedLine,
  50. currentData[0],
  51. currentData[1],
  52. currentData[2],
  53. currentData[3],
  54. -1
  55. );
  56. } else if (currentDataPos === 5) {
  57. onMapping(
  58. generatedLine,
  59. currentData[0],
  60. currentData[1],
  61. currentData[2],
  62. currentData[3],
  63. currentData[4]
  64. );
  65. }
  66. generatedColumn = currentData[0];
  67. }
  68. currentDataPos = 0;
  69. if (value === NEXT_LINE) {
  70. // Start new line
  71. generatedLine++;
  72. currentData[0] = 0;
  73. generatedColumn = -1;
  74. }
  75. } else if ((value & CONTINUATION_BIT) === 0) {
  76. // last sextet
  77. currentValue |= value << currentValuePos;
  78. const finalValue =
  79. currentValue & 1 ? -(currentValue >> 1) : currentValue >> 1;
  80. currentData[currentDataPos++] += finalValue;
  81. currentValuePos = 0;
  82. currentValue = 0;
  83. } else {
  84. currentValue |= (value & DATA_MASK) << currentValuePos;
  85. currentValuePos += 5;
  86. }
  87. }
  88. // End current segment
  89. if (currentDataPos === 1) {
  90. onMapping(generatedLine, currentData[0], -1, -1, -1, -1);
  91. } else if (currentDataPos === 4) {
  92. onMapping(
  93. generatedLine,
  94. currentData[0],
  95. currentData[1],
  96. currentData[2],
  97. currentData[3],
  98. -1
  99. );
  100. } else if (currentDataPos === 5) {
  101. onMapping(
  102. generatedLine,
  103. currentData[0],
  104. currentData[1],
  105. currentData[2],
  106. currentData[3],
  107. currentData[4]
  108. );
  109. }
  110. };
  111. module.exports = readMappings;