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

83
LINES

< > BotCompany Repo | #1030392 // javaTok_vstack - javaTok for VStack [OK, only outer loop virtualized]

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

Libraryless. Click here for Pure Java version (2848L/18K).

1  
srecord javaTok_vstack(S s) implements VStack.Computable<LS> {
2  
  new ArrayList<S> tok;
3  
  int l, i;
4  
  int label; // where are we at in routine
5  
6  
  void goTo(int label) { this.label = label; }
7  
  
8  
  public void step(VStack stack, O subComputationResult) {
9  
    switch (label) {
10  
      case 0:
11  
        l = s == null ? 0 : s.length();
12  
      
13  
      case 1:
14  
        if (i >= l) ret with goTo(2);
15  
        
16  
        int j = i;
17  
        char c, d;
18  
        
19  
        // scan for whitespace
20  
        while (j < l) {
21  
          c = s.charAt(j);
22  
          d = j+1 >= l ? '\0' : s.charAt(j+1);
23  
          if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
24  
            ++j;
25  
          else if (c == '/' && d == '*') {
26  
            do ++j; while (j < l && !regionMatches(s, j, "*/"));
27  
            j = Math.min(j+2, l);
28  
          } else if (c == '/' && d == '/') {
29  
            do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
30  
          } else
31  
            break;
32  
        }
33  
        
34  
        tok.add(javaTok_substringN(s, i, j));
35  
        i = j;
36  
        if (i >= l) break;
37  
        c = s.charAt(i);
38  
        d = i+1 >= l ? '\0' : s.charAt(i+1);
39  
    
40  
        // scan for non-whitespace
41  
        
42  
        // Special JavaX syntax: 'identifier
43  
        if (c == '\'' && Character.isJavaIdentifierStart(d) && i+2 < l && "'\\".indexOf(s.charAt(i+2)) < 0) {
44  
          j += 2;
45  
          while (j < l && Character.isJavaIdentifierPart(s.charAt(j)))
46  
            ++j;
47  
        } else if (c == '\'' || c == '"') {
48  
          char opener = c;
49  
          ++j;
50  
          while (j < l) {
51  
            int c2 = s.charAt(j);
52  
            if (c2 == opener || c2 == '\n' && opener == '\'') { // allow multi-line strings, but not for '
53  
              ++j;
54  
              break;
55  
            } else if (c2 == '\\' && j+1 < l)
56  
              j += 2;
57  
            else
58  
              ++j;
59  
          }
60  
        } else if (Character.isJavaIdentifierStart(c))
61  
          do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || s.charAt(j) == '\'')); // for stuff like "don't"
62  
        else if (Character.isDigit(c)) {
63  
          do ++j; while (j < l && Character.isDigit(s.charAt(j)));
64  
          if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L
65  
        } else if (c == '[' && d == '[') {
66  
          do ++j; while (j < l && !regionMatches(s, j, "]]"));
67  
          j = Math.min(j+2, l);
68  
        } else if (c == '[' && d == '=' && i+2 < l && s.charAt(i+2) == '[') {
69  
          do ++j; while (j+2 < l && !regionMatches(s, j, "]=]"));
70  
          j = Math.min(j+3, l);
71  
        } else
72  
          ++j;
73  
          
74  
        tok.add(javaTok_substringC(s, i, j));
75  
        i = j;
76  
        ret with goTo(1);
77  
        
78  
      case 2:
79  
        if ((tok.size() & 1) == 0) tok.add("");
80  
        stack._return(tok);
81  
    }
82  
  }
83  
}

Author comment

Began life as a copy of #1000688

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1030392
Snippet name: javaTok_vstack - javaTok for VStack [OK, only outer loop virtualized]
Eternal ID of this version: #1030392/8
Text MD5: 3a23d597e756eb8d149fa4857b2a1f09
Transpilation MD5: 9721255d062ff1f5a8a7742217d71f1f
Author: stefan
Category: javax / tokenizing
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-12-11 01:35:15
Source code size: 2842 bytes / 83 lines
Pitched / IR pitched: No / No
Views / Downloads: 209 / 455
Version history: 7 change(s)
Referenced in: [show references]