You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

LLamaGrammarElement.cs 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.InteropServices;
  4. namespace LLama.Native
  5. {
  6. /// <summary>
  7. /// grammar element type
  8. /// </summary>
  9. public enum LLamaGrammarElementType
  10. {
  11. /// <summary>
  12. /// end of rule definition
  13. /// </summary>
  14. END = 0,
  15. /// <summary>
  16. /// start of alternate definition for rule
  17. /// </summary>
  18. ALT = 1,
  19. /// <summary>
  20. /// non-terminal element: reference to rule
  21. /// </summary>
  22. RULE_REF = 2,
  23. /// <summary>
  24. /// terminal element: character (code point)
  25. /// </summary>
  26. CHAR = 3,
  27. /// <summary>
  28. /// inverse char(s) ([^a], [^a-b] [^abc])
  29. /// </summary>
  30. CHAR_NOT = 4,
  31. /// <summary>
  32. /// modifies a preceding CHAR or CHAR_ALT to
  33. /// be an inclusive range ([a-z])
  34. /// </summary>
  35. CHAR_RNG_UPPER = 5,
  36. /// <summary>
  37. /// modifies a preceding CHAR or
  38. /// CHAR_RNG_UPPER to add an alternate char to match ([ab], [a-zA])
  39. /// </summary>
  40. CHAR_ALT = 6,
  41. }
  42. /// <summary>
  43. /// An element of a grammar
  44. /// </summary>
  45. [StructLayout(LayoutKind.Sequential)]
  46. [DebuggerDisplay("{Type} {Value}")]
  47. public struct LLamaGrammarElement
  48. : IEquatable<LLamaGrammarElement>
  49. {
  50. /// <summary>
  51. /// The type of this element
  52. /// </summary>
  53. public LLamaGrammarElementType Type;
  54. /// <summary>
  55. /// Unicode code point or rule ID
  56. /// </summary>
  57. public uint Value;
  58. /// <summary>
  59. /// Construct a new LLamaGrammarElement
  60. /// </summary>
  61. /// <param name="type"></param>
  62. /// <param name="value"></param>
  63. public LLamaGrammarElement(LLamaGrammarElementType type, uint value)
  64. {
  65. Type = type;
  66. Value = value;
  67. }
  68. /// <inheritdoc />
  69. public bool Equals(LLamaGrammarElement other)
  70. {
  71. if (Type != other.Type)
  72. return false;
  73. // No need to compare values for the END rule
  74. if (Type == LLamaGrammarElementType.END)
  75. return true;
  76. return Value == other.Value;
  77. }
  78. /// <inheritdoc />
  79. public override bool Equals(object? obj)
  80. {
  81. return obj is LLamaGrammarElement other && Equals(other);
  82. }
  83. /// <inheritdoc />
  84. public override int GetHashCode()
  85. {
  86. unchecked
  87. {
  88. var hash = 2999;
  89. hash = hash * 7723 + (int)Type;
  90. hash = hash * 7723 + (int)Value;
  91. return hash;
  92. }
  93. }
  94. internal bool IsCharElement()
  95. {
  96. switch (Type)
  97. {
  98. case LLamaGrammarElementType.CHAR:
  99. case LLamaGrammarElementType.CHAR_NOT:
  100. case LLamaGrammarElementType.CHAR_ALT:
  101. case LLamaGrammarElementType.CHAR_RNG_UPPER:
  102. return true;
  103. default:
  104. return false;
  105. }
  106. }
  107. }
  108. }