parser.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. /*
  13. * A parser that translates a stream of text input into a dice object. The
  14. * string matches the following schema-rule:
  15. *
  16. * dice-string:
  17. * dice-expression
  18. * positive-integer { dice-expression }
  19. *
  20. * positive-integer: Any integer number >= 0
  21. *
  22. * dice-expression:
  23. * die-expression
  24. * die-expression + dice-expression
  25. * die-expression - dice-expression
  26. *
  27. * die-expression:
  28. * integer
  29. * positive-integer 'd' positive-integer
  30. * positive-integer 'D' positive-integer
  31. */
  32. class parser {
  33. private:
  34. std::istream & is_;
  35. dice dice_;
  36. public:
  37. parser(std::istream & is) : is_(is) {}
  38. dice parse();
  39. private:
  40. /**
  41. * Main dispatch function for parsing a dice roll.
  42. * @param s The current +/- sign attached to the parse sequence. s is ZERO
  43. * when parsing the first token, or after parsing a die. This means an
  44. * expression like '1d4+5+1d6+2d8' is evaluated as a sequence like so:
  45. * [1d4][+][5+][1d6][+][2d8]. This produces the following states of (SIGN,
  46. * input stream):
  47. * 1) ZERO, 1d4+5+1d6+2d8
  48. * 2) ZERO, +5+1d6+2d8
  49. * 3) PLUS, 5+1d6+2d8
  50. * 4) PLUS, 1d6+2d8
  51. * 5) ZERO, +2d8
  52. * 6) PLUS, 2d8
  53. */
  54. void parse_impl(sign s);
  55. /**
  56. * @sideeffect This function advances the input stream over a single numeric
  57. * token. This token represents the number of sides in the die roll.
  58. * This function appends a new die into {@see d.of}, representing (+/-)NdM,
  59. * where N is the input parameter 'value', and M is retrieved internally.
  60. * @param s The arithmatic sign attached to this roll, denoting whether this
  61. * die increases or decreases the total result of the roll. For example, the
  62. * 5E spell Bane imposes a -1d4 modifier on attack rolls, an attack roll might
  63. * go from '1d20+2+3' (1d20 + Proficiency + Ability) to '1d20+2+3-1d4'.
  64. * @param value The number of dice to be rolled.
  65. * Domain: value >= 0
  66. * @throw dice::unexpected_token if we somehow call parse_dN while the first
  67. * non-whitespace token after the 'd' char is not a number.
  68. */
  69. void parse_dN(sign s, int value);
  70. /**
  71. * @param s The arithmatic sign attached to this numeric constant. Because
  72. * value is non-negative, this token contains the +/- effect.
  73. * @param value The value associated with this modifier term.
  74. * Domain: value >= 0
  75. */
  76. void parse_const(sign s, int value);
  77. };
  78. }