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