parser.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //
  2. // parser.h
  3. // dice-roll
  4. //
  5. // Created by Sam Jaffe on 1/16/21.
  6. // Copyright © 2021 Sam Jaffe. All rights reserved.
  7. //
  8. #pragma once
  9. #include <iosfwd>
  10. #include "die.h"
  11. namespace dice {
  12. class parser {
  13. private:
  14. std::istream & is_;
  15. dice dice_;
  16. public:
  17. parser(std::istream & is) : is_(is) {}
  18. dice parse();
  19. private:
  20. /**
  21. * Main dispatch function for parsing a dice roll.
  22. * @param s The current +/- sign attached to the parse sequence. s is ZERO
  23. * when parsing the first token, or after parsing a die. This means an
  24. * expression like '1d4+5+1d6+2d8' is evaluated as a sequence like so:
  25. * [1d4][+][5+][1d6][+][2d8]. This produces the following states of (SIGN,
  26. * input stream):
  27. * 1) ZERO, 1d4+5+1d6+2d8
  28. * 2) ZERO, +5+1d6+2d8
  29. * 3) PLUS, 5+1d6+2d8
  30. * 4) PLUS, 1d6+2d8
  31. * 5) ZERO, +2d8
  32. * 6) PLUS, 2d8
  33. */
  34. void parse_impl(sign s);
  35. /**
  36. * @sideeffect This function advances the input stream over a single numeric
  37. * token. This token represents the number of sides in the die roll.
  38. * This function appends a new die into {@see d.of}, representing (+/-)NdM,
  39. * where N is the input parameter 'value', and M is retrieved internally.
  40. * @param s The arithmatic sign attached to this roll, denoting whether this
  41. * die increases or decreases the total result of the roll. For example, the
  42. * 5E spell Bane imposes a -1d4 modifier on attack rolls, an attack roll might
  43. * go from '1d20+2+3' (1d20 + Proficiency + Ability) to '1d20+2+3-1d4'.
  44. * @param value The number of dice to be rolled.
  45. * Domain: value >= 0
  46. * @throw dice::unexpected_token if we somehow call parse_dN while the first
  47. * non-whitespace token after the 'd' char is not a number.
  48. */
  49. void parse_dN(sign s, int value);
  50. /**
  51. * @param s The arithmatic sign attached to this numeric constant. Because
  52. * value is non-negative, this token contains the +/- effect.
  53. * @param value The value associated with this modifier term.
  54. * Domain: value >= 0
  55. */
  56. void parse_const(sign s, int value);
  57. };
  58. }