Skip to content

Commit

Permalink
Improve error message.
Browse files Browse the repository at this point in the history
  • Loading branch information
BearOffice committed Sep 26, 2022
1 parent 69633ee commit f582940
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion BearMarkupLanguage/BearMarkupLanguage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Version>5.0.7</Version>
<Version>5.0.8</Version>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Authors>Bear</Authors>
<Company>Bear Office</Company>
Expand Down
30 changes: 23 additions & 7 deletions BearMarkupLanguage/Interpretation/DictionaryElementInterpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private static ElementResult CollapsedInterprete(string line)
Message = "The format of this element is not valid."
}); ;

var tempDir = new OrderedDictionary<BasicElement, IBaseElement>();
var tempDic = new OrderedDictionary<BasicElement, IBaseElement>();

var isSplited = true;
var isEmpty = true;
Expand Down Expand Up @@ -88,10 +88,18 @@ private static ElementResult CollapsedInterprete(string line)
if (tempKey is null)
{
tempKey = (BasicElement)result.Value;

// check if key already existed.
if (tempDic.ContainsKey(tempKey)) return ElementResult.Fail(new InvalidFormatExceptionArgs
{
LineIndex = 0,
CharIndex = i,
Message = "Key must be unique."
});
}
else
{
tempDir.Add(tempKey, result.Value);
tempDic.Add(tempKey, result.Value);
tempKey = null;
valueEntry = false;
isSplited = false;
Expand Down Expand Up @@ -127,7 +135,7 @@ private static ElementResult CollapsedInterprete(string line)
var result = new EmptyElementInterpreter().Interprete(null, ParseMode.Collapse);
if (!result.IsSuccess) return ElementResult.PassToParent(result, 0, i);

tempDir.Add(tempKey, result.Value);
tempDic.Add(tempKey, result.Value);
i = i + ID.EmptySymbol.Length - 1;
tempKey = null;
valueEntry = false;
Expand Down Expand Up @@ -203,7 +211,7 @@ private static ElementResult CollapsedInterprete(string line)
Message = "Unnecessary split symbol."
});

return ElementResult.Success(new DictionaryElement(tempDir));
return ElementResult.Success(new DictionaryElement(tempDic));
}

private static ElementResult ExpandedInterprete(string[] lines)
Expand All @@ -223,22 +231,30 @@ private static ElementResult ExpandedInterprete(string[] lines)
});

var key = GetKey(lines[i], out var idIndex);
if (key is null)
if (key is null) return ElementResult.Fail(new InvalidFormatExceptionArgs
{
LineIndex = i,
CharIndex = 0,
Message = "Key cannot be empty or white space in expanded dictionary."
});
// check if key already existed.
if (tempDic.ContainsKey(new BasicElement(key)))
return ElementResult.Fail(new InvalidFormatExceptionArgs
{
LineIndex = i,
CharIndex = 0,
Message = "Key cannot be empty or white space in expanded dictionary."
Message = "Key must be unique."
});


// key.Length + id's length
if (idIndex + 1 == lines[i].Length)
refLines[i] = "";
else
refLines[i] = refLines[i][(idIndex + 1)..];

var result = ContextInterpreter.ContentInterprete(refLines[i..], out var endAtIndex);
if (!result.IsSuccess)
if (!result.IsSuccess)
return ElementResult.PassToParent(result, i, 0);

tempDic.Add(new BasicElement(key), result.Value);
Expand Down

0 comments on commit f582940

Please sign in to comment.