using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace LLama.Native
{
///
/// grammar element type
///
public enum LLamaGrammarElementType
{
///
/// end of rule definition
///
END = 0,
///
/// start of alternate definition for rule
///
ALT = 1,
///
/// non-terminal element: reference to rule
///
RULE_REF = 2,
///
/// terminal element: character (code point)
///
CHAR = 3,
///
/// inverse char(s) ([^a], [^a-b] [^abc])
///
CHAR_NOT = 4,
///
/// modifies a preceding CHAR or CHAR_ALT to
/// be an inclusive range ([a-z])
///
CHAR_RNG_UPPER = 5,
///
/// modifies a preceding CHAR or
/// CHAR_RNG_UPPER to add an alternate char to match ([ab], [a-zA])
///
CHAR_ALT = 6,
}
///
/// An element of a grammar
///
[StructLayout(LayoutKind.Sequential)]
[DebuggerDisplay("{Type} {Value}")]
public readonly struct LLamaGrammarElement
: IEquatable
{
///
/// The type of this element
///
public readonly LLamaGrammarElementType Type;
///
/// Unicode code point or rule ID
///
public readonly uint Value;
///
/// Construct a new LLamaGrammarElement
///
///
///
public LLamaGrammarElement(LLamaGrammarElementType type, uint value)
{
Type = type;
Value = value;
}
///
public bool Equals(LLamaGrammarElement other)
{
if (Type != other.Type)
return false;
// No need to compare values for the END rule
if (Type == LLamaGrammarElementType.END)
return true;
return Value == other.Value;
}
///
public override bool Equals(object? obj)
{
return obj is LLamaGrammarElement other && Equals(other);
}
///
public override int GetHashCode()
{
unchecked
{
var hash = 2999;
hash = hash * 7723 + (int)Type;
hash = hash * 7723 + (int)Value;
return hash;
}
}
internal bool IsCharElement()
{
switch (Type)
{
case LLamaGrammarElementType.CHAR:
case LLamaGrammarElementType.CHAR_NOT:
case LLamaGrammarElementType.CHAR_ALT:
case LLamaGrammarElementType.CHAR_RNG_UPPER:
return true;
default:
return false;
}
}
}
}