Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Parser.java 8.5KB

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