Libraryless. Click here for Pure Java version (9004L/61K/210K).
1 | !7 |
2 | |
3 | abstract sclass CtxExpr {
|
4 | abstract bool get(S s); |
5 | } |
6 | |
7 | CtxExpr > CtxNeg {
|
8 | CtxExpr e; |
9 | |
10 | *() {}
|
11 | *(CtxExpr *e) {}
|
12 | |
13 | bool get(S s) { ret !e.get(s); }
|
14 | } |
15 | |
16 | CtxExpr > CtxAnd {
|
17 | CtxExpr a, b; |
18 | |
19 | *() {}
|
20 | *(CtxExpr *a, CtxExpr *b) {}
|
21 | |
22 | bool get(S s) { ret a.get(s) && b.get(s); }
|
23 | } |
24 | |
25 | CtxExpr > CtxOr {
|
26 | CtxExpr a, b; |
27 | |
28 | *() {}
|
29 | *(CtxExpr *a, CtxExpr *b) {}
|
30 | |
31 | bool get(S s) { ret a.get(s) || b.get(s); }
|
32 | } |
33 | |
34 | CtxExpr > CtxRegexp {
|
35 | S pattern; |
36 | |
37 | *() {}
|
38 | *(S *pattern) {}
|
39 | |
40 | bool get(S s) { ret evalPattern(pattern, s); }
|
41 | } |
42 | |
43 | concept Rule {
|
44 | CtxRule1 source; |
45 | CtxExpr expr; |
46 | } |
47 | |
48 | static new TreeMap<S, S> regexpMacros; |
49 | static new TreeSet<S> patterns; |
50 | |
51 | p {
|
52 | silentGC(); |
53 | L<CtxRule1> rules = ctxFindRules(#1200000); |
54 | regexpMacros.putAll(ctxLoadRegexpMacros(#1200000)); |
55 | //pnl(regexpMacros); |
56 | print("Found " + n(rules, "rules") + ", " + n(regexpMacros, "macros"));
|
57 | for (CtxRule1 rule : rules) {
|
58 | Explain e = explain(rule.text(), ctxParsingRules(), "rule"); |
59 | if (e == null) |
60 | print("Bad rule: " + rule.text());
|
61 | else {
|
62 | cnew(Rule, source := rule, expr := ctxParseExpression(e.sub(1))); |
63 | } |
64 | } |
65 | evalRulesOnString("wie geht es dir");
|
66 | save("patterns");
|
67 | |
68 | final JTextField tfPat = jtextfield(); |
69 | final JTextField tfInput = jtextfield(); |
70 | final JTextField tfOutput = jtextfield(); |
71 | for (JTextField tf : ll(tfPat, tfInput, tfOutput)) |
72 | setFontSize(tf, 20); |
73 | |
74 | showForm( |
75 | "Pattern", tfPat, |
76 | "Input", tfInput, |
77 | "Matches", tfOutput, |
78 | func {
|
79 | bool matches = evalPattern(getText(tfPat), getText(tfInput)); |
80 | tfOutput.setText(yn(matches)); |
81 | false; |
82 | }); |
83 | |
84 | botSleep(); |
85 | } |
86 | |
87 | sS answer(S s) {
|
88 | ret evalRulesOnString(s); |
89 | } |
90 | |
91 | static S evalRulesOnString(S s) {
|
92 | for (Rule rule) try {
|
93 | if (rule.expr != null && rule.expr.get(s)) {
|
94 | //print("Fire! " + rule.source.file);
|
95 | print("Fire! " + rule.source.text());
|
96 | } |
97 | } catch e {
|
98 | //printExplainTree(rule.explain); |
99 | rethrow(e); |
100 | } |
101 | ret ""; |
102 | } |
103 | |
104 | static CtxExpr ctxParseExpression(Explain exp) {
|
105 | if (exp.is("bracket"))
|
106 | ret ctxParseExpression(exp.sub(0)); |
107 | if (exp.is("and"))
|
108 | ret new CtxAnd(ctxParseExpression(exp.sub(0)), |
109 | ctxParseExpression(exp.sub(1))); |
110 | if (exp.is("or"))
|
111 | ret new CtxOr(ctxParseExpression(exp.sub(0)), |
112 | ctxParseExpression(exp.sub(1))); |
113 | if (exp.is("neg"))
|
114 | ret new CtxNeg(ctxParseExpression(exp.sub(0)); |
115 | if (exp.is("quoted"))
|
116 | ret new CtxRegexp(unquoteCtx(exp.tok().get(1))); |
117 | if (exp.singleEqualChild()) |
118 | ret ctxParseExpression(exp.sub(0)); |
119 | throw todo(exp.className()); |
120 | } |
121 | |
122 | sbool evalPattern(S pat, S input) {
|
123 | if (empty(pat)) false; // XXX |
124 | pat = ctxExpandMacros(pat, regexpMacros); |
125 | //print("Pattern: " + pat);
|
126 | print("Pattern length: " + l(pat));
|
127 | patterns.add(pat); |
128 | ret regexpCtx(pat, input).find(); |
129 | } |
Began life as a copy of #1008852
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1008854 |
| Snippet name: | Load ctx rules + form |
| Eternal ID of this version: | #1008854/4 |
| Text MD5: | 4071d030a9121a5b1ddcd80098c85b23 |
| Transpilation MD5: | 3d6745a4e73a4911e7d0c8ea60c42c20 |
| Author: | stefan |
| Category: | javax / a.i. |
| Type: | JavaX source code |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2017-06-13 14:49:22 |
| Source code size: | 2932 bytes / 129 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 715 / 870 |
| Version history: | 3 change(s) |
| Referenced in: | [show references] |