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.

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