Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

160
LINES

< > BotCompany Repo | #1027108 // Top-Down Parser using unclear list "Patterns" v2

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 911K of libraries. Click here for Pure Java version (8929L/46K).

!7

set flag fillJPopupMenu_debug.

cmodule TopDownParsing {
  switchable S input = "Bob paid for Charlie 's college education";
  switchable bool escapeBrackets;
  TryPatterns root;
  transient JTree tree;
  transient S selectedGrouped;
  transient SingleComponentPanel scp;
  transient ReliableSingleThread rstProcessInput = dm_rstWithPreDelay(this, 500, r processInput);
  
  replace GroupingAndPatterns with Pair<S, Set<S>>.
  
  srecord Config(LS patternsToTry) {}

  // Tree has 2 types of nodes: composite and options
  abstract class Base {
    Config config;
    L<Base> children;
    L<Base> children() { ret children; }
    
    abstract S grouped();
    abstract LS allGroupings();
    
    // allGroupings + patterns used in each grouping
    abstract L<GroupingAndPatterns> allGroupingsWithUsedPatterns();
  }
  
  // composite node
  class AppliedPattern > Base {
    S pattern;
    LS arguments;
    
    L<Base> children() {
      if (children == null)
        children = map(arguments, a -> new TryPatterns(config, 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<LS> combinations = allCombinations(childGroupings);
      ret map(combinations, c -> format3_round(pattern, c));
    }
    
    L<GroupingAndPatterns> allGroupingsWithUsedPatterns() {
      LL<GroupingAndPatterns> childGroupings = map(children(), a -> a.allGroupingsWithUsedPatterns());
      ItIt<L<GroupingAndPatterns>> combinations = allCombinations(childGroupings);
      ret map(combinations, c -> pair(
        format3_round(pattern, pairsA(c)),
        concatListsAsCISet(itemPlusList(litciset(pattern), pairsB(c)))));
    }
  }
  
  // options node (try different patterns)
  class TryPatterns > Base {
    S input;

    *() {}
    *(Config *config, S input) {
      this.input = tok_deRoundOrCurlyBracket(input);
    }
    
    L<Base> children() {
      if (children == null) {
        children = new L;
        for (S pat : config.patternsToTry)
          for (LS arguments : matchesToStringLists(flexMatchIC_withBrackets_all_autoPunctuation(pat, input)))
            children.add(setAll(new AppliedPattern, +config, 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<GroupingAndPatterns> allGroupingsWithUsedPatterns() {
      ret uniquifyByFieldCI a(listPlusItem(concatMap(children(), a -> a.allGroupingsWithUsedPatterns()), pair(input, emptySet())));
    }
  }
  
  start {
    dm_watchFieldsAndNow(ll('input, 'escapeBrackets), rstProcessInput);
    dm_vmBus_onMessage unclearListChanged((mod, name) -> {
      if (eqic((S) name, "Patterns")) rstProcessInput.trigger();
    });
  }
  
  visual northCenterAndSouthWithMargins(
    jCenteredSection("Input", centerAndEastWithMargins(
      dm_centeredTextField input(),
      jPopDownButton_noText(dm_boolFieldPopupMenuItem escapeBrackets()))),
    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;
  }
  
  // API
  
  LS defaultPatterns() { ret dm_getUnclearList("patterns"); }
  
  Config defaultConfig() {
    ret new Config(defaultPatterns());
  }
  
  Config makeConfig(LS patternsToTry) {
    ret new Config(patternsToTry);
  }
  
  void processInput {
    setField(root := parse(escapeBrackets ? ai_escapeBrackets(input) : input);
    setSCPComponent(scp, makeTree());
  }
  
  TryPatterns parse(S s, O... _) {
    optPar Config config;
    optPar Cl<S> patterns;
    if (config == null)
      config = patterns != null ? new Config(asList(patterns)) : defaultConfig();
    ret new TryPatterns(config, s);
  }
  
  Cl<S> parseToGroupings(S s, O... _) {
    ret parse(s, _).allGroupings();
  }
  
  Cl<GroupingAndPatterns> parseToGroupingsWithUsedPatterns(S s, O... _) {
    ret parse(s, _).allGroupingsWithUsedPatterns();
  }
  
  TryPatterns root() { ret root; }
  
  LS allGroupings() { ret root().allGroupings(); }
  L<GroupingAndPatterns> allGroupingsWithUsedPatterns() { ret root().allGroupingsWithUsedPatterns(); }
}

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: 360 / 10699
Version history: 75 change(s)
Referenced in: [show references]