NonTerminalParser.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /// Spart License (zlib/png)
  2. ///
  3. ///
  4. /// Copyright (c) 2003 Jonathan de Halleux
  5. ///
  6. /// This software is provided 'as-is', without any express or implied warranty.
  7. /// In no event will the authors be held liable for any damages arising from
  8. /// the use of this software.
  9. ///
  10. /// Permission is granted to anyone to use this software for any purpose,
  11. /// including commercial applications, and to alter it and redistribute it
  12. /// freely, subject to the following restrictions:
  13. ///
  14. /// 1. The origin of this software must not be misrepresented; you must not
  15. /// claim that you wrote the original software. If you use this software in a
  16. /// product, an acknowledgment in the product documentation would be
  17. /// appreciated but is not required.
  18. ///
  19. /// 2. Altered source versions must be plainly marked as such, and must not be
  20. /// misrepresented as being the original software.
  21. ///
  22. /// 3. This notice may not be removed or altered from any source distribution.
  23. ///
  24. /// Author: Jonathan de Halleuxnamespace Spart.Parsers.NonTerminal
  25. namespace Spart.Parsers.NonTerminal
  26. {
  27. using System;
  28. using Spart.Scanners;
  29. /// <summary>
  30. /// NonTerminal parser abstract class
  31. /// </summary>
  32. public abstract class NonTerminalParser : Parser
  33. {
  34. private String m_ID;
  35. /// <summary>
  36. /// Default constructor
  37. /// </summary>
  38. public NonTerminalParser()
  39. :base()
  40. {
  41. ID = GetHashCode().ToString();
  42. }
  43. /// <summary>
  44. /// Rule ID, used for debugging
  45. /// </summary>
  46. public string ID
  47. {
  48. get
  49. {
  50. return m_ID;
  51. }
  52. set
  53. {
  54. if( m_ID != value )
  55. m_ID = value;
  56. }
  57. }
  58. /// <summary>
  59. /// Pre parse event
  60. /// </summary>
  61. public event PreParseEventHandler PreParse;
  62. /// <summary>
  63. /// Post parse event
  64. /// </summary>
  65. public event PostParseEventHandler PostParse;
  66. /// <summary>
  67. /// Preparse event caller
  68. /// </summary>
  69. /// <param name="scan"></param>
  70. public virtual void OnPreParse(IScanner scan)
  71. {
  72. if (PreParse != null)
  73. PreParse(this, new PreParseEventArgs(this,scan) );
  74. }
  75. /// <summary>
  76. /// Post parse event caller
  77. /// </summary>
  78. /// <param name="match"></param>
  79. /// <param name="scan"></param>
  80. public virtual void OnPostParse(ParserMatch match, IScanner scan)
  81. {
  82. if (PostParse != null)
  83. PostParse(this,new PostParseEventArgs(match,this,scan));
  84. }
  85. /// <summary>
  86. /// Adds event handlers
  87. /// </summary>
  88. /// <param name="context"></param>
  89. public void AddContext(IParserContext context)
  90. {
  91. PreParse+=new PreParseEventHandler(context.PreParse);
  92. PostParse+=new PostParseEventHandler(context.PostParse);
  93. }
  94. /// <summary>
  95. /// Removes event handlers
  96. /// </summary>
  97. /// <param name="context"></param>
  98. public void RemoveContext(IParserContext context)
  99. {
  100. PreParse-=new PreParseEventHandler(context.PreParse);
  101. PostParse-=new PostParseEventHandler(context.PostParse);
  102. }
  103. public override Parser Clone()
  104. {
  105. NonTerminalParser clone = base.Clone() as NonTerminalParser;
  106. return clone;
  107. }
  108. }
  109. }