-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
157 lines (128 loc) · 4.18 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
'use strict';
var Node = require('snapdragon-node');
var extend = require('extend-shallow');
/**
* Register the plugin with `snapdragon.use()` or `snapdragon.parser.use()`.
*
* ```js
* var Snapdragon = require('snapdragon');
* var captureSet = require('snapdragon-capture-set');
*
* // snapdragon
* var snapdragon = new Snapdragon();
* snapdragon.use(captureSet());
*
* // parser
* snapdragon.parser.use(captureSet());
* ```
* @api public
*/
module.exports = function(options) {
return function(snapdragon) {
if (snapdragon.isSnapdragon) {
snapdragon.parser.use(captureSet(options));
snapdragon.define('captureSet', function() {
return this.parser.captureSet.apply(this.parser, arguments);
});
} else if (snapdragon.isParser) {
snapdragon.use(captureSet(options));
} else {
throw new Error('expected an instance of snapdragon or snapdragon.parser');
}
};
};
/**
* Create a node of the given `type` using the specified regex or function.
*
* ```js
* parser.captureSet('brace', /^\{/, /^\}/);
* ```
* @param {String} `type`
* @param {RegExp|Function} `regex` Pass the regex to use for capturing the `open` and `close` nodes.
* @return {Object} Returns the parser instance for chaining
* @api public
*/
function captureSet(options) {
return function(parser) {
parser.define('captureOpen', function(type, regex, fn) {
this.sets[type] = this.sets[type] || [];
// noop, we really only need the `.open` and `.close` visitors
this.set(type, function() {});
// create the `open` visitor for "type"
this.set(type + '.open', function() {
var pos = this.position();
var match = this.match(regex);
if (!match || !match[0]) return;
this.setCount++;
// get the last node, either from `this.stack` or `this.ast.nodes`,
// so we can push our "parent" node onto the `nodes` array of
// that node. We don't want to just push it onto the ast, because
// we need to easily be able to pop it off of a stack when we
// get the "close" node
var prev = this.prev();
// create the "parent" node (ex: "brace")
var parent = pos(new Node({
type: type,
nodes: []
}));
// create the "open" node (ex: "brace.open")
var open = pos(new Node({
type: type + '.open',
val: match[0]
}));
// push "open" node onto `parent.nodes`, and create
// a reference to parent on `open.parent`
parent.pushNode(open);
// add a non-enumerable reference to the "match" arguments
open.define('match', match);
parent.define('match', match);
if (typeof fn === 'function') {
fn.call(this, open, parent);
}
this.push(type, parent);
prev.pushNode(parent);
});
return this;
});
parser.define('captureClose', function(type, regex, fn) {
if (typeof this.sets[type] === 'undefined') {
throw new Error('an `.open` type is not registered for ' + type);
}
var opts = extend({}, this.options, options);
// create the `close` visitor for "type"
this.set(type + '.close', function() {
var pos = this.position();
var match = this.match(regex);
if (!match || !match[0]) return;
var parent = this.pop(type);
var close = pos(new Node({
type: type + '.close',
val: match[0]
}));
if (!this.isType(parent, type)) {
if (opts.strict) {
throw new Error('missing opening "' + type + '"');
}
this.setCount--;
close.define('escaped', true);
return close;
}
if (close.suffix === '\\') {
parent.define('escaped', true);
close.define('escaped', true);
}
parent.pushNode(close);
});
return this;
});
/**
* Create a parser with open and close for parens,
* brackets or braces
*/
parser.define('captureSet', function(type, openRegex, closeRegex, fn) {
this.captureOpen(type, openRegex, fn);
this.captureClose(type, closeRegex, fn);
return this;
});
};
}