Newer
Older
TheVengeance-Project-IADE-Unity2D / Assets / Ink / InkLibs / InkCompiler / StringParser / StringParserState.cs
  1. namespace Ink
  2. {
  3. public class StringParserState
  4. {
  5. public int lineIndex {
  6. get { return currentElement.lineIndex; }
  7. set { currentElement.lineIndex = value; }
  8. }
  9. public int characterIndex {
  10. get { return currentElement.characterIndex; }
  11. set { currentElement.characterIndex = value; }
  12. }
  13. public int characterInLineIndex {
  14. get { return currentElement.characterInLineIndex; }
  15. set { currentElement.characterInLineIndex = value; }
  16. }
  17. public uint customFlags {
  18. get { return currentElement.customFlags; }
  19. set { currentElement.customFlags = value; }
  20. }
  21. public bool errorReportedAlreadyInScope {
  22. get {
  23. return currentElement.reportedErrorInScope;
  24. }
  25. }
  26. public int stackHeight {
  27. get {
  28. return _numElements;
  29. }
  30. }
  31. public class Element {
  32. public int characterIndex;
  33. public int characterInLineIndex;
  34. public int lineIndex;
  35. public bool reportedErrorInScope;
  36. public int uniqueId;
  37. public uint customFlags;
  38. public Element() {
  39. }
  40. public void CopyFrom(Element fromElement)
  41. {
  42. _uniqueIdCounter++;
  43. this.uniqueId = _uniqueIdCounter;
  44. this.characterIndex = fromElement.characterIndex;
  45. this.characterInLineIndex = fromElement.characterInLineIndex;
  46. this.lineIndex = fromElement.lineIndex;
  47. this.customFlags = fromElement.customFlags;
  48. this.reportedErrorInScope = false;
  49. }
  50. // Squash is used when succeeding from a rule,
  51. // so only the state information we wanted to carry forward is
  52. // retained. e.g. characterIndex and lineIndex are global,
  53. // however uniqueId is specific to the individual rule,
  54. // and likewise, custom flags are designed for the temporary
  55. // state of the individual rule too.
  56. public void SquashFrom(Element fromElement)
  57. {
  58. this.characterIndex = fromElement.characterIndex;
  59. this.characterInLineIndex = fromElement.characterInLineIndex;
  60. this.lineIndex = fromElement.lineIndex;
  61. this.reportedErrorInScope = fromElement.reportedErrorInScope;
  62. this.customFlags = fromElement.customFlags;
  63. }
  64. static int _uniqueIdCounter;
  65. }
  66. public StringParserState ()
  67. {
  68. const int kExpectedMaxStackDepth = 200;
  69. _stack = new Element[kExpectedMaxStackDepth];
  70. for (int i = 0; i < kExpectedMaxStackDepth; ++i) {
  71. _stack [i] = new Element ();
  72. }
  73. _numElements = 1;
  74. }
  75. public int Push()
  76. {
  77. if (_numElements >= _stack.Length)
  78. throw new System.Exception ("Stack overflow in parser state");
  79. var prevElement = _stack [_numElements - 1];
  80. var newElement = _stack[_numElements];
  81. _numElements++;
  82. newElement.CopyFrom (prevElement);
  83. return newElement.uniqueId;
  84. }
  85. public void Pop(int expectedRuleId)
  86. {
  87. if (_numElements == 1) {
  88. throw new System.Exception ("Attempting to remove final stack element is illegal! Mismatched Begin/Succceed/Fail?");
  89. }
  90. if ( currentElement.uniqueId != expectedRuleId)
  91. throw new System.Exception ("Mismatched rule IDs - do you have mismatched Begin/Succeed/Fail?");
  92. // Restore state
  93. _numElements--;
  94. }
  95. public Element Peek(int expectedRuleId)
  96. {
  97. if (currentElement.uniqueId != expectedRuleId)
  98. throw new System.Exception ("Mismatched rule IDs - do you have mismatched Begin/Succeed/Fail?");
  99. return _stack[_numElements-1];
  100. }
  101. public Element PeekPenultimate()
  102. {
  103. if (_numElements >= 2) {
  104. return _stack [_numElements - 2];
  105. } else {
  106. return null;
  107. }
  108. }
  109. // Reduce stack height while maintaining currentElement
  110. // Remove second last element: i.e. "squash last two elements together"
  111. // Used when succeeding from a rule (and ONLY when succeeding, since
  112. // the state of the top element is retained).
  113. public void Squash()
  114. {
  115. if (_numElements < 2) {
  116. throw new System.Exception ("Attempting to remove final stack element is illegal! Mismatched Begin/Succceed/Fail?");
  117. }
  118. var penultimateEl = _stack [_numElements - 2];
  119. var lastEl = _stack [_numElements - 1];
  120. penultimateEl.SquashFrom (lastEl);
  121. _numElements--;
  122. }
  123. public void NoteErrorReported()
  124. {
  125. foreach (var el in _stack) {
  126. el.reportedErrorInScope = true;
  127. }
  128. }
  129. protected Element currentElement
  130. {
  131. get {
  132. return _stack [_numElements - 1];
  133. }
  134. }
  135. private Element[] _stack;
  136. private int _numElements;
  137. }
  138. }