@drewsonne/maya-calculator-parser
Version:
Typescript package to parse plaintext calculations and create operators
178 lines • 8.62 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var collection_1 = __importDefault(require("../tokens/collection"));
var layer_2_test_1 = require("./layer-2-test");
var calendar_round_operation_token_1 = __importDefault(require("../tokens/layer-2/calendar-round-operation-token"));
var long_count_operation_token_1 = __importDefault(require("../tokens/layer-2/long-count-operation-token"));
var calendar_round_wildcard_operation_token_1 = __importDefault(require("../tokens/layer-2/calendar-round-wildcard-operation-token"));
var long_count_wildcard_operation_token_1 = __importDefault(require("../tokens/layer-2/long-count-wildcard-operation-token"));
var layer_1_test_1 = require("./layer-1-test");
var base_parser_1 = __importDefault(require("./base-parser"));
var exeption_1 = __importDefault(require("./exeption"));
var Layer2Parser = (function (_super) {
__extends(Layer2Parser, _super);
function Layer2Parser() {
return _super !== null && _super.apply(this, arguments) || this;
}
Layer2Parser.prototype.parse = function (layer1Tokens) {
var tokens = [];
var cache = [];
for (var cursor = 0; cursor < layer1Tokens.length; cursor += 1) {
var token = layer1Tokens.index(cursor);
if (layer_2_test_1.isLongCountToken(token)) {
if (token.isPartial()) {
tokens.push(long_count_wildcard_operation_token_1.default.parse(token));
}
else if (layer_2_test_1.isOperatorToken(cache[0])) {
if (tokens.length > 0) {
var lastToken = tokens.pop();
if (lastToken !== undefined) {
var operator = cache.pop();
if (operator !== undefined) {
if (layer_2_test_1.isLongCountToken(lastToken)) {
tokens.push(long_count_operation_token_1.default.parse(lastToken, operator, token));
}
else {
tokens.push(lastToken);
}
}
else {
throw new exeption_1.default(2);
}
}
else {
throw new exeption_1.default(2);
}
}
else {
throw new exeption_1.default(2);
}
}
else {
tokens.push(token);
}
}
else if (layer_2_test_1.isCalendarRoundToken(token)) {
if (token.isPartial()) {
tokens.push(calendar_round_wildcard_operation_token_1.default.parse(token));
}
else if (layer_2_test_1.isOperatorToken(cache[0])) {
if (tokens.length > 0) {
var lastToken = tokens.pop();
if (lastToken !== undefined) {
var operator = cache.pop();
if (operator !== undefined) {
if (layer_2_test_1.isCalendarRoundToken(lastToken)) {
tokens.push(calendar_round_operation_token_1.default.parse(lastToken, operator, token));
}
else {
tokens.push(lastToken);
}
}
else {
throw new exeption_1.default(2);
}
}
else {
throw new exeption_1.default(2);
}
}
else {
throw new exeption_1.default(2);
}
}
else {
tokens.push(token);
}
}
else if (layer_2_test_1.isPartialOperation(cache, token)) {
cache.push(token);
}
else if (layer_2_test_1.isFullOperation(cache, token)) {
cache.push(token);
if (layer_2_test_1.isLongCountOperation([cache[0], cache[1], cache[2]])) {
tokens.push(long_count_operation_token_1.default.parse(cache[0], cache[1], cache[2]));
cursor += 1;
}
else if (layer_2_test_1.isCalendarRoundOperation([cache[0], cache[1], cache[2]])) {
tokens.push(calendar_round_operation_token_1.default.parse(cache[0], cache[1], cache[2]));
cursor += 1;
}
else {
throw new exeption_1.default(2);
}
cache = [];
}
else if (layer_2_test_1.isOperatorToken(token)) {
var previous = tokens.pop();
if (previous !== undefined) {
var next = layer1Tokens.index(cursor + 1);
if (layer_2_test_1.isLongCountToken(previous) && layer_2_test_1.isLongCountToken(next)) {
tokens.push(long_count_operation_token_1.default.parse(previous, token, next));
cursor += 1;
}
else if (layer_2_test_1.isCalendarRoundToken(previous) && layer_2_test_1.isCalendarRoundToken(next)) {
tokens.push(calendar_round_operation_token_1.default.parse(previous, token, next));
cursor += 1;
}
else if (layer_2_test_1.isCalendarRoundToken(next) || layer_2_test_1.isLongCountToken(next)) {
var nonFunctionalTokens = [];
for (var i = tokens.length - 1; i >= 0; i -= 1) {
var element = tokens[i];
if (!(layer_1_test_1.isCommentToken(element))) {
tokens = tokens.splice(0, i);
if (layer_2_test_1.isLongCountToken(next)) {
tokens.push(long_count_operation_token_1.default.parse(element, token, next));
break;
}
else if (layer_2_test_1.isCalendarRoundToken(next)) {
tokens.push(calendar_round_operation_token_1.default.parse(element, token, next));
break;
}
else {
throw new exeption_1.default(2);
}
}
else {
nonFunctionalTokens.push(element);
}
}
}
else {
throw new exeption_1.default(2);
}
}
else {
throw new exeption_1.default(2);
}
}
else if (layer_1_test_1.isCommentToken(token) ||
layer_1_test_1.isLineEndToken(token)) {
tokens.push(token);
}
else {
throw new exeption_1.default(2);
}
}
return new collection_1.default(this.appendLineEnd(tokens));
};
return Layer2Parser;
}(base_parser_1.default));
exports.default = Layer2Parser;
//# sourceMappingURL=layer-2-parser.js.map