mirror of
https://github.com/DanilaFe/abacus
synced 2024-11-17 08:03:09 -08:00
Rewrite the parsing interfaces in Kotlin.
This commit is contained in:
parent
bd02749706
commit
b4214f5714
|
@ -124,7 +124,7 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
|||
* @param matches the list of tokens from the source string.
|
||||
* @return the construct tree expression.
|
||||
*/
|
||||
public TreeNode constructRecursive(List<Match<TokenType>> matches) {
|
||||
public TreeNode constructRecursive(List<? extends Match<TokenType>> matches) {
|
||||
if (matches.size() == 0) throw new ParseException("no tokens left in input");
|
||||
Match<TokenType> match = matches.remove(0);
|
||||
TokenType matchType = match.getType();
|
||||
|
@ -172,7 +172,7 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
|||
}
|
||||
|
||||
@Override
|
||||
public TreeNode constructTree(List<Match<TokenType>> tokens) {
|
||||
public TreeNode constructTree(List<? extends Match<TokenType>> tokens) {
|
||||
if (tokens.isEmpty()) throw new ParseException("no input tokens");
|
||||
tokens = intoPostfix(new ArrayList<>(tokens));
|
||||
Collections.reverse(tokens);
|
||||
|
|
|
@ -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 <T> the type of the tokens produced.
|
||||
*/
|
||||
public interface Tokenizer<T> {
|
||||
|
||||
/**
|
||||
* Converts a string into tokens.
|
||||
*
|
||||
* @param string the string to convert.
|
||||
* @return the list of tokens, or null on error.
|
||||
*/
|
||||
public List<T> tokenizeString(String string);
|
||||
|
||||
}
|
|
@ -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 <T> the type of tokens created by the tokenizer and used by the parser.
|
||||
*/
|
||||
public class TreeBuilder<T> {
|
||||
|
||||
/**
|
||||
* The tokenizer used to convert a string into tokens.
|
||||
*/
|
||||
private Tokenizer<T> tokenizer;
|
||||
/**
|
||||
* The parser used to parse a list of tokens into a tree.
|
||||
*/
|
||||
private Parser<T> 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<T> tokenizer, Parser<T> 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));
|
||||
}
|
||||
|
||||
}
|
|
@ -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 <T> the type of tokens accepted by this parser.
|
||||
*/
|
||||
public interface Parser<T> {
|
||||
|
||||
interface Parser<in T> {
|
||||
|
||||
/**
|
||||
* Constructs a tree out of the given tokens.
|
||||
|
@ -18,5 +19,6 @@ public interface Parser<T> {
|
|||
* @param tokens the tokens to construct a tree from.
|
||||
* @return the constructed tree, or null on error.
|
||||
*/
|
||||
public TreeNode constructTree(List<T> tokens);
|
||||
}
|
||||
fun constructTree(tokens: List<T>): TreeNode
|
||||
|
||||
}
|
21
core/src/main/kotlin/org/nwapw/abacus/parsing/Tokenizer.kt
Normal file
21
core/src/main/kotlin/org/nwapw/abacus/parsing/Tokenizer.kt
Normal file
|
@ -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 <T> the type of the tokens produced.
|
||||
*/
|
||||
interface Tokenizer<out T> {
|
||||
|
||||
/**
|
||||
* 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<T>
|
||||
|
||||
}
|
26
core/src/main/kotlin/org/nwapw/abacus/parsing/TreeBuilder.kt
Normal file
26
core/src/main/kotlin/org/nwapw/abacus/parsing/TreeBuilder.kt
Normal file
|
@ -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 <T> the type of tokens created by the tokenizer and used by the parser.
|
||||
*/
|
||||
class TreeBuilder<T>(private val tokenizer: Tokenizer<T>, private val parser: Parser<T>) {
|
||||
|
||||
/**
|
||||
* 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))
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user