Author: julien_viet
Date: 2010-11-27 07:47:01 -0500 (Sat, 27 Nov 2010)
New Revision: 5308
Modified:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/RENode.java
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/RegExpParser.java
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/regexp/TestParser.java
Log:
support for non capturing group in parser
Modified:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/RENode.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/RENode.java 2010-11-27
12:27:16 UTC (rev 5307)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/RENode.java 2010-11-27
12:47:01 UTC (rev 5308)
@@ -220,13 +220,17 @@
/** . */
private final Disjunction disjunction;
- public Group(Disjunction disjunction)
+ /** . */
+ private final boolean capturing;
+
+ public Group(Disjunction disjunction, boolean capturing)
{
if (disjunction == null)
{
throw new NullPointerException();
}
this.disjunction = disjunction;
+ this.capturing = capturing;
}
public Disjunction getDisjunction()
@@ -234,10 +238,15 @@
return disjunction;
}
+ public boolean isCapturing()
+ {
+ return capturing;
+ }
+
@Override
protected void writeTo(StringBuilder sb)
{
-
sb.append("<(>").append(disjunction).append("</)>");
+ sb.append("<").append(capturing ? "(" :
"(?:").append('>').append(disjunction).append("</").append(capturing
? ")" : ":?)").append(">");
}
}
Modified:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/RegExpParser.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/RegExpParser.java 2010-11-27
12:27:16 UTC (rev 5307)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/RegExpParser.java 2010-11-27
12:47:01 UTC (rev 5308)
@@ -147,14 +147,43 @@
index++;
break;
case '(':
- int closingParenthesis = findClosing(index, to, '(', ')');
- if (closingParenthesis == -1)
+ int endGroup = findClosing(index, to, '(', ')');
+ if (endGroup == -1)
{
throw new SyntaxException();
}
- RENode.Disjunction grouped = new RegExpParser(s, index + 1,
closingParenthesis).parseDisjunction();
- exp = new RENode.Group(grouped);
- index = closingParenthesis + 1;
+
+ // Do we have a special construct ?
+ int startGroup = index + 1;
+ boolean capturing = true;
+ if (startGroup < endGroup)
+ {
+ if (s.charAt(startGroup) == '?')
+ {
+ if (startGroup + 1 < endGroup)
+ {
+ if (s.charAt(startGroup + 1) == ':')
+ {
+ // It's a non capturing group, so it's ok
+ startGroup += 2;
+ capturing = false;
+ }
+ else
+ {
+ throw createSyntaxException("Only non capturing group syntax
is supported", index + 1, index + 3);
+ }
+ }
+ else
+ {
+ throw createSyntaxException("Group containing a single question
mark are not allowed", index, index + 2);
+ }
+ }
+ }
+
+ //
+ RENode.Disjunction grouped = new RegExpParser(s, startGroup,
endGroup).parseDisjunction();
+ exp = new RENode.Group(grouped, capturing);
+ index = endGroup + 1;
break;
case '*':
case '+':
Modified:
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/regexp/TestParser.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/regexp/TestParser.java 2010-11-27
12:27:16 UTC (rev 5307)
+++
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/regexp/TestParser.java 2010-11-27
12:47:01 UTC (rev 5308)
@@ -50,14 +50,14 @@
{
this.parser = new RegExpParser(s, from, to);
}
- ParserTester assertParseBracketExpression(String expectedValue)
+ ParserTester assertParseCharacterClass(String expectedValue)
{
RENode node = parser.parseExpression();
assertTrue(node instanceof RENode.CharacterClass);
assertEquals(expectedValue, node.toString());
return this;
}
- ParserTester assertParseExtendedRegExp(String expectedValue)
+ ParserTester assertParseDisjunction(String expectedValue)
{
int expectedIndex = parser.getTo();
RENode.Disjunction disjunction = parser.parseDisjunction();
@@ -65,7 +65,7 @@
assertEquals(expectedIndex, parser.getIndex());
return this;
}
- ParserTester assertNotParseExtendedRegExp()
+ ParserTester assertNotParseDisjunction()
{
int expectedIndex = parser.getIndex();
try
@@ -86,7 +86,7 @@
assertEquals(expectedIndex, parser.getIndex());
return this;
}
- ParserTester assertNotParseEREExpression()
+ ParserTester assertNotParseExpression()
{
int index = parser.getIndex();
try
@@ -124,16 +124,16 @@
public void testExtendedRegexp()
{
- new ParserTester("^").assertParseExtendedRegExp("<^/>");
- new
ParserTester("^$").assertParseExtendedRegExp("<^/><$/>");
- new
ParserTester("a").assertParseExtendedRegExp("<c>a</c>");
- new
ParserTester("a|b").assertParseExtendedRegExp("<c>a</c>|<c>b</c>");
- new
ParserTester("a+|b*").assertParseExtendedRegExp("<+><c>a</c></+>|<*><c>b</c></*>");
+ new ParserTester("^").assertParseDisjunction("<^/>");
+ new
ParserTester("^$").assertParseDisjunction("<^/><$/>");
+ new
ParserTester("a").assertParseDisjunction("<c>a</c>");
+ new
ParserTester("a|b").assertParseDisjunction("<c>a</c>|<c>b</c>");
+ new
ParserTester("a+|b*").assertParseDisjunction("<+><c>a</c></+>|<*><c>b</c></*>");
}
public void testExpression()
{
- new ParserTester("").assertNotParseEREExpression();
+ new ParserTester("").assertNotParseExpression();
new ParserTester("^").assertParseExpression("<^/>", 1);
new
ParserTester("^+").assertParseExpression("<+><^/></+>",
2);
new ParserTester("$").assertParseExpression("<$/>", 1);
@@ -144,13 +144,20 @@
new
ParserTester(".+").assertParseExpression("<+><./></+>",
2);
new
ParserTester("\\+").assertParseExpression("<c>+</c>", 2);
new
ParserTester("\\++").assertParseExpression("<+><c>+</c></+>",
3);
- new ParserTester("*").assertNotParseEREExpression();
- new ParserTester("+").assertNotParseEREExpression();
- new ParserTester("?").assertNotParseEREExpression();
- new ParserTester("{").assertNotParseEREExpression();
- new ParserTester("|").assertNotParseEREExpression();
+ new ParserTester("*").assertNotParseExpression();
+ new ParserTester("+").assertNotParseExpression();
+ new ParserTester("?").assertNotParseExpression();
+ new ParserTester("{").assertNotParseExpression();
+ new ParserTester("|").assertNotParseExpression();
+ }
+
+ public void testGroup()
+ {
new
ParserTester("(a)").assertParseExpression("<(><c>a</c></)>",
3);
new
ParserTester("(a(b)c)").assertParseExpression("<(><c>a</c><(><c>b</c></)><c>c</c></)>",
7);
+ new
ParserTester("(?:a)").assertParseExpression("<(?:><c>a</c></:?)>",
5);
+ new ParserTester("(?)").assertNotParseExpression();
+ new ParserTester("(?a)").assertNotParseExpression();
}
// missing stuff:
@@ -180,22 +187,22 @@
public void testParseBracketExpression()
{
- new ParserTester("[a]").assertParseBracketExpression("[a]");
- new
ParserTester("[^a]").assertParseBracketExpression("[^[a]]");
- new
ParserTester("[^a-b]").assertParseBracketExpression("[^[a-b]]");
- new
ParserTester("[a-b]").assertParseBracketExpression("[a-b]");
- new
ParserTester("[ab]").assertParseBracketExpression("[[a][b]]");
- new
ParserTester("[a&]").assertParseBracketExpression("[[a][&]]");
- new
ParserTester("[a&&b]").assertParseBracketExpression("[[a]&&[b]]");
- new
ParserTester("[a&&[^b]]").assertParseBracketExpression("[[a]&&[^[b]]]");
- new
ParserTester("[a[^b]]").assertParseBracketExpression("[[a][^[b]]]");
- new
ParserTester("[a[b]]").assertParseBracketExpression("[[a][b]]");
- new
ParserTester("[a[b]c]").assertParseBracketExpression("[[a][[b][c]]]");
- new
ParserTester("[[a]bc]").assertParseBracketExpression("[[a][[b][c]]]");
- new ParserTester("[-]").assertParseBracketExpression("[-]");
- new
ParserTester("[a-]").assertParseBracketExpression("[[a][-]]");
- new
ParserTester("[---]").assertParseBracketExpression("[---]");
- new
ParserTester("[#--]").assertParseBracketExpression("[#--]");
+ new ParserTester("[a]").assertParseCharacterClass("[a]");
+ new ParserTester("[^a]").assertParseCharacterClass("[^[a]]");
+ new
ParserTester("[^a-b]").assertParseCharacterClass("[^[a-b]]");
+ new ParserTester("[a-b]").assertParseCharacterClass("[a-b]");
+ new
ParserTester("[ab]").assertParseCharacterClass("[[a][b]]");
+ new
ParserTester("[a&]").assertParseCharacterClass("[[a][&]]");
+ new
ParserTester("[a&&b]").assertParseCharacterClass("[[a]&&[b]]");
+ new
ParserTester("[a&&[^b]]").assertParseCharacterClass("[[a]&&[^[b]]]");
+ new
ParserTester("[a[^b]]").assertParseCharacterClass("[[a][^[b]]]");
+ new
ParserTester("[a[b]]").assertParseCharacterClass("[[a][b]]");
+ new
ParserTester("[a[b]c]").assertParseCharacterClass("[[a][[b][c]]]");
+ new
ParserTester("[[a]bc]").assertParseCharacterClass("[[a][[b][c]]]");
+ new ParserTester("[-]").assertParseCharacterClass("[-]");
+ new
ParserTester("[a-]").assertParseCharacterClass("[[a][-]]");
+ new ParserTester("[---]").assertParseCharacterClass("[---]");
+ new ParserTester("[#--]").assertParseCharacterClass("[#--]");
}
}