Uses 911K of libraries. Click here for Pure Java version (5176L/26K).
1 | !7 |
2 | |
3 | cmodule TopDownParsing > DynSCP { |
4 | switchable S input = "Bob paid for Charlie 's college education"; |
5 | TryPatterns root; |
6 | |
7 | // Tree has 2 types of nodes: composite and options |
8 | class Base { |
9 | L<Base> children; |
10 | L<Base> children() { ret children; } |
11 | } |
12 | |
13 | // composite node |
14 | class AppliedPattern > Base { |
15 | S pattern; |
16 | LS arguments; |
17 | |
18 | L<Base> children() { |
19 | lock lock; |
20 | if (children == null) |
21 | children = map(arguments, a -> new TryPatterns(a)); |
22 | ret children; |
23 | } |
24 | |
25 | toString { ret stdToStringWithFields(this, 'pattern, 'arguments); } |
26 | } |
27 | |
28 | // options node (try different patterns) |
29 | class TryPatterns > Base { |
30 | S input; |
31 | |
32 | *() {} |
33 | *(S *input) {} |
34 | |
35 | L<Base> children() { |
36 | lock lock; |
37 | if (children == null) { |
38 | children = new L; |
39 | for (S pat : patterns()) |
40 | for (LS arguments : matchesToStringLists(flexMatchIC_all(pat, input))) |
41 | children.add(setAll(new AppliedPattern, pattern := pat, +arguments)); |
42 | } |
43 | ret children; |
44 | } |
45 | |
46 | toString { ret stdToStringWithFields(this, 'input) + ", n=" + l(children()); } |
47 | } |
48 | |
49 | start { |
50 | dm_reloadOnFieldChange input(); |
51 | parse(input); |
52 | //revisualize2(); |
53 | } |
54 | |
55 | JComponent visualize2() { |
56 | ret jDynamicTree(root, x -> x.children()); |
57 | } |
58 | |
59 | LS patterns() { ret dm_getUnclearList("patterns"); } |
60 | |
61 | // API |
62 | |
63 | TryPatterns parse(S s) { |
64 | setField(input := s); |
65 | TryPatterns root = new TryPatterns(s); |
66 | setField(+root); |
67 | ret root; |
68 | } |
69 | |
70 | TryPatterns root() { ret root; } |
71 | } |
Began life as a copy of #1027108
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: | #1027122 |
Snippet name: | Top-Down Parser using unclear list "Patterns" [backup v1] |
Eternal ID of this version: | #1027122/1 |
Text MD5: | b6cf9d43240fbd8504880da86b7a34d5 |
Transpilation MD5: | 1e08742e6c294dfb2f7ab0aef174088f |
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-02-21 00:52:21 |
Source code size: | 1627 bytes / 71 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 299 / 344 |
Referenced in: | [show references] |