|
| 1 | +package parsers; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.regex.Pattern; |
| 6 | +import java.util.stream.Stream; |
| 7 | + |
| 8 | +public class PTraceParserUtils { |
| 9 | + private static final String PREFIX = "<SendLog>"; |
| 10 | + |
| 11 | + private static final String evtRegex = "sent event '(\\w+) with payload \\((.+)\\)' to"; |
| 12 | + public static final Pattern evtPattern = Pattern.compile(evtRegex); |
| 13 | + |
| 14 | + private static boolean sendLogFilter(String msg) { |
| 15 | + return msg.startsWith(PREFIX); |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Produces the given stream of log lines, removing the ones that do |
| 20 | + * not originate from the SendLog source. |
| 21 | + * @param s |
| 22 | + * @return |
| 23 | + */ |
| 24 | + public static Stream<String> FilterSendLogs(Stream<String> s){ |
| 25 | + return s.filter(PTraceParserUtils::sendLogFilter); |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | + /** |
| 30 | + * These routines help convert serialised data in the form emitted by the C# runtime, |
| 31 | + * which all inherit from `PLang.CSharpRuntime.PrtType`. Should any PrtType's `.toString()` |
| 32 | + * method be changed, these conversion functions may break. |
| 33 | + */ |
| 34 | + public static class Conversions { |
| 35 | + private static final String tupleDelimiter = ","; /* Note the lack of space, vis a vis named tuples */ |
| 36 | + private static final String namedTupleDelimiter = ", "; |
| 37 | + |
| 38 | + /** |
| 39 | + * Given a serialized named tuple of the form `<k1:v1, k2:v2, ... kn:vn, >`, return an array |
| 40 | + * of Strings containing all the key-value pairs. (Note the trailing ", " after the final |
| 41 | + * key-value pair[1]; the suprious empty final element is truncated from the final result array.) |
| 42 | + * |
| 43 | + * [1]: https://github.com/p-org/P/blob/master/Src/PRuntimes/PCSharpRuntime/Values/PrtTuple.cs#L191-L191 |
| 44 | + * @param token The string representation of a k-ary named tuple |
| 45 | + * @return An array of `k` elements, containing each of the colon-delimited key-value pairs. |
| 46 | + */ |
| 47 | + public static List<String> namedTupleToKVPairs(String token) { |
| 48 | + if (token.charAt(0) != '<' || !token.endsWith(", >")) { |
| 49 | + throw new RuntimeException(String.format("Token \"%s\" does not appear to be a named tuple?", token)); |
| 50 | + } |
| 51 | + token = token.substring(1, token.length() - 3); // Eat the open brace and trailing comma/space/closing brace. |
| 52 | + return splitCommasOutsideStrings(token, ", "); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Given a serialised tuple of the form `<v1,v2,v3...vn,>`, return an array of Strings |
| 57 | + * containing all the values. (Notice the trailing "," after the final value[1]; this spurious |
| 58 | + * element is truncated from the final result.) |
| 59 | + * |
| 60 | + * [1]: https://github.com/p-org/P/blob/master/Src/PRuntimes/PCSharpRuntime/Values/PrtTuple.cs#L95-L95 |
| 61 | + * @param token |
| 62 | + * @return |
| 63 | + */ |
| 64 | + public static List<String> tupleToValues(String token) { |
| 65 | + if (token.charAt(0) != '<' || !token.endsWith(",>")) { |
| 66 | + throw new RuntimeException(String.format("Token \"%s\" does not appear to be a tuple?", token)); |
| 67 | + } |
| 68 | + token = token.substring(1, token.length() - 2); // Eat the open brace and trailing comma/closing brace |
| 69 | + return splitCommasOutsideStrings(token, ","); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Given a key-value pair of the form "key:value", where value |
| 74 | + * is an Integer, extract the int. |
| 75 | + * @param kv |
| 76 | + * @return |
| 77 | + */ |
| 78 | + public static int kvPairToInt(String kv) { |
| 79 | + return Integer.valueOf(kv.split(":")[1]); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Given a key-value pair of the form "key:value", where value |
| 84 | + * is a long, extract the long. |
| 85 | + * @param kv |
| 86 | + * @return |
| 87 | + */ |
| 88 | + public static long kvPairToLong(String kv) { |
| 89 | + return Long.valueOf(kv.split(":")[1]); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Given a key-value pair of the form "key:MachineName(value)", where value |
| 94 | + * is a long and MachineName is an arbitrary identifier, extract the long. |
| 95 | + * @param kv |
| 96 | + * @return |
| 97 | + */ |
| 98 | + public static long kvPairToMachineId(String kv) { |
| 99 | + int openParen = kv.indexOf("("); |
| 100 | + int closeParen = kv.indexOf(")"); |
| 101 | + return Long.valueOf(kv.substring(openParen + 1, closeParen)); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Given a key-value pair of the form "key:value", where value |
| 106 | + * is a float, extract the long. |
| 107 | + * @param kv |
| 108 | + * @return |
| 109 | + */ |
| 110 | + public static float kvPairToFloat(String kv) { |
| 111 | + return Float.valueOf(kv.split(":")[1]); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Given a key-value pair of the form "key:value", where value |
| 116 | + * is an enumerated value, extract it. |
| 117 | + */ |
| 118 | + public static int kvPairToEnumVal(String kv) { |
| 119 | + // Internally, the integer representation will already have been written out, |
| 120 | + // so this is equivalent to parsing an int out. |
| 121 | + return kvPairToInt(kv); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Splits `s` according to its occurrences of `delimiter`, such that the delimiters |
| 126 | + * do not fall inside a quoted string. This method assumes that there are no dangling |
| 127 | + * quotes (i.e. there are an even number of occurrences of /[^\\]"/ in s. |
| 128 | + * |
| 129 | + * @param s |
| 130 | + * @return |
| 131 | + */ |
| 132 | + private static List<String> splitCommasOutsideStrings(String str, String delimiter) |
| 133 | + { |
| 134 | + ArrayList<String> ret = new ArrayList<>(); |
| 135 | + boolean inQuotedString = (str.charAt(0) == '"'); |
| 136 | + |
| 137 | + StringBuilder currentStr = new StringBuilder(str.substring(0, 1)); |
| 138 | + |
| 139 | + // Note the slightly-pedestrian way of writing this. The alternative was to write |
| 140 | + // a gnarly regex using negative lookahead assertions (which would be O(n^2) anyway) |
| 141 | + // or taking a major dependency on something like Apache Commons. |
| 142 | + for (int i = 1; i < str.length(); i++) { |
| 143 | + if (!inQuotedString && str.substring(i).startsWith(delimiter)) { |
| 144 | + ret.add(currentStr.toString()); |
| 145 | + currentStr = new StringBuilder(); |
| 146 | + i += delimiter.length() - 1; |
| 147 | + continue; |
| 148 | + } |
| 149 | + |
| 150 | + char prev = str.charAt(i - 1); |
| 151 | + char curr = str.charAt(i); |
| 152 | + |
| 153 | + if (curr == '"' && prev != '\\') { |
| 154 | + inQuotedString = !inQuotedString; |
| 155 | + } |
| 156 | + currentStr.append(curr); |
| 157 | + } |
| 158 | + if (currentStr.length() > 0) { |
| 159 | + ret.add(currentStr.toString()); |
| 160 | + } |
| 161 | + return ret; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Given a key-value pair of the form "key:value", where value is a string, extract it. |
| 166 | + * Note: https://github.com/p-org/P/issues/447 is an issue here. |
| 167 | + */ |
| 168 | + public static String kvPairToString(String kv) { |
| 169 | + return kv.split(":", 2)[1]; |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments