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

87
LINES

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

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

Libraryless. Click here for Pure Java version (3163L/20K).

static boolean findFunctionInvocations_debug;

// these prefix tokens mark a non-invocation (TODO: expand)
static Set<S> findFunctionInvocations_pre = new HashSet(litlist(".", "void", "S", "String", "int", "bool", "boolean", "A", "Object", "O"));

static Set<String> findFunctionInvocations(S src, SS sf) {
  ret findFunctionInvocations(javaTok(src), sf);
}

static Set<String> findFunctionInvocations(L<S> tok, SS sf) {
  ret findFunctionInvocations(tok, sf, null);
}

// sf = set of functions to search for or null for any function
static Set<String> findFunctionInvocations(L<S> tok, SS sf, Collection<S> hardReferences) {
  ret findFunctionInvocations(tok, sf, hardReferences, null);
}

static Set<String> findFunctionInvocations(L<S> tok, SS sf, Collection<S> hardReferences, Set<S> haveFunctions) {
  ret findFunctionInvocations(tok, sf, hardReferences, haveFunctions, false);
}

static Set<String> findFunctionInvocations(L<S> tok, SS sf, Collection<S> hardReferences, Set<S> haveFunctions, bool includePossiblyMapLikes) {
  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;
      if (!eqOneOf(get(tok, j), ",", "and"))
        fail("bad token 'please include functions' section: " + get(tok, j));
      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 ("main".equals(f) && eq(tok.get(i+2), ".") && eqGet(tok, i+4, "<")) {
      i = findEndOfTypeArgs(tok, i+4)+1;
      f = tok.get(i);
      if (!isIdentifier(f)) continue;
    }
    
    if (findFunctionInvocations_debug)
      print("Testing identifier " + f);
      
    // e.g. ... html_findLIs(LS [auto htmlTok] tok) { ... }
    if (eqGet(tok, i-4, "[") && eqGet(tok, i-2, "auto")) {
      // ok
    } else {
      if (!"(".equals(tok.get(i+2))) {
        if (!(includePossiblyMapLikes && eqGet(tok, i+4, "(") && isIdentifier(get(tok, i+2))))
          continue;
      }
    }
    
    if (i == 1
        || !findFunctionInvocations_pre.contains(tok.get(i-2))
        || eq(get(tok, i-2), ".") && eq(get(tok, i-4), "main")) {
      boolean inSF = sf == null || sf.containsKey(f);
      if (findFunctionInvocations_debug)
        print("Possible invocation: " + f + ", inSF: " + inSF);
      if (inSF && !contains(haveFunctions, f) && l.add(f))
        if (findFunctionInvocations_debug)
          print("Found reference to standard function " + f + ": " + lineAroundToken(tok, i));
    }
  }
  
  return l;
}

download  show line numbers  debug dex   

Travelled to 10 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, tslmcundralx, tvejysmllsmz

No comments. add comment

Snippet ID: #1003268
Snippet name: findFunctionInvocations - parse Java source for function invocations - removes "please include function" lines - TODO: type arguments
Eternal ID of this version: #1003268/27
Text MD5: 36fd3626dccab4484790ca2bd786bcfe
Transpilation MD5: 1a7461694818abc15d42f467b0d94839
Author: stefan
Category: javax / html
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-11-17 17:40:04
Source code size: 3131 bytes / 87 lines
Pitched / IR pitched: No / No
Views / Downloads: 428 / 969
Version history: 26 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1003268 & http://1003268.tinybrain.de