@@ -518,20 +518,6 @@ inspect.styles.regexp.colors = ['green', 'red', 'yellow', 'cyan', 'magenta'];
518518
519519const highlightRegExpColors = inspect . styles . regexp . colors . slice ( ) ;
520520
521- function findSequence ( regexpString , i , start , end , write ) {
522- // TODO(BridgeAR): Improve the output by using a different depth for the content.
523- let seq = start ;
524- i ++ ;
525- while ( i < regexpString . length && regexpString [ i ] !== end ) {
526- seq += regexpString [ i ++ ] ;
527- }
528- if ( i < regexpString . length ) {
529- seq += end ;
530- i ++ ;
531- }
532- return seq ;
533- }
534-
535521/**
536522 * Tokenize and colorize a JavaScript RegExp pattern per ECMAScript grammar.
537523 * This is a tolerant single-pass highlighter, not a validator.
@@ -562,6 +548,24 @@ function highlightRegExp(regexpString) {
562548 return acc ;
563549 } , [ ] ) ;
564550
551+ function writeGroup ( start , end , decreaseDepth = 1 ) {
552+ depth -= decreaseDepth ;
553+ write ( start ) ;
554+ let seq = '' ;
555+ i ++ ;
556+ while ( i < regexpString . length && regexpString [ i ] !== end ) {
557+ seq += regexpString [ i ++ ] ;
558+ }
559+ depth ++ ;
560+ write ( seq ) ;
561+ depth -- ;
562+ if ( i < regexpString . length ) {
563+ write ( end ) ;
564+ i ++ ;
565+ }
566+ depth += decreaseDepth ;
567+ }
568+
565569 const write = ( str ) => {
566570 const idx = depth % palette . length ;
567571 const color = palette [ idx ] ;
@@ -584,10 +588,13 @@ function highlightRegExp(regexpString) {
584588 i ++ ;
585589 if ( i < regexpString . length ) {
586590 seq += regexpString [ i ++ ] ;
587- if ( i < regexpString . length && seq [ 1 ] === 'u' && regexpString [ i ] === '{' ) {
588- const str = findSequence ( regexpString , i , '{' , '}' ) ;
589- seq += str ;
590- i += str . length ;
591+ const next = seq [ 1 ] ;
592+ if ( next === 'u' && regexpString [ i ] === '{' ) {
593+ writeGroup ( `${ seq } {` , '}' , 0 ) ;
594+ continue ;
595+ } else if ( ( next === 'p' || next === 'P' ) && regexpString [ i ] === '{' ) {
596+ writeGroup ( `${ seq } {` , '}' , 0 ) ;
597+ continue ;
591598 } else if ( seq [ 1 ] === 'x' ) {
592599 seq += regexpString . slice ( i , i + 2 ) ;
593600 i += 2 ;
@@ -634,8 +641,8 @@ function highlightRegExp(regexpString) {
634641 if ( a === '<' && ( b === '=' || b === '!' ) ) {
635642 depth -- ;
636643 write ( '?<' ) ;
637- depth ++ ;
638644 write ( b ) ;
645+ depth ++ ;
639646 i += 2 ;
640647 } else if ( a === '<' ) {
641648 // Named capture: write '?<name>' as a single colored token
@@ -677,9 +684,8 @@ function highlightRegExp(regexpString) {
677684 const next = seq [ 1 ] ;
678685 if ( i < regexpString . length ) {
679686 if ( next === 'u' && regexpString [ i ] === '{' ) {
680- const str = findSequence ( regexpString , i , '{' , '}' ) ;
681- seq += str ;
682- i += str . length ;
687+ writeGroup ( `${ seq } {` , '}' , 0 ) ;
688+ continue ;
683689 } else if ( next === 'x' ) {
684690 seq += regexpString . slice ( i , i + 2 ) ;
685691 i += 2 ;
@@ -688,13 +694,11 @@ function highlightRegExp(regexpString) {
688694 seq += regexpString [ i ++ ] ;
689695 }
690696 } else if ( next === 'k' && regexpString [ i ] === '<' ) {
691- const str = findSequence ( regexpString , i , '<' , '>' ) ;
692- seq += str ;
693- i += str . length ;
697+ writeGroup ( `${ seq } <` , '>' ) ;
698+ continue ;
694699 } else if ( ( next === 'p' || next === 'P' ) && regexpString [ i ] === '{' ) {
695- const str = findSequence ( regexpString , i , '{' , '}' ) ;
696- seq += str ;
697- i += str . length ;
700+ writeGroup ( `${ seq } {` , '}' , 0 ) ;
701+ continue ;
698702 }
699703 }
700704 }
0 commit comments