1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | import { hasOwnProperty } from '../utils/object'
|
20 |
|
21 | export const properties = [
|
22 | {
|
23 | AssignmentNode: {},
|
24 | FunctionAssignmentNode: {}
|
25 | },
|
26 | {
|
27 | ConditionalNode: {
|
28 | latexLeftParens: false,
|
29 | latexRightParens: false,
|
30 | latexParens: false
|
31 |
|
32 |
|
33 | }
|
34 | },
|
35 | {
|
36 | 'OperatorNode:or': {
|
37 | associativity: 'left',
|
38 | associativeWith: []
|
39 | }
|
40 |
|
41 | },
|
42 | {
|
43 | 'OperatorNode:xor': {
|
44 | associativity: 'left',
|
45 | associativeWith: []
|
46 | }
|
47 | },
|
48 | {
|
49 | 'OperatorNode:and': {
|
50 | associativity: 'left',
|
51 | associativeWith: []
|
52 | }
|
53 | },
|
54 | {
|
55 | 'OperatorNode:bitOr': {
|
56 | associativity: 'left',
|
57 | associativeWith: []
|
58 | }
|
59 | },
|
60 | {
|
61 | 'OperatorNode:bitXor': {
|
62 | associativity: 'left',
|
63 | associativeWith: []
|
64 | }
|
65 | },
|
66 | {
|
67 | 'OperatorNode:bitAnd': {
|
68 | associativity: 'left',
|
69 | associativeWith: []
|
70 | }
|
71 | },
|
72 | {
|
73 | 'OperatorNode:equal': {
|
74 | associativity: 'left',
|
75 | associativeWith: []
|
76 | },
|
77 | 'OperatorNode:unequal': {
|
78 | associativity: 'left',
|
79 | associativeWith: []
|
80 | },
|
81 | 'OperatorNode:smaller': {
|
82 | associativity: 'left',
|
83 | associativeWith: []
|
84 | },
|
85 | 'OperatorNode:larger': {
|
86 | associativity: 'left',
|
87 | associativeWith: []
|
88 | },
|
89 | 'OperatorNode:smallerEq': {
|
90 | associativity: 'left',
|
91 | associativeWith: []
|
92 | },
|
93 | 'OperatorNode:largerEq': {
|
94 | associativity: 'left',
|
95 | associativeWith: []
|
96 | },
|
97 | RelationalNode: {
|
98 | associativity: 'left',
|
99 | associativeWith: []
|
100 | }
|
101 | },
|
102 | {
|
103 | 'OperatorNode:leftShift': {
|
104 | associativity: 'left',
|
105 | associativeWith: []
|
106 | },
|
107 | 'OperatorNode:rightArithShift': {
|
108 | associativity: 'left',
|
109 | associativeWith: []
|
110 | },
|
111 | 'OperatorNode:rightLogShift': {
|
112 | associativity: 'left',
|
113 | associativeWith: []
|
114 | }
|
115 | },
|
116 | {
|
117 | 'OperatorNode:to': {
|
118 | associativity: 'left',
|
119 | associativeWith: []
|
120 | }
|
121 | },
|
122 | {
|
123 | RangeNode: {}
|
124 | },
|
125 | {
|
126 | 'OperatorNode:add': {
|
127 | associativity: 'left',
|
128 | associativeWith: ['OperatorNode:add', 'OperatorNode:subtract']
|
129 | },
|
130 | 'OperatorNode:subtract': {
|
131 | associativity: 'left',
|
132 | associativeWith: []
|
133 | }
|
134 | },
|
135 | {
|
136 | 'OperatorNode:multiply': {
|
137 | associativity: 'left',
|
138 | associativeWith: [
|
139 | 'OperatorNode:multiply',
|
140 | 'OperatorNode:divide',
|
141 | 'Operator:dotMultiply',
|
142 | 'Operator:dotDivide'
|
143 | ]
|
144 | },
|
145 | 'OperatorNode:divide': {
|
146 | associativity: 'left',
|
147 | associativeWith: [],
|
148 | latexLeftParens: false,
|
149 | latexRightParens: false,
|
150 | latexParens: false
|
151 |
|
152 |
|
153 |
|
154 | },
|
155 | 'OperatorNode:dotMultiply': {
|
156 | associativity: 'left',
|
157 | associativeWith: [
|
158 | 'OperatorNode:multiply',
|
159 | 'OperatorNode:divide',
|
160 | 'OperatorNode:dotMultiply',
|
161 | 'OperatorNode:doDivide'
|
162 | ]
|
163 | },
|
164 | 'OperatorNode:dotDivide': {
|
165 | associativity: 'left',
|
166 | associativeWith: []
|
167 | },
|
168 | 'OperatorNode:mod': {
|
169 | associativity: 'left',
|
170 | associativeWith: []
|
171 | }
|
172 | },
|
173 | {
|
174 | 'OperatorNode:unaryPlus': {
|
175 | associativity: 'right'
|
176 | },
|
177 | 'OperatorNode:unaryMinus': {
|
178 | associativity: 'right'
|
179 | },
|
180 | 'OperatorNode:bitNot': {
|
181 | associativity: 'right'
|
182 | },
|
183 | 'OperatorNode:not': {
|
184 | associativity: 'right'
|
185 | }
|
186 | },
|
187 | {
|
188 | 'OperatorNode:pow': {
|
189 | associativity: 'right',
|
190 | associativeWith: [],
|
191 | latexRightParens: false
|
192 |
|
193 |
|
194 |
|
195 | },
|
196 | 'OperatorNode:dotPow': {
|
197 | associativity: 'right',
|
198 | associativeWith: []
|
199 | }
|
200 | },
|
201 | {
|
202 | 'OperatorNode:factorial': {
|
203 | associativity: 'left'
|
204 | }
|
205 | },
|
206 | {
|
207 | 'OperatorNode:transpose': {
|
208 | associativity: 'left'
|
209 | }
|
210 | }
|
211 | ]
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 | export function getPrecedence (_node, parenthesis) {
|
223 | let node = _node
|
224 | if (parenthesis !== 'keep') {
|
225 |
|
226 | node = _node.getContent()
|
227 | }
|
228 | const identifier = node.getIdentifier()
|
229 | for (let i = 0; i < properties.length; i++) {
|
230 | if (identifier in properties[i]) {
|
231 | return i
|
232 | }
|
233 | }
|
234 | return null
|
235 | }
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 | export function getAssociativity (_node, parenthesis) {
|
248 | let node = _node
|
249 | if (parenthesis !== 'keep') {
|
250 |
|
251 | node = _node.getContent()
|
252 | }
|
253 | const identifier = node.getIdentifier()
|
254 | const index = getPrecedence(node, parenthesis)
|
255 | if (index === null) {
|
256 |
|
257 | return null
|
258 | }
|
259 | const property = properties[index][identifier]
|
260 |
|
261 | if (hasOwnProperty(property, 'associativity')) {
|
262 | if (property.associativity === 'left') {
|
263 | return 'left'
|
264 | }
|
265 | if (property.associativity === 'right') {
|
266 | return 'right'
|
267 | }
|
268 |
|
269 | throw Error('\'' + identifier + '\' has the invalid associativity \'' +
|
270 | property.associativity + '\'.')
|
271 | }
|
272 |
|
273 |
|
274 | return null
|
275 | }
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 | export function isAssociativeWith (nodeA, nodeB, parenthesis) {
|
287 |
|
288 | const a = (parenthesis !== 'keep') ? nodeA.getContent() : nodeA
|
289 | const b = (parenthesis !== 'keep') ? nodeA.getContent() : nodeB
|
290 | const identifierA = a.getIdentifier()
|
291 | const identifierB = b.getIdentifier()
|
292 | const index = getPrecedence(a, parenthesis)
|
293 | if (index === null) {
|
294 |
|
295 | return null
|
296 | }
|
297 | const property = properties[index][identifierA]
|
298 |
|
299 | if (hasOwnProperty(property, 'associativeWith') &&
|
300 | (property.associativeWith instanceof Array)) {
|
301 | for (let i = 0; i < property.associativeWith.length; i++) {
|
302 | if (property.associativeWith[i] === identifierB) {
|
303 | return true
|
304 | }
|
305 | }
|
306 | return false
|
307 | }
|
308 |
|
309 |
|
310 | return null
|
311 | }
|