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

47
LINES

< > BotCompany Repo | #1017739 // Live Parse Spike [OK]

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

Download Jar. Libraryless. Click here for Pure Java version (5837L/41K).

1  
!7
2  
3  
p-exp {
4  
  S input = "Stell dir vor die Hütte brennt";
5  
  L<S> tok = javaTok(input);
6  
  Parser parser = FixedTokenIC("Stell", func -> Parser {
7  
    FixedTokenIC("dir", func -> Parser {
8  
      FixedTokenIC("vor", func -> Parser {
9  
        RestParser(voidfunc(S s) {
10  
          print("Got sentence: " + s)
11  
        })
12  
      })
13  
    })
14  
  });
15  
  tok.add("<over>");
16  
  for i over tok: {
17  
    parser = parser.consumeToken(tok.get(i), i);
18  
    if (parser == null && i < l(tok)-1)
19  
      print("No parse after " + joinSubList(tok, 0, i+1));
20  
  }
21  
}
22  
23  
abstract sclass Parser {
24  
  abstract Parser consumeToken(S t, int idx);
25  
}
26  
27  
srecord FixedTokenIC(S token, F0<Parser> nextParser) extends Parser {
28  
  Parser consumeToken(S t, int idx) {
29  
    if (even(idx)) this; // N token
30  
    if (!eqic(token, t)) null;
31  
    ret nextParser!;
32  
  }
33  
}
34  
35  
srecord RestParser(VF1<S> onParse) > Parser {
36  
  new L<S> consumed;
37  
  
38  
  Parser consumeToken(S t, int idx) {
39  
    if (eq(t, "<over>")) {
40  
      callF(onParse, join(consumed));
41  
      null;
42  
    } else {
43  
      consumed.add(t);
44  
      this;
45  
    }
46  
  }
47  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1017739
Snippet name: Live Parse Spike [OK]
Eternal ID of this version: #1017739/7
Text MD5: 2fe1a839c7cf32c7c13478f14627e9a5
Transpilation MD5: c875e4d7b1a4396b9acdc1dfaf21a5b4
Author: stefan
Category: javax / a.i.
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-08-07 20:18:52
Source code size: 1098 bytes / 47 lines
Pitched / IR pitched: No / No
Views / Downloads: 436 / 974
Version history: 6 change(s)
Referenced in: [show references]