MCPcopy Index your code
hub / github.com/josdejong/mathjs / parseRule2

Function parseRule2

src/expression/parse.js:1139–1182  ·  view source on GitHub ↗

* Infamous "rule 2" as described in https://github.com/josdejong/mathjs/issues/792#issuecomment-361065370 * And as amended in https://github.com/josdejong/mathjs/issues/2370#issuecomment-1054052164 * Explicit division gets higher precedence than implicit multiplication * when the division m

(state)

Source from the content-addressed store, hash-verified

1137 * @private
1138 */
1139 function parseRule2 (state) {
1140 let node = parseUnaryPercentage(state)
1141 let last = node
1142 const tokenStates = []
1143
1144 while (true) {
1145 // Match the "number /" part of the pattern "number / number symbol"
1146 if (state.token === '/' && rule2Node(last)) {
1147 // Look ahead to see if the next token is a number
1148 tokenStates.push(Object.assign({}, state))
1149 getTokenSkipNewline(state)
1150
1151 // Match the "number / number" part of the pattern
1152 if (state.tokenType === TOKENTYPE.NUMBER) {
1153 // Look ahead again
1154 tokenStates.push(Object.assign({}, state))
1155 getTokenSkipNewline(state)
1156
1157 // Match the "symbol" part of the pattern, or a left parenthesis
1158 if (state.tokenType === TOKENTYPE.SYMBOL || state.token === '(' || state.token === 'in') {
1159 // We've matched the pattern "number / number symbol".
1160 // Rewind once and build the "number / number" node; the symbol will be consumed later
1161 Object.assign(state, tokenStates.pop())
1162 tokenStates.pop()
1163 last = parseUnaryPercentage(state)
1164 node = new OperatorNode('/', 'divide', [node, last])
1165 } else {
1166 // Not a match, so rewind
1167 tokenStates.pop()
1168 Object.assign(state, tokenStates.pop())
1169 break
1170 }
1171 } else {
1172 // Not a match, so rewind
1173 Object.assign(state, tokenStates.pop())
1174 break
1175 }
1176 } else {
1177 break
1178 }
1179 }
1180
1181 return node
1182 }
1183
1184 /**
1185 * Unary percentage operator (treated as `value / 100`)

Callers 1

Calls 3

rule2NodeFunction · 0.90
parseUnaryPercentageFunction · 0.85
getTokenSkipNewlineFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…