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

75
LINES

< > BotCompany Repo | #1008635 // nlTok5 - ' as separate token

JavaX fragment (include)

1  
static L<S> nlTok5(S s) {
2  
  new L<S> tok;
3  
  int l = l(s);
4  
  
5  
  int i = 0;
6  
  while (i < l) {
7  
    int j = i;
8  
    char c; S cc;
9  
    
10  
    // scan for whitespace
11  
    while (j < l) {
12  
      c = s.charAt(j);
13  
      cc = s.substring(j, Math.min(j+2, l));
14  
      if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
15  
        ++j;
16  
      else if (cc.equals("/*")) {
17  
        do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
18  
        j = Math.min(j+2, l);
19  
      } else if (cc.equals("//")) {
20  
        do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
21  
      } else
22  
        break;
23  
    }
24  
    
25  
    tok.add(s.substring(i, j));
26  
    i = j;
27  
    if (i >= l) break;
28  
    c = s.charAt(i);
29  
    cc = s.substring(i, Math.min(i+2, l));
30  
31  
    // scan for non-whitespace
32  
    if (c == '\u201C' || c == '\u201D') c = '"'; // normalize quotes
33  
    if (c == '"') {
34  
      char opener = c;
35  
      ++j;
36  
      while (j < l) {
37  
        char _c = s.charAt(j);
38  
        if (_c == '\u201C' || _c == '\u201D') _c = '"'; // normalize quotes
39  
        if (_c == opener) {
40  
          ++j;
41  
          break;
42  
        } else if (s.charAt(j) == '\\' && j+1 < l)
43  
          j += 2;
44  
        else
45  
          ++j;
46  
      }
47  
      if (j-1 >= i+1) {
48  
        tok.add(opener + s.substring(i+1, j-1) + opener);
49  
        i = j;
50  
        continue;
51  
      }
52  
    } else if (Character.isJavaIdentifierStart(c))
53  
      do ++j; while (j < l && Character.isJavaIdentifierPart(s.charAt(j));
54  
    else if (Character.isDigit(c))
55  
      do ++j; while (j < l && Character.isDigit(s.charAt(j)));
56  
    else if (cc.equals("[[")) {
57  
      do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
58  
      j = Math.min(j+2, l);
59  
    } else if (cc.equals("[=") && i+2 < l && s.charAt(i+2) == '[') {
60  
      do ++j; while (j+2 < l && !s.substring(j, j+3).equals("]=]"));
61  
      j = Math.min(j+3, l);
62  
    } else if (s.substring(j, Math.min(j+3, l)).equals("..."))
63  
      j += 3;
64  
    else if (c == '$' || c == '#')
65  
      do ++j; while (j < l && Character.isDigit(s.charAt(j)));
66  
    else
67  
      ++j;
68  
69  
    tok.add(s.substring(i, j));
70  
    i = j;
71  
  }
72  
  
73  
  if ((tok.size() % 2) == 0) tok.add("");
74  
  return tok;
75  
}

Author comment

Began life as a copy of #1008572

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1008635
Snippet name: nlTok5 - ' as separate token
Eternal ID of this version: #1008635/5
Text MD5: 0805d7a71f21ec8fe85da9c5179b5cdf
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-07-06 17:47:46
Source code size: 2202 bytes / 75 lines
Pitched / IR pitched: No / No
Views / Downloads: 563 / 610
Version history: 4 change(s)
Referenced in: [show references]