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