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).

static Producer<S> javaTok_onReader(final Reader r) {
  final class X implements Producer<S> {
    new StringBuilder buf; // stores from "i"
    char c, d, e = 'x'; // just not '\0'
    bool inWhiteSpace = true;
    
    *() {
      // 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() {
      if (inWhiteSpace) {
        inWhiteSpace = false;
        while (c != '\0') {
          if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
            ncSave();
          else if (c == '/' && d == '*') {
            do ncSave(); while (c != '\0' && !(c == '*' && d == '/'));
            ncSave(); ncSave();
          } else if (c == '/' && d == '/') {
            do ncSave(); while (c != '\0' && "\r\n".indexOf(c) < 0);
          } else
            break;
        }
      } else {
        if (c == '\0') null;
        inWhiteSpace = true;
        // 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 #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: 225 / 319
Version history: 3 change(s)
Referenced in: [show references]