Компиляторы 4 лаба (7) Position

 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
import java.util.*;
import java.lang.Object;
public class Position implements Comparable<Position> {
private String text;
private int line, pos, index;
public Position (String text) {
this.text = text;
line = pos = 1;
index = 0;
}
public String Text(){return text;}
public int Line(){return line;}
public int Pos(){return pos;}
public int Index(){return index;}
public Position copyPos(){
return new Position(text);
}
public int compareTo (Position other){
return Integer.compare(index, other.index);
}
public String toString(){
return "(" + line + ',' + pos + ')';
}
public int cP() {
if (index == text.length())
return -1;
return Character.codePointAt(text, index);
}
public boolean isWhiteSpace() {
return Character.isWhitespace(cP());
}
public boolean isLetter() {
return Character.isLetter(cP());
}
public boolean isLetterOrDigit() {
return Character.isLetterOrDigit(cP());
}
public boolean isDecimalDigit() {
return (index != text.length() &&
text.charAt(index) >= '0' && text.charAt(index) <= '9');
}
public boolean isNewLine() {
if (index == text.length())
return true;
if (text.charAt(index) == '\r' &&
index + 1 < text.length())
return (text.charAt(index + 1) == '\n');
return (text.charAt(index) == '\n');
}
public void iterate() {
if (index < text.length()) {
if (isNewLine()) {
if (text.charAt(index) == '\r')
index++;
line++;
pos = 1;
} else {
if (Character.isHighSurrogate(text.charAt(index))) {
index++;
}
pos++;
}
index++;
}
}
}