lib/parse.js
function getToken() {
  tokenType = TOKENTYPE.NULL;
  token = '';

  // skip over whitespaces: space, tab, newline, and carriage return
  while (c === ' ' || c === '\t' || c === '\n' || c === '\r') {
    next();
  }

  // check for delimiters
  if (DELIMITERS[c]) {
    tokenType = TOKENTYPE.DELIMITER;
    token = c;
    next();
    return;
  }

  // check for a number
  if (isDigit(c) || c === '-') {
    tokenType = TOKENTYPE.NUMBER;

    if (c === '-') {
      token += c;
      next();

      if (!isDigit(c)) {
        throw createSyntaxError('Invalid number, digit expected', index);
      }
    }
    else if (c === '0') {
      token += c;
      next();
    }
    else {
      // digit 1-9, nothing extra to do
    }

    while (isDigit(c)) {
      token += c;
      next();
    }

    if (c === '.') {
      token += c;
      next();

      if (!isDigit(c)) {
        throw createSyntaxError('Invalid number, digit expected', index);
      }

      while (isDigit(c)) {
        token += c;
        next();
      }
    }

    if (c === 'e' || c === 'E') {
      token += c;
      next();

      if (c === '+' || c === '-') {
        token += c;
        next();
      }

      if (!isDigit(c)) {
        throw createSyntaxError('Invalid number, digit expected', index);
      }

      while (isDigit(c)) {
        token += c;
        next();
      }
    }

    return;
  }

  // check for a string
  if (c === '"') {
    tokenType = TOKENTYPE.STRING;
    next();

    while (c !== '' && c !== '"') {

      if (c === '\\') {
        // handle escape characters
        next();

        let unescaped = ESCAPE_CHARACTERS[c];
        if (unescaped !== undefined) {
          token += unescaped;
          next();
        }
        else if (c === 'u') {
          // parse escaped unicode character, like '\\u260E'
          next();

          let hex = '';
          for (let u = 0; u < 4; u++) {
            if (!isHex(c)) {
              throw createSyntaxError('Invalid unicode character');
            }
            hex += c;
            next();
          }

          token += String.fromCharCode(parseInt(hex, 16));
        }
        else {
          throw createSyntaxError('Invalid escape character "\\' + c + '"', index);
        }
      }
      else {
        // a regular character
        token += c;
        next();
      }
    }

    if (c !== '"') {
      throw createSyntaxError('End of string expected');
    }
    next();

    return;
  }

  // check for symbols (true, false, null)
  if (isAlpha(c)) {
    tokenType = TOKENTYPE.SYMBOL;

    while (isAlpha(c)) {
      token += c;
      next();
    }

    return;
  }

  // something unknown is found, wrong characters -> a syntax error
  tokenType = TOKENTYPE.UNKNOWN;
  while (c !== '') {
    token += c;
    next();
  }
  throw createSyntaxError('Syntax error in part "' + token + '"');
}
function getToken() {
  tokenType = TOKENTYPE.NULL;
  token = '';

  // skip over whitespaces: space, tab, newline, and carriage return
  while (c === ' ' || c === '\t' || c === '\n' || c === '\r') {
    next();
  }

  // check for delimiters
  if (DELIMITERS[c]) {
    tokenType = TOKENTYPE.DELIMITER;
    token = c;
    next();
    return;
  }

  // check for a number
  if (isDigit(c) || c === '-') {
    tokenType = TOKENTYPE.NUMBER;

    if (c === '-') {
      token += c;
      next();

      if (!isDigit(c)) {
        throw createSyntaxError('Invalid number, digit expected', index);
      }
    }
    else if (c === '0') {
      token += c;
      next();
    }
    else {
      // digit 1-9, nothing extra to do
    }

    while (isDigit(c)) {
      token += c;
      next();
    }

    if (c === '.') {
      token += c;
      next();

      if (!isDigit(c)) {
        throw createSyntaxError('Invalid number, digit expected', index);
      }

      while (isDigit(c)) {
        token += c;
        next();
      }
    }

    if (c === 'e' || c === 'E') {
      token += c;
      next();

      if (c === '+' || c === '-') {
        token += c;
        next();
      }

      if (!isDigit(c)) {
        throw createSyntaxError('Invalid number, digit expected', index);
      }

      while (isDigit(c)) {
        token += c;
        next();
      }
    }

    return;
  }

  // check for a string
  if (c === '"') {
    tokenType = TOKENTYPE.STRING;
    next();

    while (c !== '' && c !== '"') {

      if (c === '\\') {
        // handle escape characters
        next();

        let unescaped = ESCAPE_CHARACTERS[c];
        if (unescaped !== undefined) {
          token += unescaped;
          next();
        }
        else if (c === 'u') {
          // parse escaped unicode character, like '\\u260E'
          next();

          let hex = '';
          for (let u = 0; u < 4; u++) {
            if (!isHex(c)) {
              throw createSyntaxError('Invalid unicode character');
            }
            hex += c;
            next();
          }

          token += String.fromCharCode(parseInt(hex, 16));
        }
        else {
          throw createSyntaxError('Invalid escape character "\\' + c + '"', index);
        }
      }
      else {
        // a regular character
        token += c;
        next();
      }
    }

    if (c !== '"') {
      throw createSyntaxError('End of string expected');
    }
    next();

    return;
  }

  // check for symbols (true, false, null)
  if (isAlpha(c)) {
    tokenType = TOKENTYPE.SYMBOL;

    while (isAlpha(c)) {
      token += c;
      next();
    }

    return;
  }

  // something unknown is found, wrong characters -> a syntax error
  tokenType = TOKENTYPE.UNKNOWN;
  while (c !== '') {
    token += c;
    next();
  }
  throw createSyntaxError('Syntax error in part "' + token + '"');
}
function getToken ( ) { tokenType = TOKENTYPE . NULL ; token = '' ; // skip over whitespaces: space, tab, newline, and carriage return while ( c === ' ' || c === '\t' || c === '\n' || c === '\r' ) { next ( ) ; } // check for delimiters if ( DELIMITERS [ c ] ) { tokenType = TOKENTYPE . DELIMITER ; token = c ; next ( ) ; return ; } // check for a number if ( isDigit ( c ) || c === '-' ) { tokenType = TOKENTYPE . NUMBER ; if ( c === '-' ) { token += c ; next ( ) ; if ( ! isDigit ( c ) ) { throw createSyntaxError ( 'Invalid number, digit expected' , index ) ; } } else if ( c === '0' ) { token += c ; next ( ) ; } else { // digit 1-9, nothing extra to do } while ( isDigit ( c ) ) { token += c ; next ( ) ; } if ( c === '.' ) { token += c ; next ( ) ; if ( ! isDigit ( c ) ) { throw createSyntaxError ( 'Invalid number, digit expected' , index ) ; } while ( isDigit ( c ) ) { token += c ; next ( ) ; } } if ( c === 'e' || c === 'E' ) { token += c ; next ( ) ; if ( c === '+' || c === '-' ) { token += c ; next ( ) ; } if ( ! isDigit ( c ) ) { throw createSyntaxError ( 'Invalid number, digit expected' , index ) ; } while ( isDigit ( c ) ) { token += c ; next ( ) ; } } return ; } // check for a string if ( c === '"' ) { tokenType = TOKENTYPE . STRING ; next ( ) ; while ( c !== '' && c !== '"' ) { if ( c === '\\' ) { // handle escape characters next ( ) ; let unescaped = ESCAPE_CHARACTERS [ c ] ; if ( unescaped !== undefined ) { token += unescaped ; next ( ) ; } else if ( c === 'u' ) { // parse escaped unicode character, like '\\u260E' next ( ) ; let hex = '' ; for ( let u = 0 ; u < 4 ; u ++ ) { if ( ! isHex ( c ) ) { throw createSyntaxError ( 'Invalid unicode character' ) ; } hex += c ; next ( ) ; } token += String . fromCharCode ( parseInt ( hex , 16 ) ) ; } else { throw createSyntaxError ( 'Invalid escape character "\\' + c + '"' , index ) ; } } else { // a regular character token += c ; next ( ) ; } } if ( c !== '"' ) { throw createSyntaxError ( 'End of string expected' ) ; } next ( ) ; return ; } // check for symbols (true, false, null) if ( isAlpha ( c ) ) { tokenType = TOKENTYPE . SYMBOL ; while ( isAlpha ( c ) ) { token += c ; next ( ) ; } return ; } // something unknown is found, wrong characters -> a syntax error tokenType = TOKENTYPE . UNKNOWN ; while ( c !== '' ) { token += c ; next ( ) ; } throw createSyntaxError ( 'Syntax error in part "' + token + '"' ) ; }
Get next token in the current text.
The token and token type are available as token and tokenType
@private
Get next token in the current text . The token and token type are available as token and tokenType