-
-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathBrackets.java
More file actions
137 lines (124 loc) · 2.92 KB
/
Brackets.java
File metadata and controls
137 lines (124 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package processing.app.syntax;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Jonathan Feinberg <jdf@pobox.com>
*
*/
public class Brackets {
private volatile List<Integer> offsets = null;
public void invalidate() {
offsets = null;
}
public int findMatchingBracket(final String text, final int pos) {
if (pos < 0 || pos >= text.length())
return -1;
final char alpha = text.charAt(pos);
final char beta;
final int direction;
switch (alpha) {
case '(':
beta = ')';
direction = 1;
break;
case ')':
beta = '(';
direction = -1;
break;
case '[':
beta = ']';
direction = 1;
break;
case ']':
beta = '[';
direction = -1;
break;
case '{':
beta = '}';
direction = 1;
break;
case '}':
beta = '{';
direction = -1;
break;
default:
return -1;
}
if (offsets == null)
parse(text);
// find this bracket
int p;
for (p = 0; p < offsets.size(); p++)
if (offsets.get(p) == pos)
break;
if (p == offsets.size()) {
return -1;
}
int depth = 1;
for (p += direction; p >= 0 && p < offsets.size(); p += direction) {
final int offset = offsets.get(p);
final char c = text.charAt(offset);
if (c == alpha)
depth++;
else if (c == beta)
depth--;
if (depth == 0)
return offset;
}
return -1;
}
int pos;
private void parse(final String text) {
offsets = new ArrayList<Integer>();
final int len = text.length();
for (pos = 0; pos < len; pos++) {
final char c = text.charAt(pos);
if (c == '/' && (pos < len - 1)) {
final char d = text.charAt(++pos);
if (d == '/') {
readComment(text);
} else if (d == '*') {
readMLComment(text);
} else pos--; // Go back because there isn't a comment.
} else if (c == '"' || c == '\'') {
readString(text, c);
} else if (c == '{' || c == '[' || c == '(' || c == '}' || c == ']'
|| c == ')') {
offsets.add(pos);
}
}
}
private void readString(final String text, final char q) {
final int len = text.length();
for (pos++; pos < len; pos++) {
final char c = text.charAt(pos);
if (c == q) {
return;
}
if (c == '\\') {
pos++;
}
}
}
private void readComment(final String text) {
final int len = text.length();
for (pos++; pos < len; pos++)
if (text.charAt(pos) == '\n') {
return;
}
}
private void readMLComment(final String text) {
final int len = text.length();
for (pos++; pos < len; pos++) {
final char c = text.charAt(pos);
if (c == '*' && (pos < len - 1)) {
final char d = text.charAt(pos + 1);
if (d == '/') {
pos++;
return;
}
}
}
}
}