UNPKG

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