!7 abstract sclass CtxExpr { abstract bool get(S s); } CtxExpr > CtxNeg { CtxExpr e; *() {} *(CtxExpr *e) {} bool get(S s) { ret !e.get(s); } } CtxExpr > CtxAnd { CtxExpr a, b; *() {} *(CtxExpr *a, CtxExpr *b) {} bool get(S s) { ret a.get(s) && b.get(s); } } CtxExpr > CtxOr { CtxExpr a, b; *() {} *(CtxExpr *a, CtxExpr *b) {} bool get(S s) { ret a.get(s) || b.get(s); } } CtxExpr > CtxRegexp { S pattern; *() {} *(S *pattern) {} bool get(S s) { ret evalPattern(pattern, s); } } concept Rule { CtxRule1 source; CtxExpr expr; } static new TreeMap regexpMacros; static new TreeSet patterns; p { silentGC(); L rules = ctxFindRules(#1200000); regexpMacros.putAll(ctxLoadRegexpMacros(#1200000)); //pnl(regexpMacros); print("Found " + n(rules, "rules") + ", " + n(regexpMacros, "macros")); for (CtxRule1 rule : rules) { Explain e = explain(rule.text(), ctxParsingRules(), "rule"); if (e == null) print("Bad rule: " + rule.text()); else { cnew(Rule, source := rule, expr := ctxParseExpression(e.sub(1))); } } evalRulesOnString("wie geht es dir"); save("patterns"); final JTextField tfPat = jtextfield(); final JTextField tfInput = jtextfield(); final JTextField tfOutput = jtextfield(); for (JTextField tf : ll(tfPat, tfInput, tfOutput)) setFontSize(tf, 20); showForm( "Pattern", tfPat, "Input", tfInput, "Matches", tfOutput, func { bool matches = evalPattern(getText(tfPat), getText(tfInput)); tfOutput.setText(yn(matches)); false; }); botSleep(); } sS answer(S s) { ret evalRulesOnString(s); } static S evalRulesOnString(S s) { for (Rule rule) try { if (rule.expr != null && rule.expr.get(s)) { //print("Fire! " + rule.source.file); print("Fire! " + rule.source.text()); } } catch e { //printExplainTree(rule.explain); rethrow(e); } ret ""; } static CtxExpr ctxParseExpression(Explain exp) { if (exp.is("bracket")) ret ctxParseExpression(exp.sub(0)); if (exp.is("and")) ret new CtxAnd(ctxParseExpression(exp.sub(0)), ctxParseExpression(exp.sub(1))); if (exp.is("or")) ret new CtxOr(ctxParseExpression(exp.sub(0)), ctxParseExpression(exp.sub(1))); if (exp.is("neg")) ret new CtxNeg(ctxParseExpression(exp.sub(0)); if (exp.is("quoted")) ret new CtxRegexp(unquoteCtx(exp.tok().get(1))); if (exp.singleEqualChild()) ret ctxParseExpression(exp.sub(0)); throw todo(exp.className()); } sbool evalPattern(S pat, S input) { if (empty(pat)) false; // XXX pat = ctxExpandMacros(pat, regexpMacros); //print("Pattern: " + pat); print("Pattern length: " + l(pat)); patterns.add(pat); ret regexpCtx(pat, input).find(); }