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.

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