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

54
LINES

< > BotCompany Repo | #1031727 // tok_questionDot (new version, dev.)

JavaX fragment (include)

1  
svoid tok_questionDot(LS tok) {
2  
  jreplace(tok, "if (<id> ?. <id)", "if ($3 != null && $3.$6)");
3  
  
4  
  jreplace_dyn(tok, "<id> ?.",
5  
    func(LS tok, int i) -> S {
6  
      S var = tok.get(i);
7  
      bool isExpression = eqGetOneOf(tok, i-2, "ret", "return", ",", "=", "(", "?"); // too few
8  
      if (!isExpression) {
9  
        int j = tok_findEndOfStatement(tok, i)-1;
10  
        tokAppend_reTok(tok, j, " }");
11  
        ret "{ if (\*var*/ != null) \*var*/.";
12  
      } else
13  
        ret "\*var*/ == null ? null : \*var*/.";
14  
    });
15  
    
16  
  /*jreplace_dyn(tok, "<id>()?.",
17  
    func(LS tok, int i) -> S {
18  
      int iQ = indexOf(tok, i, "?");
19  
      S expr = joinSubList(tok, i, iQ-1);
20  
      bool isExpression = eqGetOneOf(tok, i-2, "ret", "return", ",", "=", "(", "?"); // too few
21  
      S var = makeVar();
22  
      if (!isExpression) {
23  
        int j = tok_findEndOfStatement(tok, i)-1;
24  
        tokAppend_reTok(tok, j, " }");
25  
        ret "{ var \ *var* / = \ *expr* /; if (\ *var* / != null) \ *var* /.";
26  
      } else {  // experimental
27  
        int j = tok_findEndOfExpression(tok, i)-1;
28  
        tokAppend_reTok(tok, j, ")");
29  
        ret "rCallF(\ *expr* /, \ *var* / -> \ *var* / == null ?: \ *var* /.";
30  
      }
31  
    });*/
32  
    
33  
  int iQ;
34  
  while ((iQ = jfind(tok, "?.")) >= 0) {
35  
    int iBeginningOfLHS = tok_findBeginningOfJavaXTerm(tok, iQ-2);
36  
    S expr = joinSubList(tok, iBeginningOfLHS, iQ-1);
37  
    
38  
    // Are we in a statement or an expression?
39  
    bool isExpression = eqGetOneOf(tok, iBeginningOfLHS-2, "ret", "return", ",", "=", "(", "?"); // too few
40  
    
41  
    S var = makeVar();
42  
    if (!isExpression) {
43  
      int j = tok_findEndOfStatement(tok, iQ+4)-1;
44  
      tokAppend_reTok(tok, j, " }");
45  
      tokPrepend(tok, iBeginningOfLHS, "{ var \*var*/ = ");
46  
      replaceTokens(tok, iQ, iQ+3, "; if (\*var*/ != null) \*var*/.");
47  
    } else {  // experimental
48  
      int j = tok_findEndOfExpression(tok, iQ+4)-1;
49  
      tokAppend_reTok(tok, j, ")");
50  
      tokPrepend(tok, iBeginningOfLHS, "rCallF(");
51  
      replaceTokens(tok, iQ, iQ+3, ", \*var*/ -> \*var*/ == null ?: \*var*/.");
52  
    }
53  
  }
54  
}

Author comment

Began life as a copy of #1024937

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1031727
Snippet name: tok_questionDot (new version, dev.)
Eternal ID of this version: #1031727/1
Text MD5: bfa7fbb40864cf6094bc0233029fbbf8
Author: stefan
Category: javax / transpiling
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-06-29 00:02:45
Source code size: 2122 bytes / 54 lines
Pitched / IR pitched: No / No
Views / Downloads: 128 / 152
Referenced in: [show references]