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).

!7

p-exp {
  assertEqualsVerbose(true, checkNTokenHygiene("{a b} {c d}"));
  assertEqualsVerbose(false, checkNTokenHygiene("a b} {c d}"));
  assertEqualsVerbose(false, checkNTokenHygiene("{a b} {c d"));
  assertEqualsVerbose(true, checkNTokenHygiene("a b c d"));
  assertEqualsVerbose(false, checkNTokenHygiene("c d}"));
  assertEqualsVerbose(false, checkNTokenHygiene("c d}{"));
  assertEqualsVerbose(true, checkNTokenHygiene("{c d}{"));
  assertEqualsVerbose(true, checkNTokenHygiene("}{c d}"));
  assertEqualsVerbose(false, checkNTokenHygiene("{a b"));
}

sbool checkNTokenHygiene(S s) {
  L<S> tok = javaTokNPunctuation(s);
  if (l(tok) == 1) true;
  ret print("checkForward " + sfu(tok) + ": ", checkForward(tok))
    && print("checkBackward " + sfu(tok) + ": ", checkBackward(tok));
}

sbool checkForward(L<S> tok) {
  int level = 0;
  for (int i = 0; i < l(tok)-1; i += 2) {
    S t = tok.get(i);
    print("t=" + quote(t));
    for (char c : characters(t))
      if (c == '{') ++level;
      else if (c == '}') level = max(0, level-1);
  }
  if (level == 0) true;
  for (char c : characters(last(tok)))
    if (c == '}') if (--level == 0) true;
  false;
}

sbool checkBackward(L<S> tok) {
  int level = 0;
  for (int i = l(tok)-1; i > 0; i -= 2) {
    S t = tok.get(i);
    for (char c : reversedCharacters(print("tok", tok.get(i))))
      if (c == '}') ++level;
      else if (c == '{') level = max(0, level-1);
  }
  print("level at beginning: " + level);
  if (level == 0) true;
  for (char c : reversedCharacters(first(tok)))
    if (c == '{') if (--level == 0) true;
  false;
}

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: 463 / 1048
Version history: 11 change(s)
Referenced in: