location.js 787 B

1234567891011121314151617181920212223242526
  1. import {Parser} from "./state"
  2. import {Position, getLineInfo} from "./locutil"
  3. const pp = Parser.prototype
  4. // This function is used to raise exceptions on parse errors. It
  5. // takes an offset integer (into the current `input`) to indicate
  6. // the location of the error, attaches the position to the end
  7. // of the error message, and then raises a `SyntaxError` with that
  8. // message.
  9. pp.raise = function(pos, message) {
  10. let loc = getLineInfo(this.input, pos)
  11. message += " (" + loc.line + ":" + loc.column + ")"
  12. let err = new SyntaxError(message)
  13. err.pos = pos; err.loc = loc; err.raisedAt = this.pos
  14. throw err
  15. }
  16. pp.raiseRecoverable = pp.raise
  17. pp.curPosition = function() {
  18. if (this.options.locations) {
  19. return new Position(this.curLine, this.pos - this.lineStart)
  20. }
  21. }