-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenManager.java
More file actions
40 lines (32 loc) · 1.05 KB
/
TokenManager.java
File metadata and controls
40 lines (32 loc) · 1.05 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
package icsi311;
import icsi311.Token.TokenType;
import java.util.LinkedList;
import java.util.Optional;
public class TokenManager {
private LinkedList<Token> tokens;
public TokenManager(LinkedList<Token> t){
tokens = t;
}
// tokens ahead and return the token if we aren’t past the end of the token list
public Optional<Token> Peek(int j) {
return Optional.of(tokens.get(j));
}
// returns true if the token list is not empty
public boolean MoreTokens() {
return !(tokens.isEmpty());
}
// looks at the head of the list. If the token type of the head is the same as
// what was passed in,
// remove that token from the list and return it. In all other cases, returns
// Optional.Empty(). You will use
// this extensively.
public Optional<Token> MatchAndRemove(TokenType t) {
if(tokens.get(0).getType() == t){
Token f = tokens.get(0);
tokens.remove(0);
return Optional.of(f);
}
else
return Optional.empty();
}
}