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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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,
  29. * and the types of token that may follow them.
  30. *
  31. * @author chris
  32. */
  33. public enum TokenType {
  34. /** The start of an input string. */
  35. START(TokenTypeArity.HIDDEN, "^", 0, "NUMBER_*", "BRACKET_OPEN", "MOD_*"),
  36. /** The end of an input string. */
  37. END(TokenTypeArity.HIDDEN, "$", 0),
  38. /** An opening bracket. */
  39. BRACKET_OPEN(TokenTypeArity.NULLARY, "\\(", 0, "NUMBER_*", "MOD_*", "BRACKET_OPEN"),
  40. /** A closing bracket. */
  41. BRACKET_CLOSE(TokenTypeArity.NULLARY, "\\)", 50, "OP_*", "BRACKET_*", "END"),
  42. /** A floating point number. */
  43. NUMBER_FLOAT(TokenTypeArity.NULLARY, "[0-9]+\\.[0-9]+", 1, "OP_*", "BRACKET_*", "END") {
  44. /** {@inheritDoc} */
  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_*", "BRACKET_*", "END") {
  52. /** {@inheritDoc} */
  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 positive. */
  59. MOD_POSITIVE(TokenTypeArity.UNARY, "\\+", 100, "NUMBER_*") {
  60. /** {@inheritDoc} */
  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 negative. */
  67. MOD_NEGATIVE(TokenTypeArity.UNARY, "-", 100, "NUMBER_*") {
  68. /** {@inheritDoc} */
  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. /** {@inheritDoc} */
  77. @Override
  78. public Number evaluate(final TreeToken token) {
  79. return token.getChildren().get(0).evaluate().floatValue()
  80. + token.getChildren().get(1).evaluate().floatValue();
  81. }
  82. },
  83. /** The subtraction operator. */
  84. OP_MINUS(TokenTypeArity.BINARY, "-", 6, "NUMBER_*", "BRACKET_OPEN") {
  85. /** {@inheritDoc} */
  86. @Override
  87. public Number evaluate(final TreeToken token) {
  88. return token.getChildren().get(0).evaluate().floatValue()
  89. - token.getChildren().get(1).evaluate().floatValue();
  90. }
  91. },
  92. /** The multiplication operator. */
  93. OP_MULT(TokenTypeArity.BINARY, "(?=\\()|\\*", 9, "NUMBER_*", "BRACKET_OPEN") {
  94. /** {@inheritDoc} */
  95. @Override
  96. public Number evaluate(final TreeToken token) {
  97. return token.getChildren().get(0).evaluate().floatValue()
  98. * token.getChildren().get(1).evaluate().floatValue();
  99. }
  100. },
  101. /** The division operator. */
  102. OP_DIVIDE(TokenTypeArity.BINARY, "/", 10, "NUMBER_*", "BRACKET_OPEN") {
  103. /** {@inheritDoc} */
  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 modulo operator. */
  111. OP_MOD(TokenTypeArity.BINARY, "%", 8, "NUMBER_*", "BRACKET_OPEN") {
  112. /** {@inheritDoc} */
  113. @Override
  114. public Number evaluate(final TreeToken token) {
  115. return token.getChildren().get(0).evaluate().floatValue()
  116. % token.getChildren().get(1).evaluate().floatValue();
  117. }
  118. },
  119. /** The power operator. */
  120. OP_POWER(TokenTypeArity.BINARY, "\\^", 11, "NUMBER_*", "BRACKET_OPEN") {
  121. /** {@inheritDoc} */
  122. @Override
  123. public Number evaluate(final TreeToken token) {
  124. return new Float(Math.pow(token.getChildren().get(0).evaluate().doubleValue(),
  125. token.getChildren().get(1).evaluate().doubleValue()));
  126. }
  127. },
  128. ;
  129. /** The string representation of tokens that may follow this one. */
  130. private final String[] strfollows;
  131. /** The precedence of this token. */
  132. private final int precedence;
  133. /** The list of tokens that may follow this one. */
  134. private List<TokenType> follows;
  135. /** The regular expression used to match this token. */
  136. private final Pattern regex;
  137. /** The arity of this token. */
  138. private final TokenTypeArity arity;
  139. /**
  140. * Creates a new token type with the specified arguments.
  141. *
  142. * @param arity The arity of this token
  143. * @param regex The regular expression used to match this token
  144. * @param precedence The precendence of this token
  145. * @param follows The names of the tokens which may follow this one
  146. */
  147. TokenType(final TokenTypeArity arity, final String regex,
  148. final int precedence, final String ... follows) {
  149. this.arity = arity;
  150. this.strfollows = follows;
  151. this.precedence = precedence;
  152. this.regex = Pattern.compile(regex);
  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<TokenType>();
  162. for (int i = 0; i < strfollows.length; i++) {
  163. follows.addAll(searchValueOf(strfollows[i]));
  164. }
  165. }
  166. return follows;
  167. }
  168. /**
  169. * Retrieves the arity of this token type.
  170. *
  171. * @return This token type's arity
  172. */
  173. public TokenTypeArity getArity() {
  174. return arity;
  175. }
  176. /**
  177. * Retrieves the precedence of this token type.
  178. *
  179. * @return This token type's precedence
  180. */
  181. public int getPrecedence() {
  182. return precedence;
  183. }
  184. /**
  185. * Attempts to match this token type against the specified input string
  186. * (starting at the specified offset).
  187. *
  188. * @param input The string to be matched
  189. * @param offset The offset within the string to start at
  190. * @return -1 if no match was made, otherwise the number of characters that
  191. * were matched as part of this token type.
  192. */
  193. public int match(final String input, final int offset) {
  194. final Matcher matcher = regex.matcher(input);
  195. matcher.useAnchoringBounds(false);
  196. matcher.useTransparentBounds(true);
  197. return matcher.find(offset) && matcher.start() == offset ? matcher.end() : -1;
  198. }
  199. /**
  200. * Evaluates the specified token of this token type into a number.
  201. *
  202. * @param token The token to be evaluated
  203. * @return A numerical representation of the specified token
  204. */
  205. public Number evaluate(final TreeToken token) {
  206. throw new AbstractMethodError("Can't evaluate this token type");
  207. }
  208. /**
  209. * Retrieves a list of types which match the specified name. The name
  210. * may end with an asterisk (*), which is treated as a wild card.
  211. *
  212. * @param name The name to be searched for
  213. * @return A list of matching tokens
  214. */
  215. protected static List<TokenType> searchValueOf(final String name) {
  216. final List<TokenType> res = new ArrayList<TokenType>();
  217. for (TokenType token : values()) {
  218. if ((name.endsWith("*")
  219. && token.name().startsWith(name.substring(0, name.length() - 1)))
  220. || name.equals(token.name())) {
  221. res.add(token);
  222. }
  223. }
  224. return res;
  225. }
  226. }