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

64
LINES

< > BotCompany Repo | #1001998 // pythonToJavaTok function - Python tokenizer that converts comments to Java style

JavaX fragment (include)

// no multi-line-strings/docstrings yet

static List<String> pythonToJavaTok(String s) {
  List<String> tok = new ArrayList<String>();
  int l = s.length();
  
  int i = 0;
  while (i < l) {
    int j = i;
    char c; String cc;
    
    // scan whitespace+comments into buf
    new StringBuilder buf;
    while (j < l) {
      c = s.charAt(j);
      cc = s.substring(j, Math.min(j+2, l));
      int k = j;
      if (c == ' ' || c == '\t' || c == '\r' || c == '\n') {
        ++j;
        buf.append(s.substring(k, j));
      } else if (c == '#') {
        do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
        buf.append("//").append(s.substring(k+1, j));
      } else
        break;
    }
    
    tok.add(buf.toString());
    i = j;
    if (i >= l) break;
    c = s.charAt(i);
    cc = s.substring(i, Math.min(i+2, l));

    // scan for non-whitespace
    if (c == '\'' || c == '"') {
      char opener = c;
      ++j;
      while (j < l) {
        if (s.charAt(j) == opener) {
          ++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)));
    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 (cc.equals("[[")) {
      do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
      j = Math.min(j+2, l);
    } else
      ++j;

    tok.add(s.substring(i, j));
    i = j;
  }
  
  if ((tok.size() % 2) == 0) tok.add("");
  return tok;
}

Author comment

Began life as a copy of #758

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: #1001998
Snippet name: pythonToJavaTok function - Python tokenizer that converts comments to Java style
Eternal ID of this version: #1001998/1
Text MD5: 0d6484ab9d28c667286de5c84707ee5a
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-12-11 17:27:37
Source code size: 1752 bytes / 64 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 652 / 906
Referenced in: #1002427 - Accellerating 629 (SPIKE)
#1006654 - Standard functions list 2 (LIVE, continuation of #761)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)