UNPKG

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