siphon-cli
Version:
Simple bundler for web applications. 📦🔧🧡
210 lines (209 loc) • 7.83 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parse_utils = void 0;
var structures_1 = require("../../../structures");
var errors_1 = require("../../../errors");
var utils_1 = require("../../../../utils");
var spreadcontexts = { array: true, expression: true, object: true }, commacontexts = {
array: true,
object: true,
property: true,
parameters: true,
call: true,
declaration: true,
};
var parse_utils = (function () {
function parse_utils() {
this.contexts = new structures_1.Stack();
this.i = 0;
this.from = 0;
this.end = false;
this.newline = false;
this.operators = new structures_1.Stack();
this.belly = new structures_1.Stack();
this.brackets = 0;
this.isNew = false;
}
parse_utils.prototype.allowSpread = function () {
return spreadcontexts[this.contexts.top()];
};
parse_utils.prototype.requireComma = function () {
return commacontexts[this.contexts.top()];
};
parse_utils.prototype.raise = function (message, token, at) {
errors_1.default.enc(message, this.options.sourceFile, at !== null && at !== void 0 ? at : this.i, {
token: token !== null && token !== void 0 ? token : this.text[this.i],
});
};
parse_utils.prototype.lowerPrecedence = function () {
var topOperator = this.operators.top(), bellyTop = this.belly.top();
if (topOperator === undefined)
return false;
if (utils_1.precedence[topOperator] > utils_1.precedence[bellyTop] ||
(utils_1.precedence[topOperator] === utils_1.precedence[bellyTop] &&
(0, utils_1.assoc)(topOperator) === "LR")) {
this.backtrack();
return true;
}
else
return false;
};
parse_utils.prototype.isNewCaseStatement = function () {
return (this.predict("case") ||
this.predict("default") ||
this.text[this.i] === "}");
};
parse_utils.prototype.eat = function (ptn) {
if (this.text.slice(this.i, this.i + ptn.length) !== ptn)
return false;
this.i += ptn.length;
this.belly.push(ptn);
return true;
};
parse_utils.prototype.taste = function (ptn) {
if (this.text.slice(this.i, this.i + ptn.length) !== ptn)
return false;
this.i += ptn.length;
return true;
};
parse_utils.prototype.peek = function (i) {
if (i === void 0) { i = 1; }
return this.text[this.i + i];
};
parse_utils.prototype.recede = function (i) {
if (i === void 0) { i = 1; }
this.i -= i;
};
parse_utils.prototype.next = function (i) {
if (i === void 0) { i = 1; }
this.i += i;
};
parse_utils.prototype.expect = function (ptn) {
this.outerspace();
if (this.text.slice(this.i, this.i + ptn.length) !== ptn)
this.raise("JS_UNEXPECTED_TOKEN");
};
parse_utils.prototype.backtrack = function () {
this.i -= this.belly.pop().length;
};
parse_utils.prototype.match = function (ptn) {
if (ptn[0] === this.text[this.i] &&
this.text.slice(this.i, this.i + ptn.length) === ptn &&
!(0, utils_1.isValidIdentifierCharacter)(this.peek(ptn.length))) {
this.i += ptn.length;
this.belly.push(ptn);
return true;
}
else
return false;
};
parse_utils.prototype.predict = function (ptn) {
if (this.text.slice(this.i, this.i + ptn.length) === ptn &&
!(0, utils_1.isValidIdentifierCharacter)(this.peek(ptn.length)))
return true;
else
return false;
};
parse_utils.prototype.count = function (base) {
if (base === void 0) { base = 10; }
var num = "";
switch (base) {
case 8:
while ((0, utils_1.isOctalDigit)(this.text[this.i]))
num += this.text[this.i++];
break;
case 2:
while ((0, utils_1.isBinaryDigit)(this.text[this.i]))
num += this.text[this.i++];
break;
case 10:
while ((0, utils_1.isDigit)(this.text[this.i]))
num += this.text[this.i++];
break;
case 16:
while ((0, utils_1.isHexDigit)(this.text[this.i]))
num += this.text[this.i++];
}
return num;
};
parse_utils.prototype.skip = function (contextual) {
if (this.belly.top() === "/*")
while (!(this.eat("*/") || this.text[this.i] === undefined))
this.i++;
else {
while (!(this.text[this.i] === "\n" || this.text[this.i] === undefined))
this.i++;
contextual && this.text[this.i] === "\n" && !this.newline
? (this.newline = true)
: 0,
this.i++;
}
this.belly.pop();
};
parse_utils.prototype.read = function (i) {
var marker = this.text[i++], value = "";
while (this.text[i] && this.text[i] !== marker) {
if (this.text[i] === "\\")
(value += "\\".concat(this.text[++i])), i++;
else {
if (this.text[i] === "\n" && marker !== "`")
this.raise("UNTERMINATED_STRING_LITERAL");
value += this.text[i++];
}
}
if (!this.text[i])
this.raise("UNTERMINATED_STRING_LITERAL");
return { end: i, value: marker + value + marker, marker: marker };
};
parse_utils.prototype.regex = function (i) {
var value = "";
var flags = "";
while (this.text[i] !== undefined && !/\n|\//.test(this.text[i])) {
if (this.text[i] === "\\") {
value += "\\".concat(this.text[++i]);
i++;
}
else {
if (this.text[i] === "[") {
while (this.text[i] !== undefined && this.text[i] !== "]") {
if (this.text[i] === "\\") {
value += "\\".concat(this.text[++i]);
i++;
}
else
value += this.text[i++];
}
if (this.text[i] === undefined) {
this.raise("UNTERMINATED_REGEX_LITERAL");
}
}
value += this.text[i++];
}
}
if (!this.text[i] || this.text[i] == "\n")
this.raise("UNTERMINATED_REGEX_LITERAL");
i++;
while ((0, utils_1.isAlphabetic)(this.text[i]))
flags += this.text[i++];
if ((0, utils_1.isDigit)(this.text[i]) || /\_|\$|\!|\#|\¬|\~|\@/.test(this.text[i]))
this.raise("JS_INVALID_REGEX_FLAG");
return {
end: i,
value: value,
flags: flags,
};
};
parse_utils.prototype.glazeOverComments = function () {
if (this.eat("//") || this.eat("/*"))
this.skip(true);
};
parse_utils.prototype.outerspace = function () {
while (utils_1.EMPTY_SPACE.test(this.text[this.i]))
this.i++;
this.glazeOverComments();
if (utils_1.EMPTY_SPACE.test(this.text[this.i]))
this.outerspace();
};
return parse_utils;
}());
exports.parse_utils = parse_utils;