AlternativeParser.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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.Composite
  25. namespace Spart.Parsers.Composite
  26. {
  27. using System;
  28. using Spart.Scanners;
  29. using Spart.Actions;
  30. using Spart.Parsers.NonTerminal;
  31. public class AlternativeParser : BinaryTerminalParser
  32. {
  33. public AlternativeParser(Parser first, Parser second)
  34. :base(first,second)
  35. {}
  36. public override ParserMatch ParseMain(IScanner scan)
  37. {
  38. // save scanner state
  39. long offset = scan.Offset;
  40. // apply the first parser
  41. ParserMatch m = FirstParser.Parse( scan );
  42. // if m1 successful, do m2
  43. if (m.Success)
  44. return m;
  45. // not found
  46. scan.Seek(offset);
  47. // apply the second parser
  48. m = SecondParser.Parse( scan );
  49. if (m.Success)
  50. return m;
  51. scan.Seek(offset);
  52. return scan.NoMatch;
  53. }
  54. public override Parser Clone()
  55. {
  56. return base.Clone();
  57. }
  58. }
  59. }