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

38
LINES

< > BotCompany Repo | #1025303 // tok_selfType - "selfType" to enclosing class name

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

Transpiled version (5877L) is out of date.

svoid tok_selfType(L<S> tok) {
  int i;
  mainLoop: while ((i = jfind(tok, "selfType", (_tok, nIdx) -> {
    S prev = get(_tok, nIdx-1);
    S next = get(_tok, nIdx+3);
    ret isIdentifier(next)
      || eqOneOf(next, ".", "(")
      || eq(prev, "(") && eq(next, ")")
      || eqOneOf(prev, "instanceof", "cast");
  })) >= 0) {
    // Now find class name by going backwards.
    
    int j = i, level = 1;
    while (j > 0 && level > 0) {
      S t = tok.get(j);
      if (t.equals("}")) ++level;
      if (t.equals("{")) --level;
      j -= 2;
    }
    
    // search for class name
    while (j > 0) {
      S t = tok.get(j);
      if (eqOneOf(t, "class", "interface", "enum") && isIdentifier(get(tok, j+2))) {
        S type = tok_typeArgs_declToInvocation(tok_scanType(tok, j+2));
        
        if (eqGet(tok, i+2, ".") && eqGet(tok, i+4, "class"))
          type = tok_dropTypeParameters(type);
          
        tokSet_reTok(tok, i, type);
        continue mainLoop;
      }
      j -= 2;
    }
    
    tok.set(i, "Object"); // avoid endless loop if no outer class found
  }
}

Author comment

Began life as a copy of #1023533

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: #1025303
Snippet name: tok_selfType - "selfType" to enclosing class name
Eternal ID of this version: #1025303/17
Text MD5: 0c04943da5431428faaf26a994fa1b4f
Author: stefan
Category: javax / transpiling
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-02-21 16:55:22
Source code size: 1126 bytes / 38 lines
Pitched / IR pitched: No / No
Views / Downloads: 307 / 456
Version history: 16 change(s)
Referenced in: [show references]