src/lib/Parser.js
/**
  Throws an error at the current scanner position.

  @param {string} message
  */
Throws an error at the current scanner position.
Throws an error at the current scanner position .
error(message) {
    let { charIndex, string: xml } = this.scanner;
    let column = 1;
    let excerpt = '';
    let line = 1;
    for (let i = 0; i < charIndex; ++i) {
      let char = xml[i];
      if (char === '\n') {
        column = 1;
        excerpt = '';
        line += 1;
      } else {
        column += 1;
        excerpt += char;
      }
    }
    let eol = xml.indexOf('\n', charIndex);
    excerpt += eol === -1
      ? xml.slice(charIndex)
      : xml.slice(charIndex, eol);
    let excerptStart = 0;
    if (excerpt.length > 50) {
      if (column < 40) {
        excerpt = excerpt.slice(0, 50);
      } else {
        excerptStart = column - 20;
        excerpt = excerpt.slice(excerptStart, column + 30);
      }
    }
    let err = new Error(
      `${message} (line ${line}, column ${column})\n`
        + `  ${excerpt}\n`
        + ' '.repeat(column - excerptStart + 1) + '^\n',
    );
    Object.assign(err, {
      column,
      excerpt,
      line,
      pos: charIndex,
    });
    throw err;
  }
error ( message ) { let { charIndex , string : xml } = this . scanner ; let column = 1 ; let excerpt = '' ; let line = 1 ; for ( let i = 0 ; i < charIndex ; ++ i ) { let char = xml [ i ] ; if ( char === '\n' ) { column = 1 ; excerpt = '' ; line += 1 ; } else { column += 1 ; excerpt += char ; } } let eol = xml . indexOf ( '\n' , charIndex ) ; excerpt += eol === - 1 ? xml . slice ( charIndex ) : xml . slice ( charIndex , eol ) ; let excerptStart = 0 ; if ( excerpt . length > 50 ) { if ( column < 40 ) { excerpt = excerpt . slice ( 0 , 50 ) ; } else { excerptStart = column - 20 ; excerpt = excerpt . slice ( excerptStart , column + 30 ) ; } } let err = new Error ( ` ${ message } ${ line } ${ column } \n ` + ` ${ excerpt } \n ` + ' ' . repeat ( column - excerptStart + 1 ) + '^\n' , ) ; Object . assign ( err , { column , excerpt , line , pos : charIndex , } ) ; throw err ; }
Throws an error at the current scanner position.
Throws an error at the current scanner position .
// Find the line and column where the error occurred. // Keep the excerpt below 50 chars, but always keep the error position in // view.