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

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