-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
217 lines (192 loc) · 6.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
'use strict';
/**
* Sets the `start` location and returns a function for setting
* the `end` location.
*
* ```js
* const location = require('snapdragon-location');
* const Lexer = require('snapdragon-lexer');
* const lexer = new Lexer('foo/bar');
*
* lexer.capture('slash', /^\//);
* lexer.capture('text', /^\w+/);
*
* const loc = location(lexer);
* const token = loc(lexer.advance());
* console.log(token);
* ```
* @param {String|Object} `name` (optional) Snapdragon Lexer or Tokenizer instance, or the name to use for the location property on the token. Default is `toc`.
* @param {Object} `target` Snapdragon Lexer or Tokenizer instance
* @return {Function} Returns a function that takes a `token` as its only argument
* @api public
*/
function location(name, target) {
if (isValidInstance(name)) return location('loc', name);
if (!isValidInstance(target)) return location.plugin(name);
const start = new Position(target);
return (token) => {
token[name] = new Location(start, new Position(target), target);
if (target.emit) target.emit('location', token);
return token;
};
}
/**
* Use as a plugin to add a `.location` method to your [snapdragon-lexer][]
* or [snapdragon-tokenizer][] instance to automatically add a location object
* to tokens when the `.lex()` or `.handle()` methods are used.
*
* ```js
* const Lexer = require('snapdragon-lexer');
* const location = require('snapdragon-location');
* const lexer = new Lexer();
* lexer.use(location());
* ```
* @api public
*/
location.plugin = (name) => {
if (typeof name !== 'string') name = 'loc';
return function(target) {
if (!isValidInstance(target)) {
throw new Error('expected a snapdragon Lexer or Tokenizer instance');
}
/**
* Get the current source position, with `index`, `column` and `line`.
* Used by [.location()](#location) to create the "start" and "end" positions.
*
* ```js
* const Lexer = require('snapdragon-lexer');
* const lexer = new Lexer();
* console.log(lexer.position());
* //=> Position { index: 0, column: 0, line: 1 };
* ```
* @return {Object} Returns an object with the current source position.
* @api public
*/
target.position = () => new Position(target);
/**
* Returns a function for getting the current location.
*
* ```js
* const Lexer = require('snapdragon-lexer');
* const lexer = new Lexer('foo/bar');
* lexer.use(location());
*
* lexer.set('text', function(tok) {
* // get start location before advancing lexer
* const loc = this.location();
* const match = this.match(/^\w+/);
* if (match) {
* // get end location after advancing lexer (with .match)
* return loc(this.token(match));
* }
* });
* ```
* @return {Function} Returns a function that takes a `token` as its only argument, and patches a `.loc` property onto the token.
* @api public
*/
target.location = (key) => location(key || name, target);
/**
* Override the `.lex` method to automatically patch
* location onto returned tokens in a future-proof way.
*/
target.lex = (type) => {
const loc = target.location();
const tok = target.constructor.prototype.lex.call(target, type);
if (tok) {
return loc(tok);
}
};
/**
* Override the `.handle` method to automatically patch
* location onto returned tokens in a future-proof way.
*/
target.handle = (type) => {
const loc = target.location();
const tok = target.constructor.prototype.handle.call(target, type);
if (tok) {
return loc(tok);
}
};
};
};
/**
* Create a new Position object with `index`, `column`, and `line`.
*
* ```js
* const Lexer = require('snapdragon-lexer');
* const Position = require('snapdragon-location').Position;
* const lexer = new Lexer('foo/bar');
* lexer.capture('text', /^\w+/);
* lexer.advance();
* console.log(new Position(lexer));
* //=> Position { index: 3, column: 4, line: 1 }
* ```
* @param {Object} `start` (required) Starting [position](#position)
* @param {Object} `end` (required) Ending [position](#position)
* @param {Object} `target` (optional) Snapdragon Lexer or Tokenizer instance
* @return {Object}
* @api public
*/
class Position {
constructor(lexer) {
this.index = lexer.loc.index;
this.column = lexer.loc.column;
this.line = lexer.loc.line;
}
}
/**
* Create a new Location with the given `start` and `end` positions.
*
* ```js
* const Lexer = require('snapdragon-lexer');
* const location = require('snapdragon-position');
* const lexer = new Lexer('foo/bar')
* .capture('slash', /^\//)
* .capture('text', /^\w+/);
*
* const start = new location.Position(lexer);
* lexer.advance();
* const end = new location.Position(lexer);
* console.log(new location.Location(start, end, lexer));
* // Location {
* // source: undefined,
* // start: Position { index: 0, column: 1, line: 1 },
* // end: Position { index: 3, column: 4, line: 1 } }
* ```
* @param {Object} `start` (required) Starting [position](#position)
* @param {Object} `end` (required) Ending [position](#position)
* @param {Object} `target` (optional) Snapdragon Lexer or Tokenizer instance
* @return {Object}
* @api public
*/
class Location {
constructor(start, end, target) {
this.source = isValidInstance(target) ? target.options.source : undefined;
this.start = start;
this.end = end;
}
get range() {
return [this.start.index, this.end.index];
}
}
/**
* Returns true if `target` is an instance of snapdragon lexer or tokenizer
*/
function isValidInstance(target) {
if (isObject(target)) {
return target.isLexer === true || target.isTokenizer === true;
}
return false;
}
function isObject(target) {
return target && typeof target === 'object';
}
/**
* Main export
*/
module.exports = location;
/**
* Expose `Location` and `Position` classes as properties on main export
*/
module.exports.Location = Location;
module.exports.Position = Position;