parser-mixin.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. 'use strict';
  2. const ErrorReportingMixinBase = require('./mixin-base');
  3. const ErrorReportingTokenizerMixin = require('./tokenizer-mixin');
  4. const LocationInfoTokenizerMixin = require('../location-info/tokenizer-mixin');
  5. const Mixin = require('../../utils/mixin');
  6. class ErrorReportingParserMixin extends ErrorReportingMixinBase {
  7. constructor(parser, opts) {
  8. super(parser, opts);
  9. this.opts = opts;
  10. this.ctLoc = null;
  11. this.locBeforeToken = false;
  12. }
  13. _setErrorLocation(err) {
  14. if (this.ctLoc) {
  15. err.startLine = this.ctLoc.startLine;
  16. err.startCol = this.ctLoc.startCol;
  17. err.startOffset = this.ctLoc.startOffset;
  18. err.endLine = this.locBeforeToken ? this.ctLoc.startLine : this.ctLoc.endLine;
  19. err.endCol = this.locBeforeToken ? this.ctLoc.startCol : this.ctLoc.endCol;
  20. err.endOffset = this.locBeforeToken ? this.ctLoc.startOffset : this.ctLoc.endOffset;
  21. }
  22. }
  23. _getOverriddenMethods(mxn, orig) {
  24. return {
  25. _bootstrap(document, fragmentContext) {
  26. orig._bootstrap.call(this, document, fragmentContext);
  27. Mixin.install(this.tokenizer, ErrorReportingTokenizerMixin, mxn.opts);
  28. Mixin.install(this.tokenizer, LocationInfoTokenizerMixin);
  29. },
  30. _processInputToken(token) {
  31. mxn.ctLoc = token.location;
  32. orig._processInputToken.call(this, token);
  33. },
  34. _err(code, options) {
  35. mxn.locBeforeToken = options && options.beforeToken;
  36. mxn._reportError(code);
  37. }
  38. };
  39. }
  40. }
  41. module.exports = ErrorReportingParserMixin;