-
Notifications
You must be signed in to change notification settings - Fork 0
/
PasswordLexicalAnalyzer.java
54 lines (41 loc) · 1.08 KB
/
PasswordLexicalAnalyzer.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
import java.util.LinkedList;
/**
* @author martin (cernama9@fit.cvut.cz)
* @since 6.3.15.
*/
/**
* Derived lexical analyzer. Handles PASSWORDmessages
*/
final class PasswordLexicalAnalyzer extends LexicalAnalyzer
{
@Override
public LinkedList<String> parseTokens(String input) throws SyntaxIncorrect, InputNotAccepted
{
AnalyzerStates state = AnalyzerStates.EMPTY;
LinkedList<String> tokens = new LinkedList<String>();
StringBuilder stringBuffer = new StringBuilder();
for (int i = 0; i < input.length(); i++)
{
char c = input.charAt(i);
stringBuffer.append(c);
switch (state)
{
case EMPTY:
case NUMBER:
if (c < '0' || c > '9') throw new SyntaxIncorrect("Password has to be numerical");
state = AnalyzerStates.NUMBER;
break;
}
}
if (state == AnalyzerStates.EMPTY)
throw new InputNotAccepted("Password cannot be empty - not accepted by PasswordLexicalAnalyzer");
tokens.add(stringBuffer.toString());
return tokens;
}
// ^\d+\r\n$
// realne ^\d+$ (\r\n osekne getLine)
protected enum AnalyzerStates
{
EMPTY, NUMBER
}
}