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

83
LINES

< > BotCompany Repo | #1027365 // javaTok_onReader - javaTok as Producer on BufferedReader

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

Libraryless. Click here for Pure Java version (2285L/15K).

1  
static Producer<S> javaTok_onReader(final Reader r) {
2  
  final class X implements Producer<S> {
3  
    new StringBuilder buf; // stores from "i"
4  
    char c, d, e = 'x'; // just not '\0'
5  
    bool inWhiteSpace = true;
6  
    
7  
    *() {
8  
      // fill c, d and e
9  
      nc();
10  
      nc();
11  
      nc();
12  
    }
13  
    
14  
    // get next character(s) into c, d and e
15  
    void nc() ctex {
16  
      c = d;
17  
      d = e;
18  
      if (e == '\0') ret;
19  
      int i = r.read();
20  
      e = i < 0 ? '\0' : (char) i;
21  
    }
22  
    
23  
    void ncSave() {
24  
      if (c != '\0') {
25  
        buf.append(c);
26  
        nc();
27  
      }
28  
    }
29  
    
30  
    public S next() {
31  
      if (inWhiteSpace) {
32  
        inWhiteSpace = false;
33  
        while (c != '\0') {
34  
          if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
35  
            ncSave();
36  
          else if (c == '/' && d == '*') {
37  
            do ncSave(); while (c != '\0' && !(c == '*' && d == '/'));
38  
            ncSave(); ncSave();
39  
          } else if (c == '/' && d == '/') {
40  
            do ncSave(); while (c != '\0' && "\r\n".indexOf(c) < 0);
41  
          } else
42  
            break;
43  
        }
44  
      } else {
45  
        if (c == '\0') null;
46  
        inWhiteSpace = true;
47  
        // scan for non-whitespace
48  
        if (c == '\'' || c == '"') {
49  
          char opener = c;
50  
          ncSave();
51  
          while (c != '\0') {
52  
            if (c == opener || c == '\n') { // end at \n to not propagate unclosed string literal errors
53  
              ncSave();
54  
              break;
55  
            } else if (c == '\\') {
56  
              ncSave();
57  
              ncSave();
58  
            } else
59  
              ncSave();
60  
          }
61  
        } else if (Character.isJavaIdentifierStart(c))
62  
          do ncSave(); while (Character.isJavaIdentifierPart(c) || c == '\''); // for stuff like "don't"
63  
        else if (Character.isDigit(c)) {
64  
          do ncSave(); while (Character.isDigit(c));
65  
          if (c == 'L') ncSave(); // Long constants like 1L
66  
        } else if (c == '[' && d == '[') {
67  
          do ncSave(); while (c != '\0' && !(c == ']' && d == ']'));
68  
          ncSave(); ncSave();
69  
        } else if (c == '[' && d == '=' && e == '[') {
70  
          do ncSave(); while (c != '\0' && !(c == ']' && d == '=' && e == ']'));
71  
          ncSave(); ncSave(); ncSave();
72  
        } else
73  
          ncSave();
74  
      }
75  
        
76  
      S t = buf.toString();
77  
      buf.setLength(0);
78  
      ret t;
79  
    }
80  
  }
81  
  
82  
  ret new X;
83  
}

Author comment

Began life as a copy of #1005981

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: #1027365
Snippet name: javaTok_onReader - javaTok as Producer on BufferedReader
Eternal ID of this version: #1027365/4
Text MD5: 2d834ce918e660fa0c2c98b8cb622f7f
Transpilation MD5: 0c81ab7f97b238d801a6976aa29ba29f
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 14:50:30
Source code size: 2454 bytes / 83 lines
Pitched / IR pitched: No / No
Views / Downloads: 226 / 322
Version history: 3 change(s)
Referenced in: [show references]