Adding named group capture

This commit is contained in:
LiHS 2019-08-15 13:59:41 +08:00
parent 8762ad13ab
commit ac8d90e65a
3 changed files with 15 additions and 1 deletions

View File

@ -13,6 +13,9 @@ describe('parser/javascript/subexp.js', function() {
'(test)': { '(test)': {
regexp: jasmine.objectContaining({ textValue: 'test' }) regexp: jasmine.objectContaining({ textValue: 'test' })
}, },
'(?<name>test)': {
regexp: jasmine.objectContaining({ textValue: 'test' })
},
'(?=test)': { '(?=test)': {
regexp: jasmine.objectContaining({ textValue: 'test' }) regexp: jasmine.objectContaining({ textValue: 'test' })
}, },
@ -95,6 +98,10 @@ describe('parser/javascript/subexp.js', function() {
label: 'group #1', label: 'group #1',
groupCounter: 2 groupCounter: 2
}, },
'(?<name>test)': {
label: 'group \'name\'',
groupCounter: 1
},
'(?=test)': { '(?=test)': {
label: 'positive lookahead', label: 'positive lookahead',
groupCounter: 1 groupCounter: 1

View File

@ -11,7 +11,10 @@ grammar JavascriptRegexp
repeat_spec <- ( "{" min:[0-9]+ "," max:[0-9]+ "}" repeat_spec <- ( "{" min:[0-9]+ "," max:[0-9]+ "}"
/ "{" min:[0-9]+ ",}" / "{" min:[0-9]+ ",}"
/ "{" exact:[0-9]+ "}" ) <RepeatSpec> / "{" exact:[0-9]+ "}" ) <RepeatSpec>
subexp <- "(" capture:( "?:" / "?=" / "?!" )? regexp ")" <Subexp> subexp <- "(" capture:( "?<" groupname:name ">" / "?:" / "?=" / "?!" )? regexp ")" <Subexp>
name <- alpha (alpha / numeric)*
alpha <- [a-zA-Z]
numeric <- [0-9]
charset <- "[" invert:"^"? parts:( charset_range / charset_terminal )* "]" <Charset> charset <- "[" invert:"^"? parts:( charset_range / charset_terminal )* "]" <Charset>
charset_range <- first:charset_range_terminal "-" last:charset_range_terminal <CharsetRange> charset_range <- first:charset_range_terminal "-" last:charset_range_terminal <CharsetRange>
charset_terminal <- charset_escape <CharsetEscape> charset_terminal <- charset_escape <CharsetEscape>

View File

@ -48,6 +48,10 @@ export default {
label() { label() {
if (_.has(this.labelMap, this.properties.capture.textValue)) { if (_.has(this.labelMap, this.properties.capture.textValue)) {
return this.labelMap[this.properties.capture.textValue]; return this.labelMap[this.properties.capture.textValue];
} else if (this.properties.capture !== undefined
&& this.properties.capture.properties !== undefined
&& this.properties.capture.properties.groupname) {
return `group '${this.properties.capture.properties.groupname.textValue}'`;
} else { } else {
return `group #${this.state.groupCounter++}`; return `group #${this.state.groupCounter++}`;
} }