Skip to content

Commit 7c00f21

Browse files
committed
Basic named capture groups support
1 parent be13927 commit 7c00f21

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

Diff for: lib/Builder.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ class Builder {
9393

9494
/** @var {string} _implodeString String to join with. */
9595
this._implodeString = ''
96+
97+
/** @var {array} _captureNames Save capture names to map */
98+
this._captureNames = []
9699
}
97100

98101
/**********************************************************/
@@ -254,9 +257,14 @@ class Builder {
254257
* Create capture group of given conditions.
255258
*
256259
* @param {Closure|Builder|string} condition Anonymous function with its Builder as a first parameter.
260+
* @param {String} name
257261
* @return {Builder}
258262
*/
259-
capture(conditions) {
263+
capture(conditions, name) {
264+
if (name) {
265+
this._captureNames.push(name)
266+
}
267+
260268
this._validateAndAddMethodType(METHOD_TYPE_GROUP, METHOD_TYPES_ALLOWED_FOR_CHARACTERS)
261269

262270
return this._addClosure(new Builder()._extends('(%s)'), conditions)

Diff for: lib/Language/Helpers/methodMatch.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const SimpleMethod = require('../Methods/SimpleMethod')
66
const ToMethod = require('../Methods/ToMethod')
77
const TimesMethod = require('../Methods/TimesMethod')
88
const AndMethod = require('../Methods/AndMethod')
9+
const AsMethod = require('../Methods/AsMethod')
910

1011
const SyntaxException = require('../../Exceptions/Syntax')
1112

@@ -49,7 +50,7 @@ const mapper = {
4950
'exactly': { 'class': TimesMethod, 'method': 'exactly' },
5051
'at least': { 'class': TimesMethod, 'method': 'atLeast' },
5152
'between': { 'class': AndMethod, 'method': 'between' },
52-
'capture': { 'class': DefaultMethod, 'method': 'capture' }
53+
'capture': { 'class': AsMethod, 'method': 'capture' }
5354
}
5455

5556
/**

Diff for: lib/Language/Methods/AsMethod.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict'
2+
3+
const Method = require('./Method')
4+
5+
/**
6+
* Method having simple parameter(s) ignoring "as".
7+
*/
8+
class AsMethod extends Method {
9+
/**
10+
* @inheritdoc
11+
*/
12+
setParameters(parameters) {
13+
parameters = parameters.filter((parameter) => {
14+
if (typeof parameter !== 'string') {
15+
return true
16+
}
17+
18+
const lower = parameter.toLowerCase()
19+
return lower !== 'as'
20+
})
21+
22+
return super.setParameters(parameters)
23+
}
24+
}
25+
26+
module.exports = AsMethod

0 commit comments

Comments
 (0)