location.js 811 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getLineInfo = getLineInfo;
  6. exports.SourceLocation = exports.Position = void 0;
  7. var _whitespace = require("./whitespace");
  8. class Position {
  9. constructor(line, col) {
  10. this.line = line;
  11. this.column = col;
  12. }
  13. }
  14. exports.Position = Position;
  15. class SourceLocation {
  16. constructor(start, end) {
  17. this.start = start;
  18. this.end = end;
  19. }
  20. }
  21. exports.SourceLocation = SourceLocation;
  22. function getLineInfo(input, offset) {
  23. let line = 1;
  24. let lineStart = 0;
  25. let match;
  26. _whitespace.lineBreakG.lastIndex = 0;
  27. while ((match = _whitespace.lineBreakG.exec(input)) && match.index < offset) {
  28. line++;
  29. lineStart = _whitespace.lineBreakG.lastIndex;
  30. }
  31. return new Position(line, offset - lineStart);
  32. }