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

94
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 (6104L/35K).

1  
static boolean findFunctionInvocations_debug;
2  
3  
// these prefix tokens mark a non-invocation (TODO: expand)
4  
static Set<S> findFunctionInvocations_pre = new HashSet(litlist(".", "void", "S", "String", "int", "bool", "boolean", "A", "Object", "O", "]", "double", "float", "short", "char", "long"));
5  
6  
static Set<String> findFunctionInvocations(S src, SS sf) {
7  
  ret findFunctionInvocations(javaTok(src), sf);
8  
}
9  
10  
static Set<String> findFunctionInvocations(L<S> tok, SS sf) {
11  
  ret findFunctionInvocations(tok, sf, null);
12  
}
13  
14  
// sf = set of functions to search for or null for any function
15  
static Set<String> findFunctionInvocations(L<S> tok, SS sf, Collection<S> hardReferences) {
16  
  ret findFunctionInvocations(tok, sf, hardReferences, null);
17  
}
18  
19  
static Set<String> findFunctionInvocations(L<S> tok, SS sf, Collection<S> hardReferences, Set<S> haveFunctions) {
20  
  ret findFunctionInvocations(tok, sf, hardReferences, haveFunctions, false);
21  
}
22  
23  
// tok: the code
24  
// sf: map of functions (keys) to look for - null to record every function call
25  
// hardReferences (out parameter): records every "please include"  statement if not null
26  
// haveFunctions: functions to skip (or null)
27  
// includePossiblyMapLikes: include calls with pre-brackets argument (e.g. map l(...))
28  
// mainClassName: name of program's main class (calls to this class will be skipped)
29  
static Set<S> findFunctionInvocations(LS tok, SS sf, Cl<S> hardReferences, Set<S> haveFunctions, bool includePossiblyMapLikes, S mainClassName default "main") {
30  
  new LinkedHashSet<S> l;
31  
  for (int i : jfindAll(tok, "please include functions")) {
32  
    int j = i + 6;
33  
    while licensed {
34  
      String fname = tok.get(j);
35  
      assertIdentifier("in please include functions section", get(tok, j));
36  
      l.add(fname);
37  
      add(hardReferences, fname);
38  
      j += 2;
39  
      if (eqGet(tok, j, ".")) break;
40  
      while (eqGetOneOf(tok, j, ",", "and")) j += 2;
41  
    }
42  
    clearAllTokens(tok.subList(i, j+2));
43  
  }
44  
  
45  
  for (int i : jfindAll(tok, "please include function *.")) {
46  
    String fname = tok.get(i+6);
47  
    l.add(fname);
48  
    add(hardReferences, fname);
49  
    clearAllTokens(tok.subList(i, i+10));
50  
  }
51  
52  
  int i, n = tok.size();
53  
  boolean result = false;
54  
  for (i = 1; i+2 < n; i += 2) {
55  
    S f = tok.get(i);
56  
    if (!isIdentifier_quick(f)) continue;
57  
    
58  
    // main.<A>bla()
59  
    if (eq(mainClassName, f) && eq(tok.get(i+2), ".") && eqGet(tok, i+4, "<")) {
60  
      i = findEndOfTypeArgs(tok, i+4)+1;
61  
      f = tok.get(i);
62  
      if (!isIdentifier(f)) continue;
63  
    }
64  
    
65  
    if (findFunctionInvocations_debug)
66  
      print("Testing identifier " + f);
67  
      
68  
    // e.g. ... html_findLIs(LS [auto htmlTok] tok) { ... }
69  
    if (eqGet(tok, i-4, "[") && eqGet(tok, i-2, "auto")) {
70  
      // ok
71  
    } else if (eqGet(tok, i-2, ":") && eqGet(tok, i-4, ":") && eqGet(tok, i-6, mainClassName)) {
72  
      // ok, function reference (main::bla)
73  
    } else if (eqGet(tok, i+2, "(")) {
74  
      // ok, normal function invocation
75  
    } else if (includePossiblyMapLikes && eqGet(tok, i+4, "(") && isIdentifier(get(tok, i+2))) {
76  
      // ok, mapLike invocation (bla blubb(...))
77  
    } else
78  
      // not an invocation
79  
      continue;
80  
81  
    if (i == 1
82  
        || !findFunctionInvocations_pre.contains(tok.get(i-2))
83  
        || eqGet(tok, i-2, ".") && eqGet(tok, i-4, mainClassName)) {
84  
      boolean inSF = sf == null || sf.containsKey(f);
85  
      if (findFunctionInvocations_debug)
86  
        print("Possible invocation: " + f + ", inSF: " + inSF);
87  
      if (inSF && !contains(haveFunctions, f) && l.add(f))
88  
        if (findFunctionInvocations_debug)
89  
          print("Found reference to standard function " + f + ": " + lineAroundToken(tok, i));
90  
    }
91  
  }
92  
  
93  
  return l;
94  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

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/35
Text MD5: c1de64f1e33e7bc36449a4e35450cbd3
Transpilation MD5: 287b73eb4c032dd13f4ae997c5509308
Author: stefan
Category: javax / html
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-28 21:12:46
Source code size: 3766 bytes / 94 lines
Pitched / IR pitched: No / No
Views / Downloads: 716 / 1326
Version history: 34 change(s)
Referenced in: [show references]