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

65
LINES

< > BotCompany Repo | #1008783 // "Single Word Parser" Spike 1 [abandoned]

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Transpiled version (4162L) is out of date.

!7

sclass Parser {
  new StringTree2 tree;
  
  L<S> tok(S s) { ret javaTok(s); }
  L<S> tokC(S s) { ret javaTokC(s); }
  
  void register(S text, O action) {
    addToStringTree(tree, tokC(text), action);
  }
  
  Parsing parse(S text) {
    ret nu(Parsing,
      parser := this, it := tokC(text).iterator(),
      treesInFlight := ll(new InFlight(tree)));
  }
}

sclass InFlight {
  new L history;
  StringTree2 tree;
  
  *() {}
  *(StringTree2 *tree) {}
  *(L *history, StringTree2 *tree) {}
}

sclass Parsing {
  Parser parser;
  Iterator<S> it;
  new L<InFlight> treesInFlight;
  
  void run {
    while (it.hasNext()) step();
  }
  
  void step {
    S token = it.next();
    for i over treesInFlight: {
      InFlight tree = treesInFlight.get(i);
      StringTree2 tree2 = tree.tree.get(token);
      if (tree2 == null) treesInFlight.remove(i--);
      else treesInFlight.set(i, new InFlight(tree.history, tree2));
    }
    actions();
  }
  
  void actions {
    for (StringTree2 tree : treesInFlight)
      if (tree! != null)
        runAction(tree!);
  }
  
  void runAction(O o) {
    print("Action! " + struct(o));
  }
}

p {
  new Parser p;
  p.register("I am here", "I-am-here");
  Parsing pp = p.parse("yo I am here");
  pp.run();
}

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: #1008783
Snippet name: "Single Word Parser" Spike 1 [abandoned]
Eternal ID of this version: #1008783/7
Text MD5: b2fe80e56b3867c9a33b31421b2365f4
Author: stefan
Category: javax / a.i. parsing
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-02-26 12:22:14
Source code size: 1313 bytes / 65 lines
Pitched / IR pitched: No / No
Views / Downloads: 516 / 652
Version history: 6 change(s)
Referenced in: #1008784 - "Single Word Parser" 2 [dev.]