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

71
LINES

< > BotCompany Repo | #1027122 - Top-Down Parser using unclear list "Patterns" [backup v1]

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

Uses 804K of libraries. Click here for Pure Java version (5176L/26K).

!7

cmodule TopDownParsing > DynSCP {
  switchable S input = "Bob paid for Charlie 's college education";
  TryPatterns root;
  
  // Tree has 2 types of nodes: composite and options
  class Base {
    L<Base> children;
    L<Base> children() { ret children; }
  }
  
  // composite node
  class AppliedPattern > Base {
    S pattern;
    LS arguments;
    
    L<Base> children() {
      lock lock;
      if (children == null)
        children = map(arguments, a -> new TryPatterns(a));
      ret children;
    }

    toString { ret stdToStringWithFields(this, 'pattern, 'arguments); }
  }
  
  // options node (try different patterns)
  class TryPatterns > Base {
    S input;
    
    *() {}
    *(S *input) {}
    
    L<Base> children() {
      lock lock;
      if (children == null) {
        children = new L;
        for (S pat : patterns())
          for (LS arguments : matchesToStringLists(flexMatchIC_all(pat, input)))
            children.add(setAll(new AppliedPattern, pattern := pat, +arguments));
      }
      ret children;
    }

    toString { ret stdToStringWithFields(this, 'input) + ", n=" + l(children()); }
  }
  
  start {
    dm_reloadOnFieldChange input();
    parse(input);
    //revisualize2();
  }
  
  JComponent visualize2() {
    ret jDynamicTree(root, x -> x.children());
  }
  
  LS patterns() { ret dm_getUnclearList("patterns"); }
  
  // API
  
  TryPatterns parse(S s) {
    setField(input := s);
    TryPatterns root = new TryPatterns(s);
    setField(+root);
    ret root;
  }
  
  TryPatterns root() { ret root; }
}

Author comment

Began life as a copy of #1027108

download  show line numbers  debug dex   

Travelled to 3 computer(s): mqqgnosmbjvj, tvejysmllsmz, 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: 20 / 34
Referenced in: [show references]

Formerly at http://tinybrain.de/1027122 & http://1027122.tinybrain.de