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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (c) 2006-2014 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 new Float(Math.pow(token.getChildren().get(0).evaluate()
  120. .doubleValue(),
  121. token.getChildren().get(1).evaluate().doubleValue()));
  122. }
  123. };
  124. /** The string representation of tokens that may follow this one. */
  125. private final String[] strfollows;
  126. /** The precedence of this token. */
  127. private final int precedence;
  128. /** The list of tokens that may follow this one. */
  129. private List<TokenType> follows;
  130. /** The regular expression used to match this token. */
  131. private final Pattern regex;
  132. /** The arity of this token. */
  133. private final TokenTypeArity arity;
  134. /**
  135. * Creates a new token type with the specified arguments.
  136. *
  137. * @param arity The arity of this token
  138. * @param regex The regular expression used to match this token
  139. * @param precedence The precendence of this token
  140. * @param follows The names of the tokens which may follow this one
  141. */
  142. TokenType(final TokenTypeArity arity, final String regex,
  143. final int precedence, final String... follows) {
  144. this.arity = arity;
  145. this.strfollows = follows;
  146. this.precedence = precedence;
  147. this.regex = Pattern.compile(regex);
  148. }
  149. public int getPrecedence() {
  150. return precedence;
  151. }
  152. public TokenTypeArity getArity() {
  153. return arity;
  154. }
  155. /**
  156. * Retrieves a list of token types that may follow this one.
  157. *
  158. * @return A list of this token type's possible followers
  159. */
  160. public synchronized List<TokenType> getFollowers() {
  161. if (follows == null) {
  162. follows = new ArrayList<>();
  163. for (String strfollow : strfollows) {
  164. follows.addAll(searchValueOf(strfollow));
  165. }
  166. }
  167. return follows;
  168. }
  169. /**
  170. * Attempts to match this token type against the specified input string (starting at the
  171. * specified offset).
  172. *
  173. * @param input The string to be matched
  174. * @param offset The offset within the string to start at
  175. *
  176. * @return -1 if no match was made, otherwise the number of characters that were matched as part
  177. * of this token type.
  178. */
  179. public int match(final String input, final int offset) {
  180. final Matcher matcher = regex.matcher(input);
  181. matcher.useAnchoringBounds(false);
  182. matcher.useTransparentBounds(true);
  183. return matcher.find(offset) && matcher.start() == offset
  184. ? matcher.end() : -1;
  185. }
  186. /**
  187. * Evaluates the specified token of this token type into a number.
  188. *
  189. * @param token The token to be evaluated
  190. *
  191. * @return A numerical representation of the specified token
  192. */
  193. public Number evaluate(final TreeToken token) {
  194. throw new AbstractMethodError("Can't evaluate this token type");
  195. }
  196. /**
  197. * Retrieves a list of types which match the specified name. The name may end with an asterisk
  198. * (*), which is treated as a wild card.
  199. *
  200. * @param name The name to be searched for
  201. *
  202. * @return A list of matching tokens
  203. */
  204. protected static List<TokenType> searchValueOf(final String name) {
  205. final List<TokenType> res = new ArrayList<>();
  206. for (TokenType token : values()) {
  207. if ((name.endsWith("*") && token.name().startsWith(name.substring(0,
  208. name.length() - 1))) || name.equals(token.name())) {
  209. res.add(token);
  210. }
  211. }
  212. return res;
  213. }
  214. }