MCPcopy
hub / github.com/josdejong/mathjs / parseConditional

Function parseConditional

src/expression/parse.js:754–781  ·  view source on GitHub ↗

* conditional operation * * condition ? truePart : falsePart * * Note: conditional operator is right-associative * * @return {Node} node * @private

(state)

Source from the content-addressed store, hash-verified

752 * @private
753 */
754 function parseConditional (state) {
755 let node = parseLogicalOr(state)
756
757 while (state.token === '?') { // eslint-disable-line no-unmodified-loop-condition
758 // set a conditional level, the range operator will be ignored as long
759 // as conditionalLevel === state.nestingLevel.
760 const prev = state.conditionalLevel
761 state.conditionalLevel = state.nestingLevel
762 getTokenSkipNewline(state)
763
764 const condition = node
765 const trueExpr = parseAssignment(state)
766
767 if (state.token !== ':') throw createSyntaxError(state, 'False part of conditional expression expected')
768
769 state.conditionalLevel = null
770 getTokenSkipNewline(state)
771
772 const falseExpr = parseAssignment(state) // Note: check for conditional operator again, right associativity
773
774 node = new ConditionalNode(condition, trueExpr, falseExpr)
775
776 // restore the previous conditional level
777 state.conditionalLevel = prev
778 }
779
780 return node
781 }
782
783 /**
784 * logical or, 'x or y'

Callers 1

parseAssignmentFunction · 0.85

Calls 4

parseLogicalOrFunction · 0.85
getTokenSkipNewlineFunction · 0.85
parseAssignmentFunction · 0.85
createSyntaxErrorFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…