parser.h 1.9 KB

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