@itwin/grouping-mapping-widget 0.3.2 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/formula/FormulaFunctionProvider.d.ts +18 -0
- package/lib/cjs/formula/FormulaFunctionProvider.js +136 -0
- package/lib/cjs/formula/FormulaFunctionProvider.js.map +1 -0
- package/lib/cjs/formula/FormulaOperatorsProvider.d.ts +34 -0
- package/lib/cjs/formula/FormulaOperatorsProvider.js +185 -0
- package/lib/cjs/formula/FormulaOperatorsProvider.js.map +1 -0
- package/lib/cjs/formula/FormulaSplitter.d.ts +2 -0
- package/lib/cjs/formula/FormulaSplitter.js +140 -0
- package/lib/cjs/formula/FormulaSplitter.js.map +1 -0
- package/lib/cjs/formula/FormulaTokensValidator.d.ts +5 -0
- package/lib/cjs/formula/FormulaTokensValidator.js +135 -0
- package/lib/cjs/formula/FormulaTokensValidator.js.map +1 -0
- package/lib/cjs/formula/FormulaValidator.d.ts +3 -0
- package/lib/cjs/formula/FormulaValidator.js +35 -0
- package/lib/cjs/formula/FormulaValidator.js.map +1 -0
- package/lib/cjs/formula/IResult.d.ts +5 -0
- package/lib/cjs/formula/IResult.js +3 -0
- package/lib/cjs/formula/IResult.js.map +1 -0
- package/lib/cjs/formula/InfixToPostfixConverter.d.ts +18 -0
- package/lib/cjs/formula/InfixToPostfixConverter.js +299 -0
- package/lib/cjs/formula/InfixToPostfixConverter.js.map +1 -0
- package/lib/cjs/formula/InputStream.d.ts +12 -0
- package/lib/cjs/formula/InputStream.js +36 -0
- package/lib/cjs/formula/InputStream.js.map +1 -0
- package/lib/cjs/formula/ParenthesisValidator.d.ts +7 -0
- package/lib/cjs/formula/ParenthesisValidator.js +34 -0
- package/lib/cjs/formula/ParenthesisValidator.js.map +1 -0
- package/lib/cjs/formula/Queue.d.ts +11 -0
- package/lib/cjs/formula/Queue.js +42 -0
- package/lib/cjs/formula/Queue.js.map +1 -0
- package/lib/cjs/formula/Stack.d.ts +14 -0
- package/lib/cjs/formula/Stack.js +71 -0
- package/lib/cjs/formula/Stack.js.map +1 -0
- package/lib/cjs/formula/StringBuilder.d.ts +9 -0
- package/lib/cjs/formula/StringBuilder.js +26 -0
- package/lib/cjs/formula/StringBuilder.js.map +1 -0
- package/lib/cjs/formula/Types.d.ts +8 -0
- package/lib/cjs/formula/Types.js +3 -0
- package/lib/cjs/formula/Types.js.map +1 -0
- package/lib/cjs/formula/Utils.d.ts +7 -0
- package/lib/cjs/formula/Utils.js +39 -0
- package/lib/cjs/formula/Utils.js.map +1 -0
- package/lib/cjs/widget/components/CalculatedPropertyAction.d.ts +1 -1
- package/lib/cjs/widget/components/CalculatedPropertyAction.js +3 -3
- package/lib/cjs/widget/components/CalculatedPropertyAction.js.map +1 -1
- package/lib/cjs/widget/components/CalculatedPropertyTable.d.ts +5 -2
- package/lib/cjs/widget/components/CalculatedPropertyTable.js +3 -27
- package/lib/cjs/widget/components/CalculatedPropertyTable.js.map +1 -1
- package/lib/cjs/widget/components/ConfirmMappingsImport.scss +4 -2
- package/lib/cjs/widget/components/CustomCalculationAction.d.ts +4 -2
- package/lib/cjs/widget/components/CustomCalculationAction.js +9 -4
- package/lib/cjs/widget/components/CustomCalculationAction.js.map +1 -1
- package/lib/cjs/widget/components/CustomCalculationTable.d.ts +5 -2
- package/lib/cjs/widget/components/CustomCalculationTable.js +3 -27
- package/lib/cjs/widget/components/CustomCalculationTable.js.map +1 -1
- package/lib/cjs/widget/components/GroupAction.js +2 -10
- package/lib/cjs/widget/components/GroupAction.js.map +1 -1
- package/lib/cjs/widget/components/GroupPropertyAction.d.ts +1 -1
- package/lib/cjs/widget/components/GroupPropertyAction.js +3 -3
- package/lib/cjs/widget/components/GroupPropertyAction.js.map +1 -1
- package/lib/cjs/widget/components/GroupPropertyTable.d.ts +5 -2
- package/lib/cjs/widget/components/GroupPropertyTable.js +3 -27
- package/lib/cjs/widget/components/GroupPropertyTable.js.map +1 -1
- package/lib/cjs/widget/components/MappingAction.js +2 -10
- package/lib/cjs/widget/components/MappingAction.js.map +1 -1
- package/lib/cjs/widget/components/MappingImportWizardModal.js +2 -2
- package/lib/cjs/widget/components/MappingImportWizardModal.js.map +1 -1
- package/lib/cjs/widget/components/MappingImportWizardModal.scss +6 -2
- package/lib/cjs/widget/components/PropertyMenu.js +68 -21
- package/lib/cjs/widget/components/PropertyMenu.js.map +1 -1
- package/lib/cjs/widget/components/SelectIModel.scss +2 -1
- package/lib/cjs/widget/components/SelectMapping.scss +2 -1
- package/lib/cjs/widget/components/SelectProject.js +9 -9
- package/lib/cjs/widget/components/SelectProject.js.map +1 -1
- package/lib/cjs/widget/components/SelectProject.scss +3 -8
- package/lib/cjs/widget/hooks/useFetchData.d.ts +9 -0
- package/lib/cjs/widget/hooks/useFetchData.js +41 -0
- package/lib/cjs/widget/hooks/useFetchData.js.map +1 -0
- package/lib/cjs/widget/hooks/useFormulaValidation.d.ts +6 -0
- package/lib/cjs/widget/hooks/useFormulaValidation.js +29 -0
- package/lib/cjs/widget/hooks/useFormulaValidation.js.map +1 -0
- package/lib/cjs/widget/utils.d.ts +1 -0
- package/lib/cjs/widget/utils.js +9 -0
- package/lib/cjs/widget/utils.js.map +1 -1
- package/lib/esm/formula/FormulaFunctionProvider.d.ts +18 -0
- package/lib/esm/formula/FormulaFunctionProvider.js +130 -0
- package/lib/esm/formula/FormulaFunctionProvider.js.map +1 -0
- package/lib/esm/formula/FormulaOperatorsProvider.d.ts +34 -0
- package/lib/esm/formula/FormulaOperatorsProvider.js +174 -0
- package/lib/esm/formula/FormulaOperatorsProvider.js.map +1 -0
- package/lib/esm/formula/FormulaSplitter.d.ts +2 -0
- package/lib/esm/formula/FormulaSplitter.js +136 -0
- package/lib/esm/formula/FormulaSplitter.js.map +1 -0
- package/lib/esm/formula/FormulaTokensValidator.d.ts +5 -0
- package/lib/esm/formula/FormulaTokensValidator.js +131 -0
- package/lib/esm/formula/FormulaTokensValidator.js.map +1 -0
- package/lib/esm/formula/FormulaValidator.d.ts +3 -0
- package/lib/esm/formula/FormulaValidator.js +31 -0
- package/lib/esm/formula/FormulaValidator.js.map +1 -0
- package/lib/esm/formula/IResult.d.ts +5 -0
- package/lib/esm/formula/IResult.js +2 -0
- package/lib/esm/formula/IResult.js.map +1 -0
- package/lib/esm/formula/InfixToPostfixConverter.d.ts +18 -0
- package/lib/esm/formula/InfixToPostfixConverter.js +295 -0
- package/lib/esm/formula/InfixToPostfixConverter.js.map +1 -0
- package/lib/esm/formula/InputStream.d.ts +12 -0
- package/lib/esm/formula/InputStream.js +32 -0
- package/lib/esm/formula/InputStream.js.map +1 -0
- package/lib/esm/formula/ParenthesisValidator.d.ts +7 -0
- package/lib/esm/formula/ParenthesisValidator.js +30 -0
- package/lib/esm/formula/ParenthesisValidator.js.map +1 -0
- package/lib/esm/formula/Queue.d.ts +11 -0
- package/lib/esm/formula/Queue.js +38 -0
- package/lib/esm/formula/Queue.js.map +1 -0
- package/lib/esm/formula/Stack.d.ts +14 -0
- package/lib/esm/formula/Stack.js +67 -0
- package/lib/esm/formula/Stack.js.map +1 -0
- package/lib/esm/formula/StringBuilder.d.ts +9 -0
- package/lib/esm/formula/StringBuilder.js +22 -0
- package/lib/esm/formula/StringBuilder.js.map +1 -0
- package/lib/esm/formula/Types.d.ts +8 -0
- package/lib/esm/formula/Types.js +2 -0
- package/lib/esm/formula/Types.js.map +1 -0
- package/lib/esm/formula/Utils.d.ts +7 -0
- package/lib/esm/formula/Utils.js +30 -0
- package/lib/esm/formula/Utils.js.map +1 -0
- package/lib/esm/widget/components/CalculatedPropertyAction.d.ts +1 -1
- package/lib/esm/widget/components/CalculatedPropertyAction.js +3 -3
- package/lib/esm/widget/components/CalculatedPropertyAction.js.map +1 -1
- package/lib/esm/widget/components/CalculatedPropertyTable.d.ts +5 -2
- package/lib/esm/widget/components/CalculatedPropertyTable.js +4 -28
- package/lib/esm/widget/components/CalculatedPropertyTable.js.map +1 -1
- package/lib/esm/widget/components/ConfirmMappingsImport.scss +4 -2
- package/lib/esm/widget/components/CustomCalculationAction.d.ts +4 -2
- package/lib/esm/widget/components/CustomCalculationAction.js +9 -4
- package/lib/esm/widget/components/CustomCalculationAction.js.map +1 -1
- package/lib/esm/widget/components/CustomCalculationTable.d.ts +5 -2
- package/lib/esm/widget/components/CustomCalculationTable.js +4 -28
- package/lib/esm/widget/components/CustomCalculationTable.js.map +1 -1
- package/lib/esm/widget/components/GroupAction.js +2 -10
- package/lib/esm/widget/components/GroupAction.js.map +1 -1
- package/lib/esm/widget/components/GroupPropertyAction.d.ts +1 -1
- package/lib/esm/widget/components/GroupPropertyAction.js +3 -3
- package/lib/esm/widget/components/GroupPropertyAction.js.map +1 -1
- package/lib/esm/widget/components/GroupPropertyTable.d.ts +5 -2
- package/lib/esm/widget/components/GroupPropertyTable.js +4 -28
- package/lib/esm/widget/components/GroupPropertyTable.js.map +1 -1
- package/lib/esm/widget/components/MappingAction.js +2 -10
- package/lib/esm/widget/components/MappingAction.js.map +1 -1
- package/lib/esm/widget/components/MappingImportWizardModal.js +2 -2
- package/lib/esm/widget/components/MappingImportWizardModal.js.map +1 -1
- package/lib/esm/widget/components/MappingImportWizardModal.scss +6 -2
- package/lib/esm/widget/components/PropertyMenu.js +69 -22
- package/lib/esm/widget/components/PropertyMenu.js.map +1 -1
- package/lib/esm/widget/components/SelectIModel.scss +2 -1
- package/lib/esm/widget/components/SelectMapping.scss +2 -1
- package/lib/esm/widget/components/SelectProject.js +9 -9
- package/lib/esm/widget/components/SelectProject.js.map +1 -1
- package/lib/esm/widget/components/SelectProject.scss +3 -8
- package/lib/esm/widget/hooks/useFetchData.d.ts +9 -0
- package/lib/esm/widget/hooks/useFetchData.js +35 -0
- package/lib/esm/widget/hooks/useFetchData.js.map +1 -0
- package/lib/esm/widget/hooks/useFormulaValidation.d.ts +6 -0
- package/lib/esm/widget/hooks/useFormulaValidation.js +25 -0
- package/lib/esm/widget/hooks/useFormulaValidation.js.map +1 -0
- package/lib/esm/widget/utils.d.ts +1 -0
- package/lib/esm/widget/utils.js +7 -1
- package/lib/esm/widget/utils.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FormulaSplitter.js","sourceRoot":"","sources":["../../../src/formula/FormulaSplitter.ts"],"names":[],"mappings":";;;AAAA;;;+FAG+F;AAC/F,+CAA4C;AAC5C,mDAAgD;AAChD,mCAA6G;AAE7G,MAAM,sBAAsB,GAAG,CAAC,MAAM,CAAC,CAAC;AACxC,MAAM,uBAAuB,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;AAChG,MAAM,qBAAqB,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;AACnK,MAAM,qBAAqB,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;AAE5K,MAAM,kBAAkB,GAAe;IACrC,sBAAsB;IACtB,uBAAuB;IACvB,qBAAqB;IACrB,qBAAqB;CACtB,CA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B,IAAI,oBAAY,CAAC,IAAI,CAAC,EAAE;YACtB,SAAS;SACV;QAED,IAAI,eAAO,CAAC,IAAI,CAAC,IAAI,0BAAkB,CAAC,IAAI,CAAC,EAAE;YAC7C,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC;SAClC;aAAM,IAAI,4BAAoB,CAAC,IAAI,CAAC,EAAE;YACrC,MAAM,CAAC,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC,CAAC;SACpC;aAAM,IAAI,yBAAiB,CAAC,IAAI,CAAC,EAAE;YAClC,MAAM,CAAC,IAAI,CAAC,kBAAkB,CAAC,MAAM,CAAC,CAAC,CAAC;SACzC;aAAM,IAAI,qBAAqB,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE;YAC/C,MAAM,CAAC,IAAI,CAAC,sBAAsB,CAAC,MAAM,CAAC,CAAC,CAAC;SAC7C;aAAM;YACL,MAAM,IAAI,KAAK,CAAC,4BAA4B,IAAI,IAAI,CAAC,CAAC;SACvD;KACF;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,SAAgB,YAAY,CAAC,OAAe;IAC1C,OAAO,YAAY,CAAC,OAAO,CAAC,CAAC;AAC/B,CAAC;AAFD,oCAEC","sourcesContent":["/*---------------------------------------------------------------------------------------------\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\n* See LICENSE.md in the project root for license terms and full copyright notice.\n*--------------------------------------------------------------------------------------------*/\nimport { InputStream } from \"./InputStream\";\nimport { StringBuilder } from \"./StringBuilder\";\nimport { isDecimalSeparator, isDigit, isLetterOrUnderscore, isStringDelimiter, isWhitespace } from \"./Utils\";\n\nconst fourSymbolCombinations = [\">>>=\"];\nconst threeSymbolCombinations = [\"**=\", \"<<=\", \">>=\", \"&&=\", \"||=\", \"??=\", \"===\", \"!==\", \">>>\"];\nconst twoSymbolCombinations = [\"+=\", \"-=\", \"*=\", \"/=\", \"%=\", \"&=\", \"^=\", \"|=\", \"?=\", \"==\", \"!=\", \">=\", \"<=\", \"++\", \"--\", \"**\", \"<<\", \">>\", \"&&\", \"||\", \"??\", \"?.\"];\nconst oneSymbolCombinations = [\"~\", \"!\", \"@\", \"#\", \"$\", \"%\", \"^\", \"&\", \"*\", \"(\", \")\", \"-\", \"=\", \"+\", \"[\", \"]\", \"{\", \"}\", \"\\\\\", \"|\", \";\", \":\", \",\", \".\", \"<\", \">\", \"/\", \"?\"];\n\nconst symbolCombinations: string[][] = [\n fourSymbolCombinations,\n threeSymbolCombinations,\n twoSymbolCombinations,\n oneSymbolCombinations,\n];\n\nfunction parseScientificNotation(stream: InputStream) {\n const notationBuilder = new StringBuilder();\n\n const nextChar = stream.next;\n const afterEChar = stream.peek;\n if (isDigit(afterEChar) || afterEChar === \"+\" || afterEChar === \"-\") {\n notationBuilder.push(nextChar);\n notationBuilder.push(stream.next);\n while (isDigit(stream.peek)) {\n notationBuilder.push(stream.next);\n }\n if (isDecimalSeparator(stream.peek)) {\n throw new Error(\"The exponent in scientific notation must be an integer.\");\n }\n } else {\n stream.undo;\n }\n\n return notationBuilder.value;\n}\n\nfunction parseNumber(stream: InputStream): string {\n const numberBuilder = new StringBuilder(stream.current);\n let hasSeparator = isDecimalSeparator(stream.current);\n\n while (true) {\n const peekedChar = stream.peek;\n if (peekedChar.toLowerCase() === \"e\") {\n numberBuilder.push(parseScientificNotation(stream));\n break;\n }\n\n if (isDecimalSeparator(peekedChar)) {\n if (hasSeparator) {\n throw new Error(\"A number cannot have multiple decimal separators.\");\n }\n hasSeparator = true;\n numberBuilder.push(stream.next);\n continue;\n }\n\n if (isDigit(peekedChar)) {\n numberBuilder.push(stream.next);\n } else {\n break;\n }\n }\n\n return numberBuilder.value;\n}\n\nfunction unescape(char: string): string {\n switch (char) {\n case \"n\": return \"\\n\";\n case \"t\": return \"\\t\";\n default: return char;\n }\n}\n\nfunction parseStringLiteral(stream: InputStream): string {\n const stringBuilder = new StringBuilder(stream.current);\n const delimiter = stream.current;\n\n while (stream.next !== delimiter) {\n if (stream.isEOF) {\n throw new Error(`Missing closing string delimiter (${delimiter}).`);\n }\n\n stringBuilder.push(stream.current === \"\\\\\" ? unescape(stream.next) : stream.current);\n }\n\n stringBuilder.push(stream.current);\n return stringBuilder.value;\n}\n\nfunction parseVariable(stream: InputStream): string {\n const variableBuilder = new StringBuilder(stream.current);\n\n while (isLetterOrUnderscore(stream.peek) || isDigit(stream.peek)) {\n variableBuilder.push(stream.next);\n }\n\n if (isDecimalSeparator(stream.peek)) {\n throw new Error(\"Decimal separator in variable.\");\n }\n\n return variableBuilder.value;\n}\n\nfunction parseSymbolCombination(stream: InputStream): string {\n const symbolComboBuilder = new StringBuilder(stream.current);\n while (symbolComboBuilder.length < symbolCombinations.length && oneSymbolCombinations.includes(stream.peek)) {\n symbolComboBuilder.push(stream.next);\n }\n\n for (let i = symbolCombinations.length - symbolComboBuilder.length; i < symbolCombinations.length - 1; i++) {\n const symbolCombo = symbolComboBuilder.value;\n if (symbolCombinations[i].includes(symbolCombo)) {\n return symbolCombo;\n }\n symbolComboBuilder.pop();\n stream.undo;\n }\n\n return symbolComboBuilder.value;\n}\n\nfunction parseFormula(formula: string): string[] {\n const tokens: string[] = [];\n const stream = new InputStream(formula);\n while (!stream.isEOF) {\n const char = stream.next;\n if (isWhitespace(char)) {\n continue;\n }\n\n if (isDigit(char) || isDecimalSeparator(char)) {\n tokens.push(parseNumber(stream));\n } else if (isLetterOrUnderscore(char)) {\n tokens.push(parseVariable(stream));\n } else if (isStringDelimiter(char)) {\n tokens.push(parseStringLiteral(stream));\n } else if (oneSymbolCombinations.includes(char)) {\n tokens.push(parseSymbolCombination(stream));\n } else {\n throw new Error(`Unknown symbol received \"${char}\".`);\n }\n }\n\n return tokens;\n}\n\nexport function splitFormula(formula: string): string[] {\n return parseFormula(formula);\n}\n"]}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Token } from "./InfixToPostfixConverter";
|
|
2
|
+
import type { Queue } from "./Queue";
|
|
3
|
+
import type { PropertyMap } from "./Types";
|
|
4
|
+
export declare function validateTokens(tokens: Queue<Token>, properties: PropertyMap): string;
|
|
5
|
+
//# sourceMappingURL=FormulaTokensValidator.d.ts.map
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateTokens = void 0;
|
|
4
|
+
/*---------------------------------------------------------------------------------------------
|
|
5
|
+
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
|
|
6
|
+
* See LICENSE.md in the project root for license terms and full copyright notice.
|
|
7
|
+
*--------------------------------------------------------------------------------------------*/
|
|
8
|
+
const FormulaFunctionProvider_1 = require("./FormulaFunctionProvider");
|
|
9
|
+
const FormulaOperatorsProvider_1 = require("./FormulaOperatorsProvider");
|
|
10
|
+
const InfixToPostfixConverter_1 = require("./InfixToPostfixConverter");
|
|
11
|
+
const Stack_1 = require("./Stack");
|
|
12
|
+
function isNumericalConstant(name) {
|
|
13
|
+
return [
|
|
14
|
+
"e",
|
|
15
|
+
"ln2",
|
|
16
|
+
"ln10",
|
|
17
|
+
"log2e",
|
|
18
|
+
"log10e",
|
|
19
|
+
"pi",
|
|
20
|
+
"sqrt1_2",
|
|
21
|
+
"sqrt2",
|
|
22
|
+
].includes(name.toLowerCase());
|
|
23
|
+
}
|
|
24
|
+
function validateTokens(tokens, properties) {
|
|
25
|
+
if (tokens.length === 0)
|
|
26
|
+
return "Formula cannot be empty.";
|
|
27
|
+
const argStack = new Stack_1.Stack();
|
|
28
|
+
while (tokens.length > 0) {
|
|
29
|
+
const token = tokens.dequeue();
|
|
30
|
+
switch (token.type) {
|
|
31
|
+
case InfixToPostfixConverter_1.TokenType.Number:
|
|
32
|
+
argStack.push("number");
|
|
33
|
+
break;
|
|
34
|
+
case InfixToPostfixConverter_1.TokenType.String:
|
|
35
|
+
argStack.push("string");
|
|
36
|
+
break;
|
|
37
|
+
case InfixToPostfixConverter_1.TokenType.Boolean:
|
|
38
|
+
argStack.push("boolean");
|
|
39
|
+
break;
|
|
40
|
+
case InfixToPostfixConverter_1.TokenType.Null:
|
|
41
|
+
argStack.push("undefined");
|
|
42
|
+
break;
|
|
43
|
+
case InfixToPostfixConverter_1.TokenType.Variable:
|
|
44
|
+
const isConstant = isNumericalConstant(token.value);
|
|
45
|
+
if (isConstant) {
|
|
46
|
+
argStack.push("number");
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
const prop = properties[token.value];
|
|
50
|
+
if (!prop)
|
|
51
|
+
return `Variable "${token.value}" is not available.`;
|
|
52
|
+
argStack.push(prop);
|
|
53
|
+
}
|
|
54
|
+
break;
|
|
55
|
+
case InfixToPostfixConverter_1.TokenType.Operator:
|
|
56
|
+
if (!FormulaOperatorsProvider_1.isOperator(token.value))
|
|
57
|
+
return `Operator "${token.value}" is not supported.`;
|
|
58
|
+
const operatorBounds = FormulaOperatorsProvider_1.getOperatorArgumentCountBounds(token.value);
|
|
59
|
+
if (!operatorBounds)
|
|
60
|
+
return `Operator "${token.value}" is not supported.`;
|
|
61
|
+
if (operatorBounds[0] === 2 && token.argCount === 1)
|
|
62
|
+
return `Unary operator "${token.value}" is not supported.`;
|
|
63
|
+
if (argStack.length < token.argCount) {
|
|
64
|
+
return `Too few arguments given for ${token.argCount === 1 ? "unary" : "binary"} operator "${token.value}".`;
|
|
65
|
+
}
|
|
66
|
+
const opArgs = argStack.popN(token.argCount).reverse();
|
|
67
|
+
let operationResult;
|
|
68
|
+
if (opArgs.length === 1)
|
|
69
|
+
operationResult = FormulaOperatorsProvider_1.getOperatorReturnType(token.value, opArgs[0]);
|
|
70
|
+
else if (opArgs.length === 2)
|
|
71
|
+
operationResult = FormulaOperatorsProvider_1.getOperatorReturnType(token.value, opArgs[0], opArgs[1]);
|
|
72
|
+
else
|
|
73
|
+
return `Operator "${token.value}" does not support ${token.argCount} operands.`;
|
|
74
|
+
if (operationResult.errorMessage)
|
|
75
|
+
return operationResult.errorMessage;
|
|
76
|
+
else
|
|
77
|
+
argStack.push(operationResult.value);
|
|
78
|
+
break;
|
|
79
|
+
case InfixToPostfixConverter_1.TokenType.Function:
|
|
80
|
+
const fnArg = FormulaFunctionProvider_1.getFunction(token.value);
|
|
81
|
+
if (!fnArg)
|
|
82
|
+
return `Function "${token.value}" is not supported.`;
|
|
83
|
+
const fnArgBounds = fnArg.argumentCountBounds;
|
|
84
|
+
if (fnArgBounds[0] === 0 && fnArgBounds[1] === 0 && token.argCount > 0) {
|
|
85
|
+
return `Function "${token.value}" does not accept any arguments.`;
|
|
86
|
+
}
|
|
87
|
+
else if (fnArgBounds[1] === -1 && token.argCount < fnArgBounds[0]) {
|
|
88
|
+
if (fnArgBounds[0] === 1) {
|
|
89
|
+
return `Function "${token.value}" requires at least 1 argument.`;
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
if (token.argCount === 1) {
|
|
93
|
+
return `Function "${token.value}" requires at least ${fnArgBounds[0]} arguments, but only 1 argument was given.`;
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
return `Function "${token.value}" requires at least ${fnArgBounds[0]} arguments, but only ${token.argCount} arguments were given.`;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
else if (fnArgBounds[0] === fnArgBounds[1] && token.argCount !== fnArgBounds[0]) {
|
|
101
|
+
if (fnArgBounds[0] === 1) {
|
|
102
|
+
return `Function "${token.value}" requires exactly 1 argument, but ${token.argCount} arguments were given.`;
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
if (token.argCount === 1) {
|
|
106
|
+
return `Function "${token.value}" requires exactly ${fnArgBounds[0]} arguments, but 1 argument was given.`;
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
return `Function "${token.value}" requires exactly ${fnArgBounds[0]} arguments, but ${token.argCount} arguments were given.`;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
const fArgs = argStack.popN(token.argCount).reverse();
|
|
114
|
+
if (fArgs.length < token.argCount) {
|
|
115
|
+
return `Too few arguments given for function "${token.value}".`;
|
|
116
|
+
}
|
|
117
|
+
const functionResult = FormulaFunctionProvider_1.getFormulaFunctionReturnType(fnArg, fArgs);
|
|
118
|
+
if (functionResult.errorMessage)
|
|
119
|
+
return `Function "${token.value}" is invalid. ${functionResult.errorMessage}`;
|
|
120
|
+
else
|
|
121
|
+
argStack.push(functionResult.value);
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (argStack.length !== 1) {
|
|
126
|
+
return "Formula is invalid.";
|
|
127
|
+
}
|
|
128
|
+
const resultType = argStack.pop();
|
|
129
|
+
if (!resultType || resultType === "undefined") {
|
|
130
|
+
return "Formula cannot always return null.";
|
|
131
|
+
}
|
|
132
|
+
return "";
|
|
133
|
+
}
|
|
134
|
+
exports.validateTokens = validateTokens;
|
|
135
|
+
//# sourceMappingURL=FormulaTokensValidator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FormulaTokensValidator.js","sourceRoot":"","sources":["../../../src/formula/FormulaTokensValidator.ts"],"names":[],"mappings":";;;AAAA;;;+FAG+F;AAC/F,uEAAsF;AACtF,yEAA+G;AAE/G,uEAAsD;AAEtD,mCAAgC;AAGhC,SAAS,mBAAmB,CAAC,IAAY;IACvC,OAAO;QACL,GAAG;QACH,KAAK;QACL,MAAM;QACN,OAAO;QACP,QAAQ;QACR,IAAI;QACJ,SAAS;QACT,OAAO;KACR,CAAC,QAAQ,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,CAAC;AACjC,CAAC;AAED,SAAgB,cAAc,CAAC,MAAoB,EAAE,UAAuB;IAC1E,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC;QACrB,OAAO,0BAA0B,CAAC;IAEpC,MAAM,QAAQ,GAAG,IAAI,aAAK,EAAoB,CAAC;IAE/C,OAAO,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE;QAC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,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,OAAO,EAAE,CAAC;gBACtD,IAAI,KAAK,CAAC,MAAM,GAAG,KAAK,CAAC,QAAQ,EAAE;oBACjC,OAAO,yCAAyC,KAAK,CAAC,KAAK,IAAI,CAAC;iBACjE;gBAED,MAAM,cAAc,GAAG,sDAA4B,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;gBAClE,IAAI,cAAc,CAAC,YAAY;oBAC7B,OAAO,aAAa,KAAK,CAAC,KAAK,iBAAiB,cAAc,CAAC,YAAY,EAAE,CAAC;;oBAE9E,QAAQ,CAAC,IAAI,CAAC,cAAc,CAAC,KAAM,CAAC,CAAC;gBAEvC,MAAM;SACT;KACF;IAED,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE;QACzB,OAAO,qBAAqB,CAAC;KAC9B;IAED,MAAM,UAAU,GAAG,QAAQ,CAAC,GAAG,EAAE,CAAC;IAClC,IAAI,CAAC,UAAU,IAAI,UAAU,KAAK,WAAW,EAAE;QAC7C,OAAO,oCAAoC,CAAC;KAC7C;IAED,OAAO,EAAE,CAAC;AACZ,CAAC;AArHD,wCAqHC","sourcesContent":["/*---------------------------------------------------------------------------------------------\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\n* See LICENSE.md in the project root for license terms and full copyright notice.\n*--------------------------------------------------------------------------------------------*/\nimport { getFormulaFunctionReturnType, getFunction } from \"./FormulaFunctionProvider\";\nimport { getOperatorArgumentCountBounds, getOperatorReturnType, isOperator } from \"./FormulaOperatorsProvider\";\nimport type { Token } from \"./InfixToPostfixConverter\";\nimport { TokenType } from \"./InfixToPostfixConverter\";\nimport type { Queue } from \"./Queue\";\nimport { Stack } from \"./Stack\";\nimport type { PossibleDataType, PropertyMap } from \"./Types\";\n\nfunction isNumericalConstant(name: string): boolean {\n return [\n \"e\",\n \"ln2\",\n \"ln10\",\n \"log2e\",\n \"log10e\",\n \"pi\",\n \"sqrt1_2\",\n \"sqrt2\",\n ].includes(name.toLowerCase());\n}\n\nexport function validateTokens(tokens: Queue<Token>, properties: PropertyMap): string {\n if (tokens.length === 0)\n return \"Formula cannot be empty.\";\n\n const argStack = new Stack<PossibleDataType>();\n\n while (tokens.length > 0) {\n const token = tokens.dequeue()!;\n\n switch (token.type) {\n case TokenType.Number:\n argStack.push(\"number\");\n break;\n case TokenType.String:\n argStack.push(\"string\");\n break;\n case TokenType.Boolean:\n argStack.push(\"boolean\");\n break;\n case TokenType.Null:\n argStack.push(\"undefined\");\n break;\n case TokenType.Variable:\n const isConstant = isNumericalConstant(token.value);\n if (isConstant) {\n argStack.push(\"number\");\n } else {\n const prop = properties[token.value];\n if (!prop)\n return `Variable \"${token.value}\" is not available.`;\n argStack.push(prop);\n }\n break;\n case TokenType.Operator:\n if (!isOperator(token.value))\n return `Operator \"${token.value}\" is not supported.`;\n\n const operatorBounds = getOperatorArgumentCountBounds(token.value);\n if (!operatorBounds)\n return `Operator \"${token.value}\" is not supported.`;\n\n if (operatorBounds[0] === 2 && token.argCount === 1)\n return `Unary operator \"${token.value}\" is not supported.`;\n\n if (argStack.length < token.argCount) {\n return `Too few arguments given for ${token.argCount === 1 ? \"unary\" : \"binary\"} operator \"${token.value}\".`;\n }\n const opArgs = argStack.popN(token.argCount).reverse();\n let operationResult;\n if (opArgs.length === 1)\n operationResult = getOperatorReturnType(token.value, opArgs[0]);\n else if (opArgs.length === 2)\n operationResult = getOperatorReturnType(token.value, opArgs[0], opArgs[1]);\n else\n return `Operator \"${token.value}\" does not support ${token.argCount} operands.`;\n\n if (operationResult.errorMessage)\n return operationResult.errorMessage;\n else\n argStack.push(operationResult.value!);\n\n break;\n case TokenType.Function:\n const fnArg = getFunction(token.value);\n if (!fnArg)\n return `Function \"${token.value}\" is not supported.`;\n\n const fnArgBounds = fnArg.argumentCountBounds;\n if (fnArgBounds[0] === 0 && fnArgBounds[1] === 0 && token.argCount > 0) {\n return `Function \"${token.value}\" does not accept any arguments.`;\n } else if (fnArgBounds[1] === -1 && token.argCount < fnArgBounds[0]) {\n if (fnArgBounds[0] === 1) {\n return `Function \"${token.value}\" requires at least 1 argument.`;\n } else {\n if (token.argCount === 1) {\n return `Function \"${token.value}\" requires at least ${fnArgBounds[0]} arguments, but only 1 argument was given.`;\n } else {\n return `Function \"${token.value}\" requires at least ${fnArgBounds[0]} arguments, but only ${token.argCount} arguments were given.`;\n }\n }\n } else if (fnArgBounds[0] === fnArgBounds[1] && token.argCount !== fnArgBounds[0]) {\n if (fnArgBounds[0] === 1) {\n return `Function \"${token.value}\" requires exactly 1 argument, but ${token.argCount} arguments were given.`;\n } else {\n if (token.argCount === 1) {\n return `Function \"${token.value}\" requires exactly ${fnArgBounds[0]} arguments, but 1 argument was given.`;\n } else {\n return `Function \"${token.value}\" requires exactly ${fnArgBounds[0]} arguments, but ${token.argCount} arguments were given.`;\n }\n }\n }\n\n const fArgs = argStack.popN(token.argCount).reverse();\n if (fArgs.length < token.argCount) {\n return `Too few arguments given for function \"${token.value}\".`;\n }\n\n const functionResult = getFormulaFunctionReturnType(fnArg, fArgs);\n if (functionResult.errorMessage)\n return `Function \"${token.value}\" is invalid. ${functionResult.errorMessage}`;\n else\n argStack.push(functionResult.value!);\n\n break;\n }\n }\n\n if (argStack.length !== 1) {\n return \"Formula is invalid.\";\n }\n\n const resultType = argStack.pop();\n if (!resultType || resultType === \"undefined\") {\n return \"Formula cannot always return null.\";\n }\n\n return \"\";\n}\n\n"]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateFormula = void 0;
|
|
4
|
+
/*---------------------------------------------------------------------------------------------
|
|
5
|
+
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
|
|
6
|
+
* See LICENSE.md in the project root for license terms and full copyright notice.
|
|
7
|
+
*--------------------------------------------------------------------------------------------*/
|
|
8
|
+
const FormulaSplitter_1 = require("./FormulaSplitter");
|
|
9
|
+
const FormulaTokensValidator_1 = require("./FormulaTokensValidator");
|
|
10
|
+
const InfixToPostfixConverter_1 = require("./InfixToPostfixConverter");
|
|
11
|
+
const ParenthesisValidator_1 = require("./ParenthesisValidator");
|
|
12
|
+
function validateFormula(formula, properties) {
|
|
13
|
+
var _a;
|
|
14
|
+
const parenthesisState = ParenthesisValidator_1.validateParenthesis(formula);
|
|
15
|
+
if (ParenthesisValidator_1.ParenthesisState.NotClosed === parenthesisState) {
|
|
16
|
+
return "Opened but not closed parenthesis found.";
|
|
17
|
+
}
|
|
18
|
+
else if (ParenthesisValidator_1.ParenthesisState.NotOpened === parenthesisState) {
|
|
19
|
+
return "Closed but not opened parenthesis found.";
|
|
20
|
+
}
|
|
21
|
+
let infixFormulaTokens;
|
|
22
|
+
try {
|
|
23
|
+
infixFormulaTokens = FormulaSplitter_1.splitFormula(formula);
|
|
24
|
+
}
|
|
25
|
+
catch (ex) {
|
|
26
|
+
return ex.message;
|
|
27
|
+
}
|
|
28
|
+
const postfixFormulaTokens = InfixToPostfixConverter_1.convertInfixToPostfix(infixFormulaTokens);
|
|
29
|
+
if (undefined === postfixFormulaTokens.value) {
|
|
30
|
+
return (_a = postfixFormulaTokens.errorMessage) !== null && _a !== void 0 ? _a : "Unknown error";
|
|
31
|
+
}
|
|
32
|
+
return FormulaTokensValidator_1.validateTokens(postfixFormulaTokens.value, properties);
|
|
33
|
+
}
|
|
34
|
+
exports.validateFormula = validateFormula;
|
|
35
|
+
//# sourceMappingURL=FormulaValidator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FormulaValidator.js","sourceRoot":"","sources":["../../../src/formula/FormulaValidator.ts"],"names":[],"mappings":";;;AAAA;;;+FAG+F;AAC/F,uDAAiD;AACjD,qEAA0D;AAC1D,uEAAkE;AAClE,iEAA+E;AAG/E,SAAgB,eAAe,CAAC,OAAe,EAAE,UAAuB;;IACtE,MAAM,gBAAgB,GAAG,0CAAmB,CAAC,OAAO,CAAC,CAAC;IACtD,IAAI,uCAAgB,CAAC,SAAS,KAAK,gBAAgB,EAAE;QACnD,OAAO,0CAA0C,CAAC;KACnD;SAAM,IAAI,uCAAgB,CAAC,SAAS,KAAK,gBAAgB,EAAE;QAC1D,OAAO,0CAA0C,CAAC;KACnD;IAED,IAAI,kBAAkB,CAAC;IACvB,IAAI;QACF,kBAAkB,GAAG,8BAAY,CAAC,OAAO,CAAC,CAAC;KAC5C;IAAC,OAAO,EAAE,EAAE;QACX,OAAQ,EAAY,CAAC,OAAO,CAAC;KAC9B;IAED,MAAM,oBAAoB,GAAG,+CAAqB,CAAC,kBAAkB,CAAC,CAAC;IAEvE,IAAI,SAAS,KAAK,oBAAoB,CAAC,KAAK,EAAE;QAC5C,OAAO,MAAA,oBAAoB,CAAC,YAAY,mCAAI,eAAe,CAAC;KAC7D;IAED,OAAO,uCAAc,CAAC,oBAAoB,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;AAChE,CAAC;AAtBD,0CAsBC","sourcesContent":["/*---------------------------------------------------------------------------------------------\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\n* See LICENSE.md in the project root for license terms and full copyright notice.\n*--------------------------------------------------------------------------------------------*/\nimport { splitFormula } from \"./FormulaSplitter\";\nimport { validateTokens } from \"./FormulaTokensValidator\";\nimport { convertInfixToPostfix } from \"./InfixToPostfixConverter\";\nimport { ParenthesisState, validateParenthesis } from \"./ParenthesisValidator\";\nimport type { PropertyMap } from \"./Types\";\n\nexport function validateFormula(formula: string, properties: PropertyMap): string {\n const parenthesisState = validateParenthesis(formula);\n if (ParenthesisState.NotClosed === parenthesisState) {\n return \"Opened but not closed parenthesis found.\";\n } else if (ParenthesisState.NotOpened === parenthesisState) {\n return \"Closed but not opened parenthesis found.\";\n }\n\n let infixFormulaTokens;\n try {\n infixFormulaTokens = splitFormula(formula);\n } catch (ex) {\n return (ex as Error).message;\n }\n\n const postfixFormulaTokens = convertInfixToPostfix(infixFormulaTokens);\n\n if (undefined === postfixFormulaTokens.value) {\n return postfixFormulaTokens.errorMessage ?? \"Unknown error\";\n }\n\n return validateTokens(postfixFormulaTokens.value, properties);\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IResult.js","sourceRoot":"","sources":["../../../src/formula/IResult.ts"],"names":[],"mappings":"","sourcesContent":["/*---------------------------------------------------------------------------------------------\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\n* See LICENSE.md in the project root for license terms and full copyright notice.\n*--------------------------------------------------------------------------------------------*/\nexport interface IResult<T> {\n value?: T;\n errorMessage?: string;\n}\n"]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { IResult } from "./IResult";
|
|
2
|
+
import { Queue } from "./Queue";
|
|
3
|
+
export declare enum TokenType {
|
|
4
|
+
Number = 0,
|
|
5
|
+
Function = 1,
|
|
6
|
+
Operator = 2,
|
|
7
|
+
Variable = 3,
|
|
8
|
+
String = 4,
|
|
9
|
+
Boolean = 5,
|
|
10
|
+
Null = 6
|
|
11
|
+
}
|
|
12
|
+
export interface Token {
|
|
13
|
+
value: string;
|
|
14
|
+
type: TokenType;
|
|
15
|
+
argCount: number;
|
|
16
|
+
}
|
|
17
|
+
export declare function convertInfixToPostfix(infix: string[]): IResult<Queue<Token>>;
|
|
18
|
+
//# sourceMappingURL=InfixToPostfixConverter.d.ts.map
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.convertInfixToPostfix = exports.TokenType = void 0;
|
|
4
|
+
/*---------------------------------------------------------------------------------------------
|
|
5
|
+
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
|
|
6
|
+
* See LICENSE.md in the project root for license terms and full copyright notice.
|
|
7
|
+
*--------------------------------------------------------------------------------------------*/
|
|
8
|
+
const FormulaFunctionProvider_1 = require("./FormulaFunctionProvider");
|
|
9
|
+
const FormulaOperatorsProvider_1 = require("./FormulaOperatorsProvider");
|
|
10
|
+
const Queue_1 = require("./Queue");
|
|
11
|
+
const Stack_1 = require("./Stack");
|
|
12
|
+
const Utils_1 = require("./Utils");
|
|
13
|
+
function isNumber(part) {
|
|
14
|
+
return !isNaN(Number(part)) || !isNaN(parseFloat(part));
|
|
15
|
+
}
|
|
16
|
+
function isStringLiteral(token) {
|
|
17
|
+
if (token.length < 2) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
const firstCharacter = token[0];
|
|
21
|
+
const lastCharacter = token[token.length - 1];
|
|
22
|
+
return firstCharacter === lastCharacter && Utils_1.isStringDelimiter(firstCharacter);
|
|
23
|
+
}
|
|
24
|
+
function isBoolean(token) {
|
|
25
|
+
const lower = token.toLowerCase();
|
|
26
|
+
return lower === "true" || lower === "false";
|
|
27
|
+
}
|
|
28
|
+
function isNull(token) {
|
|
29
|
+
return token.toLowerCase() === "null";
|
|
30
|
+
}
|
|
31
|
+
var TokenType;
|
|
32
|
+
(function (TokenType) {
|
|
33
|
+
TokenType[TokenType["Number"] = 0] = "Number";
|
|
34
|
+
TokenType[TokenType["Function"] = 1] = "Function";
|
|
35
|
+
TokenType[TokenType["Operator"] = 2] = "Operator";
|
|
36
|
+
TokenType[TokenType["Variable"] = 3] = "Variable";
|
|
37
|
+
TokenType[TokenType["String"] = 4] = "String";
|
|
38
|
+
TokenType[TokenType["Boolean"] = 5] = "Boolean";
|
|
39
|
+
TokenType[TokenType["Null"] = 6] = "Null";
|
|
40
|
+
})(TokenType = exports.TokenType || (exports.TokenType = {}));
|
|
41
|
+
var InternalTokenType;
|
|
42
|
+
(function (InternalTokenType) {
|
|
43
|
+
InternalTokenType[InternalTokenType["Number"] = 0] = "Number";
|
|
44
|
+
InternalTokenType[InternalTokenType["Function"] = 1] = "Function";
|
|
45
|
+
InternalTokenType[InternalTokenType["Operator"] = 2] = "Operator";
|
|
46
|
+
InternalTokenType[InternalTokenType["Variable"] = 3] = "Variable";
|
|
47
|
+
InternalTokenType[InternalTokenType["OpeningParenthesis"] = 4] = "OpeningParenthesis";
|
|
48
|
+
InternalTokenType[InternalTokenType["ClosingParenthesis"] = 5] = "ClosingParenthesis";
|
|
49
|
+
InternalTokenType[InternalTokenType["ArgumentSeparator"] = 6] = "ArgumentSeparator";
|
|
50
|
+
InternalTokenType[InternalTokenType["String"] = 7] = "String";
|
|
51
|
+
InternalTokenType[InternalTokenType["Boolean"] = 8] = "Boolean";
|
|
52
|
+
InternalTokenType[InternalTokenType["Null"] = 9] = "Null";
|
|
53
|
+
})(InternalTokenType || (InternalTokenType = {}));
|
|
54
|
+
function getExternalQueue(internal) {
|
|
55
|
+
const external = new Queue_1.Queue();
|
|
56
|
+
while (internal.length > 0) {
|
|
57
|
+
const t = internal.dequeue();
|
|
58
|
+
switch (t.type) {
|
|
59
|
+
case InternalTokenType.Number:
|
|
60
|
+
external.enqueue({ value: t.value, type: TokenType.Number, argCount: t.argCount });
|
|
61
|
+
break;
|
|
62
|
+
case InternalTokenType.Function:
|
|
63
|
+
external.enqueue({ value: t.value, type: TokenType.Function, argCount: t.argCount });
|
|
64
|
+
break;
|
|
65
|
+
case InternalTokenType.Operator:
|
|
66
|
+
external.enqueue({ value: t.value, type: TokenType.Operator, argCount: t.argCount });
|
|
67
|
+
break;
|
|
68
|
+
case InternalTokenType.Variable:
|
|
69
|
+
external.enqueue({ value: t.value, type: TokenType.Variable, argCount: t.argCount });
|
|
70
|
+
break;
|
|
71
|
+
case InternalTokenType.String:
|
|
72
|
+
external.enqueue({ value: t.value, type: TokenType.String, argCount: t.argCount });
|
|
73
|
+
break;
|
|
74
|
+
case InternalTokenType.Boolean:
|
|
75
|
+
external.enqueue({ value: t.value, type: TokenType.Boolean, argCount: t.argCount });
|
|
76
|
+
break;
|
|
77
|
+
case InternalTokenType.Null:
|
|
78
|
+
external.enqueue({ value: t.value, type: TokenType.Null, argCount: t.argCount });
|
|
79
|
+
break;
|
|
80
|
+
default:
|
|
81
|
+
return { errorMessage: `Missing closing parenthesis ")".` };
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return { value: external };
|
|
85
|
+
}
|
|
86
|
+
function incrementArgCount(token) {
|
|
87
|
+
if (!token.argCountIncremented) {
|
|
88
|
+
token.argCount++;
|
|
89
|
+
token.argCountIncremented = true;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function addOperand(ctx, type) {
|
|
93
|
+
ctx.resultQueue.enqueue({ value: ctx.currentToken, type, argCount: 0, argCountIncremented: false });
|
|
94
|
+
const currentOperators = ctx.operatorStack.peekN(2);
|
|
95
|
+
const op1 = currentOperators[0];
|
|
96
|
+
if (undefined !== op1) {
|
|
97
|
+
incrementArgCount(op1);
|
|
98
|
+
if (op1.argCount === 1) {
|
|
99
|
+
const op2 = currentOperators[1];
|
|
100
|
+
if (undefined !== op2 && op2.argCount !== 0) {
|
|
101
|
+
incrementArgCount(op2);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
const addNumber = (ctx) => {
|
|
107
|
+
addOperand(ctx, InternalTokenType.Number);
|
|
108
|
+
return { value: true };
|
|
109
|
+
};
|
|
110
|
+
const addVariable = (ctx) => {
|
|
111
|
+
ctx = { ...ctx, currentToken: ctx.currentToken.toLowerCase() };
|
|
112
|
+
if (undefined !== ctx.previousToken && isNumber(ctx.previousToken)) {
|
|
113
|
+
addOperator({ ...ctx, currentToken: "*" });
|
|
114
|
+
addOperand({ ...ctx, previousToken: "*" }, InternalTokenType.Variable);
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
addOperand(ctx, InternalTokenType.Variable);
|
|
118
|
+
}
|
|
119
|
+
return { value: true };
|
|
120
|
+
};
|
|
121
|
+
const addFunction = (ctx) => {
|
|
122
|
+
const currentOperator = ctx.operatorStack.peek();
|
|
123
|
+
if (undefined !== currentOperator) {
|
|
124
|
+
incrementArgCount(currentOperator);
|
|
125
|
+
}
|
|
126
|
+
ctx.operatorStack.push({ value: ctx.currentToken, type: InternalTokenType.Function, argCount: 0, argCountIncremented: false });
|
|
127
|
+
return { value: true };
|
|
128
|
+
};
|
|
129
|
+
const addOperator = (ctx) => {
|
|
130
|
+
if (!FormulaOperatorsProvider_1.isSupportedOperator(ctx.currentToken)) {
|
|
131
|
+
return { errorMessage: `Operator "${ctx.currentToken}" is not supported.` };
|
|
132
|
+
}
|
|
133
|
+
const binary = undefined !== ctx.previousToken && "(" !== ctx.previousToken && "," !== ctx.previousToken && !FormulaOperatorsProvider_1.isOperator(ctx.previousToken);
|
|
134
|
+
const unary = !binary;
|
|
135
|
+
const op = unary ? FormulaOperatorsProvider_1.getUnaryOperator(ctx.currentToken) : FormulaOperatorsProvider_1.getBinaryOperator(ctx.currentToken);
|
|
136
|
+
if (unary && undefined === op) {
|
|
137
|
+
return { errorMessage: `Unary "${ctx.currentToken}" is not supported.` };
|
|
138
|
+
}
|
|
139
|
+
const o1 = ctx.currentToken;
|
|
140
|
+
const o1Associativity = FormulaOperatorsProvider_1.getOperatorAssociativity(op);
|
|
141
|
+
const o1Precedence = FormulaOperatorsProvider_1.getOperatorPrecedence(op);
|
|
142
|
+
while (ctx.operatorStack.length > 0) {
|
|
143
|
+
const o2 = ctx.operatorStack.peek();
|
|
144
|
+
if (o2.value === "(") {
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
const o2Unary = o2.argCount === 0;
|
|
148
|
+
const op2 = o2Unary ? FormulaOperatorsProvider_1.getUnaryOperator(o2.value) : FormulaOperatorsProvider_1.getBinaryOperator(o2.value);
|
|
149
|
+
const o2Precedence = FormulaOperatorsProvider_1.getOperatorPrecedence(op2);
|
|
150
|
+
if (!(o2Precedence > o1Precedence || (o2Precedence === o1Precedence && o1Associativity === FormulaOperatorsProvider_1.OperatorAssociativity.Left))) {
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
ctx.resultQueue.enqueue(ctx.operatorStack.pop());
|
|
154
|
+
const currentOperator = ctx.operatorStack.peek();
|
|
155
|
+
if (undefined !== currentOperator) {
|
|
156
|
+
incrementArgCount(currentOperator);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
ctx.operatorStack.push({
|
|
160
|
+
value: o1,
|
|
161
|
+
type: InternalTokenType.Operator,
|
|
162
|
+
argCount: binary ? 1 : 0,
|
|
163
|
+
argCountIncremented: false,
|
|
164
|
+
});
|
|
165
|
+
return { value: true };
|
|
166
|
+
};
|
|
167
|
+
const openParenthesis = (ctx) => {
|
|
168
|
+
if (undefined !== ctx.previousToken) {
|
|
169
|
+
if (InternalTokenType.Number === ctx.previousTokenType || InternalTokenType.ClosingParenthesis === ctx.previousTokenType) {
|
|
170
|
+
addOperator({ ...ctx, currentToken: "*" });
|
|
171
|
+
}
|
|
172
|
+
else if (InternalTokenType.Variable === ctx.previousTokenType) {
|
|
173
|
+
return { errorMessage: `Function "${ctx.previousToken}" is not supported.` };
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
const currentOperator = ctx.operatorStack.peek();
|
|
177
|
+
if (undefined !== currentOperator && currentOperator.type !== InternalTokenType.Function) {
|
|
178
|
+
incrementArgCount(currentOperator);
|
|
179
|
+
}
|
|
180
|
+
ctx.operatorStack.push({ value: "(", type: InternalTokenType.OpeningParenthesis, argCount: 0, argCountIncremented: false });
|
|
181
|
+
return { value: true };
|
|
182
|
+
};
|
|
183
|
+
const closeParenthesis = (ctx) => {
|
|
184
|
+
while (ctx.operatorStack.length > 0 && ctx.operatorStack.peek().value !== "(") {
|
|
185
|
+
ctx.resultQueue.enqueue(ctx.operatorStack.pop());
|
|
186
|
+
}
|
|
187
|
+
const leftParenthesis = ctx.operatorStack.pop();
|
|
188
|
+
if (undefined === leftParenthesis) {
|
|
189
|
+
return { errorMessage: `Closing parenthesis ")" found, but the opening parenthesis "(" is missing.` };
|
|
190
|
+
}
|
|
191
|
+
const potentialFnToken = ctx.operatorStack.peek();
|
|
192
|
+
if (undefined !== potentialFnToken && FormulaFunctionProvider_1.isFunction(potentialFnToken.value)) {
|
|
193
|
+
potentialFnToken.argCount = leftParenthesis.argCount;
|
|
194
|
+
ctx.resultQueue.enqueue(ctx.operatorStack.pop());
|
|
195
|
+
}
|
|
196
|
+
return { value: true };
|
|
197
|
+
};
|
|
198
|
+
const addString = (ctx) => {
|
|
199
|
+
const withoutDelimiters = ctx.currentToken.substring(1, ctx.currentToken.length - 1);
|
|
200
|
+
addOperand({ ...ctx, currentToken: withoutDelimiters }, InternalTokenType.String);
|
|
201
|
+
return { value: true };
|
|
202
|
+
};
|
|
203
|
+
const addBoolean = (ctx) => {
|
|
204
|
+
addOperand({ ...ctx, currentToken: ctx.currentToken.toLowerCase() }, InternalTokenType.Boolean);
|
|
205
|
+
return { value: true };
|
|
206
|
+
};
|
|
207
|
+
const addNull = (ctx) => {
|
|
208
|
+
addOperand({ ...ctx, currentToken: ctx.currentToken.toLowerCase() }, InternalTokenType.Null);
|
|
209
|
+
return { value: true };
|
|
210
|
+
};
|
|
211
|
+
const separateFunctionArgumentSeparator = (ctx) => {
|
|
212
|
+
while (ctx.operatorStack.length > 0 && ctx.operatorStack.peek().value !== "(") {
|
|
213
|
+
ctx.resultQueue.enqueue(ctx.operatorStack.pop());
|
|
214
|
+
const currentOperator = ctx.operatorStack.peek();
|
|
215
|
+
if (undefined !== currentOperator) {
|
|
216
|
+
incrementArgCount(currentOperator);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
const last2Operators = ctx.operatorStack.peekN(2);
|
|
220
|
+
if (last2Operators.length < 2 || last2Operators[0].type !== InternalTokenType.OpeningParenthesis || last2Operators[1].type !== InternalTokenType.Function) {
|
|
221
|
+
return { errorMessage: `Function argument separator "," found outside of a function call.` };
|
|
222
|
+
}
|
|
223
|
+
last2Operators[0].argCountIncremented = false;
|
|
224
|
+
return { value: true };
|
|
225
|
+
};
|
|
226
|
+
function getTokenType(token, nextToken) {
|
|
227
|
+
if (isNumber(token)) {
|
|
228
|
+
return InternalTokenType.Number;
|
|
229
|
+
}
|
|
230
|
+
else if (isStringLiteral(token)) {
|
|
231
|
+
return InternalTokenType.String;
|
|
232
|
+
}
|
|
233
|
+
else if (isBoolean(token)) {
|
|
234
|
+
return InternalTokenType.Boolean;
|
|
235
|
+
}
|
|
236
|
+
else if (isNull(token)) {
|
|
237
|
+
return InternalTokenType.Null;
|
|
238
|
+
}
|
|
239
|
+
else if (FormulaFunctionProvider_1.isFunction(token) && "(" === nextToken) {
|
|
240
|
+
return InternalTokenType.Function;
|
|
241
|
+
}
|
|
242
|
+
else if (FormulaOperatorsProvider_1.isOperator(token)) {
|
|
243
|
+
return InternalTokenType.Operator;
|
|
244
|
+
}
|
|
245
|
+
else if ("(" === token) {
|
|
246
|
+
return InternalTokenType.OpeningParenthesis;
|
|
247
|
+
}
|
|
248
|
+
else if (")" === token) {
|
|
249
|
+
return InternalTokenType.ClosingParenthesis;
|
|
250
|
+
}
|
|
251
|
+
else if ("," === token) {
|
|
252
|
+
return InternalTokenType.ArgumentSeparator;
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
return InternalTokenType.Variable;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
const tokenInterpreters = new Map([
|
|
259
|
+
[InternalTokenType.Number, addNumber],
|
|
260
|
+
[InternalTokenType.ArgumentSeparator, separateFunctionArgumentSeparator],
|
|
261
|
+
[InternalTokenType.ClosingParenthesis, closeParenthesis],
|
|
262
|
+
[InternalTokenType.Function, addFunction],
|
|
263
|
+
[InternalTokenType.OpeningParenthesis, openParenthesis],
|
|
264
|
+
[InternalTokenType.Operator, addOperator],
|
|
265
|
+
[InternalTokenType.Variable, addVariable],
|
|
266
|
+
[InternalTokenType.String, addString],
|
|
267
|
+
[InternalTokenType.Boolean, addBoolean],
|
|
268
|
+
[InternalTokenType.Null, addNull],
|
|
269
|
+
]);
|
|
270
|
+
function convertInfixToPostfix(infix) {
|
|
271
|
+
const resultQueue = new Queue_1.Queue();
|
|
272
|
+
const operatorStack = new Stack_1.Stack();
|
|
273
|
+
for (let index = 0; index < infix.length; index++) {
|
|
274
|
+
const token = infix[index];
|
|
275
|
+
const ctx = {
|
|
276
|
+
resultQueue,
|
|
277
|
+
operatorStack,
|
|
278
|
+
currentToken: token,
|
|
279
|
+
currentTokenType: getTokenType(token, index < infix.length - 1 ? infix[index + 1] : undefined),
|
|
280
|
+
previousToken: index > 0 ? infix[index - 1] : undefined,
|
|
281
|
+
previousTokenType: index > 0 ? getTokenType(infix[index - 1], token) : undefined,
|
|
282
|
+
};
|
|
283
|
+
const fn = tokenInterpreters.get(ctx.currentTokenType);
|
|
284
|
+
const tmpResult = fn(ctx);
|
|
285
|
+
if (undefined === tmpResult.value) {
|
|
286
|
+
return { errorMessage: tmpResult.errorMessage };
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
while (operatorStack.length > 0) {
|
|
290
|
+
resultQueue.enqueue(operatorStack.pop());
|
|
291
|
+
const currentOperator = operatorStack.peek();
|
|
292
|
+
if (undefined !== currentOperator) {
|
|
293
|
+
incrementArgCount(currentOperator);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return getExternalQueue(resultQueue);
|
|
297
|
+
}
|
|
298
|
+
exports.convertInfixToPostfix = convertInfixToPostfix;
|
|
299
|
+
//# sourceMappingURL=InfixToPostfixConverter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"InfixToPostfixConverter.js","sourceRoot":"","sources":["../../../src/formula/InfixToPostfixConverter.ts"],"names":[],"mappings":";;;AAAA;;;+FAG+F;AAC/F,uEAAuD;AACvD,yEAA0L;AAE1L,mCAAgC;AAChC,mCAAgC;AAChC,mCAA4C;AAE5C,SAAS,QAAQ,CAAC,IAAY;IAC5B,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC;AAC1D,CAAC;AAED,SAAS,eAAe,CAAC,KAAa;IACpC,IAAI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AAG,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;YAC9F,aAAa,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS;YACvD,iBAAiB,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS;SACjF,CAAC;QAEF,MAAM,EAAE,GAAG,iBAAiB,CAAC,GAAG,CAAC,GAAG,CAAC,gBAAgB,CAAE,CAAC;QAExD,MAAM,SAAS,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC;QAC1B,IAAI,SAAS,KAAK,SAAS,CAAC,KAAK,EAAE;YACjC,OAAO,EAAE,YAAY,EAAE,SAAS,CAAC,YAAY,EAAE,CAAC;SACjD;KACF;IAED,OAAO,aAAa,CAAC,MAAM,GAAG,CAAC,EAAE;QAC/B,WAAW,CAAC,OAAO,CAAC,aAAa,CAAC,GAAG,EAAG,CAAC,CAAC;QAE1C,MAAM,eAAe,GAAG,aAAa,CAAC,IAAI,EAAE,CAAC;QAC7C,IAAI,SAAS,KAAK,eAAe,EAAE;YACjC,iBAAiB,CAAC,eAAe,CAAC,CAAC;SACpC;KACF;IAED,OAAO,gBAAgB,CAAC,WAAW,CAAC,CAAC;AACvC,CAAC;AAlCD,sDAkCC","sourcesContent":["/*---------------------------------------------------------------------------------------------\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\n* See LICENSE.md in the project root for license terms and full copyright notice.\n*--------------------------------------------------------------------------------------------*/\nimport { isFunction } from \"./FormulaFunctionProvider\";\nimport { getBinaryOperator, getOperatorAssociativity, getOperatorPrecedence, getUnaryOperator, isOperator, isSupportedOperator, OperatorAssociativity } from \"./FormulaOperatorsProvider\";\nimport type { IResult } from \"./IResult\";\nimport { Queue } from \"./Queue\";\nimport { Stack } from \"./Stack\";\nimport { isStringDelimiter } from \"./Utils\";\n\nfunction isNumber(part: string): boolean {\n return !isNaN(Number(part)) || !isNaN(parseFloat(part));\n}\n\nfunction isStringLiteral(token: string): boolean {\n if (token.length < 2) {\n return false;\n }\n\n const firstCharacter = token[0];\n const lastCharacter = token[token.length - 1];\n return firstCharacter === lastCharacter && isStringDelimiter(firstCharacter);\n}\n\nfunction isBoolean(token: string): boolean {\n const lower = token.toLowerCase();\n return lower === \"true\" || lower === \"false\";\n}\n\nfunction isNull(token: string): boolean {\n return token.toLowerCase() === \"null\";\n}\n\nexport enum TokenType {\n Number = 0,\n Function = 1,\n Operator = 2,\n Variable = 3,\n String = 4,\n Boolean = 5,\n Null = 6,\n}\n\nenum InternalTokenType {\n Number = 0,\n Function = 1,\n Operator = 2,\n Variable = 3,\n OpeningParenthesis = 4,\n ClosingParenthesis = 5,\n ArgumentSeparator = 6,\n String = 7,\n Boolean = 8,\n Null = 9,\n}\n\nexport interface Token {\n value: string;\n type: TokenType;\n argCount: number;\n}\n\ninterface InternalToken {\n value: string;\n type: InternalTokenType;\n argCount: number;\n argCountIncremented: boolean;\n}\n\nfunction getExternalQueue(internal: Queue<InternalToken>): IResult<Queue<Token>> {\n const external = new Queue<Token>();\n while (internal.length > 0) {\n const t = internal.dequeue()!;\n\n switch (t.type) {\n case InternalTokenType.Number:\n external.enqueue({ value: t.value, type: TokenType.Number, argCount: t.argCount });\n break;\n case InternalTokenType.Function:\n external.enqueue({ value: t.value, type: TokenType.Function, argCount: t.argCount });\n break;\n case InternalTokenType.Operator:\n external.enqueue({ value: t.value, type: TokenType.Operator, argCount: t.argCount });\n break;\n case InternalTokenType.Variable:\n external.enqueue({ value: t.value, type: TokenType.Variable, argCount: t.argCount });\n break;\n case InternalTokenType.String:\n external.enqueue({ value: t.value, type: TokenType.String, argCount: t.argCount });\n break;\n case InternalTokenType.Boolean:\n external.enqueue({ value: t.value, type: TokenType.Boolean, argCount: t.argCount });\n break;\n case InternalTokenType.Null:\n external.enqueue({ value: t.value, type: TokenType.Null, argCount: t.argCount });\n break;\n default:\n return { errorMessage: `Missing closing parenthesis \")\".` };\n }\n }\n\n return { value: external };\n}\n\ninterface IConverterContext {\n resultQueue: Queue<InternalToken>;\n operatorStack: Stack<InternalToken>;\n currentToken: string;\n currentTokenType: InternalTokenType;\n previousToken?: string;\n previousTokenType?: InternalTokenType;\n}\n\ntype TokenInterpreter = (ctx: IConverterContext) => IResult<boolean>;\n\nfunction incrementArgCount(token: InternalToken) {\n if (!token.argCountIncremented) {\n token.argCount++;\n token.argCountIncremented = true;\n }\n}\n\nfunction addOperand(ctx: IConverterContext, type: InternalTokenType) {\n ctx.resultQueue.enqueue({ value: ctx.currentToken, type, argCount: 0, argCountIncremented: false });\n\n const currentOperators = ctx.operatorStack.peekN(2);\n const op1 = currentOperators[0];\n if (undefined !== op1) {\n incrementArgCount(op1);\n if (op1.argCount === 1) {\n const op2 = currentOperators[1];\n if (undefined !== op2 && op2.argCount !== 0) {\n incrementArgCount(op2);\n }\n }\n }\n}\n\nconst addNumber: TokenInterpreter = (ctx: IConverterContext) => {\n addOperand(ctx, InternalTokenType.Number);\n return { value: true };\n};\n\nconst addVariable: TokenInterpreter = (ctx: IConverterContext) => {\n ctx = { ...ctx, currentToken: ctx.currentToken.toLowerCase() };\n if (undefined !== ctx.previousToken && isNumber(ctx.previousToken)) {\n addOperator({ ...ctx, currentToken: \"*\" });\n addOperand({ ...ctx, previousToken: \"*\" }, InternalTokenType.Variable);\n } else {\n addOperand(ctx, InternalTokenType.Variable);\n }\n return { value: true };\n};\n\nconst addFunction: TokenInterpreter = (ctx: IConverterContext) => {\n const currentOperator = ctx.operatorStack.peek();\n if (undefined !== currentOperator) {\n incrementArgCount(currentOperator);\n }\n\n ctx.operatorStack.push({ value: ctx.currentToken, type: InternalTokenType.Function, argCount: 0, argCountIncremented: false });\n return { value: true };\n};\n\nconst addOperator: TokenInterpreter = (ctx: IConverterContext) => {\n if (!isSupportedOperator(ctx.currentToken)) {\n return { errorMessage: `Operator \"${ctx.currentToken}\" is not supported.` };\n }\n\n const binary = undefined !== ctx.previousToken && \"(\" !== ctx.previousToken && \",\" !== ctx.previousToken && !isOperator(ctx.previousToken);\n const unary = !binary;\n\n const op = unary ? getUnaryOperator(ctx.currentToken) : getBinaryOperator(ctx.currentToken);\n if (unary && undefined === op) {\n return { errorMessage: `Unary \"${ctx.currentToken}\" is not supported.` };\n }\n\n const o1 = ctx.currentToken;\n const o1Associativity = getOperatorAssociativity(op);\n const o1Precedence = getOperatorPrecedence(op);\n while (ctx.operatorStack.length > 0) {\n const o2 = ctx.operatorStack.peek()!;\n\n if (o2.value === \"(\") {\n break;\n }\n\n const o2Unary = o2.argCount === 0;\n const op2 = o2Unary ? getUnaryOperator(o2.value) : getBinaryOperator(o2.value);\n const o2Precedence = getOperatorPrecedence(op2);\n if (!(o2Precedence > o1Precedence || (o2Precedence === o1Precedence && o1Associativity === OperatorAssociativity.Left))) {\n break;\n }\n\n ctx.resultQueue.enqueue(ctx.operatorStack.pop()!);\n\n const currentOperator = ctx.operatorStack.peek();\n if (undefined !== currentOperator) {\n incrementArgCount(currentOperator);\n }\n }\n\n ctx.operatorStack.push({\n value: o1,\n type: InternalTokenType.Operator,\n argCount: binary ? 1 : 0,\n argCountIncremented: false,\n });\n\n return { value: true };\n};\n\nconst openParenthesis: TokenInterpreter = (ctx: IConverterContext) => {\n if (undefined !== ctx.previousToken) {\n if (InternalTokenType.Number === ctx.previousTokenType || InternalTokenType.ClosingParenthesis === ctx.previousTokenType) {\n addOperator({ ...ctx, currentToken: \"*\" });\n } else if (InternalTokenType.Variable === ctx.previousTokenType) {\n return { errorMessage: `Function \"${ctx.previousToken}\" is not supported.` };\n }\n }\n\n const currentOperator = ctx.operatorStack.peek();\n if (undefined !== currentOperator && currentOperator.type !== InternalTokenType.Function) {\n incrementArgCount(currentOperator);\n }\n\n ctx.operatorStack.push({ value: \"(\", type: InternalTokenType.OpeningParenthesis, argCount: 0, argCountIncremented: false });\n return { value: true };\n};\n\nconst closeParenthesis: TokenInterpreter = (ctx: IConverterContext) => {\n while (ctx.operatorStack.length > 0 && ctx.operatorStack.peek()!.value !== \"(\") {\n ctx.resultQueue.enqueue(ctx.operatorStack.pop()!);\n }\n\n const leftParenthesis = ctx.operatorStack.pop();\n if (undefined === leftParenthesis) {\n return { errorMessage: `Closing parenthesis \")\" found, but the opening parenthesis \"(\" is missing.` };\n }\n\n const potentialFnToken = ctx.operatorStack.peek();\n if (undefined !== potentialFnToken && isFunction(potentialFnToken.value)) {\n potentialFnToken.argCount = leftParenthesis.argCount;\n ctx.resultQueue.enqueue(ctx.operatorStack.pop()!);\n }\n\n return { value: true };\n};\n\nconst addString: TokenInterpreter = (ctx: IConverterContext) => {\n const withoutDelimiters = ctx.currentToken.substring(1, ctx.currentToken.length - 1);\n addOperand({ ...ctx, currentToken: withoutDelimiters }, InternalTokenType.String);\n return { value: true };\n};\n\nconst addBoolean: TokenInterpreter = (ctx: IConverterContext) => {\n addOperand({ ...ctx, currentToken: ctx.currentToken.toLowerCase() }, InternalTokenType.Boolean);\n return { value: true };\n};\n\nconst addNull: TokenInterpreter = (ctx: IConverterContext) => {\n addOperand({ ...ctx, currentToken: ctx.currentToken.toLowerCase() }, InternalTokenType.Null);\n return { value: true };\n};\n\nconst separateFunctionArgumentSeparator: TokenInterpreter = (ctx: IConverterContext) => {\n while (ctx.operatorStack.length > 0 && ctx.operatorStack.peek()!.value !== \"(\") {\n ctx.resultQueue.enqueue(ctx.operatorStack.pop()!);\n\n const currentOperator = ctx.operatorStack.peek();\n if (undefined !== currentOperator) {\n incrementArgCount(currentOperator);\n }\n }\n\n const last2Operators = ctx.operatorStack.peekN(2);\n if (last2Operators.length < 2 || last2Operators[0].type !== InternalTokenType.OpeningParenthesis || last2Operators[1].type !== InternalTokenType.Function) {\n return { errorMessage: `Function argument separator \",\" found outside of a function call.` };\n }\n\n last2Operators[0].argCountIncremented = false;\n return { value: true };\n};\n\nfunction getTokenType(token: string, nextToken: string | undefined): InternalTokenType {\n if (isNumber(token)) {\n return InternalTokenType.Number;\n } else if (isStringLiteral(token)) {\n return InternalTokenType.String;\n } else if (isBoolean(token)) {\n return InternalTokenType.Boolean;\n } else if (isNull(token)) {\n return InternalTokenType.Null;\n } else if (isFunction(token) && \"(\" === nextToken) {\n return InternalTokenType.Function;\n } else if (isOperator(token)) {\n return InternalTokenType.Operator;\n } else if (\"(\" === token) {\n return InternalTokenType.OpeningParenthesis;\n } else if (\")\" === token) {\n return InternalTokenType.ClosingParenthesis;\n } else if (\",\" === token) {\n return InternalTokenType.ArgumentSeparator;\n } else {\n return InternalTokenType.Variable;\n }\n}\n\nconst tokenInterpreters: Map<InternalTokenType, TokenInterpreter> = new Map([\n [InternalTokenType.Number, addNumber],\n [InternalTokenType.ArgumentSeparator, separateFunctionArgumentSeparator],\n [InternalTokenType.ClosingParenthesis, closeParenthesis],\n [InternalTokenType.Function, addFunction],\n [InternalTokenType.OpeningParenthesis, openParenthesis],\n [InternalTokenType.Operator, addOperator],\n [InternalTokenType.Variable, addVariable],\n [InternalTokenType.String, addString],\n [InternalTokenType.Boolean, addBoolean],\n [InternalTokenType.Null, addNull],\n]);\n\nexport function convertInfixToPostfix(infix: string[]): IResult<Queue<Token>> {\n const resultQueue = new Queue<InternalToken>();\n const operatorStack = new Stack<InternalToken>();\n\n for (let index = 0; index < infix.length; index++) {\n const token = infix[index];\n\n const ctx: IConverterContext = {\n resultQueue,\n operatorStack,\n currentToken: token,\n currentTokenType: getTokenType(token, index < infix.length - 1 ? infix[index + 1] : undefined),\n previousToken: index > 0 ? infix[index - 1] : undefined,\n previousTokenType: index > 0 ? getTokenType(infix[index - 1], token) : undefined,\n };\n\n const fn = tokenInterpreters.get(ctx.currentTokenType)!;\n\n const tmpResult = fn(ctx);\n if (undefined === tmpResult.value) {\n return { errorMessage: tmpResult.errorMessage };\n }\n }\n\n while (operatorStack.length > 0) {\n resultQueue.enqueue(operatorStack.pop()!);\n\n const currentOperator = operatorStack.peek();\n if (undefined !== currentOperator) {\n incrementArgCount(currentOperator);\n }\n }\n\n return getExternalQueue(resultQueue);\n}\n"]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare class InputStream {
|
|
2
|
+
private readonly _stream;
|
|
3
|
+
private _nextIndex;
|
|
4
|
+
private _current;
|
|
5
|
+
constructor(_stream: string);
|
|
6
|
+
get next(): string;
|
|
7
|
+
get undo(): string;
|
|
8
|
+
get current(): string;
|
|
9
|
+
get peek(): string;
|
|
10
|
+
get isEOF(): boolean;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=InputStream.d.ts.map
|