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

59
LINES

< > BotCompany Repo | #1017474 // groupUsingWordTree - uses curly brackets; values of word tree don't matter [dev.]

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

Libraryless. Click here for Pure Java version (4150L/27K).

1  
sbool groupUsingWordTree_debug;
2  
sbool groupUsingWordTree_printReplacements;
3  
4  
sS groupUsingWordTree(S input, Map<S, O> tree) {  
5  
  ret join(groupUsingWordTree(javaTok(input), tree));
6  
}
7  
  
8  
static L<S> groupUsingWordTree(L<S> tok, Map<S, O> tree) {  
9  
  for (int i = 1; i < l(tok); )
10  
    i = groupUsingWordTree_scanFrom(tok, i, tree);
11  
  ret tok;
12  
}
13  
      
14  
// returns index of first unreplaced token
15  
// every replacement ends up as a single token plus some ""
16  
static int groupUsingWordTree_scanFrom(L<S> tok, int i, Map<S, O> tree) {
17  
  int j = i;
18  
  S best = null; // best replacement found
19  
  int bestIndex = 0; // till where to replace
20  
  
21  
  while true {
22  
    if (j > i) { // no default in root!
23  
      S def = cast tree.get("<default>");
24  
      if (def != null) {
25  
        best = join(subList(tok, i, j+1)); bestIndex = j;
26  
        //print("best solution: " + sfu(bestSolution));
27  
      }
28  
    }
29  
   
30  
    if (j >= l(tok)) break;
31  
    
32  
    O t = tree.get(tok.get(j));
33  
    if (groupUsingWordTree_debug)
34  
      print(i + "-" + j + " / " + joinSubList(tok, i, j+1) + " => " + sfu(t));
35  
    if (t == null) break;
36  
    if (!t instanceof Map) {
37  
      int k = j+2;
38  
      
39  
      // skip if already grouped
40  
      if (!(eqGet(tok, i-2, "{") && eqGet(tok, k, "}"))) {
41  
        LS subList = subList(tok, i, k-1);
42  
        S replacement = curly(join(subList));
43  
        if (groupUsingWordTree_printReplacements)
44  
          print("groupUsingWordTree: replacing " + sfu(subList) + " with " + quote(replacement));
45  
        replaceTokens(tok, i, k-1, replacement);
46  
      }
47  
      ret k;
48  
    }
49  
    tree = (Map) t;
50  
    j += 2;
51  
  }
52  
  
53  
  if (best != null) {
54  
    int k = bestIndex;
55  
    replaceTokens(tok, i, k-1, best);
56  
    ret k;
57  
  }
58  
  ret i+2;
59  
}

Author comment

Began life as a copy of #1014205

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: #1017474
Snippet name: groupUsingWordTree - uses curly brackets; values of word tree don't matter [dev.]
Eternal ID of this version: #1017474/3
Text MD5: 7e594f96dc3d5c7aee1b21adee14f7ad
Transpilation MD5: 88169bceb46042ed170340aa601432c8
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-09 11:04:08
Source code size: 1758 bytes / 59 lines
Pitched / IR pitched: No / No
Views / Downloads: 442 / 497
Version history: 2 change(s)
Referenced in: [show references]