Browse Source

Bug fixes after running tests.

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
Mihai 2 years ago
parent
commit
2ae1891c13
3 changed files with 4 additions and 4 deletions
  1. +1
    -1
      LLama.Unittest/GrammarParserTest.cs
  2. +2
    -2
      LLama/Grammar/GrammarParser.cs
  3. +1
    -1
      LLama/Grammar/ParseState.cs

+ 1
- 1
LLama.Unittest/GrammarParserTest.cs View File

@@ -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),


+ 2
- 2
LLama/Grammar/GrammarParser.cs View File

@@ -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] == '='))


+ 1
- 1
LLama/Grammar/ParseState.cs View File

@@ -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()


Loading…
Cancel
Save