Skip to content

Commit

Permalink
Add more logical expression validations
Browse files Browse the repository at this point in the history
  • Loading branch information
Elscrux committed Oct 25, 2023
1 parent 9341dcd commit 60085dc
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/converter/dimacs/LogicalExpressionParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export class LogicalExpressionParser {
let wasOperator = false;
let wasVariable = false;
let wasClose = false;
let wasOpen = false;
let wasNegate = false;

if (tokens.length > 0) {
switch (tokens[0].name) {
Expand All @@ -62,8 +64,19 @@ export class LogicalExpressionParser {
case TokenName.and:
if (wasOperator)
errors.push("Two operators (AND/OR) can't be next to each other");
if (wasOpen)
errors.push(
`Operator '${token.lexeme}' can't be after an opening parenthesis`
);
break;
case TokenName.negate:
if (wasClose)
errors.push(
`Negation '${token.lexeme}' is after a closing parenthesis`
);
if (wasVariable)
errors.push(`Negation '${token.lexeme}' is after a variable`);
break;
case TokenName.negatedVariable:
case TokenName.variable:
if (wasVariable)
errors.push(
Expand All @@ -78,13 +91,21 @@ export class LogicalExpressionParser {
if (wasVariable)
errors.push("Parenthesis can't be opened after a variable");
break;
case TokenName.close:
if (wasOperator)
errors.push(
"Parenthesis can't be closed after an operator (AND/OR)"
);
if (wasNegate)
errors.push("Parenthesis can't be closed after a negation");
break;
}

wasOperator = token.name == TokenName.and || token.name == TokenName.or;
wasVariable =
token.name == TokenName.variable ||
token.name == TokenName.negatedVariable;
wasVariable = token.name == TokenName.variable;
wasOpen = token.name == TokenName.open;
wasClose = token.name == TokenName.close;
wasNegate = token.name == TokenName.negate;
}

return errors;
Expand Down

0 comments on commit 60085dc

Please sign in to comment.