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

71
LINES

< > BotCompany Repo | #1034624 // javaTok_producer - javaTok as Producer

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

Transpiled version (116L) is out of date.

static Producer<S> javaTok_producer(S s) {
  ret new Producer<S> {
    final int l = strL(s);
    int i = 0; // current character index
    bool inWhitespace = true;
    
    public S next() {
      int j = i;
      char c, d;
        
      if (inWhitespace) {
        // scan for whitespace
        while (j < l) {
          c = s.charAt(j);
          d = j+1 >= l ? '\0' : s.charAt(j+1);
          if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
            ++j;
          else if (c == '/' && d == '*') {
            do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
            j = Math.min(j+2, l);
          } else if (c == '/' && d == '/') {
            do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
          } else
            break;
        }
        
        S t = quickSubstring(s, i, j);      
        i = j;
        inWhitespace = false;
        ret t;
      } else {
        if (i >= l) null; // end
        
        c = s.charAt(i);
        d = i+1 >= l ? '\0' : s.charAt(i+1);
    
        // scan for non-whitespace
        if (c == '\'' || c == '"') {
          char opener = c;
          ++j;
          while (j < l) {
            if (s.charAt(j) == opener || s.charAt(j) == '\n') { // end at \n to not propagate unclosed string literal errors
              ++j;
              break;
            } else if (s.charAt(j) == '\\' && j+1 < l)
              j += 2;
            else
              ++j;
          }
        } else if (Character.isJavaIdentifierStart(c))
          do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); // for stuff like "don't"
        else if (Character.isDigit(c)) {
          do ++j; while (j < l && Character.isDigit(s.charAt(j)));
          if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L
        } else if (c == '[' && d == '[') {
          do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
          j = Math.min(j+2, l);
        } else if (c == '[' && d == '=' && i+2 < l && s.charAt(i+2) == '[') {
          do ++j; while (j+2 < l && !s.substring(j, j+3).equals("]=]"));
          j = Math.min(j+3, l);
        } else
          ++j;
          
        S t = quickSubstring(s, i, j);
        i = j;
        inWhitespace = true;
        ret t;
      }
    }
  };
}

Author comment

Began life as a copy of #1005977

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034624
Snippet name: javaTok_producer - javaTok as Producer
Eternal ID of this version: #1034624/5
Text MD5: d611bcb14b30b48d485ae07cdb963bb6
Author: stefan
Category: javax / parsing
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-02-24 19:21:36
Source code size: 2407 bytes / 71 lines
Pitched / IR pitched: No / No
Views / Downloads: 128 / 183
Version history: 4 change(s)
Referenced in: [show references]