- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenParser.java
More file actions
Latest commit
133 lines (102 loc) · 4.28 KB
/
Copy pathTokenParser.java
File metadata and controls
133 lines (102 loc) · 4.28 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
importjava.util.regex.*;
importjava.util.ArrayList;
importjava.util.Arrays;
publicclassTokenParser {
publicTokenTabletokenTable = newTokenTable();
// Return whether the string is a number
publicbooleanisNumber(Strings) {
returnPattern.matches("^\\d*\\.?\\d+$", s);
}
// Return whether the string is a digit
publicbooleanisDigit(Strings) {
returnPattern.matches("^\\d$", s);
}
// Return whether the String s is a decimal point
// for the given fragment
publicbooleanisDecimal(Stringfragment, Strings) {
return (s.equals(".") ? (fragment.replaceAll("[^\\.]", "").length() == 0) : false);
}
// Return whether the String is a space
publicbooleanisSpace(Strings) {
returns.equals(" ");
}
// Return whether the String is an End Of Line character
publicbooleanisEOL(Strings) {
returns.equals("\n");
}
publicbooleanisStringCharacter(Strings) {
return (s.equals("'") || s.equals("\""));
}
publicbooleanisSubString(Strings) {
return (s.indexOf('"') >= 0 || s.indexOf("'") >= 0);
}
publicbooleanisDelimiter(Strings) {
return (this.isSpace(s) || this.isEOL(s) || tokenTable.isToken(s));
}
publicbooleanisAssignment(Strings, Stringc) {
return (s.substring(s.length() - 1) + c).equals(":=");
}
publicbooleanisLogical(Strings) {
// return false;
return (Pattern.matches("^>|>=|<>|=|<=|<$", s));
}
publicTokentransform(Stringfragment) {
if (tokenTable.isToken(fragment))
returnnewToken(fragment, fragment, tokenTable.getCode(fragment));
if (this.isSpace(fragment))
returnnewToken(fragment, "SPACE", tokenTable.getCode("SPACE"));
if (this.isEOL(fragment))
returnnewToken(fragment, "EOL", tokenTable.getCode("EOL"));
if (this.isSubString(fragment))
returnnewToken(fragment, "STRING", tokenTable.getCode("STRING"));
if (this.isNumber(fragment))
returnnewToken(fragment, "NUMBER", tokenTable.getCode("NUMBER"));
returnnewToken(fragment, "IDENTIFIER", tokenTable.getCode("IDENTIFIER"));
}
publicTokengetTokenObject(Stringstr) {
ArrayList<String> chars = newArrayList<String>(Arrays.asList(str.split("(?!^)")));
StringcurrentChar = "";
StringnextChar = "";
Stringfragment = "";
do {
currentChar = chars.remove(0);
fragment += currentChar;
nextChar = chars.isEmpty() ? "" : chars.get(0);
// Assignment opperator check
// ABC:= -> fragment ABC
if (this.isAssignment(fragment, nextChar)) {
if (fragment.equals(":")) fragment += nextChar;
elsefragment = fragment.substring(0, fragment.length() - 1);
}
// String sub loop
if (this.isStringCharacter(fragment)) {
Stringstringpart;
do {
stringpart = chars.isEmpty() ? "" : chars.remove(0);
fragment += stringpart;
} while (!this.isStringCharacter(stringpart) && !chars.isEmpty());
// TODO: THROW Exception for unbound strings
// if (!isStringCharacter(stringpart)) {
// throw new Exception("No end of string in data", str);
// }
break;
}
// Number sub loop
if (this.isDigit(fragment)) {
Stringnumberpart = chars.isEmpty() ? "" : chars.remove(0);
while (this.isDigit(numberpart) || this.isDecimal(fragment, numberpart)) {
fragment += numberpart;
numberpart = chars.isEmpty() ? "" : chars.remove(0);
}
// TODO: THROW Exception for bad number
//if (!this.isNumber(fragment)) {
// throw new Exception("Not a Number: " + fragment);
// }
break;
}
// Logical opperator check <> >= <=
if ( this.isLogical(fragment) && this.isLogical(nextChar) ) fragment += nextChar;
} while (!this.isDelimiter(fragment) && !this.isDelimiter(nextChar) && !chars.isEmpty());
returnthis.transform(fragment);
}
}