A set of classes for parsing, evaluating, and formatting die roll strings.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SA_DiceFormatter.h 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // SA_DiceFormatter.h
  3. //
  4. // Copyright (c) 2016 Said Achmiz.
  5. //
  6. // This software is licensed under the MIT license.
  7. // See the file "LICENSE" for more information.
  8. #import <Foundation/Foundation.h>
  9. /*********************/
  10. #pragma mark Constants
  11. /*********************/
  12. /*
  13. These constants describe one of several behavior modes for die string results
  14. formatting.
  15. NOTE: Each of the modes have their own set of settings, which may be configured
  16. via various of the SA_DiceFormatter properties. Be sure to use the
  17. appropriate settings for the currently set behavior mode.
  18. NOTE 2: SA_DiceParser also has a behavior mode property, and can operate in
  19. one of several parser behavior modes. Each formatter behavior mode is
  20. appropriate for formatting results generated by some parser behavior modes but
  21. not others. Attempting to format a results tree in a formatter mode that does
  22. not support the parser mode in which those results were generated causes
  23. undefined behavior. Thus, when using any a formatter and a parser together, we
  24. must always make sure that the two objects are set to operate in compatible
  25. behavior modes.
  26. Each formatter behavior mode is described below. Each description also lists
  27. the parser modes which are supported by that formatter mode.
  28. ======================
  29. ==== DEFAULT mode ====
  30. ======================
  31. "Default" mode is an alias for whatever default behavior is currently set for
  32. new SA_DiceFormatter instances. (The "default default" behavior for the
  33. current implementation is "legacy".)
  34. =====================
  35. ==== LEGACY mode ====
  36. =====================
  37. Legacy mode mostly emulates the output format of DiceBot by Sabin (and Dawn by
  38. xthemage before it). (It adds optional error reporting, and several minor
  39. aesthetic enhancement options.)
  40. The legacy mode output format reproduces the entire die roll expression, in
  41. an expanded form (with whitespace inserted between operators and operands);
  42. prints each individual die roll (grouped by roll command), along with the
  43. sum of each die group. The final result is also printed, as is a label
  44. (if any). Typical legacy mode output looks like this:
  45. (Hasan_the_Great) 4d6 < 3 3 4 6 = 16 > + 2d4 < 2 4 = 6 > + 3 + 20 = 45
  46. This output line would be generated by a roll string as follows:
  47. 4d6+2d4+3+20;Hasan_the_Great
  48. Here are some typical output strings for erroneous or malformed input strings.
  49. INPUT:
  50. 8d6+4d0+5
  51. OUTPUT:
  52. 8d6 < 3 1 2 5 2 2 1 6 = 22 > + 4d0 < ERROR > [ERROR: Invalid die size (zero or negative)]
  53. Legacy mode does not support attributed text of any kind.
  54. SUPPORTED PARSER MODES: Legacy.
  55. =====================
  56. ==== SIMPLE mode ====
  57. =====================
  58. Simple mode generates a very minimal output format. It prints the final result
  59. of a die roll expression, or the word 'ERROR' if any error occurred. It also
  60. prints the label (if any).
  61. SUPPORTED PARSER MODES: Feepbot, Legacy.
  62. =====================
  63. ==== MODERN mode ====
  64. =====================
  65. >>> NOT YET IMPLEMENTED <<<
  66. Modern mode supports a comprehensive range of commands and capabilities, and
  67. has many configurable options for extensive customizability of the output
  68. format. It also supports attributed text.
  69. SUPPORTED PARSER MODES: Feepbot, Legacy, Modern.
  70. ======================
  71. ==== FEEPBOT mode ====
  72. ======================
  73. >>> NOT YET IMPLEMENTED <<<
  74. Feepbot mode emulates the output format of feepbot by feep.
  75. SUPPORTED PARSER MODES: Feepbot, Legacy.
  76. */
  77. typedef enum
  78. {
  79. SA_DiceFormatterBehaviorDefault = 0,
  80. SA_DiceFormatterBehaviorSimple = 1,
  81. SA_DiceFormatterBehaviorLegacy = 1337,
  82. SA_DiceFormatterBehaviorModern = 2001,
  83. SA_DiceFormatterBehaviorFeepbot = 65516
  84. } SA_DiceFormatterBehavior;
  85. /************************************************/
  86. #pragma mark - SA_DiceFormatter class declaration
  87. /************************************************/
  88. @interface SA_DiceFormatter : NSObject
  89. /**********************************/
  90. #pragma mark - Properties (general)
  91. /**********************************/
  92. @property SA_DiceFormatterBehavior formatterBehavior;
  93. /*************************************************/
  94. #pragma mark - Properties ("legacy" behavior mode)
  95. /*************************************************/
  96. @property BOOL legacyModeErrorReportingEnabled;
  97. /****************************************/
  98. #pragma mark - "Class property" accessors
  99. /****************************************/
  100. + (void)setDefaultFormatterBehavior:(SA_DiceFormatterBehavior)defaultFormatterBehavior;
  101. + (SA_DiceFormatterBehavior)defaultFormatterBehavior;
  102. /********************************************/
  103. #pragma mark - Initializers & factory methods
  104. /********************************************/
  105. - (instancetype)init;
  106. - (instancetype)initWithBehavior:(SA_DiceFormatterBehavior)formatterBehavior NS_DESIGNATED_INITIALIZER;
  107. + (instancetype)defaultFormatter;
  108. + (instancetype)formatterWithBehavior:(SA_DiceFormatterBehavior)formatterBehavior;
  109. /****************************/
  110. #pragma mark - Public methods
  111. /****************************/
  112. - (NSString *)stringFromExpression:(NSDictionary *)expression;
  113. - (NSAttributedString *)attributedStringFromExpression:(NSDictionary *)expression;
  114. @end