From 903249252ac188be1f90038e3f1a70b15314b6a7 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Mon, 28 Mar 2016 14:10:33 -0400 Subject: Added some simple utilities for handling expression tokens --- .../main/java/bjc/utils/funcutils/ListUtils.java | 87 ++++++++++++++++++++++ 1 file changed, 87 insertions(+) (limited to 'BJC-Utils2/src/main/java/bjc') diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java index fc1894e..b1a9dfd 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java @@ -1,9 +1,11 @@ package bjc.utils.funcutils; +import java.util.Deque; import java.util.function.Consumer; import java.util.function.Function; import bjc.utils.data.GenHolder; +import bjc.utils.data.Pair; import bjc.utils.funcdata.FunctionalList; /** @@ -128,4 +130,89 @@ public class ListUtils { + currPart.unwrap((vl) -> vl.toString()) + "\nPreviously formed groups: " + ret.toString()); } + + /** + * Split tokens in a list of tokens into multiple tokens. + * + * The intended use is for expression parsers so that you can enter + * something like 1+1 instead of 1 + 1. + * + * @param input + * The tokens to split + * @param ops + * The operators to split on. + * @return A list of tokens split on all the operators + */ + public static FunctionalList splitTokens( + FunctionalList input, + Deque> ops) { + GenHolder> ret = new GenHolder<>(input); + + ops.forEach( + (op) -> ret.transform((oldRet) -> oldRet.flatMap((tok) -> { + return op.merge((opName, opRegex) -> { + if (tok.contains(opName)) { + FunctionalList splitTokens = + new FunctionalList<>( + tok.split(opRegex)); + + FunctionalList rt = + new FunctionalList<>(); + + int tkSize = splitTokens.getSize(); + splitTokens.forEachIndexed((idx, tk) -> { + + if (idx != tkSize && idx != 0) { + rt.add(opName); + rt.add(tk); + + } else { + rt.add(tk); + + } + }); + + return rt; + } else { + return new FunctionalList<>(tok); + } + }); + }))); + + return ret.unwrap((l) -> l); + } + + /** + * Split off affixes from tokens + * + * @param input + * The tokens to deaffix + * @param ops + * The affixes to remove + * @return The tokens that have been deaffixed + */ + @SuppressWarnings("unchecked") + public static FunctionalList deAffixTokens( + FunctionalList input, + Deque> ops) { + GenHolder> ret = new GenHolder<>(input); + + ops.forEach( + (op) -> ret.transform((oldRet) -> oldRet.flatMap((tok) -> { + return (FunctionalList) op + .merge((opName, opRegex) -> { + if (tok.startsWith(opName)) { + return new FunctionalList<>(op, + tok.split(opRegex)[1]); + } else if (tok.endsWith(opName)) { + return new FunctionalList<>( + tok.split(opRegex)[0], op); + } else { + return new FunctionalList<>(tok); + } + }); + }))); + + return ret.unwrap((l) -> l); + } } -- cgit v1.2.3