Skip to content

Commit 8824b3a

Browse files
authored
Merge pull request #9 from sgreben/fix-complement
Fix complement, support pattern flags
2 parents 47f7c1d + 979f3c0 commit 8824b3a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+553
-284
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
22
/settings.xml
33
/gpg-params
44
/gpg-home
5+
/.project
6+
/.classpath
7+
/.settings
8+
/.vscode

README.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ There's a [discussion](https://www.reddit.com/r/java/comments/4tyk90/github_sgre
2424
<dependency>
2525
<groupId>com.github.sgreben</groupId>
2626
<artifactId>regex-builder</artifactId>
27-
<version>1.0.2</version>
27+
<version>1.1.0</version>
2828
</dependency>
2929
```
3030

@@ -254,7 +254,7 @@ public static boolean sameIP(String twoLogs) {
254254
### Expression builder
255255

256256
| Builder method | `java.util.regex` syntax |
257-
|-----------------------------|--------------------------|
257+
| --------------------------- | ------------------------ |
258258
| repeat(e, N) | e{N} |
259259
| repeat(e) | e* |
260260
| repeat(e).possessive() | e*+ |
@@ -284,25 +284,25 @@ public static boolean sameIP(String twoLogs) {
284284

285285
### CharClass builder
286286

287-
| Builder method | `java.util.regex` syntax |
288-
|---------------------------------------|--------------------------|
289-
| range(from, to) | [from-to] |
290-
| range(f1, t1, ..., fN, tN) | [f1-t1f2-t2...fN-tN] |
291-
| oneOf("abcde") | [abcde] |
292-
| union(class1, ..., classN) | [[class1]...[classN]] |
293-
| complement(class1) | [\^[class1]] |
294-
| anyChar() | . |
295-
| digit() | \d |
296-
| nonDigit() | \D |
297-
| hexDigit() | [a-fA-F0-9] |
298-
| nonHexDigit() | [\^[a-fA-F0-9]] |
299-
| wordChar() | \w |
300-
| nonWordChar() | \W |
301-
| wordBoundary() | \b |
302-
| nonWordBoundary() | \B |
303-
| whitespaceChar() | \s |
304-
| nonWhitespaceChar() | \S |
305-
| verticalWhitespaceChar() | \v |
306-
| nonVerticalWhitespaceChar() | \V |
307-
| horizontalWhitespaceChar() | \h |
308-
| nonHorizontalWhitespaceChar()| \H |
287+
| Builder method | `java.util.regex` syntax |
288+
| ----------------------------- | ------------------------ |
289+
| range(from, to) | [from-to] |
290+
| range(f1, t1, ..., fN, tN) | [f1-t1f2-t2...fN-tN] |
291+
| oneOf("abcde") | [abcde] |
292+
| union(class1, ..., classN) | [[class1]...[classN]] |
293+
| complement(class1) | [\^class1] |
294+
| anyChar() | . |
295+
| digit() | \d |
296+
| nonDigit() | \D |
297+
| hexDigit() | [a-fA-F0-9] |
298+
| nonHexDigit() | [\^a-fA-F0-9] |
299+
| wordChar() | \w |
300+
| nonWordChar() | \W |
301+
| wordBoundary() | \b |
302+
| nonWordBoundary() | \B |
303+
| whitespaceChar() | \s |
304+
| nonWhitespaceChar() | \S |
305+
| verticalWhitespaceChar() | \v |
306+
| nonVerticalWhitespaceChar() | \V |
307+
| horizontalWhitespaceChar() | \h |
308+
| nonHorizontalWhitespaceChar() | \H |

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>com.github.sgreben</groupId>
55
<artifactId>regex-builder</artifactId>
66
<packaging>jar</packaging>
7-
<version>1.0.2</version>
7+
<version>1.1.0</version>
88
<name>${project.groupId}:${project.artifactId}</name>
99
<description>Construct regular expressions as pure Java code.</description>
1010
<url>https://github.com/sgreben/regex-builder/</url>

src/main/java/com/github/sgreben/regex_builder/CaptureGroup.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/**
99
* A regex capture group "(...)"
1010
*/
11-
public class CaptureGroup extends Unary implements Expression {
11+
public class CaptureGroup extends Unary {
1212

1313
public CaptureGroup(Expression expression) {
1414
super(expression);
@@ -22,4 +22,4 @@ public void compile(CaptureGroupIndex index, java.util.List<TOKEN> output) {
2222
}
2323
output.add(new END_GROUP());
2424
}
25-
}
25+
}

src/main/java/com/github/sgreben/regex_builder/CharClass.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ private static CharClass[] convertStrings(Object[] os) {
133133

134134
public abstract void accept(CharClassVisitor visitor);
135135

136+
public abstract CharClass complement();
137+
136138
public abstract void compile(java.util.List<TOKEN> output);
137139

138140
public static class Posix {
@@ -210,4 +212,4 @@ public static CharClass Mirrored() {
210212
return new com.github.sgreben.regex_builder.charclass.Java("javaMirrored");
211213
}
212214
}
213-
}
215+
}

src/main/java/com/github/sgreben/regex_builder/Pattern.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,51 @@
55
public class Pattern {
66
private final java.util.regex.Pattern rawPattern;
77
private final CaptureGroupIndex groupIndex;
8-
8+
99
public static Pattern compile(Expression expression) {
1010
return Compiler.compile(expression);
1111
}
12-
12+
13+
public static Pattern compile(Expression expression, int flags) {
14+
return Compiler.compile(expression, flags);
15+
}
16+
1317
public static Pattern quote(String literal) {
1418
return Compiler.compile(Re.string(literal));
1519
}
16-
17-
public Pattern(java.util.regex.Pattern rawPattern,
18-
CaptureGroupIndex groupIndex) {
20+
21+
public Pattern(java.util.regex.Pattern rawPattern, CaptureGroupIndex groupIndex) {
1922
this.rawPattern = rawPattern;
2023
this.groupIndex = groupIndex;
2124
}
22-
25+
2326
public Matcher matcher(CharSequence input) {
2427
java.util.regex.Matcher matcher = rawPattern.matcher(input);
2528
return new Matcher(matcher, groupIndex);
2629
}
27-
30+
2831
public static boolean matches(Expression regex, CharSequence input) {
2932
return compile(regex).matcher(input).matches();
3033
}
31-
34+
3235
public String[] split(CharSequence input) {
3336
return rawPattern.split(input);
3437
}
35-
38+
3639
public String[] split(CharSequence input, int limit) {
3740
return rawPattern.split(input, limit);
3841
}
39-
42+
4043
public java.util.stream.Stream<String> splitAsStream(CharSequence input) {
4144
return rawPattern.splitAsStream(input);
4245
}
43-
46+
4447
public String pattern() {
4548
return rawPattern.pattern();
4649
}
47-
50+
4851
@Override
4952
public String toString() {
5053
return rawPattern.toString();
5154
}
52-
}
55+
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
package com.github.sgreben.regex_builder.charclass;
22

3+
import com.github.sgreben.regex_builder.CharClass;
34
import com.github.sgreben.regex_builder.tokens.DOT;
45
import com.github.sgreben.regex_builder.tokens.TOKEN;
56

67
public class AnyCharacter extends Nullary {
8+
@Override
9+
public CharClass complement() {
10+
return oneOf("");
11+
}
12+
13+
@Override
714
public void compile(java.util.List<TOKEN> output) {
815
output.add(new DOT());
916
}
10-
}
17+
}
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
package com.github.sgreben.regex_builder.charclass;
22

3+
import com.github.sgreben.regex_builder.CharClass;
34
import com.github.sgreben.regex_builder.tokens.RAW;
45
import com.github.sgreben.regex_builder.tokens.TOKEN;
56

67
public class BeginInput extends Nullary {
7-
public BeginInput() {}
8+
public BeginInput() {
9+
}
10+
11+
@Override
12+
public CharClass complement() {
13+
return new RawComplement(this);
14+
}
15+
16+
@Override
817
public void compile(java.util.List<TOKEN> output) {
918
output.add(new RAW("\\A"));
1019
}
11-
}
20+
}
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
package com.github.sgreben.regex_builder.charclass;
22

3-
import com.github.sgreben.regex_builder.CharClass;
4-
5-
import java.util.List;
6-
import java.util.LinkedList;
7-
import java.lang.Iterable;
83
import java.util.Collections;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import com.github.sgreben.regex_builder.CharClass;
97

108
abstract class Binary extends CharClassBase {
119
private final List<CharClass> children;
12-
10+
1311
public Binary(CharClass leftChild, CharClass rightChild) {
1412
List<CharClass> children = new LinkedList<CharClass>();
1513
children.add(leftChild);
1614
children.add(rightChild);
1715
this.children = Collections.unmodifiableList(children);
1816
}
19-
17+
18+
@Override
2019
public Iterable<CharClass> children() {
2120
return children;
2221
}
23-
}
22+
}

src/main/java/com/github/sgreben/regex_builder/charclass/CharClassBase.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import com.github.sgreben.regex_builder.CharClass;
44

55
public abstract class CharClassBase extends CharClass {
6+
@Override
67
public void accept(CharClassVisitor visitor) {
78
visitor.visitPre(this);
8-
for(CharClass child : children ()) {
9+
for (CharClass child : children()) {
910
child.accept(visitor);
1011
}
1112
visitor.visitPost(this);
1213
}
13-
}
14+
}
Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
package com.github.sgreben.regex_builder.charclass;
22

3-
import com.github.sgreben.regex_builder.tokens.CARET;
4-
import com.github.sgreben.regex_builder.tokens.END_CHAR_CLASS;
5-
import com.github.sgreben.regex_builder.tokens.START_CHAR_CLASS;
6-
import com.github.sgreben.regex_builder.tokens.TOKEN;
73
import com.github.sgreben.regex_builder.CharClass;
4+
import com.github.sgreben.regex_builder.tokens.TOKEN;
85

96
public class Complement extends Unary {
10-
public Complement(CharClass child) { super(child); }
11-
public void compile(java.util.List<TOKEN> output) {
12-
output.add(new START_CHAR_CLASS());
13-
output.add(new CARET());
14-
for(CharClass child : children()) {
15-
child.compile(output);
16-
}
17-
output.add(new END_CHAR_CLASS());
7+
final CharClass child;
8+
9+
public Complement(final CharClass child) {
10+
super(child);
11+
this.child = child;
12+
}
13+
14+
@Override
15+
public CharClass complement() {
16+
return child;
17+
}
18+
19+
@Override
20+
public void compile(final java.util.List<TOKEN> output) {
21+
child.complement().compile(output);
1822
}
1923
}
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
package com.github.sgreben.regex_builder.charclass;
22

3+
import com.github.sgreben.regex_builder.CharClass;
34
import com.github.sgreben.regex_builder.tokens.RAW;
45
import com.github.sgreben.regex_builder.tokens.TOKEN;
56

67
public class Digit extends Nullary {
7-
public Digit() {}
8+
public Digit() {
9+
}
10+
11+
@Override
12+
public CharClass complement() {
13+
return new NonDigit();
14+
}
15+
16+
@Override
817
public void compile(java.util.List<TOKEN> output) {
918
output.add(new RAW("\\d"));
1019
}
11-
}
20+
}
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
package com.github.sgreben.regex_builder.charclass;
22

3+
import com.github.sgreben.regex_builder.CharClass;
34
import com.github.sgreben.regex_builder.tokens.RAW;
45
import com.github.sgreben.regex_builder.tokens.TOKEN;
56

67
public class EndInput extends Nullary {
7-
public EndInput() {}
8+
public EndInput() {
9+
}
10+
11+
@Override
12+
public CharClass complement() {
13+
return new RawComplement(this);
14+
}
15+
16+
@Override
817
public void compile(java.util.List<TOKEN> output) {
918
output.add(new RAW("\\z"));
1019
}
11-
}
20+
}
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
package com.github.sgreben.regex_builder.charclass;
22

3+
import com.github.sgreben.regex_builder.CharClass;
34
import com.github.sgreben.regex_builder.tokens.RAW;
45
import com.github.sgreben.regex_builder.tokens.TOKEN;
56

67
public class EndInputBeforeFinalTerminator extends Nullary {
7-
public EndInputBeforeFinalTerminator() {}
8+
public EndInputBeforeFinalTerminator() {
9+
}
10+
11+
@Override
12+
public CharClass complement() {
13+
return new RawComplement(this);
14+
}
15+
16+
@Override
817
public void compile(java.util.List<TOKEN> output) {
918
output.add(new RAW("\\z"));
1019
}
11-
}
20+
}

0 commit comments

Comments
 (0)