-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathComplexToken.java
More file actions
79 lines (71 loc) · 2.94 KB
/
ComplexToken.java
File metadata and controls
79 lines (71 loc) · 2.94 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
package Tokens;
/**
* ComplexToken breaks a token into complex types, loops and scoped functions. It then
* creates another base toekn inside of the scoped fuction.
*
* @author Keenan Leverty
* @author Peter Matern
* @author Chris Allender
*/
class ComplexToken extends Token {
BaseToken nextBase;
SimpleToken forToken;
int type = -1;
/**
* Determines what type of complex token the token is. Makes a new base token with the rest of the input.
* @param input String to be tokenized
* @param scope The scope of the token itself, used to tab in for formatting
*/
ComplexToken(String input, int scope) {
super(input, scope);
if (input.startsWith("while")) {
nextBase = new BaseToken(input.substring(input.indexOf("{") + 1, input.lastIndexOf("}") + 1), scope + 1);
type = 0;
}
else if (input.startsWith("for")) {
forToken = new SimpleToken(input.substring(input.indexOf("(") + 1, input.indexOf(";") + 1), scope);
nextBase = new BaseToken(input.substring(input.indexOf("{") + 1, input.lastIndexOf("}") + 1), scope + 1);
type = 1;
} else if (input.startsWith("if")) {
nextBase = new BaseToken(input.substring(input.indexOf("{") + 1, input.lastIndexOf("}") + 1), scope + 1);
type = 2;
} else if (input.startsWith("voidmain")) {
nextBase = new BaseToken(input.substring(input.indexOf("{") + 1, input.lastIndexOf("}") + 1), scope);
type = 3;
}
}
/**
* Formats the token based on the type given.
*
* @return the token as a python string
*/
@Override
public String toString() {
String output = "";
switch (type) {
case 0: {
output += gimmeTabs();
output += "while ";
output += input.substring(input.indexOf("(") + 1, input.indexOf(")")) + " :\n";
output += nextBase.toString();
break;
} case 1: { // Forloop broken into a while loop, uses the tokens from the inside to change the output in python
output += gimmeTabs() + forToken.toString();
output += gimmeTabs() + "while " + input.substring(input.indexOf(";") + 1, input.indexOf(";", input.indexOf(";") + 1)) + " :\n";
output += nextBase.toString();
output += nextBase.gimmeTabs() + input.substring(input.indexOf(";", input.indexOf(";") + 1) + 1, input.indexOf(")")) + "\n";
break;
} case 2: {
output += gimmeTabs();
output += "if ";
output += input.substring(input.indexOf("(") + 1, input.indexOf(")")) + " :\n";
output += nextBase.toString();
break;
} case 3: {
output += "\n" + nextBase.toString();
break;
}
}
return output;
}
}