alasql
Version:
AlaSQL.js - JavaScript SQL database library for relational and graph data manipulation with support of localStorage, IndexedDB, and Excel
108 lines (103 loc) • 3.61 kB
JavaScript
/**
Strip all comments.
Based om the https://github.com/lehni/uncomment.js/blob/master/uncomment.js
I just replaced JavaScript's '//' to SQL's '--' and remove other stuff
TODO: Fixed [aaa/*bbb] for column names
TODO: Bug if -- comments in the last line
*/
alasql.utils.uncomment = function uncomment(str) {
// Add some padding so we can always look ahead and behind by two chars
str = ('__' + str + '__').split('');
var quote = false,
quoteSign,
// regularExpression = false,
// characterClass = false,
blockComment = false,
lineComment = false;
// preserveComment = false;
for (var i = 0, l = str.length; i < l; i++) {
// console.log(i,str[i]);
// When checking for quote escaping, we also need to check that the
// escape sign itself is not escaped, as otherwise '\\' would cause
// the wrong impression of an unclosed string:
var unescaped = str[i - 1] !== '\\' || str[i - 2] === '\\';
if (quote) {
if (str[i] === quoteSign && unescaped)
quote = false;
// } else if (regularExpression) {
// Make sure '/'' inside character classes is not considered the end
// of the regular expression.
// if (str[i] === '[' && unescaped) {
// characterClass = true;
// } else if (str[i] === ']' && unescaped && characterClass) {
// characterClass = false;
// } else if (str[i] === '/' && unescaped && !characterClass) {
// regularExpression = false;
// }
} else if (blockComment) {
// Is the block comment closing?
if (str[i] === '*' && str[i + 1] === '/') {
// if (!preserveComment)
str[i] = str[i + 1] = '';
blockComment /* = preserveComment*/ = false;
// Increase by 1 to skip closing '/', as it would be mistaken
// for a regexp otherwise
i++;
} else { //if (!preserveComment) {
str[i] = '';
}
} else if (lineComment) {
// One-line comments end with the line-break
if (str[i + 1] == '\n' || str[i + 1] == '\r')
lineComment = false;
str[i] = '';
} else {
if (str[i] == '"' || str[i] == "'") {
quote = true;
quoteSign = str[i];
} else if (str[i] == '[' && str[i-1] != "@") {
quote = true;
quoteSign = ']';
// } else if (str[i] === '-' && str[i + 1] === '-') {
// str[i] = '';
// lineComment = true;
} else if (str[i] === '/' && str[i + 1] === '*') {
// Do not filter out conditional comments /*@ ... */
// and comments marked as protected /*! ... */
// preserveComment = /[@!]/.test(str[i + 2]);
// if (!preserveComment)
str[i] = '';
blockComment = true;
// console.log('block');
// } else if (str[i + 1] === '/') {
// str[i] = '';
// lineComment = true;
// } else {
// We need to make sure we don't count normal divisions as
// regular expresions. Matching this properly is difficult,
// but if we assume that normal division always have a space
// after /, a simple check for white space or '='' (for /=)
// is enough to distinguish divisions from regexps.
// TODO: Develop a proper check for regexps.
// if (!/[\s=]/.test(str[i + 1])) {
// regularExpression = true;
// }
// }
}
}
}
// Remove padding again.
str = str.join('').slice(2, -2);
/*
// Strip empty lines that contain only white space and line breaks, as they
// are left-overs from comment removal.
str = str.replace(/^[ \t]+(\r\n|\n|\r)/gm, function(all) {
return '';
});
// Replace a sequence of more than two line breaks with only two.
str = str.replace(/(\r\n|\n|\r)(\r\n|\n|\r)+/g, function(all, lineBreak) {
return lineBreak + lineBreak;
});
*/
return str;
};