From b4214f57142a89f90ccf2fd4b64ff0d7de0128d7 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sat, 23 Sep 2017 16:07:59 -0700 Subject: [PATCH] Rewrite the parsing interfaces in Kotlin. --- .../abacus/parsing/ShuntingYardParser.java | 4 +- .../org/nwapw/abacus/parsing/Tokenizer.java | 20 -------- .../org/nwapw/abacus/parsing/TreeBuilder.java | 48 ------------------- .../org/nwapw/abacus/parsing/Parser.kt} | 18 +++---- .../org/nwapw/abacus/parsing/Tokenizer.kt | 21 ++++++++ .../org/nwapw/abacus/parsing/TreeBuilder.kt | 26 ++++++++++ 6 files changed, 59 insertions(+), 78 deletions(-) delete mode 100644 core/src/main/java/org/nwapw/abacus/parsing/Tokenizer.java delete mode 100644 core/src/main/java/org/nwapw/abacus/parsing/TreeBuilder.java rename core/src/main/{java/org/nwapw/abacus/parsing/Parser.java => kotlin/org/nwapw/abacus/parsing/Parser.kt} (52%) create mode 100644 core/src/main/kotlin/org/nwapw/abacus/parsing/Tokenizer.kt create mode 100644 core/src/main/kotlin/org/nwapw/abacus/parsing/TreeBuilder.kt diff --git a/core/src/main/java/org/nwapw/abacus/parsing/ShuntingYardParser.java b/core/src/main/java/org/nwapw/abacus/parsing/ShuntingYardParser.java index f405aa9..67bc1f6 100644 --- a/core/src/main/java/org/nwapw/abacus/parsing/ShuntingYardParser.java +++ b/core/src/main/java/org/nwapw/abacus/parsing/ShuntingYardParser.java @@ -124,7 +124,7 @@ public class ShuntingYardParser implements Parser>, PluginListe * @param matches the list of tokens from the source string. * @return the construct tree expression. */ - public TreeNode constructRecursive(List> matches) { + public TreeNode constructRecursive(List> matches) { if (matches.size() == 0) throw new ParseException("no tokens left in input"); Match match = matches.remove(0); TokenType matchType = match.getType(); @@ -172,7 +172,7 @@ public class ShuntingYardParser implements Parser>, PluginListe } @Override - public TreeNode constructTree(List> tokens) { + public TreeNode constructTree(List> tokens) { if (tokens.isEmpty()) throw new ParseException("no input tokens"); tokens = intoPostfix(new ArrayList<>(tokens)); Collections.reverse(tokens); diff --git a/core/src/main/java/org/nwapw/abacus/parsing/Tokenizer.java b/core/src/main/java/org/nwapw/abacus/parsing/Tokenizer.java deleted file mode 100644 index 00606d0..0000000 --- a/core/src/main/java/org/nwapw/abacus/parsing/Tokenizer.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.nwapw.abacus.parsing; - -import java.util.List; - -/** - * Interface that provides the ability to convert a string into a list of tokens. - * - * @param the type of the tokens produced. - */ -public interface Tokenizer { - - /** - * Converts a string into tokens. - * - * @param string the string to convert. - * @return the list of tokens, or null on error. - */ - public List tokenizeString(String string); - -} diff --git a/core/src/main/java/org/nwapw/abacus/parsing/TreeBuilder.java b/core/src/main/java/org/nwapw/abacus/parsing/TreeBuilder.java deleted file mode 100644 index 78c2331..0000000 --- a/core/src/main/java/org/nwapw/abacus/parsing/TreeBuilder.java +++ /dev/null @@ -1,48 +0,0 @@ -package org.nwapw.abacus.parsing; - -import org.nwapw.abacus.tree.TreeNode; - -import java.util.List; - -/** - * TreeBuilder class used to piece together a Tokenizer and - * Parser of the same kind. This is used to essentially avoid - * working with any parameters at all, and the generics - * in this class are used only to ensure the tokenizer and parser - * are of the same type. - * - * @param the type of tokens created by the tokenizer and used by the parser. - */ -public class TreeBuilder { - - /** - * The tokenizer used to convert a string into tokens. - */ - private Tokenizer tokenizer; - /** - * The parser used to parse a list of tokens into a tree. - */ - private Parser parser; - - /** - * Create a new Tree Builder with the given tokenizer and parser - * - * @param tokenizer the tokenizer to turn strings into tokens - * @param parser the parser to turn tokens into a tree - */ - public TreeBuilder(Tokenizer tokenizer, Parser parser) { - this.tokenizer = tokenizer; - this.parser = parser; - } - - /** - * Parse the given string into a tree. - * - * @param input the string to parse into a tree. - * @return the resulting tree. - */ - public TreeNode fromString(String input) { - return parser.constructTree(tokenizer.tokenizeString(input)); - } - -} diff --git a/core/src/main/java/org/nwapw/abacus/parsing/Parser.java b/core/src/main/kotlin/org/nwapw/abacus/parsing/Parser.kt similarity index 52% rename from core/src/main/java/org/nwapw/abacus/parsing/Parser.java rename to core/src/main/kotlin/org/nwapw/abacus/parsing/Parser.kt index f2c80fc..be2dde3 100644 --- a/core/src/main/java/org/nwapw/abacus/parsing/Parser.java +++ b/core/src/main/kotlin/org/nwapw/abacus/parsing/Parser.kt @@ -1,16 +1,17 @@ -package org.nwapw.abacus.parsing; +package org.nwapw.abacus.parsing -import org.nwapw.abacus.tree.TreeNode; - -import java.util.List; +import org.nwapw.abacus.tree.TreeNode /** - * An itnerface that provides the ability to convert a list of tokens + * Converter from tokens into a parse tree. + * + * An interface that provides the ability to convert a list of tokens * into a parse tree. * * @param the type of tokens accepted by this parser. */ -public interface Parser { + +interface Parser { /** * Constructs a tree out of the given tokens. @@ -18,5 +19,6 @@ public interface Parser { * @param tokens the tokens to construct a tree from. * @return the constructed tree, or null on error. */ - public TreeNode constructTree(List tokens); -} + fun constructTree(tokens: List): TreeNode + +} \ No newline at end of file diff --git a/core/src/main/kotlin/org/nwapw/abacus/parsing/Tokenizer.kt b/core/src/main/kotlin/org/nwapw/abacus/parsing/Tokenizer.kt new file mode 100644 index 0000000..642aed6 --- /dev/null +++ b/core/src/main/kotlin/org/nwapw/abacus/parsing/Tokenizer.kt @@ -0,0 +1,21 @@ +package org.nwapw.abacus.parsing + +/** + * Converter from string to tokens. + * + * Interface that converts a string into a list + * of tokens of a certain type. + * + * @param the type of the tokens produced. + */ +interface Tokenizer { + + /** + * Converts a string into tokens. + * + * @param string the string to convert. + * @return the list of tokens, or null on error. + */ + fun tokenizeString(string: String): List + +} diff --git a/core/src/main/kotlin/org/nwapw/abacus/parsing/TreeBuilder.kt b/core/src/main/kotlin/org/nwapw/abacus/parsing/TreeBuilder.kt new file mode 100644 index 0000000..cdcd24b --- /dev/null +++ b/core/src/main/kotlin/org/nwapw/abacus/parsing/TreeBuilder.kt @@ -0,0 +1,26 @@ +package org.nwapw.abacus.parsing + +import org.nwapw.abacus.tree.TreeNode + +/** + * Class to combine a [Tokenizer] and a [Parser] + * + * TreeBuilder class used to piece together a Tokenizer and + * Parser of the same kind. This is used to essentially avoid + * working with any parameters at all, and the generics + * in this class are used only to ensure the tokenizer and parser + * are of the same type. + * + * @param the type of tokens created by the tokenizer and used by the parser. + */ +class TreeBuilder(private val tokenizer: Tokenizer, private val parser: Parser) { + + /** + * Parses the given [string] into a tree. + * + * @param string the string to parse into a tree. + * @return the resulting tree. + */ + fun fromString(string: String): TreeNode = parser.constructTree(tokenizer.tokenizeString(string)) + +} \ No newline at end of file