mirror of
https://github.com/DanilaFe/abacus
synced 2024-12-22 07:20:09 -08:00
Switch the Lexer and TreeBuilder to using exceptions.
This commit is contained in:
parent
a3bfc34c1c
commit
31996219ad
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
package org.nwapw.abacus.parsing;
|
package org.nwapw.abacus.parsing;
|
||||||
|
|
||||||
|
import org.nwapw.abacus.exception.TokenizeException;
|
||||||
import org.nwapw.abacus.lexing.Lexer;
|
import org.nwapw.abacus.lexing.Lexer;
|
||||||
import org.nwapw.abacus.lexing.pattern.Match;
|
import org.nwapw.abacus.lexing.pattern.Match;
|
||||||
import org.nwapw.abacus.lexing.pattern.Pattern;
|
import org.nwapw.abacus.lexing.pattern.Pattern;
|
||||||
|
@ -42,7 +43,9 @@ public class LexerTokenizer implements Tokenizer<Match<TokenType>>, PluginListen
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Match<TokenType>> tokenizeString(String string) {
|
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
|
@Override
|
||||||
|
|
|
@ -42,9 +42,7 @@ public class TreeBuilder<T> {
|
||||||
* @return the resulting tree.
|
* @return the resulting tree.
|
||||||
*/
|
*/
|
||||||
public TreeNode fromString(String input) {
|
public TreeNode fromString(String input) {
|
||||||
List<T> tokens = tokenizer.tokenizeString(input);
|
return parser.constructTree(tokenizer.tokenizeString(input));
|
||||||
if (tokens == null) return null;
|
|
||||||
return parser.constructTree(tokens);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -88,7 +88,7 @@ class Abacus(val configuration: Configuration) {
|
||||||
* @param input the input string to parse
|
* @param input the input string to parse
|
||||||
* @return the resulting tree, null if the tree builder or the produced tree are null.
|
* @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.
|
* Evaluates the given tree.
|
||||||
*
|
*
|
||||||
|
|
|
@ -22,7 +22,6 @@ public class CalculationTests {
|
||||||
|
|
||||||
private void testOutput(String input, String parseOutput, String output) {
|
private void testOutput(String input, String parseOutput, String output) {
|
||||||
TreeNode parsedTree = abacus.parseString(input);
|
TreeNode parsedTree = abacus.parseString(input);
|
||||||
Assert.assertNotNull(parsedTree);
|
|
||||||
Assert.assertEquals(parsedTree.toString(), parseOutput);
|
Assert.assertEquals(parsedTree.toString(), parseOutput);
|
||||||
NumberInterface result = abacus.evaluateTree(parsedTree).getValue();
|
NumberInterface result = abacus.evaluateTree(parsedTree).getValue();
|
||||||
Assert.assertNotNull(result);
|
Assert.assertNotNull(result);
|
||||||
|
@ -31,7 +30,6 @@ public class CalculationTests {
|
||||||
|
|
||||||
private void testDomainException(String input, String parseOutput) {
|
private void testDomainException(String input, String parseOutput) {
|
||||||
TreeNode parsedTree = abacus.parseString(input);
|
TreeNode parsedTree = abacus.parseString(input);
|
||||||
Assert.assertNotNull(parsedTree);
|
|
||||||
Assert.assertEquals(parsedTree.toString(), parseOutput);
|
Assert.assertEquals(parsedTree.toString(), parseOutput);
|
||||||
try {
|
try {
|
||||||
abacus.evaluateTree(parsedTree);
|
abacus.evaluateTree(parsedTree);
|
||||||
|
|
|
@ -142,9 +142,6 @@ public class AbacusController implements PluginListener {
|
||||||
private String attemptCalculation() {
|
private String attemptCalculation() {
|
||||||
try {
|
try {
|
||||||
TreeNode constructedTree = abacus.parseString(inputField.getText());
|
TreeNode constructedTree = abacus.parseString(inputField.getText());
|
||||||
if (constructedTree == null) {
|
|
||||||
return ERR_SYNTAX;
|
|
||||||
}
|
|
||||||
EvaluationResult result = abacus.evaluateTree(constructedTree);
|
EvaluationResult result = abacus.evaluateTree(constructedTree);
|
||||||
NumberInterface evaluatedNumber = result.getValue();
|
NumberInterface evaluatedNumber = result.getValue();
|
||||||
String resultingString = evaluatedNumber.toString();
|
String resultingString = evaluatedNumber.toString();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user