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

71
LINES

< > BotCompany Repo | #1034624 // javaTok_producer - javaTok as Producer

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

Transpiled version (116L) is out of date.

1  
static Producer<S> javaTok_producer(S s) {
2  
  ret new Producer<S> {
3  
    final int l = strL(s);
4  
    int i = 0; // current character index
5  
    bool inWhitespace = true;
6  
    
7  
    public S next() {
8  
      int j = i;
9  
      char c, d;
10  
        
11  
      if (inWhitespace) {
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  
        S t = quickSubstring(s, i, j);      
28  
        i = j;
29  
        inWhitespace = false;
30  
        ret t;
31  
      } else {
32  
        if (i >= l) null; // end
33  
        
34  
        c = s.charAt(i);
35  
        d = i+1 >= l ? '\0' : s.charAt(i+1);
36  
    
37  
        // scan for non-whitespace
38  
        if (c == '\'' || c == '"') {
39  
          char opener = c;
40  
          ++j;
41  
          while (j < l) {
42  
            if (s.charAt(j) == opener || s.charAt(j) == '\n') { // end at \n to not propagate unclosed string literal errors
43  
              ++j;
44  
              break;
45  
            } else if (s.charAt(j) == '\\' && j+1 < l)
46  
              j += 2;
47  
            else
48  
              ++j;
49  
          }
50  
        } else if (Character.isJavaIdentifierStart(c))
51  
          do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); // for stuff like "don't"
52  
        else if (Character.isDigit(c)) {
53  
          do ++j; while (j < l && Character.isDigit(s.charAt(j)));
54  
          if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L
55  
        } else if (c == '[' && d == '[') {
56  
          do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
57  
          j = Math.min(j+2, l);
58  
        } else if (c == '[' && d == '=' && i+2 < l && s.charAt(i+2) == '[') {
59  
          do ++j; while (j+2 < l && !s.substring(j, j+3).equals("]=]"));
60  
          j = Math.min(j+3, l);
61  
        } else
62  
          ++j;
63  
          
64  
        S t = quickSubstring(s, i, j);
65  
        i = j;
66  
        inWhitespace = true;
67  
        ret t;
68  
      }
69  
    }
70  
  };
71  
}

Author comment

Began life as a copy of #1005977

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034624
Snippet name: javaTok_producer - javaTok as Producer
Eternal ID of this version: #1034624/5
Text MD5: d611bcb14b30b48d485ae07cdb963bb6
Author: stefan
Category: javax / parsing
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-02-24 19:21:36
Source code size: 2407 bytes / 71 lines
Pitched / IR pitched: No / No
Views / Downloads: 130 / 186
Version history: 4 change(s)
Referenced in: [show references]