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

50
LINES

< > BotCompany Repo | #1017480 // Test grouping hygiene [OK, but not that useful maybe]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Libraryless. Click here for Pure Java version (6598L/48K).

1  
!7
2  
3  
p-exp {
4  
  assertEqualsVerbose(true, checkNTokenHygiene("{a b} {c d}"));
5  
  assertEqualsVerbose(false, checkNTokenHygiene("a b} {c d}"));
6  
  assertEqualsVerbose(false, checkNTokenHygiene("{a b} {c d"));
7  
  assertEqualsVerbose(true, checkNTokenHygiene("a b c d"));
8  
  assertEqualsVerbose(false, checkNTokenHygiene("c d}"));
9  
  assertEqualsVerbose(false, checkNTokenHygiene("c d}{"));
10  
  assertEqualsVerbose(true, checkNTokenHygiene("{c d}{"));
11  
  assertEqualsVerbose(true, checkNTokenHygiene("}{c d}"));
12  
  assertEqualsVerbose(false, checkNTokenHygiene("{a b"));
13  
}
14  
15  
sbool checkNTokenHygiene(S s) {
16  
  L<S> tok = javaTokNPunctuation(s);
17  
  if (l(tok) == 1) true;
18  
  ret print("checkForward " + sfu(tok) + ": ", checkForward(tok))
19  
    && print("checkBackward " + sfu(tok) + ": ", checkBackward(tok));
20  
}
21  
22  
sbool checkForward(L<S> tok) {
23  
  int level = 0;
24  
  for (int i = 0; i < l(tok)-1; i += 2) {
25  
    S t = tok.get(i);
26  
    print("t=" + quote(t));
27  
    for (char c : characters(t))
28  
      if (c == '{') ++level;
29  
      else if (c == '}') level = max(0, level-1);
30  
  }
31  
  if (level == 0) true;
32  
  for (char c : characters(last(tok)))
33  
    if (c == '}') if (--level == 0) true;
34  
  false;
35  
}
36  
37  
sbool checkBackward(L<S> tok) {
38  
  int level = 0;
39  
  for (int i = l(tok)-1; i > 0; i -= 2) {
40  
    S t = tok.get(i);
41  
    for (char c : reversedCharacters(print("tok", tok.get(i))))
42  
      if (c == '}') ++level;
43  
      else if (c == '{') level = max(0, level-1);
44  
  }
45  
  print("level at beginning: " + level);
46  
  if (level == 0) true;
47  
  for (char c : reversedCharacters(first(tok)))
48  
    if (c == '{') if (--level == 0) true;
49  
  false;
50  
}

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: #1017480
Snippet name: Test grouping hygiene [OK, but not that useful maybe]
Eternal ID of this version: #1017480/12
Text MD5: 7c41b09e700f40e354893599a7e0d9ee
Transpilation MD5: 93f17095b9a277a547dfa7f33ca3c4d0
Author: stefan
Category: javax / a.i.
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-07-23 21:59:26
Source code size: 1640 bytes / 50 lines
Pitched / IR pitched: No / No
Views / Downloads: 464 / 1049
Version history: 11 change(s)
Referenced in: [show references]