1 | ;
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.createParserClass = void 0;
|
7 |
|
8 | var _factory = require("../utils/factory");
|
9 |
|
10 | var _object = require("../utils/object");
|
11 |
|
12 | var _customs = require("../utils/customs");
|
13 |
|
14 | var _log = require("../utils/log");
|
15 |
|
16 | var name = 'Parser';
|
17 | var dependencies = ['parse'];
|
18 | var createParserClass =
|
19 | /* #__PURE__ */
|
20 | (0, _factory.factory)(name, dependencies, function (_ref) {
|
21 | var parse = _ref.parse;
|
22 |
|
23 | /**
|
24 | * @constructor Parser
|
25 | * Parser contains methods to evaluate or parse expressions, and has a number
|
26 | * of convenience methods to get, set, and remove variables from memory. Parser
|
27 | * keeps a scope containing variables in memory, which is used for all
|
28 | * evaluations.
|
29 | *
|
30 | * Methods:
|
31 | * const result = parser.evaluate(expr) // evaluate an expression
|
32 | * const value = parser.get(name) // retrieve a variable from the parser
|
33 | * const values = parser.getAll() // retrieve all defined variables
|
34 | * parser.set(name, value) // set a variable in the parser
|
35 | * parser.remove(name) // clear a variable from the
|
36 | * // parsers scope
|
37 | * parser.clear() // clear the parsers scope
|
38 | *
|
39 | * Example usage:
|
40 | * const parser = new Parser()
|
41 | * // Note: there is a convenience method which can be used instead:
|
42 | * // const parser = new math.parser()
|
43 | *
|
44 | * // evaluate expressions
|
45 | * parser.evaluate('sqrt(3^2 + 4^2)') // 5
|
46 | * parser.evaluate('sqrt(-4)') // 2i
|
47 | * parser.evaluate('2 inch in cm') // 5.08 cm
|
48 | * parser.evaluate('cos(45 deg)') // 0.7071067811865476
|
49 | *
|
50 | * // define variables and functions
|
51 | * parser.evaluate('x = 7 / 2') // 3.5
|
52 | * parser.evaluate('x + 3') // 6.5
|
53 | * parser.evaluate('function f(x, y) = x^y') // f(x, y)
|
54 | * parser.evaluate('f(2, 3)') // 8
|
55 | *
|
56 | * // get and set variables and functions
|
57 | * const x = parser.get('x') // 7
|
58 | * const f = parser.get('f') // function
|
59 | * const g = f(3, 2) // 9
|
60 | * parser.set('h', 500)
|
61 | * const i = parser.evaluate('h / 2') // 250
|
62 | * parser.set('hello', function (name) {
|
63 | * return 'hello, ' + name + '!'
|
64 | * })
|
65 | * parser.evaluate('hello("user")') // "hello, user!"
|
66 | *
|
67 | * // clear defined functions and variables
|
68 | * parser.clear()
|
69 | *
|
70 | */
|
71 | function Parser() {
|
72 | if (!(this instanceof Parser)) {
|
73 | throw new SyntaxError('Constructor must be called with the new operator');
|
74 | }
|
75 |
|
76 | this.scope = {};
|
77 | }
|
78 | /**
|
79 | * Attach type information
|
80 | */
|
81 |
|
82 |
|
83 | Parser.prototype.type = 'Parser';
|
84 | Parser.prototype.isParser = true;
|
85 | /**
|
86 | * Parse an expression and return the parsed function node.
|
87 | * The node tree can be compiled via `code = node.compile(math)`,
|
88 | * and the compiled code can be executed as `code.evaluate([scope])`
|
89 | * @param {string} expr
|
90 | * @return {Node} node
|
91 | * @throws {Error}
|
92 | */
|
93 |
|
94 | Parser.prototype.parse = function (expr) {
|
95 | throw new Error('Parser.parse is deprecated. Use math.parse instead.');
|
96 | };
|
97 | /**
|
98 | * Parse and compile an expression, return the compiled javascript code.
|
99 | * The node can be evaluated via code.evaluate([scope])
|
100 | * @param {string} expr
|
101 | * @return {{evaluate: function}} code
|
102 | * @throws {Error}
|
103 | */
|
104 |
|
105 |
|
106 | Parser.prototype.compile = function (expr) {
|
107 | throw new Error('Parser.compile is deprecated. Use math.compile instead.');
|
108 | };
|
109 | /**
|
110 | * Parse and evaluate the given expression
|
111 | * @param {string} expr A string containing an expression, for example "2+3"
|
112 | * @return {*} result The result, or undefined when the expression was empty
|
113 | * @throws {Error}
|
114 | */
|
115 |
|
116 |
|
117 | Parser.prototype.evaluate = function (expr) {
|
118 | // TODO: validate arguments
|
119 | return parse(expr).compile().evaluate(this.scope);
|
120 | };
|
121 | /**
|
122 | * Parse and evaluate the given expression
|
123 | * @param {string} expr A string containing an expression, for example "2+3"
|
124 | * @return {*} result The result, or undefined when the expression was empty
|
125 | * @throws {Error}
|
126 | */
|
127 | // TODO: Deprecated since v6.0.0. Clean up some day
|
128 |
|
129 |
|
130 | Parser.prototype.eval = function (expr) {
|
131 | (0, _log.warnOnce)('Method Parser.eval is renamed to Parser.evaluate. Please use the new method name.');
|
132 | return this.evaluate(expr);
|
133 | };
|
134 | /**
|
135 | * Get a variable (a function or variable) by name from the parsers scope.
|
136 | * Returns undefined when not found
|
137 | * @param {string} name
|
138 | * @return {* | undefined} value
|
139 | */
|
140 |
|
141 |
|
142 | Parser.prototype.get = function (name) {
|
143 | // TODO: validate arguments
|
144 | return name in this.scope ? (0, _customs.getSafeProperty)(this.scope, name) : undefined;
|
145 | };
|
146 | /**
|
147 | * Get a map with all defined variables
|
148 | * @return {Object} values
|
149 | */
|
150 |
|
151 |
|
152 | Parser.prototype.getAll = function () {
|
153 | return (0, _object.extend)({}, this.scope);
|
154 | };
|
155 | /**
|
156 | * Set a symbol (a function or variable) by name from the parsers scope.
|
157 | * @param {string} name
|
158 | * @param {* | undefined} value
|
159 | */
|
160 |
|
161 |
|
162 | Parser.prototype.set = function (name, value) {
|
163 | // TODO: validate arguments
|
164 | return (0, _customs.setSafeProperty)(this.scope, name, value);
|
165 | };
|
166 | /**
|
167 | * Remove a variable from the parsers scope
|
168 | * @param {string} name
|
169 | */
|
170 |
|
171 |
|
172 | Parser.prototype.remove = function (name) {
|
173 | // TODO: validate arguments
|
174 | delete this.scope[name];
|
175 | };
|
176 | /**
|
177 | * Clear the scope with variables and functions
|
178 | */
|
179 |
|
180 |
|
181 | Parser.prototype.clear = function () {
|
182 | for (var _name in this.scope) {
|
183 | if ((0, _object.hasOwnProperty)(this.scope, _name)) {
|
184 | delete this.scope[_name];
|
185 | }
|
186 | }
|
187 | };
|
188 |
|
189 | return Parser;
|
190 | }, {
|
191 | isClass: true
|
192 | });
|
193 | exports.createParserClass = createParserClass; |
\ | No newline at end of file |