mirror of
https://github.com/corda/corda.git
synced 2025-01-19 11:16:54 +00:00
Regex: implement alternatives
Now we support regular expressions like 'A|B|C'. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
This commit is contained in:
parent
c3a06a600a
commit
d4a2f58eb5
@ -57,5 +57,7 @@ public class Regex {
|
|||||||
expectMatch("[0-9A-Fa-f]+", "08ef");
|
expectMatch("[0-9A-Fa-f]+", "08ef");
|
||||||
expectNoMatch("[0-9A-Fa-f]+", "08@ef");
|
expectNoMatch("[0-9A-Fa-f]+", "08@ef");
|
||||||
expectGroups("(?:a)", "a");
|
expectGroups("(?:a)", "a");
|
||||||
|
expectGroups("a|(b|c)", "a", (String)null);
|
||||||
|
expectGroups("a|(b|c)", "c", "c");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -143,9 +143,13 @@ class Compiler implements PikeVMOpcodes {
|
|||||||
private final boolean capturing;
|
private final boolean capturing;
|
||||||
|
|
||||||
private ArrayList<Expression> list = new ArrayList<Expression>();
|
private ArrayList<Expression> list = new ArrayList<Expression>();
|
||||||
|
private ArrayList<Group> alternatives;
|
||||||
|
|
||||||
public Group(boolean capturing) {
|
public Group(boolean capturing, ArrayList<Expression> initialList) {
|
||||||
this.capturing = capturing;
|
this.capturing = capturing;
|
||||||
|
if (initialList != null) {
|
||||||
|
list.addAll(initialList);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void push(Expression expr) {
|
public void push(Expression expr) {
|
||||||
@ -160,6 +164,14 @@ class Compiler implements PikeVMOpcodes {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void startAlternative() {
|
||||||
|
if (alternatives == null) {
|
||||||
|
alternatives = new ArrayList<Group>();
|
||||||
|
}
|
||||||
|
alternatives.add(new Group(false, list));
|
||||||
|
list.clear();
|
||||||
|
}
|
||||||
|
|
||||||
public Expression pop() {
|
public Expression pop() {
|
||||||
Expression result = list.remove(list.size() - 1);
|
Expression result = list.remove(list.size() - 1);
|
||||||
return result;
|
return result;
|
||||||
@ -172,9 +184,27 @@ class Compiler implements PikeVMOpcodes {
|
|||||||
output.add(SAVE_OFFSET);
|
output.add(SAVE_OFFSET);
|
||||||
output.add(2 * groupIndex);
|
output.add(2 * groupIndex);
|
||||||
}
|
}
|
||||||
|
int[] jumps = null;
|
||||||
|
if (alternatives != null) {
|
||||||
|
jumps = new int[alternatives.size()];
|
||||||
|
int i = 0;
|
||||||
|
for (Group alternative : alternatives) {
|
||||||
|
output.add(SPLIT);
|
||||||
|
int jump = output.markJump();
|
||||||
|
alternative.writeCode(output);
|
||||||
|
output.add(JMP);
|
||||||
|
jumps[i++] = output.markJump();
|
||||||
|
output.setJump(jump);
|
||||||
|
}
|
||||||
|
}
|
||||||
for (Expression expr : list) {
|
for (Expression expr : list) {
|
||||||
expr.writeCode(output);
|
expr.writeCode(output);
|
||||||
}
|
}
|
||||||
|
if (jumps != null) {
|
||||||
|
for (int jump : jumps) {
|
||||||
|
output.setJump(jump);
|
||||||
|
}
|
||||||
|
}
|
||||||
if (capturing) {
|
if (capturing) {
|
||||||
output.add(SAVE_OFFSET);
|
output.add(SAVE_OFFSET);
|
||||||
output.add(2 * groupIndex + 1);
|
output.add(2 * groupIndex + 1);
|
||||||
@ -186,7 +216,7 @@ class Compiler implements PikeVMOpcodes {
|
|||||||
private final Group group;
|
private final Group group;
|
||||||
|
|
||||||
public Group0() {
|
public Group0() {
|
||||||
group = new Group(true);
|
group = new Group(true, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeCode(Output output) {
|
public void writeCode(Output output) {
|
||||||
@ -249,7 +279,7 @@ class Compiler implements PikeVMOpcodes {
|
|||||||
+ regex.substring(index));
|
+ regex.substring(index));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
current.push(groups.push(new Group(capturing)));
|
current.push(groups.push(new Group(capturing, null)));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
case ')':
|
case ')':
|
||||||
@ -268,6 +298,9 @@ class Compiler implements PikeVMOpcodes {
|
|||||||
index = characterClassParser.getEndOffset() - 1;
|
index = characterClassParser.getEndOffset() - 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
case '|':
|
||||||
|
current.startAlternative();
|
||||||
|
continue;
|
||||||
default:
|
default:
|
||||||
throw new RuntimeException("Parse error @" + index + ": " + regex);
|
throw new RuntimeException("Parse error @" + index + ": " + regex);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user