parser.h 2.6 KB

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