SymbolIds is now SortedDictionary (although I'm not sure it really needs to be) because the test was failing due to expected value being in another order. The C++ data structure if SymbolIds is std::map<std::string, uint32_t> so the items are ordered by key.tags/v0.5.1
| @@ -68,7 +68,7 @@ namespace LLama.Unittest | |||||
| new LLamaGrammarElement(LLamaGrammarElementType.END, 0), | new LLamaGrammarElement(LLamaGrammarElementType.END, 0), | ||||
| new LLamaGrammarElement(LLamaGrammarElementType.RULE_REF, 1), | new LLamaGrammarElement(LLamaGrammarElementType.RULE_REF, 1), | ||||
| new LLamaGrammarElement(LLamaGrammarElementType.RULE_REF, 4), | new LLamaGrammarElement(LLamaGrammarElementType.RULE_REF, 4), | ||||
| new LLamaGrammarElement(LLamaGrammarElementType.END, 0), | |||||
| new LLamaGrammarElement(LLamaGrammarElementType.ALT, 0), | |||||
| new LLamaGrammarElement(LLamaGrammarElementType.RULE_REF, 1), | new LLamaGrammarElement(LLamaGrammarElementType.RULE_REF, 1), | ||||
| new LLamaGrammarElement(LLamaGrammarElementType.END, 0), | new LLamaGrammarElement(LLamaGrammarElementType.END, 0), | ||||
| new LLamaGrammarElement(LLamaGrammarElementType.CHAR, 45), | new LLamaGrammarElement(LLamaGrammarElementType.CHAR, 45), | ||||
| @@ -42,7 +42,7 @@ namespace LLama.Grammar | |||||
| private uint GetSymbolId(ParseState state, ReadOnlySpan<byte> src, int len) | private uint GetSymbolId(ParseState state, ReadOnlySpan<byte> src, int len) | ||||
| { | { | ||||
| uint nextId = (uint)state.SymbolIds.Count; | uint nextId = (uint)state.SymbolIds.Count; | ||||
| string key = Encoding.UTF8.GetString(src.Slice(0, len).ToArray()); | |||||
| string key = Encoding.UTF8.GetString(src.Slice(0, src.Length - len).ToArray()); | |||||
| if (state.SymbolIds.TryGetValue(key, out uint existingId)) | if (state.SymbolIds.TryGetValue(key, out uint existingId)) | ||||
| { | { | ||||
| @@ -344,7 +344,7 @@ namespace LLama.Grammar | |||||
| ReadOnlySpan<byte> nameEnd = ParseName(src); | ReadOnlySpan<byte> nameEnd = ParseName(src); | ||||
| ReadOnlySpan<byte> pos = ParseSpace(nameEnd, false); | ReadOnlySpan<byte> pos = ParseSpace(nameEnd, false); | ||||
| int nameLen = src.Length - nameEnd.Length; | int nameLen = src.Length - nameEnd.Length; | ||||
| uint ruleId = GetSymbolId(state, src.Slice(0, nameLen), nameLen); | |||||
| uint ruleId = GetSymbolId(state, src.Slice(0, nameLen), 0); | |||||
| string name = Encoding.UTF8.GetString(src.Slice(0, nameLen).ToArray()); | string name = Encoding.UTF8.GetString(src.Slice(0, nameLen).ToArray()); | ||||
| if (!(pos[0] == ':' && pos[1] == ':' && pos[2] == '=')) | if (!(pos[0] == ':' && pos[1] == ':' && pos[2] == '=')) | ||||
| @@ -12,7 +12,7 @@ namespace LLama.Grammar | |||||
| /// </summary> | /// </summary> | ||||
| public class ParseState | public class ParseState | ||||
| { | { | ||||
| public Dictionary<string, uint> SymbolIds { get; } = new Dictionary<string, uint>(); | |||||
| public SortedDictionary<string, uint> SymbolIds { get; } = new SortedDictionary<string, uint>(); | |||||
| public List<List<LLamaGrammarElement>> Rules { get; } = new List<List<LLamaGrammarElement>>(); | public List<List<LLamaGrammarElement>> Rules { get; } = new List<List<LLamaGrammarElement>>(); | ||||
| public IEnumerable<List<LLamaGrammarElement>> CRules() | public IEnumerable<List<LLamaGrammarElement>> CRules() | ||||