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).

1  
!7
2  
3  
abstract sclass InFlight {
4  
  L actions() { null; }
5  
  abstract L<InFlight> consumeToken(S token);
6  
}
7  
8  
InFlight > JustAction {
9  
  O action;
10  
  
11  
  *() {}
12  
  *(O *action) {}
13  
  
14  
  L actions() { ret ll(action); }
15  
  L<InFlight> consumeToken(S token) { null; }
16  
}
17  
18  
InFlight > AcceptWord {
19  
  S word;
20  
  InFlight next;
21  
  
22  
  *() {}
23  
  *(S *word, InFlight *next) {}
24  
  
25  
  L<InFlight> consumeToken(S token) {
26  
    if (!eqic(token, word)) null;
27  
    ret ll(next);
28  
  }
29  
}
30  
31  
sclass Parsing {
32  
  Iterator<S> it;
33  
  new L<InFlight> inFlight;
34  
  
35  
  *(S text, InFlight... l) {
36  
    addAll(inFlight, l);
37  
    it = javaTokC(text).iterator();
38  
  }
39  
  
40  
  void run {
41  
    actions();
42  
    while (it.hasNext()) step();
43  
  }
44  
  
45  
  void step {
46  
    S token = it.next();
47  
    new L<InFlight> l2;
48  
    for (InFlight f : inFlight)
49  
      addAll(l2, f.consumeToken(token));
50  
    print("New number: " + l(l2));
51  
    inFlight = l2;
52  
    actions();
53  
  }
54  
  
55  
  void actions {
56  
    for (InFlight f : inFlight)
57  
      for (O action : unnull(f.actions()))
58  
        runAction(action);
59  
  }
60  
  
61  
  void runAction(O o) {
62  
    print("Action! " + struct(o));
63  
  }
64  
}
65  
66  
p {
67  
  Parsing pp = new Parsing("I am here",
68  
    JustAction("start"),
69  
    AcceptWord("yes", JustAction("yes")),
70  
    AcceptWord("i", AcceptWord("am", AcceptWord("here",
71  
      JustAction("I am here!"));
72  
  pp.run();
73  
}

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: 415 / 521
Version history: 5 change(s)
Referenced in: [show references]