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

Function parseRange

src/expression/parse.js:979–1027  ·  view source on GitHub ↗

* parse range, "start:end", "start:step:end", ":", "start:", ":end", etc * @return {Node} node * @private

(state)

Source from the content-addressed store, hash-verified

977 * @private
978 */
979 function parseRange (state) {
980 let node
981 const params = []
982
983 if (state.token === ':') {
984 if (state.conditionalLevel === state.nestingLevel) {
985 // we are in the midst of parsing a conditional operator, so not
986 // a range, but rather an empty true-expr, which is considered a
987 // syntax error
988 throw createSyntaxError(
989 state,
990 'The true-expression of a conditional operator may not be empty')
991 } else {
992 // implicit start of range = 1 (one-based)
993 node = new ConstantNode(1)
994 }
995 } else {
996 // explicit start
997 node = parseAddSubtract(state)
998 }
999
1000 if (state.token === ':' && (state.conditionalLevel !== state.nestingLevel)) {
1001 // we ignore the range operator when a conditional operator is being processed on the same level
1002 params.push(node)
1003
1004 // parse step and end
1005 while (state.token === ':' && params.length < 3) { // eslint-disable-line no-unmodified-loop-condition
1006 getTokenSkipNewline(state)
1007
1008 if (state.token === ')' || state.token === ']' || state.token === ',' || state.token === '') {
1009 // implicit end
1010 params.push(new SymbolNode('end'))
1011 } else {
1012 // explicit end
1013 params.push(parseAddSubtract(state))
1014 }
1015 }
1016
1017 if (params.length === 3) {
1018 // params = [start, step, end]
1019 node = new RangeNode(params[0], params[2], params[1]) // start, end, step
1020 } else { // length === 2
1021 // params = [start, end]
1022 node = new RangeNode(params[0], params[1]) // start, end
1023 }
1024 }
1025
1026 return node
1027 }
1028
1029 /**
1030 * add or subtract

Callers 1

parseConversionFunction · 0.85

Calls 3

createSyntaxErrorFunction · 0.85
parseAddSubtractFunction · 0.85
getTokenSkipNewlineFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…