mirror of
https://github.com/corda/corda.git
synced 2025-02-11 05:12:09 +00:00
Implement Pattern / Matcher classes based on the PikeVM
Based on the just-implemented PikeVM, let's test it with a specific regular expression. At this point, no parsing is implemented but instead an explicit program executing a(bb)?a is hardcoded. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
This commit is contained in:
parent
944f5f3567
commit
e6ad10de04
@ -19,7 +19,7 @@ import java.util.List;
|
||||
* @author zsombor and others
|
||||
*
|
||||
*/
|
||||
public abstract class Pattern {
|
||||
public abstract class Pattern implements PikeVMOpcodes {
|
||||
|
||||
public static final int UNIX_LINES = 1;
|
||||
public static final int CASE_INSENSITIVE = 2;
|
||||
@ -46,6 +46,23 @@ public abstract class Pattern {
|
||||
try {
|
||||
return new TrivialPattern(regex, flags);
|
||||
} catch (UnsupportedOperationException handledBelow) { }
|
||||
if (flags != 0) {
|
||||
throw new UnsupportedOperationException("TODO");
|
||||
}
|
||||
if ("a(bb)?a".equals(regex)) {
|
||||
int[] program = new int[] {
|
||||
SAVE_OFFSET, 0,
|
||||
CHAR, 'a',
|
||||
SPLIT, 14,
|
||||
SAVE_OFFSET, 2,
|
||||
CHAR, 'b',
|
||||
CHAR, 'b',
|
||||
SAVE_OFFSET, 3,
|
||||
/* 14 */ CHAR, 'a',
|
||||
SAVE_OFFSET, 1
|
||||
};
|
||||
return new RegexPattern(regex, flags, new PikeVM(program, 1));
|
||||
}
|
||||
throw new UnsupportedOperationException("Cannot handle regex " + regex);
|
||||
}
|
||||
|
||||
|
55
test/regex/RegexMatcher.java
Normal file
55
test/regex/RegexMatcher.java
Normal file
@ -0,0 +1,55 @@
|
||||
/* Copyright (c) 2008-2013, Avian Contributors
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software
|
||||
for any purpose with or without fee is hereby granted, provided
|
||||
that the above copyright notice and this permission notice appear
|
||||
in all copies.
|
||||
|
||||
There is NO WARRANTY for this software. See license.txt for
|
||||
details. */
|
||||
|
||||
package regex;
|
||||
|
||||
/**
|
||||
* A minimal implementation of a regular expression matcher.
|
||||
*
|
||||
* @author Johannes Schindelin
|
||||
*/
|
||||
public class RegexMatcher extends Matcher {
|
||||
private final PikeVM vm;
|
||||
private char[] array;
|
||||
int[] groupStart, groupEnd;
|
||||
|
||||
RegexMatcher(PikeVM vm, CharSequence string) {
|
||||
super(string);
|
||||
this.vm = vm;
|
||||
}
|
||||
|
||||
private final PikeVM.Result adapter = new PikeVM.Result() {
|
||||
public void set(int[] start, int[] end) {
|
||||
RegexMatcher.this.start = start[0];
|
||||
RegexMatcher.this.end = end[0];
|
||||
RegexMatcher.this.groupStart = start;
|
||||
RegexMatcher.this.groupEnd = end;
|
||||
}
|
||||
};
|
||||
|
||||
public Matcher reset() {
|
||||
start = end = -1;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Matcher reset(CharSequence input) {
|
||||
this.input = input;
|
||||
array = input.toString().toCharArray();
|
||||
return reset();
|
||||
}
|
||||
|
||||
public boolean matches() {
|
||||
return vm.matches(array, 0, array.length, true, true, adapter);
|
||||
}
|
||||
|
||||
public boolean find(int offset) {
|
||||
throw new UnsupportedOperationException("TODO");
|
||||
}
|
||||
}
|
29
test/regex/RegexPattern.java
Normal file
29
test/regex/RegexPattern.java
Normal file
@ -0,0 +1,29 @@
|
||||
/* Copyright (c) 2008-2013, Avian Contributors
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software
|
||||
for any purpose with or without fee is hereby granted, provided
|
||||
that the above copyright notice and this permission notice appear
|
||||
in all copies.
|
||||
|
||||
There is NO WARRANTY for this software. See license.txt for
|
||||
details. */
|
||||
|
||||
package regex;
|
||||
|
||||
/**
|
||||
* A minimal implementation of a regular expression engine.
|
||||
*
|
||||
* @author Johannes Schindelin
|
||||
*/
|
||||
public class RegexPattern extends Pattern {
|
||||
private PikeVM vm;
|
||||
|
||||
public RegexMatcher matcher(CharSequence string) {
|
||||
return new RegexMatcher(vm, string);
|
||||
}
|
||||
|
||||
RegexPattern(String regex, int flags, PikeVM vm) {
|
||||
super(regex, flags);
|
||||
this.vm = vm;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user