Skip to content

Commit

Permalink
Merge pull request #9 from sgreben/fix-complement
Browse files Browse the repository at this point in the history
Fix complement, support pattern flags
  • Loading branch information
sgreben authored May 4, 2020
2 parents 47f7c1d + 979f3c0 commit 8824b3a
Show file tree
Hide file tree
Showing 45 changed files with 553 additions and 284 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
/settings.xml
/gpg-params
/gpg-home
/.project
/.classpath
/.settings
/.vscode
48 changes: 24 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ There's a [discussion](https://www.reddit.com/r/java/comments/4tyk90/github_sgre
<dependency>
<groupId>com.github.sgreben</groupId>
<artifactId>regex-builder</artifactId>
<version>1.0.2</version>
<version>1.1.0</version>
</dependency>
```

Expand Down Expand Up @@ -254,7 +254,7 @@ public static boolean sameIP(String twoLogs) {
### Expression builder

| Builder method | `java.util.regex` syntax |
|-----------------------------|--------------------------|
| --------------------------- | ------------------------ |
| repeat(e, N) | e{N} |
| repeat(e) | e* |
| repeat(e).possessive() | e*+ |
Expand Down Expand Up @@ -284,25 +284,25 @@ public static boolean sameIP(String twoLogs) {

### CharClass builder

| Builder method | `java.util.regex` syntax |
|---------------------------------------|--------------------------|
| range(from, to) | [from-to] |
| range(f1, t1, ..., fN, tN) | [f1-t1f2-t2...fN-tN] |
| oneOf("abcde") | [abcde] |
| union(class1, ..., classN) | [[class1]...[classN]] |
| complement(class1) | [\^[class1]] |
| anyChar() | . |
| digit() | \d |
| nonDigit() | \D |
| hexDigit() | [a-fA-F0-9] |
| nonHexDigit() | [\^[a-fA-F0-9]] |
| wordChar() | \w |
| nonWordChar() | \W |
| wordBoundary() | \b |
| nonWordBoundary() | \B |
| whitespaceChar() | \s |
| nonWhitespaceChar() | \S |
| verticalWhitespaceChar() | \v |
| nonVerticalWhitespaceChar() | \V |
| horizontalWhitespaceChar() | \h |
| nonHorizontalWhitespaceChar()| \H |
| Builder method | `java.util.regex` syntax |
| ----------------------------- | ------------------------ |
| range(from, to) | [from-to] |
| range(f1, t1, ..., fN, tN) | [f1-t1f2-t2...fN-tN] |
| oneOf("abcde") | [abcde] |
| union(class1, ..., classN) | [[class1]...[classN]] |
| complement(class1) | [\^class1] |
| anyChar() | . |
| digit() | \d |
| nonDigit() | \D |
| hexDigit() | [a-fA-F0-9] |
| nonHexDigit() | [\^a-fA-F0-9] |
| wordChar() | \w |
| nonWordChar() | \W |
| wordBoundary() | \b |
| nonWordBoundary() | \B |
| whitespaceChar() | \s |
| nonWhitespaceChar() | \S |
| verticalWhitespaceChar() | \v |
| nonVerticalWhitespaceChar() | \V |
| horizontalWhitespaceChar() | \h |
| nonHorizontalWhitespaceChar() | \H |
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.github.sgreben</groupId>
<artifactId>regex-builder</artifactId>
<packaging>jar</packaging>
<version>1.0.2</version>
<version>1.1.0</version>
<name>${project.groupId}:${project.artifactId}</name>
<description>Construct regular expressions as pure Java code.</description>
<url>https://github.com/sgreben/regex-builder/</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* A regex capture group "(...)"
*/
public class CaptureGroup extends Unary implements Expression {
public class CaptureGroup extends Unary {

public CaptureGroup(Expression expression) {
super(expression);
Expand All @@ -22,4 +22,4 @@ public void compile(CaptureGroupIndex index, java.util.List<TOKEN> output) {
}
output.add(new END_GROUP());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ private static CharClass[] convertStrings(Object[] os) {

public abstract void accept(CharClassVisitor visitor);

public abstract CharClass complement();

public abstract void compile(java.util.List<TOKEN> output);

public static class Posix {
Expand Down Expand Up @@ -210,4 +212,4 @@ public static CharClass Mirrored() {
return new com.github.sgreben.regex_builder.charclass.Java("javaMirrored");
}
}
}
}
29 changes: 16 additions & 13 deletions src/main/java/com/github/sgreben/regex_builder/Pattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,51 @@
public class Pattern {
private final java.util.regex.Pattern rawPattern;
private final CaptureGroupIndex groupIndex;

public static Pattern compile(Expression expression) {
return Compiler.compile(expression);
}


public static Pattern compile(Expression expression, int flags) {
return Compiler.compile(expression, flags);
}

public static Pattern quote(String literal) {
return Compiler.compile(Re.string(literal));
}

public Pattern(java.util.regex.Pattern rawPattern,
CaptureGroupIndex groupIndex) {

public Pattern(java.util.regex.Pattern rawPattern, CaptureGroupIndex groupIndex) {
this.rawPattern = rawPattern;
this.groupIndex = groupIndex;
}

public Matcher matcher(CharSequence input) {
java.util.regex.Matcher matcher = rawPattern.matcher(input);
return new Matcher(matcher, groupIndex);
}

public static boolean matches(Expression regex, CharSequence input) {
return compile(regex).matcher(input).matches();
}

public String[] split(CharSequence input) {
return rawPattern.split(input);
}

public String[] split(CharSequence input, int limit) {
return rawPattern.split(input, limit);
}

public java.util.stream.Stream<String> splitAsStream(CharSequence input) {
return rawPattern.splitAsStream(input);
}

public String pattern() {
return rawPattern.pattern();
}

@Override
public String toString() {
return rawPattern.toString();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package com.github.sgreben.regex_builder.charclass;

import com.github.sgreben.regex_builder.CharClass;
import com.github.sgreben.regex_builder.tokens.DOT;
import com.github.sgreben.regex_builder.tokens.TOKEN;

public class AnyCharacter extends Nullary {
@Override
public CharClass complement() {
return oneOf("");
}

@Override
public void compile(java.util.List<TOKEN> output) {
output.add(new DOT());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package com.github.sgreben.regex_builder.charclass;

import com.github.sgreben.regex_builder.CharClass;
import com.github.sgreben.regex_builder.tokens.RAW;
import com.github.sgreben.regex_builder.tokens.TOKEN;

public class BeginInput extends Nullary {
public BeginInput() {}
public BeginInput() {
}

@Override
public CharClass complement() {
return new RawComplement(this);
}

@Override
public void compile(java.util.List<TOKEN> output) {
output.add(new RAW("\\A"));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package com.github.sgreben.regex_builder.charclass;

import com.github.sgreben.regex_builder.CharClass;

import java.util.List;
import java.util.LinkedList;
import java.lang.Iterable;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import com.github.sgreben.regex_builder.CharClass;

abstract class Binary extends CharClassBase {
private final List<CharClass> children;

public Binary(CharClass leftChild, CharClass rightChild) {
List<CharClass> children = new LinkedList<CharClass>();
children.add(leftChild);
children.add(rightChild);
this.children = Collections.unmodifiableList(children);
}


@Override
public Iterable<CharClass> children() {
return children;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import com.github.sgreben.regex_builder.CharClass;

public abstract class CharClassBase extends CharClass {
@Override
public void accept(CharClassVisitor visitor) {
visitor.visitPre(this);
for(CharClass child : children ()) {
for (CharClass child : children()) {
child.accept(visitor);
}
visitor.visitPost(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package com.github.sgreben.regex_builder.charclass;

import com.github.sgreben.regex_builder.tokens.CARET;
import com.github.sgreben.regex_builder.tokens.END_CHAR_CLASS;
import com.github.sgreben.regex_builder.tokens.START_CHAR_CLASS;
import com.github.sgreben.regex_builder.tokens.TOKEN;
import com.github.sgreben.regex_builder.CharClass;
import com.github.sgreben.regex_builder.tokens.TOKEN;

public class Complement extends Unary {
public Complement(CharClass child) { super(child); }
public void compile(java.util.List<TOKEN> output) {
output.add(new START_CHAR_CLASS());
output.add(new CARET());
for(CharClass child : children()) {
child.compile(output);
}
output.add(new END_CHAR_CLASS());
final CharClass child;

public Complement(final CharClass child) {
super(child);
this.child = child;
}

@Override
public CharClass complement() {
return child;
}

@Override
public void compile(final java.util.List<TOKEN> output) {
child.complement().compile(output);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package com.github.sgreben.regex_builder.charclass;

import com.github.sgreben.regex_builder.CharClass;
import com.github.sgreben.regex_builder.tokens.RAW;
import com.github.sgreben.regex_builder.tokens.TOKEN;

public class Digit extends Nullary {
public Digit() {}
public Digit() {
}

@Override
public CharClass complement() {
return new NonDigit();
}

@Override
public void compile(java.util.List<TOKEN> output) {
output.add(new RAW("\\d"));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package com.github.sgreben.regex_builder.charclass;

import com.github.sgreben.regex_builder.CharClass;
import com.github.sgreben.regex_builder.tokens.RAW;
import com.github.sgreben.regex_builder.tokens.TOKEN;

public class EndInput extends Nullary {
public EndInput() {}
public EndInput() {
}

@Override
public CharClass complement() {
return new RawComplement(this);
}

@Override
public void compile(java.util.List<TOKEN> output) {
output.add(new RAW("\\z"));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package com.github.sgreben.regex_builder.charclass;

import com.github.sgreben.regex_builder.CharClass;
import com.github.sgreben.regex_builder.tokens.RAW;
import com.github.sgreben.regex_builder.tokens.TOKEN;

public class EndInputBeforeFinalTerminator extends Nullary {
public EndInputBeforeFinalTerminator() {}
public EndInputBeforeFinalTerminator() {
}

@Override
public CharClass complement() {
return new RawComplement(this);
}

@Override
public void compile(java.util.List<TOKEN> output) {
output.add(new RAW("\\z"));
}
}
}
Loading

0 comments on commit 8824b3a

Please sign in to comment.