1 /*
2 * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package hat.util;
26
27 import java.util.function.BiFunction;
28 import java.util.function.Function;
29 import java.util.function.Predicate;
30 import java.util.function.Supplier;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
33
34 public record Regex(Function<Match,Match> factory,Pattern pattern) {
35 public interface Result {
36 }
37
38 public interface Match extends Result {
39 Regex regex();
40 Matcher matcher();
41 default String stringOf(int idx) {
42 return matcher().group(idx);
43 }
44
45 default float floatOf(int idx) {
46 return Float.parseFloat(stringOf(idx));
47 }
48
49 default int intOf(int idx) {
50 return Integer.parseInt(stringOf(idx));
51 }
52 default int[] intArrayOf(int from, int count) {
53 int[] ints = new int[count];
54 for (int i = 0; i<count; i++) {
55 ints[i]= Integer.parseInt(stringOf(from + i));
56 }
57 return ints;
58 }
59 default float[] floatArrayOf(int from, int count) {
60 float[] floats = new float[count];
61 for (int i = 0; i<count; i++) {
62 floats[i]=Float.parseFloat(stringOf(from + i));
63 }
64 return floats;
65 }
66 default int count(){
67 return matcher().groupCount();
68 }
69 }
70
71 interface FAIL extends Result {
72 static FAIL of() {
73 return new FAIL(){};
74 }
75 }
76
77 public static Regex of(String... strings) {
78 return of(m->m, strings);
79 }
80 public static Regex of(Function<Match,Match> factory, String... strings) {
81 return new Regex(factory,Pattern.compile(String.join("", strings)));
82 }
83 public static Result any(String line, Regex... regexes) {
84 for (Regex r : regexes) {
85 if (r.is(line) instanceof Match match) {
86 return match;
87 }
88 }
89 return FAIL.of();
90 }
91 public Result is(String s, Predicate<Matcher> matcherPredicate, BiFunction<Regex,Matcher, Match> factory) {
92 if (pattern.matcher(s) instanceof Matcher matcher && matcher.matches() && matcherPredicate.test(matcher)) {
93 return factory.apply(this, matcher);
94 } else {
95 return FAIL.of();
96 }
97 }
98
99 public Result is(String s, BiFunction<Regex,Matcher, Match> factory) {
100 return is(s, _->true,factory);
101 }
102 public Result is(String s, Predicate<Matcher> matcherPredicate) {
103 record DefaultMatch(Regex regex, Matcher matcher) implements Match { }
104 return is(s, matcherPredicate, DefaultMatch::new);
105 }
106 public Result is(String s) {
107 return is(s, _->true);
108 }
109 public boolean matches(String s, Predicate<Matcher> matcherPredicate) {
110 return is(s, matcherPredicate) instanceof Match;
111 }
112 public boolean matches(String s) {
113 return is(s) instanceof Match;
114 }
115 public boolean matchesOrThrow(String s) {
116 if(is(s) instanceof Match) {
117 return true;
118 }
119 throw new RuntimeException("failed expected match");
120 }
121 }