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

76
LINES

< > BotCompany Repo | #1008762 // javaTokC_noMLS_onReader - javaTokC as Producer on BufferedReader, no multi-line strings

JavaX fragment (include)

1  
static Producer<S> javaTokC_noMLS_onReader(final BufferedReader 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  
    
6  
    *() {
7  
      // fill c, d and e
8  
      nc();
9  
      nc();
10  
      nc();
11  
    }
12  
    
13  
    // get next character(s) into c, d and e
14  
    void nc() ctex {
15  
      c = d;
16  
      d = e;
17  
      if (e == '\0') ret;
18  
      int i = r.read();
19  
      e = i < 0 ? '\0'
20  
        : i == '\0' ? '_' // shouldn't happen anymore
21  
        : (char) i;
22  
    }
23  
    
24  
    void ncSave() {
25  
      if (c != '\0') {
26  
        buf.append(c);
27  
        nc();
28  
      }
29  
    }
30  
    
31  
    public S next() {
32  
      // scan for whitespace
33  
      while (c != '\0') {
34  
        if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
35  
          nc();
36  
        else if (c == '/' && d == '*') {
37  
          do nc(); while (c != '\0' && !(c == '*' && d == '/'));
38  
          nc(); nc();
39  
        } else if (c == '/' && d == '/') {
40  
          do nc(); while (c != '\0' && "\r\n".indexOf(c) < 0);
41  
        } else
42  
          break;
43  
      }
44  
      
45  
      if (c == '\0') ret null;
46  
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
67  
        ncSave();
68  
        
69  
      S t = buf.toString();
70  
      buf.setLength(0);
71  
      ret t;
72  
    }
73  
  }
74  
  
75  
  ret new X;
76  
}

Author comment

Began life as a copy of #1005981

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1008762
Snippet name: javaTokC_noMLS_onReader - javaTokC as Producer on BufferedReader, no multi-line strings
Eternal ID of this version: #1008762/3
Text MD5: 0de3d66eb9b48d4672cb1ef671229e3c
Author: stefan
Category: javax / parsing
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-06-02 23:57:02
Source code size: 2023 bytes / 76 lines
Pitched / IR pitched: No / No
Views / Downloads: 578 / 648
Version history: 2 change(s)
Referenced in: [show references]