|
1
|
|
|
package yalep.code; |
|
2
|
|
|
|
|
3
|
|
|
import org.antlr.v4.runtime.CharStreams; |
|
4
|
|
|
import org.antlr.v4.runtime.CommonTokenStream; |
|
5
|
|
|
import yalep.antlr.LogicalExpressionErrorListener; |
|
6
|
|
|
import yalep.antlr.generated.LogicalExpressionLexer; |
|
7
|
|
|
import yalep.antlr.generated.LogicalExpressionParser; |
|
8
|
|
|
import yalep.exceptions.WrongExpressionFormatException; |
|
9
|
|
|
|
|
10
|
|
|
import java.util.HashSet; |
|
11
|
|
|
import java.util.Set; |
|
12
|
|
|
|
|
13
|
|
|
public class LogicalExpressionValidityChecker { |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
// on change of grammar should change it too |
|
16
|
|
|
private static final String ALLOWED_TOKENS = "()!&|^ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
protected static void expressionIsValid(String rawExpression) throws WrongExpressionFormatException { |
|
20
|
|
|
rawExpression = rawExpression.replaceAll(" ",""); |
|
21
|
|
|
String expression = getLogicalExpressionAndValidateBasics(rawExpression); |
|
22
|
|
|
checkSyntaxErrors(expression); |
|
23
|
|
|
checkInvalidTokens(expression); |
|
24
|
|
|
|
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
private static void checkSyntaxErrors(String expression) throws WrongExpressionFormatException { |
|
28
|
|
|
LogicalExpressionLexer lexer = new LogicalExpressionLexer(CharStreams.fromString(expression)); |
|
29
|
|
|
lexer.removeErrorListeners(); |
|
30
|
|
|
CommonTokenStream tokens = new CommonTokenStream(lexer); |
|
31
|
|
|
LogicalExpressionParser parser = new LogicalExpressionParser(tokens); |
|
32
|
|
|
parser.removeErrorListeners(); |
|
33
|
|
|
parser.addErrorListener(new LogicalExpressionErrorListener()); |
|
34
|
|
|
try { |
|
35
|
|
|
parser.eval(); |
|
36
|
|
|
} catch (RuntimeException e) { |
|
37
|
|
|
throw new WrongExpressionFormatException(e.getMessage()); |
|
38
|
|
|
} |
|
39
|
|
|
if (parser.getNumberOfSyntaxErrors() != 0) { |
|
40
|
|
|
throw new WrongExpressionFormatException("Expression has syntax errors"); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
private static void checkInvalidTokens(String expression) throws WrongExpressionFormatException { |
|
45
|
|
|
Set<Character> tokenSet = new HashSet<>(); |
|
46
|
|
|
for (Character c : ALLOWED_TOKENS.toCharArray()) { |
|
47
|
|
|
tokenSet.add(c); |
|
48
|
|
|
} |
|
49
|
|
|
for (Character c : expression.toCharArray()) { |
|
50
|
|
|
if (!tokenSet.contains(c)) { |
|
51
|
|
|
throw new WrongExpressionFormatException("Expression contains wrong tokens"); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private static String getLogicalExpressionAndValidateBasics(String rawExpression) throws WrongExpressionFormatException { |
|
57
|
|
|
int equalSignPos = rawExpression.indexOf("="); |
|
58
|
|
|
if (equalSignPos == -1) { |
|
59
|
|
|
throw new WrongExpressionFormatException("Expression doesn't contain '='"); |
|
60
|
|
|
} |
|
61
|
|
|
String answerString = rawExpression.substring(equalSignPos + 1); |
|
62
|
|
|
|
|
63
|
|
|
if (!(answerString.equals("1") || answerString.equals("0"))) { |
|
64
|
|
|
throw new WrongExpressionFormatException("Wrong answer format"); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return rawExpression.substring(0, equalSignPos); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|