UNPKG

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