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