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

80
LINES

< > BotCompany Repo | #1005981 // javaTokC_onReader - javaTokC as Producer on BufferedReader

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

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

static Producer<S> javaTokC_onReader(final Reader r) {
  final class X implements Producer<S> {
    new StringBuilder buf; // stores from "i"
    char c, d, e = 'x'; // just not '\0'
    
    *() {
      // fill c, d and e
      nc();
      nc();
      nc();
    }
    
    // get next character(s) into c, d and e
    void nc() ctex {
      c = d;
      d = e;
      if (e == '\0') ret;
      int i = r.read();
      e = i < 0 ? '\0' : (char) i;
    }
    
    void ncSave() {
      if (c != '\0') {
        buf.append(c);
        nc();
      }
    }
    
    public S next() {
      // scan for whitespace
      while (c != '\0') {
        if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
          nc();
        else if (c == '/' && d == '*') {
          do nc(); while (c != '\0' && !(c == '*' && d == '/'));
          nc(); nc();
        } else if (c == '/' && d == '/') {
          do nc(); while (c != '\0' && "\r\n".indexOf(c) < 0);
        } else
          break;
      }
      
      if (c == '\0') ret null;

      // scan for non-whitespace
      if (c == '\'' || c == '"') {
        char opener = c;
        ncSave();
        while (c != '\0') {
          if (c == opener || c == '\n') { // end at \n to not propagate unclosed string literal errors
            ncSave();
            break;
          } else if (c == '\\') {
            ncSave();
            ncSave();
          } else
            ncSave();
        }
      } else if (Character.isJavaIdentifierStart(c))
        do ncSave(); while (Character.isJavaIdentifierPart(c) || c == '\''); // for stuff like "don't"
      else if (Character.isDigit(c)) {
        do ncSave(); while (Character.isDigit(c));
        if (c == 'L') ncSave(); // Long constants like 1L
      } else if (c == '[' && d == '[') {
        do ncSave(); while (c != '\0' && !(c == ']' && d == ']'));
        ncSave(); ncSave();
      } else if (c == '[' && d == '=' && e == '[') {
        do ncSave(); while (c != '\0' && !(c == ']' && d == '=' && e == ']'));
        ncSave(); ncSave(); ncSave();
      } else
        ncSave();
        
      S t = buf.toString();
      buf.setLength(0);
      ret t;
    }
  }
  
  ret new X;
}

Author comment

Began life as a copy of #1005977

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1005981
Snippet name: javaTokC_onReader - javaTokC as Producer on BufferedReader
Eternal ID of this version: #1005981/2
Text MD5: 6e5c4fd8f60f1f97060ed886de21c8fd
Transpilation MD5: 747cb39971630313eb280a7268bf890e
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:42:34
Source code size: 2257 bytes / 80 lines
Pitched / IR pitched: No / No
Views / Downloads: 572 / 645
Version history: 1 change(s)
Referenced in: [show references]