UNPKG

cor-lang

Version:
168 lines (128 loc) 4.05 kB
(function(cor){ // This is a version, // the original code can be found at // https://github.com/jashkenas/coffeescript/tree/master/src/sourcemap.litcoffee var Class = cor.Class, VLQ_SHIFT = 5, VLQ_CONTINUATION_BIT = 1 << VLQ_SHIFT, VLQ_VALUE_MASK = VLQ_CONTINUATION_BIT - 1, BASE64_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; function toVlq(value) { var nextChunk, vlq = '', signBit = value < 0 ? 1 : 0, valueToEncode = (Math.abs(value) << 1) + signBit; while (valueToEncode || !vlq) { nextChunk = valueToEncode & VLQ_VALUE_MASK; valueToEncode = valueToEncode >> VLQ_SHIFT; if (valueToEncode) { nextChunk |= VLQ_CONTINUATION_BIT; } vlq += toBase64(nextChunk); } return vlq; }; function toBase64(value) { var b64 = BASE64_CHARS[value]; if (! b64) { throw 'Can not encode ' + value + ' to base-64'; } return b64; } var LineMap = Class({ line : null, segments: null, init: function(l) { this.line = l; this.segments = []; }, add: function(generatedColumn, sourceLine, sourceColumn) { this.segments[generatedColumn] = { line : this.line, column : generatedColumn, sourceLine : sourceLine, sourceColumn: sourceColumn }; return this.segments; } }); var SourceMap = Class({ lines: null, names: null, init: function() { this.lines = []; //this.names = []; }, add: function(sourceLine, sourceColumn, generatedLine, generatedColumn) { var line = this.lines[generatedLine]; if (! line) { line = this.lines[generatedLine] = new LineMap(generatedLine); } line.add(generatedColumn, sourceLine, sourceColumn); }, generate: function(config) { config = config || {}; var i, j, line, sm, segment, segmentsLen, currentLine = 0, lastSourceLine = 0, lastSourceColumn = 0, lastColumn = 0, linesLen = this.lines.length, mapping = '', segmentSep = ''; for (i = 0; i < linesLen; i++) { line = this.lines[i]; if (! line) { continue } segmentsLen = line.segments.length; for (j = 0; j < segmentsLen; j++) { segment = line.segments[j]; if (! segment) { continue } while (currentLine < segment.line) { segmentSep = '', lastColumn = 0; mapping += ';'; currentLine++; } mapping += segmentSep; mapping += toVlq(segment.column - lastColumn); mapping += toVlq(0); mapping += toVlq(segment.sourceLine - lastSourceLine); mapping += toVlq(segment.sourceColumn - lastSourceColumn); lastColumn = segment.column; lastSourceLine = segment.sourceLine; lastSourceColumn = segment.sourceColumn; segmentSep = ','; } } sm = { version : 3, file : '', sourceRoot : '', sources : [''], sourcesContent: [null], names : [], mappings : mapping }; if (config.file) { sm.file = config.file; } if (config.sourceRoot){ sm.sourceRoot = config.sourceRoot; } if (config.source) { sm.sources = [config.source]; } if (config.sourceContent) { sm.sourcesContent = [config.sourceContent]; } return JSON.stringify(sm); } }); cor.SourceMap = SourceMap; })(typeof cor === 'undefined' ? {} : cor);