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

43
LINES

< > BotCompany Repo | #1002118 // getBracketMap - uses curly and round brackets (also takes x-tok [token lists containing sublists])

JavaX fragment (include)

1  
// map: index of opening bracket -> index of closing bracket
2  
static Map<Int, Int> getBracketMap(L tok) {
3  
  ret getBracketMap(tok, getBracketMap_opening, getBracketMap_closing);
4  
}
5  
6  
static Map<Int> getBracketMap(L tok, Collection<S> opening, Collection<S> closing) {
7  
  ret getBracketMap(tok, opening, closing, 0, l(tok));
8  
}
9  
10  
static Map<Int> getBracketMap(L tok, Cl<S> opening, Cl<S> closing, int from, int to) {
11  
  new TreeMap<Int> map;
12  
  new L<Int> stack;
13  
  for (int i = from|1; i < to; i+= 2) {
14  
    O t = tok.get(i);
15  
    if (opening.contains(t))
16  
      stack.add(i);
17  
    else if (closing.contains(t))
18  
      if (!empty(stack))
19  
        map.put(liftLast(stack), i);
20  
  }
21  
  ret map;
22  
}
23  
24  
static Map<Int> getBracketMap(LS tok, IPred<S> opening, IPred<S> closing) {
25  
  ret getBracketMap(tok, opening, closing, 0, l(tok));
26  
}
27  
28  
static Map<Int> getBracketMap(LS tok, IPred<S> opening, IPred<S> closing, int from, int to) {
29  
  new TreeMap<Int> map;
30  
  new L<Int> stack;
31  
  for (int i = from|1; i < to; i+= 2) {
32  
    S t = tok.get(i);
33  
    if (opening.get(t))
34  
      stack.add(i);
35  
    else if (closing.get(t))
36  
      if (!empty(stack))
37  
        map.put(liftLast(stack), i);
38  
  }
39  
  ret map;
40  
}
41  
42  
static Set<S> getBracketMap_opening = lithashset("{", "(");
43  
static Set<S> getBracketMap_closing = lithashset("}", ")");

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1002118
Snippet name: getBracketMap - uses curly and round brackets (also takes x-tok [token lists containing sublists])
Eternal ID of this version: #1002118/12
Text MD5: e557f5ee3c0661211de1cada5959e009
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-01 18:21:00
Source code size: 1327 bytes / 43 lines
Pitched / IR pitched: No / No
Views / Downloads: 754 / 1658
Version history: 11 change(s)
Referenced in: [show references]