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

Function parseStringToken

src/expression/parse.js:1550–1589  ·  view source on GitHub ↗

* Parse a string surrounded by single or double quotes * @param {Object} state * @param {"'" | "\""} quote * @return {string}

(state, quote)

Source from the content-addressed store, hash-verified

1548 * @return {string}
1549 */
1550 function parseStringToken (state, quote) {
1551 let str = ''
1552
1553 while (currentCharacter(state) !== '' && currentCharacter(state) !== quote) {
1554 if (currentCharacter(state) === '\\') {
1555 next(state)
1556
1557 const char = currentCharacter(state)
1558 const escapeChar = ESCAPE_CHARACTERS[char]
1559 if (escapeChar !== undefined) {
1560 // an escaped control character like \" or \n
1561 str += escapeChar
1562 state.index += 1
1563 } else if (char === 'u') {
1564 // escaped unicode character
1565 const unicode = state.expression.slice(state.index + 1, state.index + 5)
1566 if (/^[0-9A-Fa-f]{4}$/.test(unicode)) { // test whether the string holds four hexadecimal values
1567 str += String.fromCharCode(parseInt(unicode, 16))
1568 state.index += 5
1569 } else {
1570 throw createSyntaxError(state, `Invalid unicode character \\u${unicode}`)
1571 }
1572 } else {
1573 throw createSyntaxError(state, `Bad escape character \\${char}`)
1574 }
1575 } else {
1576 // any regular character
1577 str += currentCharacter(state)
1578 next(state)
1579 }
1580 }
1581
1582 getToken(state)
1583 if (state.token !== quote) {
1584 throw createSyntaxError(state, `End of string ${quote} expected`)
1585 }
1586 getToken(state)
1587
1588 return str
1589 }
1590
1591 /**
1592 * parse the matrix

Callers 2

parseStringFunction · 0.85
parseObjectFunction · 0.85

Calls 4

currentCharacterFunction · 0.85
createSyntaxErrorFunction · 0.85
getTokenFunction · 0.85
nextFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…