A set of classes for parsing, evaluating, and formatting die roll strings.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

SA_DiceFormatter.h 5.2KB

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