Skip to content

Commit

Permalink
Fix bugs, optimize codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
BearOffice committed Jan 6, 2022
1 parent e57d614 commit 5aee125
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 64 deletions.
2 changes: 1 addition & 1 deletion BearMLLib/BearMLLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Version>3.1.2</Version>
<Version>3.1.3</Version>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Authors>Bear</Authors>
<Company>Bear Office</Company>
Expand Down
18 changes: 12 additions & 6 deletions BearMLLib/Core/GroupParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ internal static Group ParseFromRaw(ReferList<string> raw, bool isDefault, out in
{
if (LineAnalyzer.IsGroupLine(raw[i]))
{
var lineCount = 0;
foreach (var line in raw[checkFrom..i])
{
CheckLine(line, checkFrom);
CheckLine(line, checkFrom + lineCount);
group.OrderedLine.Add(new TaggedLine(false, line));
lineCount++;
}

return group;
Expand All @@ -40,11 +42,13 @@ internal static Group ParseFromRaw(ReferList<string> raw, bool isDefault, out in
var contentType = LineAnalyzer.GetContentType(raw[i..]);
var content = IContentParser.GetParser(contentType).ParseFromRaw(raw[i..], out var contentDepth);



var lineCount = 0;
foreach (var line in raw[checkFrom..(i - keyDepth)])
{
CheckLine(line, checkFrom);
CheckLine(line, checkFrom + lineCount);
group.OrderedLine.Add(new TaggedLine(false, line));
lineCount++;
}

var pair = new KeyContentPair(key, content, raw[(i - keyDepth)..(i + contentDepth + 1)]);
Expand All @@ -59,10 +63,12 @@ internal static Group ParseFromRaw(ReferList<string> raw, bool isDefault, out in

if (checkFrom < raw.Count)
{
foreach(var line in raw[checkFrom..])
var lineCount = 0;
foreach (var line in raw[checkFrom..])
{
CheckLine(line, checkFrom);
CheckLine(line, checkFrom + lineCount);
group.OrderedLine.Add(new TaggedLine(false, line));
lineCount++;
}
}

Expand Down Expand Up @@ -100,7 +106,7 @@ private static string GetGroupNameFromRaw(string rawLine)
private static void CheckLine(string rawLine, int offset)
{
if (!(LineAnalyzer.IsNullOrWhiteSpaceLine(rawLine) || LineAnalyzer.IsCommentLine(rawLine)))
ErrorHandler.This.Add(ErrorType.InvalidLine, offset + 1, rawLine);
ErrorHandler.This.Add(ErrorType.InvalidLine, offset, rawLine);
}
}
}
44 changes: 22 additions & 22 deletions BearMLLib/Core/Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ namespace BearMLLib.Core
{
internal class Reader
{
private readonly string[] _raw;
private readonly ReferList<string> _raw;
private int _currentLinePos;

internal Reader()
{
_raw = new string[0];
_raw = new ReferList<string>(new string[0]);
_currentLinePos = 0;
}

internal Reader(string[] raw)
{
_raw = raw;
_raw = new ReferList<string>(raw);
_currentLinePos = 0;
}

internal Reader(string path)
{
_raw = File.ReadAllLines(path);
_raw = new ReferList<string>(File.ReadAllLines(path));
_currentLinePos = 0;
}

Expand All @@ -37,31 +37,31 @@ internal ConfigsContainer GenerateConfigs()
ErrorHandler.This.Message += ThrowError;

var container = new ConfigsContainer();
var startPos = 0;

// create default group at start.
var defaultGroup = GroupParser.ParseFromRaw(new ReferList<string>(new string[0]), true, out _);
container.GroupsDic.Add(defaultGroup.Name, defaultGroup);
container.OrderedLine.Add(new TaggedLine(true, defaultGroup.Name));
if (LineAnalyzer.IsGroupLine(_raw[startPos]))
{
// create empty default group at start.
var defaultGroup = GroupParser.ParseFromRaw(new ReferList<string>(new string[0]), true, out _);
container.GroupsDic.Add(defaultGroup.Name, defaultGroup);
container.OrderedLine.Add(new TaggedLine(true, defaultGroup.Name));
}
else
{
var defaultGroup = GroupParser.ParseFromRaw(_raw[startPos..], true, out var groupDepth);
container.GroupsDic.Add(defaultGroup.Name, defaultGroup);
container.OrderedLine.Add(new TaggedLine(true, defaultGroup.Name));

for (var i = 0; i < _raw.Length; i++)
startPos += groupDepth + 1;
}

for (var i = startPos; i < _raw.Count; i++)
{
_currentLinePos = i;

if (LineAnalyzer.IsGroupLine(_raw[i]))
{
var group = GroupParser.ParseFromRaw(new ReferList<string>(_raw[i..]), false, out var groupDepth);
container.GroupsDic.Add(group.Name, group);
container.OrderedLine.Add(new TaggedLine(true, group.Name));

i += groupDepth;
}
else if (LineAnalyzer.IsKeyContentLine(_raw[i]))
{
// if default group contains key content line, clear the default group which is created previously.
container.GroupsDic.Clear();
container.OrderedLine.RemoveAt(0);

var group = GroupParser.ParseFromRaw(new ReferList<string>(_raw[i..]), true, out var groupDepth);
var group = GroupParser.ParseFromRaw(_raw[i..], false, out var groupDepth);
container.GroupsDic.Add(group.Name, group);
container.OrderedLine.Add(new TaggedLine(true, group.Name));

Expand Down
73 changes: 39 additions & 34 deletions Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,43 @@
using BearMLLib;
using BearMLLib.Core;
using BearMLLib.Helpers;
using BearMLLib.Serialization.Conversion;
using Demo;

var bearML = new BearML(@"config.txt");
Format.This.ValueMode = ValueMode.ForceEscaped;
Console.WriteLine("int is {0}", bearML.GetContent<string>("int"));
Console.WriteLine("double is {0}", bearML.GetContent<double>("double"));
Console.WriteLine("time is {0}", bearML.GetContent<TimeSpan>("time"));

Console.WriteLine();

Console.WriteLine("literial's comment is '{0}'", bearML.GetComment("literial"));
Console.WriteLine("literial's key alias is '{0}'", bearML.GetKeyAlias("literial"));
Console.WriteLine("literial is:\n{0}", bearML.GetContent<string>("li"));

Console.WriteLine();

Console.WriteLine("escaped is:\n{0}", bearML.GetContent<string>("escaped"));

Console.WriteLine();

Console.WriteLine("expanded is {0}",
ListVisualizer.Visualize(bearML.GetContent<List<List<string>>>("list group", "expanded")));
Console.WriteLine("folded is {0}",
ListVisualizer.Visualize(bearML.GetContent<List<List<string>>>("list group", "folded")));
Console.WriteLine("mixed is {0}",
ListVisualizer.Visualize(bearML.GetContent<List<List<string>>>("list group", "mixed")));

Console.WriteLine();

var weatherObj = bearML.DeserializeObjectGroup<Weather>("weather obj");
Console.WriteLine("Temp is {0} degree. Time is {1}.", weatherObj.Temperature, weatherObj.Time);
weatherObj = new Weather() { Time = new TimeSpan(10, 11, 12), Temperature = 22 };
bearML.ChangeObjectGroup("weather obj", weatherObj);
Console.WriteLine("Temp is {0} degree. Time is {1}.", weatherObj.Temperature, weatherObj.Time);
namespace Demo;
class Program
{
static void Main()
{
var bearML = new BearML(@"config.txt");
Format.This.ValueMode = ValueMode.ForceEscaped;
Console.WriteLine("int is {0}", bearML.GetContent<string>("int"));
Console.WriteLine("double is {0}", bearML.GetContent<double>("double"));
Console.WriteLine("time is {0}", bearML.GetContent<TimeSpan>("time"));

Console.WriteLine();

Console.WriteLine("literial's comment is '{0}'", bearML.GetComment("literial"));
Console.WriteLine("literial's key alias is '{0}'", bearML.GetKeyAlias("literial"));
Console.WriteLine("literial is:\n{0}", bearML.GetContent<string>("li"));

Console.WriteLine();

Console.WriteLine("escaped is:\n{0}", bearML.GetContent<string>("escaped"));

Console.WriteLine();

Console.WriteLine("expanded is {0}",
ListVisualizer.Visualize(bearML.GetContent<List<List<string>>>("list group", "expanded")));
Console.WriteLine("folded is {0}",
ListVisualizer.Visualize(bearML.GetContent<List<List<string>>>("list group", "folded")));
Console.WriteLine("mixed is {0}",
ListVisualizer.Visualize(bearML.GetContent<List<List<string>>>("list group", "mixed")));

Console.WriteLine();

var weatherObj = bearML.DeserializeObjectGroup<Weather>("weather obj");
Console.WriteLine("Temp is {0} degree. Time is {1}.", weatherObj.Temperature, weatherObj.Time);
weatherObj = new Weather() { Time = new TimeSpan(10, 11, 12), Temperature = 22 };
bearML.ChangeObjectGroup("weather obj", weatherObj);
Console.WriteLine("Temp is {0} degree. Time is {1}.", weatherObj.Temperature, weatherObj.Time);
}
}
1 change: 0 additions & 1 deletion Demo/Weather.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using BearMLLib.Serialization;

namespace Demo;

internal class Weather
{
public TimeSpan Time { get; set; }
Expand Down

0 comments on commit 5aee125

Please sign in to comment.