Switch the Lexer and TreeBuilder to using exceptions.

This commit is contained in:
Danila Fedorin 2017-09-23 15:31:35 -07:00
parent a3bfc34c1c
commit 31996219ad
6 changed files with 29 additions and 10 deletions

View File

@ -0,0 +1,23 @@
package org.nwapw.abacus.exception;
/**
* Exception thrown by Lexers when they are unable to tokenize the input string.
*/
public class TokenizeException extends AbacusException {
/**
* Create a new tokenize exception with no additional data.
*/
public TokenizeException() {
this("");
}
/**
* Create a new tokenize exception with the given message.
* @param message the message to use.
*/
public TokenizeException(String message){
super("Failed to tokenize string", message);
}
}

View File

@ -1,5 +1,6 @@
package org.nwapw.abacus.parsing;
import org.nwapw.abacus.exception.TokenizeException;
import org.nwapw.abacus.lexing.Lexer;
import org.nwapw.abacus.lexing.pattern.Match;
import org.nwapw.abacus.lexing.pattern.Pattern;
@ -42,7 +43,9 @@ public class LexerTokenizer implements Tokenizer<Match<TokenType>>, PluginListen
@Override
public List<Match<TokenType>> tokenizeString(String string) {
return lexer.lexAll(string, 0, TOKEN_SORTER);
List<Match<TokenType>> tokens = lexer.lexAll(string, 0, TOKEN_SORTER);
if(tokens == null) throw new TokenizeException();
return tokens;
}
@Override

View File

@ -42,9 +42,7 @@ public class TreeBuilder<T> {
* @return the resulting tree.
*/
public TreeNode fromString(String input) {
List<T> tokens = tokenizer.tokenizeString(input);
if (tokens == null) return null;
return parser.constructTree(tokens);
return parser.constructTree(tokenizer.tokenizeString(input));
}
}

View File

@ -88,7 +88,7 @@ class Abacus(val configuration: Configuration) {
* @param input the input string to parse
* @return the resulting tree, null if the tree builder or the produced tree are null.
*/
fun parseString(input: String): TreeNode? = treeBuilder.fromString(input)
fun parseString(input: String): TreeNode = treeBuilder.fromString(input)
/**
* Evaluates the given tree.
*

View File

@ -22,7 +22,6 @@ public class CalculationTests {
private void testOutput(String input, String parseOutput, String output) {
TreeNode parsedTree = abacus.parseString(input);
Assert.assertNotNull(parsedTree);
Assert.assertEquals(parsedTree.toString(), parseOutput);
NumberInterface result = abacus.evaluateTree(parsedTree).getValue();
Assert.assertNotNull(result);
@ -31,7 +30,6 @@ public class CalculationTests {
private void testDomainException(String input, String parseOutput) {
TreeNode parsedTree = abacus.parseString(input);
Assert.assertNotNull(parsedTree);
Assert.assertEquals(parsedTree.toString(), parseOutput);
try {
abacus.evaluateTree(parsedTree);

View File

@ -142,9 +142,6 @@ public class AbacusController implements PluginListener {
private String attemptCalculation() {
try {
TreeNode constructedTree = abacus.parseString(inputField.getText());
if (constructedTree == null) {
return ERR_SYNTAX;
}
EvaluationResult result = abacus.evaluateTree(constructedTree);
NumberInterface evaluatedNumber = result.getValue();
String resultingString = evaluatedNumber.toString();