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 2.3 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System.Diagnostics;
  2. using System.Runtime.InteropServices;
  3. namespace LLama.Native
  4. {
  5. /// <summary>
  6. /// grammar element type
  7. /// </summary>
  8. public enum LLamaGrammarElementType
  9. {
  10. /// <summary>
  11. /// end of rule definition
  12. /// </summary>
  13. END = 0,
  14. /// <summary>
  15. /// start of alternate definition for rule
  16. /// </summary>
  17. ALT = 1,
  18. /// <summary>
  19. /// non-terminal element: reference to rule
  20. /// </summary>
  21. RULE_REF = 2,
  22. /// <summary>
  23. /// terminal element: character (code point)
  24. /// </summary>
  25. CHAR = 3,
  26. /// <summary>
  27. /// inverse char(s) ([^a], [^a-b] [^abc])
  28. /// </summary>
  29. CHAR_NOT = 4,
  30. /// <summary>
  31. /// modifies a preceding CHAR or CHAR_ALT to
  32. /// be an inclusive range ([a-z])
  33. /// </summary>
  34. CHAR_RNG_UPPER = 5,
  35. /// <summary>
  36. /// modifies a preceding CHAR or
  37. /// CHAR_RNG_UPPER to add an alternate char to match ([ab], [a-zA])
  38. /// </summary>
  39. CHAR_ALT = 6,
  40. }
  41. /// <summary>
  42. /// An element of a grammar
  43. /// </summary>
  44. [StructLayout(LayoutKind.Sequential)]
  45. [DebuggerDisplay("{Type} {Value}")]
  46. public record struct LLamaGrammarElement
  47. {
  48. /// <summary>
  49. /// The type of this element
  50. /// </summary>
  51. public LLamaGrammarElementType Type;
  52. /// <summary>
  53. /// Unicode code point or rule ID
  54. /// </summary>
  55. public uint Value;
  56. /// <summary>
  57. /// Construct a new LLamaGrammarElement
  58. /// </summary>
  59. /// <param name="type"></param>
  60. /// <param name="value"></param>
  61. public LLamaGrammarElement(LLamaGrammarElementType type, uint value)
  62. {
  63. Type = type;
  64. Value = value;
  65. }
  66. internal bool IsCharElement()
  67. {
  68. switch (Type)
  69. {
  70. case LLamaGrammarElementType.CHAR:
  71. case LLamaGrammarElementType.CHAR_NOT:
  72. case LLamaGrammarElementType.CHAR_ALT:
  73. case LLamaGrammarElementType.CHAR_RNG_UPPER:
  74. return true;
  75. default:
  76. return false;
  77. }
  78. }
  79. }
  80. }