!7 cmodule TopDownParsing { switchable S input = "Bob paid for Charlie 's college education"; TryPatterns root; transient JTree tree; transient S selectedGrouped; transient SingleComponentPanel scp; transient ReliableSingleThread rstProcessInput = dm_rstWithPreDelay(this, 500, r processInput); replace GroupingAndPatterns with Pair>. // Tree has 2 types of nodes: composite and options abstract class Base { L children; L children() { ret children; } abstract S grouped(); abstract LS allGroupings(); // allGroupings + patterns used in each grouping abstract L allGroupingsWithUsedPatterns(); } // composite node class AppliedPattern > Base { S pattern; LS arguments; L children() { if (children == null) children = map(arguments, a -> new TryPatterns(a)); ret children; } toString { ret shortName(this) + "(" + quote(pattern) + ")" + (empty(arguments) ? "" : " with " + join(" + " , quoteAll(arguments))); } S grouped() { ret format3_round(pattern, toStringArray(arguments)); } LS allGroupings() { LLS childGroupings = map(children(), a -> a.allGroupings()); print(quote(grouped()) + " got child groupings: " + childGroupings); ItIt combinations = allCombinations(childGroupings); ret map(combinations, c -> format3_round(pattern, c)); } L allGroupingsWithUsedPatterns() { LL childGroupings = map(children(), a -> a.allGroupingsWithUsedPatterns()); ItIt> combinations = allCombinations(childGroupings); ret map(combinations, c -> pair( format3_round(pattern, pairsA(c)), concatListsAsCISet(itemPlusList(pattern, pairsB(c))))); } } // options node (try different patterns) class TryPatterns > Base { S input; *() {} *(S *input) {} L children() { if (children == null) { children = new L; for (S pat : patterns()) for (LS arguments : matchesToStringLists(flexMatchIC_all(pat, input))) children.add(setAll(new AppliedPattern, pattern := pat, +arguments)); } ret children; } toString { ret shortName(this) + "(" + quote(input) + ")" + (empty(children) ? "" : " [" + nMatches(children()) + "]"); } S grouped() { ret empty(children()) ? input : first(children).grouped(); } LS allGroupings() { ret uniquifyCI(listPlusItem(concatMap(children(), a -> a.allGroupings()), input)); } L allGroupingsWithUsedPatterns() { ret uniquifyByFieldCI a(listPlusItem(concatMap(children(), a -> a.allGroupingsWithUsedPatterns()), pair(input, emptySet()))); } } start { dm_watchFieldAndNow input(rstProcessInput); dm_vmBus_onMessage unclearListChanged((mod, name) -> { if (eqic((S) name, "Patterns")) rstProcessInput.trigger(); }); } visual northCenterAndSouthWithMargins( jCenteredSection("Input", dm_centeredTextField input()), scp = singleComponentPanel(makeTree()), jCenteredSection("Grouped", dm_label selectedGrouped())); JTree makeTree() { tree = jDynamicTree(root, x -> x.children()); onTreeSelectionChanged(tree, r { Base base = cast jtree_selectedUserObject(tree); setField(selectedGrouped := base == null ? null : base.grouped()); }); ret tree; } LS patterns() { ret dm_getUnclearList("patterns"); } // API void processInput { setField(root := parse(input)); setSCPComponent(scp, makeTree()); } TryPatterns parse(S s) { ret new TryPatterns(s); } Cl parseToGroupings(S s) { ret parse(s).allGroupings(); } TryPatterns root() { ret root; } LS allGroupings() { ret root().allGroupings(); } L allGroupingsWithUsedPatterns() { ret root().allGroupingsWithUsedPatterns(); } }