Uses 911K of libraries. Click here for Pure Java version (8929L/46K).
1 | !7 |
2 | |
3 | set flag fillJPopupMenu_debug. |
4 | |
5 | cmodule TopDownParsing {
|
6 | switchable S input = "Bob paid for Charlie 's college education"; |
7 | switchable bool escapeBrackets; |
8 | TryPatterns root; |
9 | transient JTree tree; |
10 | transient S selectedGrouped; |
11 | transient SingleComponentPanel scp; |
12 | transient ReliableSingleThread rstProcessInput = dm_rstWithPreDelay(this, 500, r processInput); |
13 | |
14 | replace GroupingAndPatterns with Pair<S, Set<S>>. |
15 | |
16 | srecord Config(LS patternsToTry) {}
|
17 | |
18 | // Tree has 2 types of nodes: composite and options |
19 | abstract class Base {
|
20 | Config config; |
21 | L<Base> children; |
22 | L<Base> children() { ret children; }
|
23 | |
24 | abstract S grouped(); |
25 | abstract LS allGroupings(); |
26 | |
27 | // allGroupings + patterns used in each grouping |
28 | abstract L<GroupingAndPatterns> allGroupingsWithUsedPatterns(); |
29 | } |
30 | |
31 | // composite node |
32 | class AppliedPattern > Base {
|
33 | S pattern; |
34 | LS arguments; |
35 | |
36 | L<Base> children() {
|
37 | if (children == null) |
38 | children = map(arguments, a -> new TryPatterns(config, a)); |
39 | ret children; |
40 | } |
41 | |
42 | toString { ret shortName(this) + "(" + quote(pattern) + ")"
|
43 | + (empty(arguments) ? "" : " with " + join(" + " , quoteAll(arguments))); }
|
44 | |
45 | S grouped() {
|
46 | ret format3_round(pattern, toStringArray(arguments)); |
47 | } |
48 | |
49 | LS allGroupings() {
|
50 | LLS childGroupings = map(children(), a -> a.allGroupings()); |
51 | print(quote(grouped()) + " got child groupings: " + childGroupings); |
52 | ItIt<LS> combinations = allCombinations(childGroupings); |
53 | ret map(combinations, c -> format3_round(pattern, c)); |
54 | } |
55 | |
56 | L<GroupingAndPatterns> allGroupingsWithUsedPatterns() {
|
57 | LL<GroupingAndPatterns> childGroupings = map(children(), a -> a.allGroupingsWithUsedPatterns()); |
58 | ItIt<L<GroupingAndPatterns>> combinations = allCombinations(childGroupings); |
59 | ret map(combinations, c -> pair( |
60 | format3_round(pattern, pairsA(c)), |
61 | concatListsAsCISet(itemPlusList(litciset(pattern), pairsB(c))))); |
62 | } |
63 | } |
64 | |
65 | // options node (try different patterns) |
66 | class TryPatterns > Base {
|
67 | S input; |
68 | |
69 | *() {}
|
70 | *(Config *config, S input) {
|
71 | this.input = tok_deRoundOrCurlyBracket(input); |
72 | } |
73 | |
74 | L<Base> children() {
|
75 | if (children == null) {
|
76 | children = new L; |
77 | for (S pat : config.patternsToTry) |
78 | for (LS arguments : matchesToStringLists(flexMatchIC_withBrackets_all_autoPunctuation(pat, input))) |
79 | children.add(setAll(new AppliedPattern, +config, pattern := pat, +arguments)); |
80 | } |
81 | ret children; |
82 | } |
83 | |
84 | toString { ret shortName(this) + "(" + quote(input) + ")"
|
85 | + (empty(children) ? "" : " [" + nMatches(children()) + "]"); } |
86 | |
87 | S grouped() {
|
88 | ret empty(children()) ? input : first(children).grouped(); |
89 | } |
90 | |
91 | LS allGroupings() {
|
92 | ret uniquifyCI(listPlusItem(concatMap(children(), a -> a.allGroupings()), input)); |
93 | } |
94 | |
95 | L<GroupingAndPatterns> allGroupingsWithUsedPatterns() {
|
96 | ret uniquifyByFieldCI a(listPlusItem(concatMap(children(), a -> a.allGroupingsWithUsedPatterns()), pair(input, emptySet()))); |
97 | } |
98 | } |
99 | |
100 | start {
|
101 | dm_watchFieldsAndNow(ll('input, 'escapeBrackets), rstProcessInput);
|
102 | dm_vmBus_onMessage unclearListChanged((mod, name) -> {
|
103 | if (eqic((S) name, "Patterns")) rstProcessInput.trigger(); |
104 | }); |
105 | } |
106 | |
107 | visual northCenterAndSouthWithMargins( |
108 | jCenteredSection("Input", centerAndEastWithMargins(
|
109 | dm_centeredTextField input(), |
110 | jPopDownButton_noText(dm_boolFieldPopupMenuItem escapeBrackets()))), |
111 | scp = singleComponentPanel(makeTree()), |
112 | jCenteredSection("Grouped", dm_label selectedGrouped()));
|
113 | |
114 | JTree makeTree() {
|
115 | tree = jDynamicTree(root, x -> x.children()); |
116 | onTreeSelectionChanged(tree, r {
|
117 | Base base = cast jtree_selectedUserObject(tree); |
118 | setField(selectedGrouped := base == null ? null : base.grouped()); |
119 | }); |
120 | ret tree; |
121 | } |
122 | |
123 | // API |
124 | |
125 | LS defaultPatterns() { ret dm_getUnclearList("patterns"); }
|
126 | |
127 | Config defaultConfig() {
|
128 | ret new Config(defaultPatterns()); |
129 | } |
130 | |
131 | Config makeConfig(LS patternsToTry) {
|
132 | ret new Config(patternsToTry); |
133 | } |
134 | |
135 | void processInput {
|
136 | setField(root := parse(escapeBrackets ? ai_escapeBrackets(input) : input); |
137 | setSCPComponent(scp, makeTree()); |
138 | } |
139 | |
140 | TryPatterns parse(S s, O... _) {
|
141 | optPar Config config; |
142 | optPar Cl<S> patterns; |
143 | if (config == null) |
144 | config = patterns != null ? new Config(asList(patterns)) : defaultConfig(); |
145 | ret new TryPatterns(config, s); |
146 | } |
147 | |
148 | Cl<S> parseToGroupings(S s, O... _) {
|
149 | ret parse(s, _).allGroupings(); |
150 | } |
151 | |
152 | Cl<GroupingAndPatterns> parseToGroupingsWithUsedPatterns(S s, O... _) {
|
153 | ret parse(s, _).allGroupingsWithUsedPatterns(); |
154 | } |
155 | |
156 | TryPatterns root() { ret root; }
|
157 | |
158 | LS allGroupings() { ret root().allGroupings(); }
|
159 | L<GroupingAndPatterns> allGroupingsWithUsedPatterns() { ret root().allGroupingsWithUsedPatterns(); }
|
160 | } |
download show line numbers debug dex old transpilations
Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
| Snippet ID: | #1027108 |
| Snippet name: | Top-Down Parser using unclear list "Patterns" v2 |
| Eternal ID of this version: | #1027108/76 |
| Text MD5: | 2e8372ac95d8bcbbb30e95b5cd140bc8 |
| Transpilation MD5: | f6d3031aaec6b7c684d3424c5556c17d |
| Author: | stefan |
| Category: | javax / a.i. |
| Type: | JavaX source code (Dynamic Module) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2020-03-11 19:05:31 |
| Source code size: | 5121 bytes / 160 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 720 / 11115 |
| Version history: | 75 change(s) |
| Referenced in: | [show references] |