location.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.SourceLocation = exports.Position = undefined;
  6. var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
  7. var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  8. exports.getLineInfo = getLineInfo;
  9. var _whitespace = require("./whitespace");
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. // These are used when `options.locations` is on, for the
  12. // `startLoc` and `endLoc` properties.
  13. var Position = exports.Position = function Position(line, col) {
  14. (0, _classCallCheck3.default)(this, Position);
  15. this.line = line;
  16. this.column = col;
  17. };
  18. var SourceLocation = exports.SourceLocation = function SourceLocation(start, end) {
  19. (0, _classCallCheck3.default)(this, SourceLocation);
  20. this.start = start;
  21. this.end = end;
  22. };
  23. // The `getLineInfo` function is mostly useful when the
  24. // `locations` option is off (for performance reasons) and you
  25. // want to find the line/column position for a given character
  26. // offset. `input` should be the code string that the offset refers
  27. // into.
  28. function getLineInfo(input, offset) {
  29. for (var line = 1, cur = 0;;) {
  30. _whitespace.lineBreakG.lastIndex = cur;
  31. var match = _whitespace.lineBreakG.exec(input);
  32. if (match && match.index < offset) {
  33. ++line;
  34. cur = match.index + match[0].length;
  35. } else {
  36. return new Position(line, offset - cur);
  37. }
  38. }
  39. }