UNPKG

dicelang

Version:

JavaScript interpreter of the Roll20 dice language

45 lines (44 loc) 1.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function parenthesize(input, openParenChar, closeParenChar) { if (openParenChar === void 0) { openParenChar = '('; } if (closeParenChar === void 0) { closeParenChar = ')'; } var result = []; var firstOpenParen = input.indexOf(openParenChar); var firstCloseParen = input.indexOf(closeParenChar); if (firstOpenParen < 0 && firstCloseParen < 0) { return [input]; } else if (firstOpenParen < 0 || firstCloseParen < 0 || firstCloseParen < firstOpenParen) { throw new Error("UNMATCHED PARENTHESIS"); } if (firstOpenParen > 0) { result.push(input.slice(0, firstOpenParen)); } var parenStack = [firstOpenParen]; var matchingParen = firstOpenParen; while (parenStack.length > 0 && matchingParen < input.length) { ++matchingParen; if (input.charAt(matchingParen) === openParenChar) { parenStack.push(matchingParen); } else if (input.charAt(matchingParen) === closeParenChar) { parenStack.pop(); } } if (parenStack.length > 0) { throw new Error("UNMATCHED PARENTHESIS"); } result.push(parenthesize(input.slice(firstOpenParen + 1, matchingParen))); if (matchingParen < input.length - 1) { result.push.apply(result, parenthesize(input.slice(matchingParen + 1))); } return result; } exports.parenthesize = parenthesize; function splitOperators(input, operatorMatcher) { if (operatorMatcher === void 0) { operatorMatcher = /[+-*/]/g; } var result = []; return result; } exports.splitOperators = splitOperators;