Skip to content

Commit

Permalink
generate capturing groups during comment building
Browse files Browse the repository at this point in the history
  • Loading branch information
DarthKipsu committed Oct 21, 2015
1 parent 5a952fd commit cf7c78c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,26 @@
public class CommentSyntax {

private final String beginSolutionRegex;
private final String capturingGroups;
private final String endSolutionRegex;
private final String solutionFileRegex;
private final String stubRegex;
private final Pattern stubReplacePattern;

private CommentSyntax(
String beginSolutionRegex,
String capturingGroups,
String endSolutionRegex,
String solutionFileRegex,
String stubRegex) {
this.beginSolutionRegex = beginSolutionRegex;
this.capturingGroups = capturingGroups;
this.endSolutionRegex = endSolutionRegex;
this.solutionFileRegex = solutionFileRegex;
this.stubRegex = stubRegex;

this.stubReplacePattern = Pattern.compile(stubRegex);
}
}

/**
* Will create a new Comment syntax with the specified comment syntaxes.
Expand All @@ -43,6 +46,10 @@ public String getBeginSolution() {
return beginSolutionRegex;
}

public String getCapturingGroups() {
return capturingGroups;
}

public String getEndSolution() {
return endSolutionRegex;
}
Expand Down Expand Up @@ -70,13 +77,22 @@ public static class Builder {
private static final String SPACES = "[ \\t]*";
private static final String STUB = "STUB:[ \\t]*";

private String beginCapturingGroups;
private String beginSolutionRegex;
private int capturingIndex;
private String endCapturingGroups;
private String endSolutionRegex;
private String solutionFileRegex;
private String stubRegex;

/**
* Creates a new CommentSyntac Builder.
*/
public Builder() {
beginCapturingGroups = "";
beginSolutionRegex = "";
capturingIndex = 2;
endCapturingGroups = "";
endSolutionRegex = "";
solutionFileRegex = "";
stubRegex = "";
Expand All @@ -92,11 +108,12 @@ public Builder addSingleLineComment(String singleLine) {
}
String lineStart = "((" + SPACES + ")" + singleLine + SPACES;

beginSolutionRegex += lineStart + BEGIN_SOLUTION + ".*)";
endSolutionRegex += lineStart + END_SOLUTION + ".*)";
solutionFileRegex += lineStart + SOLUTION_FILE + ".*)";
beginSolutionRegex += lineStart + BEGIN_SOLUTION + "(.*))";
endSolutionRegex += lineStart + END_SOLUTION + "(.*))";
solutionFileRegex += lineStart + SOLUTION_FILE + "(.*))";
stubRegex += lineStart + STUB + "(.*))";

increaseCapturingGroups();
return this;
}

Expand All @@ -119,6 +136,7 @@ public Builder addMultiLineComment(String beginning,
solutionFileRegex += beginComment + SOLUTION_FILE + endComment;
stubRegex += beginComment + STUB + "(.*[^ \\t])" + endComment;

increaseCapturingGroups();
return this;
}

Expand All @@ -133,10 +151,17 @@ public CommentSyntax build() {
}
return new CommentSyntax(
beginSolutionRegex,
beginCapturingGroups + endCapturingGroups,
endSolutionRegex,
solutionFileRegex,
stubRegex);
}

private void increaseCapturingGroups() {
beginCapturingGroups += "$" + capturingIndex++;
endCapturingGroups += "$" + capturingIndex++;
capturingIndex++;
}

private void addOrPunctuations() {
beginSolutionRegex += "|";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class ExerciseBuilder {
private static final Logger logger = LoggerFactory.getLogger(ExerciseBuilder.class);

private String beginSolutionRegex;
private String capturingGroups;
private String endSolutionRegex;
private String solutionFileRegex;
private String stubRegex;
Expand All @@ -32,6 +33,7 @@ public ExerciseBuilder() {

public ExerciseBuilder(CommentSyntax commentSyntax) {
beginSolutionRegex = commentSyntax.getBeginSolution();
capturingGroups = commentSyntax.getCapturingGroups();
endSolutionRegex = commentSyntax.getEndSolution();
solutionFileRegex = commentSyntax.getSolutionFile();
stubRegex = commentSyntax.getStub();
Expand Down Expand Up @@ -68,7 +70,7 @@ private void prepareStubFile(Path file) {
skipLine = false;
} else if (line.matches(stubRegex)) {
Matcher stubMatcher = stubReplacePattern.matcher(line);
filteredLines.add(stubMatcher.replaceAll("$2$5$3$6"));
filteredLines.add(stubMatcher.replaceAll(capturingGroups));
} else if (!skipLine) {
filteredLines.add(line);
}
Expand Down

0 comments on commit cf7c78c

Please sign in to comment.