Libraryless. Click here for Pure Java version (185L/2K).
1 | static LS pineTok(S s) { |
2 | new ArrayList<S> tok; |
3 | int l = s == null ? 0 : s.length(); |
4 | |
5 | int i = 0; |
6 | while (i < l) { |
7 | int j = i; |
8 | char c, d; |
9 | |
10 | // scan for whitespace |
11 | while (j < l) { |
12 | c = s.charAt(j); |
13 | d = j+1 >= l ? '\0' : s.charAt(j+1); |
14 | if (c == ' ' || c == '\t' || c == '\r' || c == '\n') |
15 | ++j; |
16 | else if (c == '/' && d == '*') { |
17 | do ++j; while (j < l && !regionMatches(s, j, "*/")); |
18 | j = Math.min(j+2, l); |
19 | } else if (c == '/' && d == '/') { |
20 | do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0); |
21 | } else |
22 | break; |
23 | } |
24 | |
25 | tok.add(javaTok_substringN(s, i, j)); |
26 | i = j; |
27 | if (i >= l) break; |
28 | c = s.charAt(i); |
29 | d = i+1 >= l ? '\0' : s.charAt(i+1); |
30 | |
31 | // scan for non-whitespace |
32 | |
33 | if (c == '\'' || c == '"') { |
34 | char opener = c; |
35 | ++j; |
36 | while (j < l) { |
37 | int c2 = s.charAt(j); |
38 | if (c2 == opener || c2 == '\n' && opener == '\'') { // allow multi-line strings, but not for ' |
39 | ++j; |
40 | break; |
41 | } else if (c2 == '\\' && j+1 < l) |
42 | j += 2; |
43 | else |
44 | ++j; |
45 | } |
46 | } else if (Character.isJavaIdentifierStart(c)) |
47 | do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || s.charAt(j) == '\'')); // for stuff like "don't" |
48 | else if (Character.isDigit(c)) { |
49 | do ++j; while (j < l && Character.isDigit(s.charAt(j))); |
50 | if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L |
51 | } else |
52 | ++j; |
53 | |
54 | tok.add(s.substring(i, j)); |
55 | i = j; |
56 | } |
57 | |
58 | if ((tok.size() % 2) == 0) tok.add(""); |
59 | ret tok; |
60 | } |
Began life as a copy of #1000688
download show line numbers debug dex old transpilations
Travelled to 2 computer(s): mqqgnosmbjvj, wnsclhtenguj
No comments. add comment
Snippet ID: | #1037256 |
Snippet name: | pineTok function - PineScript tokenizer |
Eternal ID of this version: | #1037256/6 |
Text MD5: | 776c72196c6d0fdfd7f6a224d3b2e2c0 |
Transpilation MD5: | d929a639c987a21d4ef429c71d6cc54a |
Author: | stefan |
Category: | |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2024-08-10 13:47:13 |
Source code size: | 1854 bytes / 60 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 156 / 231 |
Version history: | 5 change(s) |
Referenced in: | [show references] |