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

84
LINES

< > BotCompany Repo | #1000688 - javaTok function - new Java tokenizer

JavaX fragment (include)

1  
// TODO: extended multi-line strings
2  
3  
static int javaTok_n, javaTok_elements;
4  
static bool javaTok_opt;
5  
6  
static List<String> javaTok(String s) {
7  
  ++javaTok_n;
8  
  ArrayList<String> tok = new ArrayList();
9  
  int l = s == null ? 0 : s.length();
10  
  
11  
  int i = 0, n = 0;
12  
  while (i < l) {
13  
    int j = i;
14  
    char c, d;
15  
    
16  
    // scan for whitespace
17  
    while (j < l) {
18  
      c = s.charAt(j);
19  
      d = j+1 >= l ? '\0' : s.charAt(j+1);
20  
      if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
21  
        ++j;
22  
      else if (c == '/' && d == '*') {
23  
        do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
24  
        j = Math.min(j+2, l);
25  
      } else if (c == '/' && d == '/') {
26  
        do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
27  
      } else
28  
        break;
29  
    }
30  
    
31  
    tok.add(javaTok_substringN(s, i, j));
32  
    ++n;
33  
    i = j;
34  
    if (i >= l) break;
35  
    c = s.charAt(i);
36  
    d = i+1 >= l ? '\0' : s.charAt(i+1);
37  
38  
    // scan for non-whitespace
39  
    
40  
    // Special JavaX syntax: 'identifier
41  
    if (c == '\'' && Character.isJavaIdentifierStart(d) && i+2 < l && "'\\".indexOf(s.charAt(i+2)) < 0) {
42  
      j += 2;
43  
      while (j < l && Character.isJavaIdentifierPart(s.charAt(j)))
44  
        ++j;
45  
    } else if (c == '\'' || c == '"') {
46  
      char opener = c;
47  
      ++j;
48  
      while (j < l) {
49  
        int c2 = s.charAt(j);
50  
        if (c2 == opener || c2 == '\n' && opener == '\'') { // allow multi-line strings, but not for '
51  
          ++j;
52  
          break;
53  
        } else if (c2 == '\\' && j+1 < l)
54  
          j += 2;
55  
        else
56  
          ++j;
57  
      }
58  
    } else if (Character.isJavaIdentifierStart(c))
59  
      do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || s.charAt(j) == '\'')); // for stuff like "don't"
60  
    else if (Character.isDigit(c)) {
61  
      do ++j; while (j < l && Character.isDigit(s.charAt(j)));
62  
      if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L
63  
    } else if (c == '[' && d == '[') {
64  
      do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
65  
      j = Math.min(j+2, l);
66  
    } else if (c == '[' && d == '=' && i+2 < l && s.charAt(i+2) == '[') {
67  
      do ++j; while (j+2 < l && !s.substring(j, j+3).equals("]=]"));
68  
      j = Math.min(j+3, l);
69  
    } else
70  
      ++j;
71  
      
72  
    tok.add(javaTok_substringC(s, i, j));
73  
    ++n;
74  
    i = j;
75  
  }
76  
  
77  
  if ((tok.size() % 2) == 0) tok.add("");
78  
  javaTok_elements += tok.size();
79  
  return tok;
80  
}
81  
82  
static List<String> javaTok(List<String> tok) {
83  
  return javaTokWithExisting(join(tok), tok);
84  
}

Author comment

Began life as a copy of #1000647

download  show line numbers  debug dex   

Travelled to 13 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, tslmcundralx, tvejysmllsmz, xrpafgyirdlv

No comments. add comment

Snippet ID: #1000688
Snippet name: javaTok function - new Java tokenizer
Eternal ID of this version: #1000688/15
Text MD5: 191ec145eedf331eb16850f2d7ab5af3
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-07-02 20:30:28
Source code size: 2602 bytes / 84 lines
Pitched / IR pitched: No / No
Views / Downloads: 617 / 7735
Version history: 14 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1000688 & http://1000688.tinybrain.de