-
Notifications
You must be signed in to change notification settings - Fork 0
/
FotoMessageLengthLexicalInstancedAnalyzer.java
94 lines (80 loc) · 1.84 KB
/
FotoMessageLengthLexicalInstancedAnalyzer.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
import java.util.LinkedList;
/**
* @author martin (cernama9@fit.cvut.cz)
* @since 7.3.15.
*/
/**
* Derived lexical analyzer capable of analyzing the string one char at a time.
*/
final class FotoMessageLengthLexicalInstancedAnalyzer extends LexicalAnalyzer
{
private AnalyzerStates state;
private LinkedList<String> tokens;
/**
* Constructor
*/
FotoMessageLengthLexicalInstancedAnalyzer()
{
this.state = AnalyzerStates.EMPTY;
this.tokens = new LinkedList<String>();
}
/**
* Processes a character
*
* @param c Character of the input string
* @throws SyntaxIncorrect
*/
public void parseCharacter(char c) throws SyntaxIncorrect
{
handleInput(c);
}
/**
* 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.DONE;
}
/**
* Internal input handling
*
* @param c Character of the input string
* @throws SyntaxIncorrect
*/
private void handleInput(char c) throws SyntaxIncorrect
{
switch (state)
{
case NUMBER:
if (c == ' ')
{
state = AnalyzerStates.DONE;
break;
}
case EMPTY:
if (c < '0' || c > '9') throw new SyntaxIncorrect("Length has to be numerical");
state = AnalyzerStates.NUMBER;
break;
case DONE:
throw new SyntaxIncorrect("Cannot read anymore, word already accepted");
}
}
@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.DONE)
throw new InputNotAccepted("Length not specified - not accepted by PayloadTypeLexicalAnalyzer");
tokens.add(state.toString());
return tokens;
}
protected enum AnalyzerStates
{
EMPTY, NUMBER, DONE
}
}