A set of classes for parsing, evaluating, and formatting die roll strings.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

SA_DiceParser.h 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // SA_DiceParser.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 roll string parsing.
  13. Each behavior mode defines a set of capabilities - what sorts of expressions,
  14. operators, roll commands, etc. the parser recognizes in that mode, and the
  15. syntax for using those capabilities.
  16. The structure and content of the expression tree generated by the parser from
  17. a given die roll string also depends on the behavior mode that the parser is
  18. set to.
  19. NOTE: While SA_DiceEvaluator is modeless (it correctly evaluates all
  20. expressions generated by the parser in any supported mode),
  21. SA_DiceFormatter is modal. SA_DiceFormatter provides several
  22. formatter behaviors, each of which supports one or more parser modes. Using the
  23. wrong formatter behavior mode for an expression tree (that is, passing an
  24. SA_DiceFormatter instance an expression tree that was was generated
  25. by a parser mode that is not supported by the formatter’s currently set
  26. formatter behavior mode) results in undefined behavior.
  27. See SA_DiceFormatter.h for a list of which formatter behavior modes are
  28. appropriate for use with which parser modes. See also SA_DiceBot.h for a
  29. discussion of how the currently set parser mode affects what die-roll-related
  30. bot commands are available. (Together, the parser behavior mode and the results
  31. formatter behavior mode define the behavior and capabilities of an SA_DiceBot.)
  32. Each mode is described below.
  33. ======================
  34. ==== DEFAULT mode ====
  35. ======================
  36. “Default” mode is an alias for whatever default behavior is currently set for
  37. new SA_DiceParser instances. (The “default default” behavior for the current
  38. implementation is “legacy”.)
  39. =====================
  40. ==== LEGACY mode ====
  41. =====================
  42. Legacy mode (mostly) emulates DiceBot by Sabin (and Dawn by xthemage before
  43. it). It replicates the parsing and evaluation functions of those venerable
  44. bots, providing the following capabilities:
  45. 1. Arithmetic operations: addition, subtraction, multiplication. Syntax is as
  46. normal for such operations, e.g.:
  47. 2+3
  48. 25*10
  49. 5-3*2
  50. Normal operator precedence and behavior (commutativity, associativity) apply.
  51. 2. Simple roll-and-sum. Roll X dice, each with Y sides, and take the sum of
  52. the rolled values, by inputting ‘XdY’ where X is a nonnegative integer and Y
  53. is a positive integer, e.g.:
  54. 1d20
  55. 5d6
  56. 8d27
  57. 3. Left-associative recursive roll-and-sum. Roll X dice, each with Y sides,
  58. and take the sum of the rolled values, by inputting ‘XdY’, where Y is a
  59. positive integer and X may be a nonnegative integer or a recursive roll-and-sum
  60. expression, e.g.:
  61. 5d6d10
  62. 1d20d6
  63. 4d4d4d4d4d4d4
  64. The above capabilities may be used indiscriminately in a single roll string:
  65. 1d20-5
  66. 5d6*10
  67. 1d20+2d4*10
  68. 5d6d10-2*3
  69. 5+3-2*4d6+2d10d3-20+5d4*2
  70. NOTE: The ‘d’ operator takes precedence over arithmetic operators. (Legacy
  71. mode does not support parentheses.)
  72. NOTE 2: Legacy mode does not support whitespace within roll strings.
  73. TODO: Document exploding dice, Fudge dice, and ‘k’ and ‘l’ operators.
  74. =====================
  75. ==== MODERN mode ====
  76. =====================
  77. >>> NOT YET IMPLEMENTED <<<
  78. Modern mode provides a comprehensive range of commands and capabilities.
  79. ======================
  80. ==== FEEPBOT mode ====
  81. ======================
  82. >>> NOT YET IMPLEMENTED <<<
  83. Feepbot mode emulates feepbot by feep.
  84. */
  85. typedef NS_ENUM(NSUInteger, SA_DiceParserBehavior) {
  86. SA_DiceParserBehaviorDefault = 0,
  87. SA_DiceParserBehaviorLegacy = 1337,
  88. SA_DiceParserBehaviorModern = 2001,
  89. SA_DiceParserBehaviorFeepbot = 65536
  90. };
  91. /*********************************************/
  92. #pragma mark - SA_DiceParser class declaration
  93. /*********************************************/
  94. @interface SA_DiceParser : NSObject
  95. /************************/
  96. #pragma mark - Properties
  97. /************************/
  98. @property SA_DiceParserBehavior parserBehavior;
  99. /******************************/
  100. #pragma mark - Class properties
  101. /******************************/
  102. @property (class) SA_DiceParserBehavior defaultParserBehavior;
  103. /********************************************/
  104. #pragma mark - Initializers & factory methods
  105. /********************************************/
  106. -(instancetype) init;
  107. -(instancetype) initWithBehavior:(SA_DiceParserBehavior)parserBehavior NS_DESIGNATED_INITIALIZER;
  108. +(instancetype) defaultParser;
  109. +(instancetype) parserWithBehavior:(SA_DiceParserBehavior)parserBehavior;
  110. /****************************/
  111. #pragma mark - Public methods
  112. /****************************/
  113. -(SA_DiceExpression *) expressionForString:(NSString *)dieRollString;
  114. @end