You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TokenType.java 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.calc;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.regex.Matcher;
  21. import java.util.regex.Pattern;
  22. /**
  23. * Describes the different types of possible token, their arities, precedence, and the types of
  24. * token that may follow them.
  25. */
  26. public enum TokenType {
  27. /** The start of an input string. */
  28. START(TokenTypeArity.HIDDEN, "^", 0, "NUMBER_*", "BRACKET_OPEN", "MOD_*"),
  29. /** The end of an input string. */
  30. END(TokenTypeArity.HIDDEN, "$", 0),
  31. /** An opening bracket. */
  32. BRACKET_OPEN(TokenTypeArity.NULLARY, "\\(", 0, "NUMBER_*", "MOD_*",
  33. "BRACKET_OPEN"),
  34. /** A closing bracket. */
  35. BRACKET_CLOSE(TokenTypeArity.NULLARY, "\\)", 50, "OP_*", "BRACKET_*",
  36. "END"),
  37. /** A floating point number. */
  38. NUMBER_FLOAT(TokenTypeArity.NULLARY, "[0-9]+\\.[0-9]+", 1, "OP_*",
  39. "BRACKET_*", "END") {
  40. @Override
  41. public Number evaluate(final TreeToken token) {
  42. return Float.valueOf(token.getToken().getContent());
  43. }
  44. },
  45. /** An integer. */
  46. NUMBER_INT(TokenTypeArity.NULLARY, "[0-9]+", 1, "OP_*",
  47. "BRACKET_*", "END") {
  48. @Override
  49. public Number evaluate(final TreeToken token) {
  50. return Float.valueOf(token.getToken().getContent());
  51. }
  52. },
  53. /** A modifier signalling the following number is
  54. * positive. */
  55. MOD_POSITIVE(TokenTypeArity.UNARY, "\\+", 100, "NUMBER_*") {
  56. @Override
  57. public Number evaluate(final TreeToken token) {
  58. return token.getChildren().get(0).evaluate();
  59. }
  60. },
  61. /** A modifier signalling the following number is
  62. * negative. */
  63. MOD_NEGATIVE(TokenTypeArity.UNARY, "-", 100, "NUMBER_*") {
  64. @Override
  65. public Number evaluate(final TreeToken token) {
  66. return -1 * token.getChildren().get(0).evaluate().floatValue();
  67. }
  68. },
  69. /** The addition operator. */
  70. OP_PLUS(TokenTypeArity.BINARY, "\\+", 7, "NUMBER_*", "BRACKET_OPEN") {
  71. @Override
  72. public Number evaluate(final TreeToken token) {
  73. return token.getChildren().get(0).evaluate().floatValue()
  74. + token.getChildren().get(1).evaluate().floatValue();
  75. }
  76. },
  77. /** The subtraction operator. */
  78. OP_MINUS(TokenTypeArity.BINARY, "-", 6, "NUMBER_*", "BRACKET_OPEN") {
  79. @Override
  80. public Number evaluate(final TreeToken token) {
  81. return token.getChildren().get(0).evaluate().floatValue()
  82. - token.getChildren().get(1).evaluate().floatValue();
  83. }
  84. },
  85. /** The multiplication operator. */
  86. OP_MULT(TokenTypeArity.BINARY, "(?=\\()|\\*", 9, "NUMBER_*",
  87. "BRACKET_OPEN") {
  88. @Override
  89. public Number evaluate(final TreeToken token) {
  90. return token.getChildren().get(0).evaluate().floatValue()
  91. * token.getChildren().get(1).evaluate().floatValue();
  92. }
  93. },
  94. /** The division operator. */
  95. OP_DIVIDE(TokenTypeArity.BINARY, "/", 10, "NUMBER_*", "BRACKET_OPEN") {
  96. @Override
  97. public Number evaluate(final TreeToken token) {
  98. return token.getChildren().get(0).evaluate().floatValue()
  99. / token.getChildren().get(1).evaluate().floatValue();
  100. }
  101. },
  102. /** The modulo operator. */
  103. OP_MOD(TokenTypeArity.BINARY, "%", 8, "NUMBER_*", "BRACKET_OPEN") {
  104. @Override
  105. public Number evaluate(final TreeToken token) {
  106. return token.getChildren().get(0).evaluate().floatValue()
  107. % token.getChildren().get(1).evaluate().floatValue();
  108. }
  109. },
  110. /** The power operator. */
  111. OP_POWER(TokenTypeArity.BINARY, "\\^", 11, "NUMBER_*", "BRACKET_OPEN") {
  112. @Override
  113. public Number evaluate(final TreeToken token) {
  114. return (float) Math.pow(token.getChildren().get(0).evaluate().doubleValue(),
  115. token.getChildren().get(1).evaluate().doubleValue());
  116. }
  117. };
  118. /** The string representation of tokens that may follow this one. */
  119. private final String[] strfollows;
  120. /** The precedence of this token. */
  121. private final int precedence;
  122. /** The list of tokens that may follow this one. */
  123. private List<TokenType> follows;
  124. /** The regular expression used to match this token. */
  125. private final Pattern regex;
  126. /** The arity of this token. */
  127. private final TokenTypeArity arity;
  128. /**
  129. * Creates a new token type with the specified arguments.
  130. *
  131. * @param arity The arity of this token
  132. * @param regex The regular expression used to match this token
  133. * @param precedence The precendence of this token
  134. * @param follows The names of the tokens which may follow this one
  135. */
  136. TokenType(final TokenTypeArity arity, final String regex,
  137. final int precedence, final String... follows) {
  138. this.arity = arity;
  139. this.strfollows = follows;
  140. this.precedence = precedence;
  141. this.regex = Pattern.compile(regex);
  142. }
  143. public int getPrecedence() {
  144. return precedence;
  145. }
  146. public TokenTypeArity getArity() {
  147. return arity;
  148. }
  149. /**
  150. * Retrieves a list of token types that may follow this one.
  151. *
  152. * @return A list of this token type's possible followers
  153. */
  154. public synchronized List<TokenType> getFollowers() {
  155. if (follows == null) {
  156. follows = new ArrayList<>();
  157. for (String strfollow : strfollows) {
  158. follows.addAll(searchValueOf(strfollow));
  159. }
  160. }
  161. return follows;
  162. }
  163. /**
  164. * Attempts to match this token type against the specified input string (starting at the
  165. * specified offset).
  166. *
  167. * @param input The string to be matched
  168. * @param offset The offset within the string to start at
  169. *
  170. * @return -1 if no match was made, otherwise the number of characters that were matched as part
  171. * of this token type.
  172. */
  173. public int match(final String input, final int offset) {
  174. final Matcher matcher = regex.matcher(input);
  175. matcher.useAnchoringBounds(false);
  176. matcher.useTransparentBounds(true);
  177. return matcher.find(offset) && matcher.start() == offset
  178. ? matcher.end() : -1;
  179. }
  180. /**
  181. * Evaluates the specified token of this token type into a number.
  182. *
  183. * @param token The token to be evaluated
  184. *
  185. * @return A numerical representation of the specified token
  186. */
  187. public Number evaluate(final TreeToken token) {
  188. throw new AbstractMethodError("Can't evaluate this token type");
  189. }
  190. /**
  191. * Retrieves a list of types which match the specified name. The name may end with an asterisk
  192. * (*), which is treated as a wild card.
  193. *
  194. * @param name The name to be searched for
  195. *
  196. * @return A list of matching tokens
  197. */
  198. protected static List<TokenType> searchValueOf(final String name) {
  199. final List<TokenType> res = new ArrayList<>();
  200. for (TokenType token : values()) {
  201. if ((name.endsWith("*") && token.name().startsWith(name.substring(0,
  202. name.length() - 1))) || name.equals(token.name())) {
  203. res.add(token);
  204. }
  205. }
  206. return res;
  207. }
  208. }