3 var utils = require('./utils');
4 var define = require('define-property');
10 var TEXT_REGEX = '(\\[(?=.*\\])|\\])+';
11 var not = utils.createRegex(TEXT_REGEX);
17 function parsers(brackets) {
18 brackets.state = brackets.state || {};
19 brackets.parser.sets.bracket = brackets.parser.sets.bracket || [];
22 .capture('escape', function() {
23 if (this.isInside('bracket')) return;
24 var pos = this.position();
25 var m = this.match(/^\\(.)/);
38 .capture('text', function() {
39 if (this.isInside('bracket')) return;
40 var pos = this.position();
41 var m = this.match(not);
42 if (!m || !m[0]) return;
51 * POSIX character classes: "[[:alpha:][:digits:]]"
54 .capture('posix', function() {
55 var pos = this.position();
56 var m = this.match(/^\[:(.*?):\](?=.*\])/);
59 var inside = this.isInside('bracket');
66 insideBracket: inside,
76 .capture('bracket', function() {})
82 .capture('bracket.open', function() {
83 var parsed = this.parsed;
84 var pos = this.position();
85 var m = this.match(/^\[(?=.*\])/);
88 var prev = this.prev();
89 var last = utils.last(prev.nodes);
91 if (parsed.slice(-1) === '\\' && !this.isInside('bracket')) {
92 last.val = last.val.slice(0, last.val.length - 1);
100 type: 'bracket.open',
104 if (last.type === 'bracket.open' || this.isInside('bracket')) {
105 open.val = '\\' + open.val;
106 open.type = 'bracket.inner';
116 define(node, 'parent', prev);
117 define(open, 'parent', node);
118 this.push('bracket', node);
119 prev.nodes.push(node);
126 .capture('bracket.inner', function() {
127 if (!this.isInside('bracket')) return;
128 var pos = this.position();
129 var m = this.match(not);
130 if (!m || !m[0]) return;
132 var next = this.input.charAt(0);
136 type: 'bracket.inner',
140 if (val === '\\\\') {
144 var first = val.charAt(0);
145 var last = val.slice(-1);
148 val = '^' + val.slice(1);
151 if (last === '\\' || (val === '^' && next === ']')) {
152 val += this.input[0];
164 .capture('bracket.close', function() {
165 var parsed = this.parsed;
166 var pos = this.position();
167 var m = this.match(/^\]/);
170 var prev = this.prev();
171 var last = utils.last(prev.nodes);
173 if (parsed.slice(-1) === '\\' && !this.isInside('bracket')) {
174 last.val = last.val.slice(0, last.val.length - 1);
183 type: 'bracket.close',
188 if (last.type === 'bracket.open') {
189 node.type = 'bracket.inner';
194 var bracket = this.pop('bracket');
195 if (!this.isType(bracket, 'bracket')) {
196 if (this.options.strict) {
197 throw new Error('missing opening "["');
199 node.type = 'bracket.inner';
204 bracket.nodes.push(node);
205 define(node, 'parent', bracket);
213 module.exports = parsers;
219 module.exports.TEXT_REGEX = TEXT_REGEX;