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

37
LINES

< > BotCompany Repo | #1026296 // tok_tildeCalls - reflective calls/field access with ~. or .~

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (3580L/21K).

1  
svoid tok_tildeCalls(LS tok) {
2  
  int iTilde;
3  
  
4  
  jreplace(tok, ".~", "~.");
5  
  
6  
  // calls without type 
7  
  while ((iTilde = jfind(tok, "~.<id>(")) >= 0) {
8  
    int iStart = tok_findBeginningOfJavaXTerm(tok, iTilde-2);
9  
    S fname = tok.get(iTilde+4);
10  
    tokPrepend(tok, iStart, "call(");
11  
    replaceTokens(tok, iTilde, iTilde+8, ", " + quote(fname)
12  
      + (eqGet(tok, iTilde+8, ")") ? "" : ", "));
13  
    reTok(tok, iStart, iTilde+8);
14  
  }
15  
  
16  
  // calls with type
17  
  while ((iTilde = jfind(tok, "~.<id> <id>(")) >= 0) {
18  
    int iStart = tok_findBeginningOfJavaXTerm(tok, iTilde-2);
19  
    S type = get(tok, iTilde+4);
20  
    int iFname = iTilde+6;
21  
    S fname = tok.get(iFname);
22  
    // TODO: brackets around the whole thing
23  
    tokPrepend(tok, iStart, "(" + type + ") call(");
24  
    replaceTokens(tok, iTilde, iFname+4, ", " + quote(fname)
25  
      + (eqGet(tok, iFname+4, ")") ? "" : ", "));
26  
    reTok(tok, iStart, iFname+4);
27  
  }
28  
  
29  
  // field access
30  
  while ((iTilde = jfind(tok, "~.<id>")) >= 0) {
31  
    int iStart = tok_findBeginningOfJavaXTerm(tok, iTilde-2);
32  
    S fieldName = tok.get(iTilde+4);
33  
    tokPrepend(tok, iStart, "getOpt " + fieldName + "(");
34  
    replaceTokens(tok, iTilde, iTilde+5, ")");
35  
    reTok(tok, iStart, iTilde+5);
36  
  }
37  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1026296
Snippet name: tok_tildeCalls - reflective calls/field access with ~. or .~
Eternal ID of this version: #1026296/15
Text MD5: 0942be6b02d15e68b7496d87682f883a
Transpilation MD5: b724f84e49ac8baafa260bd6acc2242a
Author: stefan
Category: javax / transpiling
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-04-01 16:27:27
Source code size: 1265 bytes / 37 lines
Pitched / IR pitched: No / No
Views / Downloads: 215 / 345
Version history: 14 change(s)
Referenced in: [show references]