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

Function getToken

src/expression/parse.js:291–487  ·  view source on GitHub ↗

* Get next token in the current string expr. * The token and token type are available as token and tokenType * @private

(state)

Source from the content-addressed store, hash-verified

289 * @private
290 */
291 function getToken (state) {
292 state.tokenType = TOKENTYPE.NULL
293 state.token = ''
294 state.comment = ''
295
296 // skip over ignored characters:
297 while (true) {
298 // comments:
299 if (currentCharacter(state) === '#') {
300 while (currentCharacter(state) !== '\n' &&
301 currentCharacter(state) !== '') {
302 state.comment += currentCharacter(state)
303 next(state)
304 }
305 }
306 // whitespace: space, tab, and newline when inside parameters
307 if (parse.isWhitespace(currentCharacter(state), state.nestingLevel)) {
308 next(state)
309 } else {
310 break
311 }
312 }
313
314 // check for end of expression
315 if (currentCharacter(state) === '') {
316 // token is still empty
317 state.tokenType = TOKENTYPE.DELIMITER
318 return
319 }
320
321 // check for new line character
322 if (currentCharacter(state) === '\n' && !state.nestingLevel) {
323 state.tokenType = TOKENTYPE.DELIMITER
324 state.token = currentCharacter(state)
325 next(state)
326 return
327 }
328
329 const c1 = currentCharacter(state)
330 const c2 = currentString(state, 2)
331 const c3 = currentString(state, 3)
332 if (c3.length === 3 && DELIMITERS[c3]) {
333 state.tokenType = TOKENTYPE.DELIMITER
334 state.token = c3
335 next(state)
336 next(state)
337 next(state)
338 return
339 }
340
341 // check for delimiters consisting of 2 characters
342 // Special case: the check for '?.' is to prevent a case like 'a?.3:.7' from being interpreted as optional chaining
343 // TODO: refactor the tokenization into some better way to deal with cases like 'a?.3:.7', see https://github.com/josdejong/mathjs/pull/3584
344 if (
345 c2.length === 2 &&
346 DELIMITERS[c2] &&
347 (c2 !== '?.' || !parse.isDigit(state.expression.charAt(state.index + 2)))
348 ) {

Callers 13

getTokenSkipNewlineFunction · 0.85
parseStartFunction · 0.85
parseBlockFunction · 0.85
parseLeftHandOperatorsFunction · 0.85
parseCustomNodesFunction · 0.85
parseSymbolFunction · 0.85
parseAccessorsFunction · 0.85
parseStringTokenFunction · 0.85
parseMatrixFunction · 0.85
parseRowFunction · 0.85
parseObjectFunction · 0.85
parseNumberFunction · 0.85

Calls 12

hasOwnPropertyFunction · 0.90
currentCharacterFunction · 0.85
currentStringFunction · 0.85
prevCharacterFunction · 0.85
nextCharacterFunction · 0.85
createSyntaxErrorFunction · 0.85
isWhitespaceMethod · 0.80
isDigitMethod · 0.80
isDigitDotMethod · 0.80
isAlphaMethod · 0.80
isDecimalMarkMethod · 0.80
nextFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…