A set of classes for parsing, evaluating, and formatting die roll strings.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

SA_DiceFormatter.h 5.2KB

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