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

73
LINES

< > BotCompany Repo | #1008784 // "Single Word Parser" 2 [dev.]

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

Libraryless. Click here for Pure Java version (1248L/9K/31K).

!7

abstract sclass InFlight {
  L actions() { null; }
  abstract L<InFlight> consumeToken(S token);
}

InFlight > JustAction {
  O action;
  
  *() {}
  *(O *action) {}
  
  L actions() { ret ll(action); }
  L<InFlight> consumeToken(S token) { null; }
}

InFlight > AcceptWord {
  S word;
  InFlight next;
  
  *() {}
  *(S *word, InFlight *next) {}
  
  L<InFlight> consumeToken(S token) {
    if (!eqic(token, word)) null;
    ret ll(next);
  }
}

sclass Parsing {
  Iterator<S> it;
  new L<InFlight> inFlight;
  
  *(S text, InFlight... l) {
    addAll(inFlight, l);
    it = javaTokC(text).iterator();
  }
  
  void run {
    actions();
    while (it.hasNext()) step();
  }
  
  void step {
    S token = it.next();
    new L<InFlight> l2;
    for (InFlight f : inFlight)
      addAll(l2, f.consumeToken(token));
    print("New number: " + l(l2));
    inFlight = l2;
    actions();
  }
  
  void actions {
    for (InFlight f : inFlight)
      for (O action : unnull(f.actions()))
        runAction(action);
  }
  
  void runAction(O o) {
    print("Action! " + struct(o));
  }
}

p {
  Parsing pp = new Parsing("I am here",
    JustAction("start"),
    AcceptWord("yes", JustAction("yes")),
    AcceptWord("i", AcceptWord("am", AcceptWord("here",
      JustAction("I am here!"));
  pp.run();
}

Author comment

Began life as a copy of #1008783

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: #1008784
Snippet name: "Single Word Parser" 2 [dev.]
Eternal ID of this version: #1008784/6
Text MD5: 831f72d1b1cdb7583d2e81895bfe3763
Transpilation MD5: 89f976be364c13d5ee5035000b9446f5
Author: stefan
Category: javax / a.i. parsing
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-06-06 07:08:36
Source code size: 1371 bytes / 73 lines
Pitched / IR pitched: No / No
Views / Downloads: 409 / 513
Version history: 5 change(s)
Referenced in: [show references]