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

69
LINES

< > BotCompany Repo | #1034873 // findFunctionInvocations_v2 - parse Java source for function invocations - removes "please include function" lines - TODO: type arguments

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

Transpiled version (6163L) is out of date.

sbool findFunctionInvocations_v2_debug;

// tok: the code
// sf: map of functions (keys) to look for - null to record every function call
// hardReferences (out parameter): records every "please include"  statement if not null
// haveFunctions: functions to skip (or null)
// includePossiblyMapLikes: include calls with pre-brackets argument (e.g. map l(...))
// mainClassName: name of program's main class (calls to this class will be skipped)
static Set<S> findFunctionInvocations_v2(LS tok, SS sf, Cl<S> hardReferences, Set<S> haveFunctions, bool includePossiblyMapLikes, S mainClassName, IPred<S> isKnownClassName) {
  new LinkedHashSet<S> l;
  for (int i : jfindAll(tok, "please include functions")) {
    int j = i + 6;
    while licensed {
      String fname = tok.get(j);
      assertIdentifier("in please include functions section", get(tok, j));
      l.add(fname);
      add(hardReferences, fname);
      j += 2;
      if (eqGet(tok, j, ".")) break;
      while (eqGetOneOf(tok, j, ",", "and")) j += 2;
    }
    clearAllTokens(tok.subList(i, j+2));
  }
  
  for (int i : jfindAll(tok, "please include function *.")) {
    String fname = tok.get(i+6);
    l.add(fname);
    add(hardReferences, fname);
    clearAllTokens(tok.subList(i, i+10));
  }

  int i, n = tok.size();
  boolean result = false;
  for (i = 1; i+2 < n; i += 2) {
    S f = tok.get(i);
    if (!isIdentifier_quick(f)) continue;
    
    // main.<A>bla()
    if (eq(mainClassName, f) && eq(tok.get(i+2), ".") && eqGet(tok, i+4, "<")) {
      i = findEndOfTypeArgs(tok, i+4)+1;
      f = tok.get(i);
      if (!isIdentifier(f)) continue;
    }
    
    // e.g. ... html_findLIs(LS [auto htmlTok] tok) { ... }
    if (eqGet(tok, i-4, "[") && eqGet(tok, i-2, "auto")) {
      // ok
    } else if (eqGet(tok, i-2, ":") && eqGet(tok, i-4, ":") && eqGet(tok, i-6, mainClassName)) {
      // ok, function reference (main::bla)
    } else if (eqGet(tok, i+2, "(")) {
      // ok, normal function invocation
    } else if (includePossiblyMapLikes && eqGet(tok, i+4, "(") && isIdentifier(get(tok, i+2))) {
      // ok, mapLike invocation (bla blubb(...))
    } else
      // not an invocation
      continue;

    if (!tok_isMethodDeclarationNotSFInvocation(tok, i, mainClassName, isKnownClassName)) {
      bool inSF = sf == null || sf.containsKey(f);
      if (findFunctionInvocations_v2_debug)
        print("Possible invocation: " + f + ", inSF: " + inSF);
      if (inSF && !contains(haveFunctions, f) && l.add(f))
        if (findFunctionInvocations_v2_debug)
          print("Found reference to standard function " + f + ": " + lineAroundToken(tok, i));
    }
  }
  
  ret l;
}

Author comment

Began life as a copy of #1003268

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034873
Snippet name: findFunctionInvocations_v2 - parse Java source for function invocations - removes "please include function" lines - TODO: type arguments
Eternal ID of this version: #1034873/5
Text MD5: 70eb94ecde9da78591ed68e9906b0425
Author: stefan
Category: javax / transpiling
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-12 22:32:47
Source code size: 2721 bytes / 69 lines
Pitched / IR pitched: No / No
Views / Downloads: 114 / 193
Version history: 4 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)