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

68
LINES

< > BotCompany Repo | #1005592 // javaTok_streaming function - javaTok using streamed output

JavaX fragment (include)

1  
static abstract class JavaTok_Stream {
2  
  abstract void add(int i, int j);
3  
}
4  
5  
static void javaTok_streaming(S s, JavaTok_Stream stream) {
6  
  int l = s.length();
7  
  
8  
  int i = 0, n = 0;
9  
  while (i < l) {
10  
    int j = i;
11  
    char c, d;
12  
    
13  
    // scan for whitespace
14  
    while (j < l) {
15  
      c = s.charAt(j);
16  
      d = j+1 >= l ? '\0' : s.charAt(j+1);
17  
      if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
18  
        ++j;
19  
      else if (c == '/' && d == '*') {
20  
        do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
21  
        j = Math.min(j+2, l);
22  
      } else if (c == '/' && d == '/') {
23  
        do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
24  
      } else
25  
        break;
26  
    }
27  
    
28  
    stream.add(i, j);
29  
    ++n;
30  
    i = j;
31  
    if (i >= l) break;
32  
    c = s.charAt(i);
33  
    d = i+1 >= l ? '\0' : s.charAt(i+1);
34  
35  
    // scan for non-whitespace
36  
    if (c == '\'' || c == '"') {
37  
      char opener = c;
38  
      ++j;
39  
      while (j < l) {
40  
        if (s.charAt(j) == opener || s.charAt(j) == '\n') { // end at \n to not propagate unclosed string literal errors
41  
          ++j;
42  
          break;
43  
        } else if (s.charAt(j) == '\\' && j+1 < l)
44  
          j += 2;
45  
        else
46  
          ++j;
47  
      }
48  
    } else if (Character.isJavaIdentifierStart(c))
49  
      do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); // for stuff like "don't"
50  
    else if (Character.isDigit(c)) {
51  
      do ++j; while (j < l && Character.isDigit(s.charAt(j)));
52  
      if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L
53  
    } else if (c == '[' && d == '[') {
54  
      do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
55  
      j = Math.min(j+2, l);
56  
    } else if (c == '[' && d == '=' && i+2 < l && s.charAt(i+2) == '[') {
57  
      do ++j; while (j+2 < l && !s.substring(j, j+3).equals("]=]"));
58  
      j = Math.min(j+3, l);
59  
    } else
60  
      ++j;
61  
62  
    stream.add(i, j);
63  
    ++n;
64  
    i = j;
65  
  }
66  
  
67  
  if ((n % 2) == 0) stream.add(i, i);
68  
}

Author comment

Began life as a copy of #1000688

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1005592
Snippet name: javaTok_streaming function - javaTok using streamed output
Eternal ID of this version: #1005592/1
Text MD5: 29a1dc6b0d2aa4c8115d0dd44dd2473b
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-11-28 20:14:52
Source code size: 2066 bytes / 68 lines
Pitched / IR pitched: No / No
Views / Downloads: 554 / 556
Referenced in: [show references]