@@ -50,6 +50,7 @@ public class RegexParameterizedPatternParser implements ParameterizedPatternPars
5050 private RegexParameterizedPatternBuilder builder ;
5151 private String defaultParameterPattern ;
5252 private ParameterStore store ;
53+ private CaptureType type ;
5354
5455 RegexParameterizedPatternParser (RegexParameterizedPatternBuilder builder ,
5556 String defaultParameterPattern , String pattern )
@@ -94,44 +95,61 @@ public RegexParameterizedPatternParser(final CaptureType type, final String defa
9495 this .defaultParameterPattern = defaultParameterPattern ;
9596 this .pattern = pattern ;
9697 this .chars = pattern .toCharArray ();
98+ this .type = type ;
9799
98- if (chars .length > 0 )
99- {
100- int parameterIndex = 0 ;
101- int cursor = 0 ;
102- while (cursor < chars .length )
103- {
104- char c = chars [cursor ];
105- if (c == '{' )
106- {
107- int startPos = cursor ;
108- CapturingGroup group = ParseTools .balancedCapture (chars , startPos , chars .length - 1 , type );
109- cursor = group .getEnd ();
100+ int cursor = 0 ;
101+ try {
110102
111- groups .add (new RegexGroup (group , parameterIndex ++));
112- }
113- else if (c == '\\' && !ParseTools .isEscaped (chars , cursor ))
103+ if (chars .length > 0 )
104+ {
105+ int parameterIndex = 0 ;
106+ while (cursor < chars .length )
114107 {
115- if (chars .length - 1 > cursor )
108+ char c = chars [cursor ];
109+ if (!ParseTools .isEscaped (chars , cursor ))
116110 {
117- if (chars [cursor + 1 ] != type .getBegin ()
118- && chars [cursor + 1 ] != type .getEnd ()
119- && chars [cursor + 1 ] != '\\' )
111+ if (c == '{' )
120112 {
121- throw new ParameterizedPatternSyntaxException ("Illegal escape sequence at index " + cursor ,
122- new String (chars ), cursor );
113+ int startPos = cursor ;
114+ CapturingGroup group = ParseTools .balancedCapture (chars , startPos , chars .length - 1 , type );
115+ cursor = group .getEnd ();
116+
117+ groups .add (new RegexGroup (group , parameterIndex ++));
118+ }
119+ else if (c == '\\' )
120+ {
121+ if (chars .length - 1 > cursor )
122+ {
123+ if (chars [cursor + 1 ] != type .getBegin ()
124+ && chars [cursor + 1 ] != type .getEnd ()
125+ && chars [cursor + 1 ] != '\\' )
126+ {
127+ throw new ParameterizedPatternSyntaxException ("Illegal escape sequence at index " + cursor ,
128+ new String (chars ), cursor );
129+ }
130+ }
131+ else if (chars .length - 1 == cursor )
132+ {
133+ throw new ParameterizedPatternSyntaxException (
134+ "Illegal partial escape sequence at index [" + cursor + "] of [" + new String (chars )
135+ + "]" , new String (chars ), cursor );
136+ }
123137 }
124138 }
125- else if (chars .length - 1 == cursor )
126- {
127- throw new ParameterizedPatternSyntaxException ("Illegal partial escape sequence at index " + cursor ,
128- new String (chars ), cursor );
129- }
130- }
131139
132- cursor ++;
140+ cursor ++;
141+ }
133142 }
134143 }
144+ catch (ParameterizedPatternSyntaxException e )
145+ {
146+ throw e ;
147+ }
148+ catch (Exception e )
149+ {
150+ throw new ParameterizedPatternSyntaxException ("Error parsing parameterized pattern due to: " + e .getMessage (),
151+ pattern , cursor );
152+ }
135153 }
136154
137155 @ Override
@@ -155,14 +173,14 @@ public Pattern getCompiledPattern(ParameterStore store)
155173 {
156174 patternBuilder .append (REGEX_ESCAPE_BEGIN );
157175 String literal = String .valueOf (Arrays .copyOfRange (chars , last .getEnd () + 1 , capture .getStart ()));
158- patternBuilder .append (unescapeBackslashes (literal ));
176+ patternBuilder .append (unescape (literal ));
159177 patternBuilder .append (REGEX_ESCAPE_END );
160178 }
161179 else if ((last == null ) && (capture .getStart () > 0 ))
162180 {
163181 patternBuilder .append (REGEX_ESCAPE_BEGIN );
164182 String literal = String .valueOf (Arrays .copyOfRange (chars , 0 , capture .getStart ()));
165- patternBuilder .append (unescapeBackslashes (literal ));
183+ patternBuilder .append (unescape (literal ));
166184 patternBuilder .append (REGEX_ESCAPE_END );
167185 }
168186
@@ -204,13 +222,13 @@ else if ((last == null) && (capture.getStart() > 0))
204222 {
205223 patternBuilder .append (REGEX_ESCAPE_BEGIN );
206224 String literal = String .valueOf (Arrays .copyOfRange (chars , last .getEnd () + 1 , chars .length ));
207- patternBuilder .append (unescapeBackslashes (literal ));
225+ patternBuilder .append (unescape (literal ));
208226 patternBuilder .append (REGEX_ESCAPE_END );
209227 }
210228 else if (last == null )
211229 {
212230 patternBuilder .append (REGEX_ESCAPE_BEGIN );
213- patternBuilder .append (unescapeBackslashes (String .valueOf (chars )));
231+ patternBuilder .append (unescape (String .valueOf (chars )));
214232 patternBuilder .append (REGEX_ESCAPE_END );
215233 }
216234
@@ -219,9 +237,11 @@ else if (last == null)
219237 return compiledPattern ;
220238 }
221239
222- private String unescapeBackslashes (String literal )
240+ private String unescape (String literal )
223241 {
224- return literal .replace ("\\ \\ " , "\\ " );
242+ String result = literal .replace ("\\ \\ " , "\\ " );
243+ result = result .replace ("\\ " + type .getBegin (), String .valueOf (type .getBegin ()));
244+ return result ;
225245 }
226246
227247 @ Override
0 commit comments