Skip to content

Commit

Permalink
move columns calculation via buffer back
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Jan 15, 2017
1 parent eea5e94 commit 58175e6
Showing 1 changed file with 19 additions and 33 deletions.
52 changes: 19 additions & 33 deletions lib/scanner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,36 +40,43 @@ var MIN_BUFFER_SIZE = 16 * 1024;
var OFFSET_MASK = 0x00FFFFFF;
var TYPE_OFFSET = 24;
var SafeUint32Array = typeof Uint32Array !== 'undefined' ? Uint32Array : Array; // fallback on Array when TypedArray is not supported
var lastIndexOf = Array.prototype.lastIndexOf; // some browser implementations have no TypedArray#lastIndexOf

function computeLines(scanner, source) {
function computeLinesAndColumns(scanner, source) {
var sourceLength = source.length;
var start = firstCharOffset(source);
var lines = scanner.lines;
var line = scanner.initLine;
var columns = scanner.columns;
var column = scanner.initColumn;

if (lines === null || lines.length < sourceLength + 1) {
lines = new SafeUint32Array(Math.max(sourceLength + 1024, MIN_BUFFER_SIZE));
columns = new SafeUint32Array(lines.length);
}

for (var i = start; i < sourceLength; i++) {
var code = source.charCodeAt(i);

lines[i] = line;
columns[i] = column++;

if (code === N || code === R || code === F) {
if (code === R && i + 1 < sourceLength && source.charCodeAt(i + 1) === N) {
i++;
lines[i] = line;
columns[i] = column;
}

line++;
column = 1;
}
}

lines[i] = line;

scanner.lineComputed = true;
scanner.linesAnsColumnsComputed = true;
scanner.lines = lines;
scanner.columns = columns;
}

function tokenLayout(scanner, source, startPos) {
Expand Down Expand Up @@ -148,6 +155,7 @@ function tokenLayout(scanner, source, startPos) {
var Scanner = function(source, initLine, initColumn) {
this.offsetAndType = null;
this.lines = null;
this.columns = null;

this.setSource(source || '', initLine, initColumn);
};
Expand All @@ -158,10 +166,8 @@ Scanner.prototype = {

this.source = source;
this.initLine = typeof initLine === 'undefined' ? 1 : initLine;
this.initColumn = (typeof initColumn === 'undefined' ? 1 : initColumn) - start;
this.lastLocationLine = this.initLine;
this.lastLocationLineOffset = 1 - this.initColumn;
this.lineComputed = false;
this.initColumn = typeof initColumn === 'undefined' ? 1 : initColumn;
this.linesAnsColumnsComputed = false;

this.eof = false;
this.currentToken = -1;
Expand Down Expand Up @@ -250,36 +256,16 @@ Scanner.prototype = {
this.next();
},

getLocation: function(offset, source) {
if (!this.lineComputed) {
computeLines(this, this.source);
}

var line = this.lines[offset];
var column = offset;
var lineOffset;

if (line === this.initLine) {
column += this.initColumn;
} else {
// try get precomputed line offset
if (line === this.lastLocationLine) {
lineOffset = this.lastLocationLineOffset;
} else {
// try avoid to compute line offset since it's expensive for long lines
lineOffset = lastIndexOf.call(this.lines, line - 1, offset);
this.lastLocationLine = line;
this.lastLocationLineOffset = lineOffset;
}

column -= lineOffset;
getLocation: function(offset, filename) {
if (!this.linesAnsColumnsComputed) {
computeLinesAndColumns(this, this.source);
}

return {
source: source,
source: filename,
offset: offset,
line: line,
column: column
line: this.lines[offset],
column: this.columns[offset]
};
},

Expand Down

0 comments on commit 58175e6

Please sign in to comment.