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

72
LINES

< > BotCompany Repo | #1027377 // javaTokWithUnifiedNumbersAndIdentifiers

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

Libraryless. Click here for Pure Java version (107L/1K).

1  
// TODO: extended multi-line strings
2  
3  
static LS javaTokWithUnifiedNumbersAndIdentifiers(String s) {
4  
  ArrayList<String> tok = new ArrayList();
5  
  int l = s == null ? 0 : s.length();
6  
  
7  
  int i = 0, n = 0;
8  
  while (i < l) {
9  
    int j = i;
10  
    char c, d;
11  
    
12  
    // scan for whitespace
13  
    while (j < l) {
14  
      c = s.charAt(j);
15  
      d = j+1 >= l ? '\0' : s.charAt(j+1);
16  
      if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
17  
        ++j;
18  
      else if (c == '/' && d == '*') {
19  
        do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
20  
        j = Math.min(j+2, l);
21  
      } else if (c == '/' && d == '/') {
22  
        do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
23  
      } else
24  
        break;
25  
    }
26  
    
27  
    tok.add(javaTok_substringN(s, i, j));
28  
    ++n;
29  
    i = j;
30  
    if (i >= l) break;
31  
    c = s.charAt(i);
32  
    d = i+1 >= l ? '\0' : s.charAt(i+1);
33  
34  
    // scan for non-whitespace
35  
    
36  
    // Special JavaX syntax: 'identifier
37  
    if (c == '\'' && Character.isJavaIdentifierStart(d) && i+2 < l && "'\\".indexOf(s.charAt(i+2)) < 0) {
38  
      j += 2;
39  
      while (j < l && Character.isJavaIdentifierPart(s.charAt(j)))
40  
        ++j;
41  
    } else if (c == '\'' || c == '"') {
42  
      char opener = c;
43  
      ++j;
44  
      while (j < l) {
45  
        int c2 = s.charAt(j);
46  
        if (c2 == opener || c2 == '\n' && opener == '\'') { // allow multi-line strings, but not for '
47  
          ++j;
48  
          break;
49  
        } else if (c2 == '\\' && j+1 < l)
50  
          j += 2;
51  
        else
52  
          ++j;
53  
      }
54  
    } else if (Character.isJavaIdentifierStart(c) || Character.isDigit(c)) {
55  
      do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || s.charAt(j) == '\'')); // for stuff like "don't"
56  
    } else if (c == '[' && d == '[') {
57  
      do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
58  
      j = Math.min(j+2, l);
59  
    } else if (c == '[' && d == '=' && 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
63  
      ++j;
64  
      
65  
    tok.add(javaTok_substringC(s, i, j));
66  
    ++n;
67  
    i = j;
68  
  }
69  
  
70  
  if ((tok.size() % 2) == 0) tok.add("");
71  
  ret tok;
72  
}

Author comment

Began life as a copy of #1000688

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1027377
Snippet name: javaTokWithUnifiedNumbersAndIdentifiers
Eternal ID of this version: #1027377/1
Text MD5: bacfd33bbb1accc32b6448fdc3aec223
Transpilation MD5: ec1aa8019d613083dd39a6c0b9ff638c
Author: stefan
Category: javax / parsing
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-03-15 16:35:03
Source code size: 2253 bytes / 72 lines
Pitched / IR pitched: No / No
Views / Downloads: 190 / 273
Referenced in: [show references]