-
Notifications
You must be signed in to change notification settings - Fork 0
/
PayloadTypeLexicalInstancedAnalyzer.java
131 lines (116 loc) · 3.18 KB
/
PayloadTypeLexicalInstancedAnalyzer.java
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
import java.util.LinkedList;
/**
* @author martin (cernama9@fit.cvut.cz)
* @since 6.3.15.
*/
/**
* Derived lexical analyzer capable of analyzing the payload type (INFO/FOTO)
*/
final class PayloadTypeLexicalInstancedAnalyzer extends LexicalAnalyzer
{
private AnalyzerStates state;
private LinkedList<String> tokens;
private PayloadModes mode;
/**
* Constructor
*/
PayloadTypeLexicalInstancedAnalyzer()
{
this.state = AnalyzerStates.TYPE_CHAR1;
this.tokens = new LinkedList<String>();
this.mode = PayloadModes.UNKNOWN;
}
/**
* Processes a character
*
* @param c Character of the input string
* @throws SyntaxIncorrect
*/
public void parseCharacter(char c) throws SyntaxIncorrect
{
handleInput(c);
}
/**
* Returns the payload type recognized. Returns null if not recognized
* @return The payload's type. null if not yet recognized
*/
public PayloadModes getPayloadType()
{
if (!isStateFinal()) return null;
return mode;
}
/**
* Checks whether the analyzer has reached a final state
*
* @return True if final state has been reached, false otherwise
*/
public boolean isStateFinal()
{
return state == AnalyzerStates.TYPE_DONE;
}
/**
* Checks whether the analyzer has reached a final state
*
* @return True if final state has been reached, false otherwise
*/
private void handleInput(char c) throws SyntaxIncorrect
{
switch (state)
{
case TYPE_CHAR1:
if (c == 'I')
{
mode = PayloadModes.INFO;
}
else if (c == 'F')
{
mode = PayloadModes.FOTO;
}
else
{
throw new SyntaxIncorrect("Unrecognized char 1 (\"" + c + "\") of type (neither I nor F)");
}
state = AnalyzerStates.TYPE_CHAR2;
break;
case TYPE_CHAR2:
if ((mode == PayloadModes.INFO && c != 'N') || (mode == PayloadModes.FOTO && c != 'O'))
throw new SyntaxIncorrect("Unrecognized char 2 (\"" + c + "\") of type (neither N nor O)");
state = AnalyzerStates.TYPE_CHAR3;
break;
case TYPE_CHAR3:
if ((mode == PayloadModes.INFO && c != 'F') || (mode == PayloadModes.FOTO && c != 'T'))
throw new SyntaxIncorrect("Unrecognized char 3 (\"" + c + "\") of type (neither F nor T)");
state = AnalyzerStates.TYPE_CHAR4;
break;
case TYPE_CHAR4:
if ((mode == PayloadModes.INFO && c != 'O') || (mode == PayloadModes.FOTO && c != 'O'))
throw new SyntaxIncorrect("Unrecognized char 4 (\"" + c + "\") of type (neither O nor O)");
state = AnalyzerStates.TYPE_SPACE;
break;
case TYPE_SPACE:
if (c != ' ')
throw new SyntaxIncorrect("Unrecognized char 5 (\"" + c + "\") of type (must be a space)");
state = AnalyzerStates.TYPE_DONE;
}
}
@Override
public LinkedList<String> parseTokens(String input) throws SyntaxIncorrect, InputNotAccepted
{
for (int i = 0; i < input.length(); i++)
{
handleInput(input.charAt(i));
}
if (state != AnalyzerStates.TYPE_DONE)
throw new InputNotAccepted("Type incomplete - not accepted by PayloadTypeLexicalAnalyzer");
tokens.add(state.toString());
return tokens;
}
protected enum AnalyzerStates
{
TYPE_CHAR1, TYPE_CHAR2, TYPE_CHAR3, TYPE_CHAR4, TYPE_SPACE, TYPE_DONE
}
public enum PayloadModes
{
UNKNOWN, INFO, FOTO
}
}