| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
- import java.util.stream.Collectors;
-
- class Token {
-
- static class OpDefToken extends Token {
-
- static OpDefToken opDefTokenFromString(final String string) {
- final String trimmedLine = string.substring(2, string.length() - 2);
- final int newOpEnd = trimmedLine.indexOf(" ");
-
- if (newOpEnd == -1) {
- throw new IllegalArgumentException("Incomplete operation definition");
- }
-
- final Token newOpToken = Token.fromString(trimmedLine.substring(0, newOpEnd)).get(0);
-
- if (!(newOpToken instanceof OpToken)) {
- throw new IllegalArgumentException("Cannot redefine numbers");
- }
-
- final List<Token> newOpDefTokens = Token.fromString(trimmedLine.substring(newOpEnd + 1));
- return new OpDefToken(((OpToken) newOpToken).getOp(), newOpDefTokens);
- }
-
- private final String newOp;
-
- private final List<Token> newOpDefTokens;
-
- private OpDefToken(final String newOp, final List<Token> newOpDefTokens) {
- this.newOp = newOp;
- this.newOpDefTokens = newOpDefTokens;
- }
-
- String getNewOp() {
- return newOp;
- }
-
- List<Token> getNewOpDefTokens() {
- return newOpDefTokens;
- }
-
- }
-
- static class OpToken extends Token {
-
- private final String op;
-
- OpToken(final String op) {
- this.op = op;
- }
-
- String getOp() {
- return op;
- }
-
- }
-
- static class IntToken extends Token {
-
- private final int rawValue;
-
- IntToken(final int rawValue) {
- this.rawValue = rawValue;
- }
-
- int getRawValue() {
- return rawValue;
- }
-
- }
-
- static List<Token> fromString(final String string) {
- if (string.startsWith(":")) {
- return Collections.singletonList(OpDefToken.opDefTokenFromString(string));
- } else if (string.matches("[A-z+/*\\-]+(?:-[A-z+/*\\-]+)*")) {
- return Collections.singletonList(new OpToken(string.toLowerCase()));
- } else if (string.matches("\\d+")) {
- return Collections.singletonList(new IntToken(Integer.parseInt(string)));
- } else {
- return Arrays.stream(string.split(" "))
- .map(Token::fromString)
- .flatMap(List::stream)
- .collect(Collectors.toList());
- }
- }
-
- }
|