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.

Parser.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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.text.ParseException;
  24. import java.util.ArrayList;
  25. import java.util.Arrays;
  26. import java.util.Collections;
  27. import java.util.Comparator;
  28. import java.util.List;
  29. /**
  30. * The parser takes the output from a {@link Lexer} and applies precedence rules to build the tokens
  31. * into a tree.
  32. */
  33. public class Parser {
  34. /** A list of token types sorted by their precedence. */
  35. protected static final List<TokenType> TOKENS_BY_PRECEDENCE;
  36. /** The lexer whose output will be parsed. */
  37. protected final Lexer lexer;
  38. static {
  39. TOKENS_BY_PRECEDENCE = new ArrayList<>(Arrays.asList(
  40. TokenType.values()));
  41. Collections.sort(TOKENS_BY_PRECEDENCE,
  42. new TokenTypePrecedenceComparator());
  43. }
  44. public Parser(final Lexer lexer) {
  45. this.lexer = lexer;
  46. }
  47. /**
  48. * Parses the output of this parser's lexer, and returns a {@link TreeToken} representing the
  49. * parsed formula.
  50. *
  51. * @return A token tree corresponding to the lexer's token output
  52. *
  53. * @throws ParseException If the lexer encounters a parse error, or if an error occurs while
  54. * parsing the lexer's output (such as a non-sensical formula such as one
  55. * involving a mis-matched bracket).
  56. */
  57. public TreeToken parse() throws ParseException {
  58. final List<TreeToken> tokens = new ArrayList<>();
  59. for (Token token : lexer.tokenise()) {
  60. tokens.add(new TreeToken(token));
  61. }
  62. return parse(tokens);
  63. }
  64. /**
  65. * Parses the specified tokens into a tree.
  66. *
  67. * @param tokens The tokens to be parsed
  68. *
  69. * @return A single tree containing all of the specified tokens
  70. *
  71. * @throws ParseException If the tokens contain mismatched brackets
  72. */
  73. protected TreeToken parse(final List<TreeToken> tokens)
  74. throws ParseException {
  75. while (tokens.size() > 1) {
  76. for (TokenType type : TOKENS_BY_PRECEDENCE) {
  77. final int offset = findTokenType(tokens, type);
  78. if (offset > -1) {
  79. switch (type.getArity()) {
  80. case HIDDEN:
  81. parseHiddenOperator(tokens, offset);
  82. break;
  83. case BINARY:
  84. parseBinaryOperator(tokens, offset);
  85. break;
  86. case UNARY:
  87. parseUnaryOperator(tokens, offset);
  88. break;
  89. case NULLARY:
  90. parseNullaryOperator(tokens, offset);
  91. break;
  92. }
  93. break;
  94. }
  95. }
  96. }
  97. return tokens.get(0);
  98. }
  99. /**
  100. * Parses an operator that takes no operands.
  101. *
  102. * @param tokens The supply of tokens from which the operator will be parsed
  103. * @param offset The offset at which the operator occurs
  104. *
  105. * @throws ParseException If the operator is a bracket and that bracket is mismatched
  106. */
  107. protected void parseNullaryOperator(final List<TreeToken> tokens,
  108. final int offset)
  109. throws ParseException {
  110. if (tokens.get(offset).getToken().getType()
  111. == TokenType.BRACKET_CLOSE
  112. || tokens.get(offset).getToken().getType()
  113. == TokenType.BRACKET_OPEN) {
  114. parseBracket(tokens, offset);
  115. } else {
  116. parseNumber(tokens, offset);
  117. }
  118. }
  119. /**
  120. * Parses a bracket operator.
  121. *
  122. * @param tokens The supply of tokens from which the operator will be parsed
  123. * @param offset The offset at which the operator occurs
  124. *
  125. * @throws ParseException If the operator is a bracket and that bracket is mismatched
  126. */
  127. protected void parseBracket(final List<TreeToken> tokens, final int offset)
  128. throws ParseException {
  129. final List<TreeToken> stack = new ArrayList<>();
  130. for (int i = offset - 1; i > 0; i--) {
  131. if (tokens.get(i).getToken().getType() == TokenType.BRACKET_OPEN
  132. && !tokens.get(i).isProcessed()) {
  133. tokens.add(i, parse(stack));
  134. tokens.get(i).setProcessed();
  135. tokens.remove(i + 1);
  136. tokens.remove(i + 1);
  137. return;
  138. } else {
  139. stack.add(0, tokens.get(i));
  140. tokens.remove(i);
  141. }
  142. }
  143. throw new ParseException("Couldn't find matching opening bracket",
  144. offset);
  145. }
  146. /**
  147. * Parses an operator that takes two operands.
  148. *
  149. * @param tokens The supply of tokens from which the operator will be parsed
  150. * @param offset The offset at which the operator occurs
  151. */
  152. protected void parseBinaryOperator(final List<TreeToken> tokens,
  153. final int offset) {
  154. tokens.get(offset).addChild(tokens.get(offset - 1));
  155. tokens.get(offset).addChild(tokens.get(offset + 1));
  156. tokens.get(offset).setProcessed();
  157. tokens.remove(offset + 1);
  158. tokens.remove(offset - 1);
  159. }
  160. /**
  161. * Parses an operator that takes one operand.
  162. *
  163. * @param tokens The supply of tokens from which the operator will be parsed
  164. * @param offset The offset at which the operator occurs
  165. */
  166. protected void parseUnaryOperator(final List<TreeToken> tokens,
  167. final int offset) {
  168. tokens.get(offset).addChild(tokens.get(offset + 1));
  169. tokens.get(offset).setProcessed();
  170. tokens.remove(offset + 1);
  171. }
  172. /**
  173. * Parses an operator that does not actually correspond to a piece of the input (such as the
  174. * START and END operators).
  175. *
  176. * @param tokens The supply of tokens from which the operator will be parsed
  177. * @param offset The offset at which the operator occurs
  178. */
  179. protected void parseHiddenOperator(final List<TreeToken> tokens,
  180. final int offset) {
  181. tokens.remove(offset);
  182. }
  183. /**
  184. * Parses a number.
  185. *
  186. * @param tokens The supply of tokens from which the operator will be parsed
  187. * @param offset The offset at which the operator occurs
  188. */
  189. protected void parseNumber(final List<TreeToken> tokens, final int offset) {
  190. tokens.get(offset).setProcessed();
  191. }
  192. /**
  193. * Retrieves the offset of the first token within the input list that has a type corresponding
  194. * to the specified {@link TokenType}.
  195. *
  196. * @param tokens The tokens to be searched
  197. * @param type The desired token type
  198. *
  199. * @return The index of the first token with that type, or -1 if none found
  200. */
  201. protected static int findTokenType(final List<TreeToken> tokens,
  202. final TokenType type) {
  203. for (int i = 0; i < tokens.size(); i++) {
  204. if (tokens.get(i).getToken().getType() == type && !tokens.get(i)
  205. .isProcessed()) {
  206. return i;
  207. }
  208. }
  209. return -1;
  210. }
  211. /**
  212. * A class which compares token types based on their precedence.
  213. */
  214. protected static class TokenTypePrecedenceComparator implements
  215. Comparator<TokenType> {
  216. @Override
  217. public int compare(final TokenType o1, final TokenType o2) {
  218. return o2.getPrecedence() - o1.getPrecedence();
  219. }
  220. }
  221. }