* Get next token in the current string expr. * The token and token type are available as token and tokenType * @private
(state)
| 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 | ) { |
no test coverage detected
searching dependent graphs…