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)

svoid tok_questionDot(LS tok) {
  jreplace(tok, "if (<id> ?. <id)", "if ($3 != null && $3.$6)");
  
  jreplace_dyn(tok, "<id> ?.",
    func(LS tok, int i) -> S {
      S var = tok.get(i);
      bool isExpression = eqGetOneOf(tok, i-2, "ret", "return", ",", "=", "(", "?"); // too few
      if (!isExpression) {
        int j = tok_findEndOfStatement(tok, i)-1;
        tokAppend_reTok(tok, j, " }");
        ret "{ if (\*var*/ != null) \*var*/.";
      } else
        ret "\*var*/ == null ? null : \*var*/.";
    });
    
  /*jreplace_dyn(tok, "<id>()?.",
    func(LS tok, int i) -> S {
      int iQ = indexOf(tok, i, "?");
      S expr = joinSubList(tok, i, iQ-1);
      bool isExpression = eqGetOneOf(tok, i-2, "ret", "return", ",", "=", "(", "?"); // too few
      S var = makeVar();
      if (!isExpression) {
        int j = tok_findEndOfStatement(tok, i)-1;
        tokAppend_reTok(tok, j, " }");
        ret "{ var \ *var* / = \ *expr* /; if (\ *var* / != null) \ *var* /.";
      } else {  // experimental
        int j = tok_findEndOfExpression(tok, i)-1;
        tokAppend_reTok(tok, j, ")");
        ret "rCallF(\ *expr* /, \ *var* / -> \ *var* / == null ?: \ *var* /.";
      }
    });*/
    
  int iQ;
  while ((iQ = jfind(tok, "?.")) >= 0) {
    int iBeginningOfLHS = tok_findBeginningOfJavaXTerm(tok, iQ-2);
    S expr = joinSubList(tok, iBeginningOfLHS, iQ-1);
    
    // Are we in a statement or an expression?
    bool isExpression = eqGetOneOf(tok, iBeginningOfLHS-2, "ret", "return", ",", "=", "(", "?"); // too few
    
    S var = makeVar();
    if (!isExpression) {
      int j = tok_findEndOfStatement(tok, iQ+4)-1;
      tokAppend_reTok(tok, j, " }");
      tokPrepend(tok, iBeginningOfLHS, "{ var \*var*/ = ");
      replaceTokens(tok, iQ, iQ+3, "; if (\*var*/ != null) \*var*/.");
    } else {  // experimental
      int j = tok_findEndOfExpression(tok, iQ+4)-1;
      tokAppend_reTok(tok, j, ")");
      tokPrepend(tok, iBeginningOfLHS, "rCallF(");
      replaceTokens(tok, iQ, iQ+3, ", \*var*/ -> \*var*/ == null ?: \*var*/.");
    }
  }
}

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: 129 / 153
Referenced in: