Improve parsing
This commit is contained in:
@@ -30,9 +30,10 @@ namespace Filtration.Parser.Services
|
||||
}
|
||||
|
||||
// Converts a string into an ItemFilterCommentBlock maintaining newlines and spaces but removing # characters
|
||||
public IItemFilterCommentBlock TranslateStringToItemFilterCommentBlock(string inputString, IItemFilterScript parentItemFilterScript)
|
||||
public IItemFilterCommentBlock TranslateStringToItemFilterCommentBlock(string inputString, IItemFilterScript parentItemFilterScript, string originalString = "")
|
||||
{
|
||||
var itemFilterCommentBlock = new ItemFilterCommentBlock(parentItemFilterScript);
|
||||
itemFilterCommentBlock.OriginalText = originalString;
|
||||
|
||||
foreach (var line in new LineReader(() => new StringReader(inputString)))
|
||||
{
|
||||
@@ -40,14 +41,15 @@ namespace Filtration.Parser.Services
|
||||
itemFilterCommentBlock.Comment += trimmedLine + Environment.NewLine;
|
||||
}
|
||||
|
||||
itemFilterCommentBlock.Comment = itemFilterCommentBlock.Comment.TrimEnd('\r', '\n');
|
||||
|
||||
itemFilterCommentBlock.Comment = itemFilterCommentBlock.Comment.TrimEnd('\r', '\n');
|
||||
|
||||
itemFilterCommentBlock.IsEdited = false;
|
||||
return itemFilterCommentBlock;
|
||||
}
|
||||
|
||||
// This method converts a string into a ItemFilterBlock. This is used for pasting ItemFilterBlocks
|
||||
// and reading ItemFilterScripts from a file.
|
||||
public IItemFilterBlock TranslateStringToItemFilterBlock(string inputString, IItemFilterScript parentItemFilterScript, bool initialiseBlockGroupHierarchyBuilder = false)
|
||||
public IItemFilterBlock TranslateStringToItemFilterBlock(string inputString, IItemFilterScript parentItemFilterScript, string originalString = "", bool initialiseBlockGroupHierarchyBuilder = false)
|
||||
{
|
||||
if (initialiseBlockGroupHierarchyBuilder)
|
||||
{
|
||||
@@ -57,6 +59,7 @@ namespace Filtration.Parser.Services
|
||||
_masterComponentCollection = parentItemFilterScript.ItemFilterScriptSettings.ThemeComponentCollection;
|
||||
var block = new ItemFilterBlock(parentItemFilterScript);
|
||||
var showHideFound = false;
|
||||
block.OriginalText = originalString;
|
||||
|
||||
foreach (var line in new LineReader(() => new StringReader(inputString)))
|
||||
{
|
||||
@@ -295,8 +298,9 @@ namespace Filtration.Parser.Services
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
block.IsEdited = false;
|
||||
return block;
|
||||
}
|
||||
|
||||
@@ -516,6 +520,11 @@ namespace Filtration.Parser.Services
|
||||
// TODO: Private
|
||||
public string TranslateItemFilterCommentBlockToString(IItemFilterCommentBlock itemFilterCommentBlock)
|
||||
{
|
||||
if (!itemFilterCommentBlock.IsEdited)
|
||||
{
|
||||
return itemFilterCommentBlock.OriginalText;
|
||||
}
|
||||
|
||||
// TODO: Tests
|
||||
// TODO: # Section: text?
|
||||
var commentWithHashes = string.Empty;
|
||||
@@ -535,16 +544,16 @@ namespace Filtration.Parser.Services
|
||||
// TODO: Private
|
||||
public string TranslateItemFilterBlockToString(IItemFilterBlock block)
|
||||
{
|
||||
var outputString = string.Empty;
|
||||
|
||||
if (!block.Enabled)
|
||||
{
|
||||
outputString += "#Disabled Block Start" + Environment.NewLine;
|
||||
if(!block.IsEdited)
|
||||
{
|
||||
return block.OriginalText;
|
||||
}
|
||||
|
||||
var outputString = string.Empty;
|
||||
|
||||
if (!string.IsNullOrEmpty(block.Description))
|
||||
{
|
||||
outputString += (block.Enabled ? "# " : "## ") + block.Description + Environment.NewLine;
|
||||
outputString += "# " + block.Description + Environment.NewLine;
|
||||
}
|
||||
|
||||
outputString += (!block.Enabled ? "#" : string.Empty) + block.Action.GetAttributeDescription();
|
||||
@@ -566,11 +575,6 @@ namespace Filtration.Parser.Services
|
||||
outputString += (!block.Enabled ? _disabledNewLine : _newLine) + blockItem.OutputText;
|
||||
}
|
||||
}
|
||||
|
||||
if (!block.Enabled)
|
||||
{
|
||||
outputString += Environment.NewLine + "#Disabled Block End";
|
||||
}
|
||||
|
||||
return outputString;
|
||||
}
|
||||
|
||||
@@ -51,94 +51,36 @@ namespace Filtration.Parser.Services
|
||||
public static string PreprocessDisabledBlocks(string inputString)
|
||||
{
|
||||
bool inDisabledBlock = false;
|
||||
var showHideFound = false;
|
||||
|
||||
var lines = Regex.Split(inputString, "\r\n|\r|\n").ToList();
|
||||
var linesToRemove = new List<int>();
|
||||
|
||||
for (var i = 0; i < lines.Count; i++)
|
||||
{
|
||||
if (lines[i].StartsWith("#Disabled Block Start"))
|
||||
{
|
||||
inDisabledBlock = true;
|
||||
linesToRemove.Add(i);
|
||||
continue;
|
||||
}
|
||||
if (inDisabledBlock)
|
||||
{
|
||||
if (lines[i].StartsWith("#Disabled Block End"))
|
||||
{
|
||||
inDisabledBlock = false;
|
||||
showHideFound = false;
|
||||
linesToRemove.Add(i);
|
||||
continue;
|
||||
}
|
||||
|
||||
lines[i] = lines[i].TrimStart('#');
|
||||
lines[i] = lines[i].TrimStart(' ');
|
||||
lines[i] = lines[i].Replace("#", " # ");
|
||||
var spaceOrEndOfLinePos = lines[i].IndexOf(" ", StringComparison.Ordinal) > 0 ? lines[i].IndexOf(" ", StringComparison.Ordinal) : lines[i].Length;
|
||||
var lineOption = lines[i].Substring(0, spaceOrEndOfLinePos);
|
||||
|
||||
// If we haven't found a Show or Hide line yet, then this is probably the block comment.
|
||||
// Put its # back on and skip to the next line.
|
||||
if (lineOption != "Show" && lineOption != "Hide" && showHideFound == false)
|
||||
{
|
||||
lines[i] = "#" + lines[i];
|
||||
continue;
|
||||
}
|
||||
|
||||
if (lineOption == "Show")
|
||||
{
|
||||
lines[i] = lines[i].Replace("Show", "ShowDisabled");
|
||||
showHideFound = true;
|
||||
}
|
||||
else if (lineOption == "Hide")
|
||||
{
|
||||
lines[i] = lines[i].Replace("Hide", "HideDisabled");
|
||||
showHideFound = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var i = linesToRemove.Count - 1; i >= 0; i--)
|
||||
{
|
||||
lines.RemoveAt(linesToRemove[i]);
|
||||
}
|
||||
|
||||
return lines.Aggregate((c, n) => c + Environment.NewLine + n);
|
||||
}
|
||||
|
||||
public static string ConvertCommentedToDisabled(string inputString)
|
||||
{
|
||||
var lines = Regex.Split(inputString, "\r\n|\r|\n");
|
||||
var parsingCommented = false;
|
||||
for (var i = 0; i < lines.Length; i++)
|
||||
{
|
||||
if (!parsingCommented && lines[i].StartsWith("#"))
|
||||
if (!inDisabledBlock && lines[i].StartsWith("#"))
|
||||
{
|
||||
string curLine = Regex.Replace(lines[i].Substring(1), @"\s+", "");
|
||||
if ((curLine.StartsWith("Show") || curLine.StartsWith("Hide")) && (curLine.Length == 4 || curLine[4] == '#'))
|
||||
{
|
||||
parsingCommented = true;
|
||||
lines[i] = "#Disabled Block Start" + Environment.NewLine + lines[i];
|
||||
inDisabledBlock = true;
|
||||
lines[i] = lines[i].Substring(1).TrimStart(' ');
|
||||
lines[i] = lines[i].Substring(0, 4) + "Disabled" + lines[i].Substring(4);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else
|
||||
if (inDisabledBlock)
|
||||
{
|
||||
if (parsingCommented && !lines[i].StartsWith("#"))
|
||||
if (!lines[i].StartsWith("#"))
|
||||
{
|
||||
parsingCommented = false;
|
||||
lines[i - 1] += Environment.NewLine + "#Disabled Block End";
|
||||
inDisabledBlock = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
lines[i] = lines[i].Substring(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
if(parsingCommented)
|
||||
{
|
||||
lines[lines.Length - 1] += Environment.NewLine + "#Disabled Block End";
|
||||
}
|
||||
|
||||
return string.Join(Environment.NewLine, lines);
|
||||
return lines.Aggregate((c, n) => c + Environment.NewLine + n);
|
||||
}
|
||||
|
||||
public IItemFilterScript TranslateStringToItemFilterScript(string inputString)
|
||||
@@ -146,18 +88,14 @@ namespace Filtration.Parser.Services
|
||||
var script = _itemFilterScriptFactory.Create();
|
||||
_blockGroupHierarchyBuilder.Initialise(script.ItemFilterBlockGroups.First());
|
||||
|
||||
//Check for possible different disabled block syntaxes if the script is not from Filtration
|
||||
if (!inputString.Contains("Script edited with Filtration"))
|
||||
{
|
||||
inputString = ConvertCommentedToDisabled(inputString);
|
||||
}
|
||||
//Remove old disabled tags
|
||||
inputString = Regex.Replace(inputString, @"#Disabled\sBlock\s(Start|End).*?\n", "");
|
||||
inputString = (inputString.EndsWith("\n#Disabled Block End")) ? inputString.Substring(0, inputString.Length - 19) : inputString;
|
||||
|
||||
var originalLines = Regex.Split(inputString, "\r\n|\r|\n");
|
||||
|
||||
inputString = inputString.Replace("\t", "");
|
||||
if (inputString.Contains("#Disabled Block Start"))
|
||||
{
|
||||
inputString = PreprocessDisabledBlocks(inputString);
|
||||
}
|
||||
inputString = PreprocessDisabledBlocks(inputString);
|
||||
|
||||
var conditionBoundaries = IdentifyBlockBoundaries(inputString);
|
||||
|
||||
@@ -195,14 +133,24 @@ namespace Filtration.Parser.Services
|
||||
var block = new string[end - begin];
|
||||
Array.Copy(lines, begin, block, 0, end - begin);
|
||||
var blockString = string.Join("\r\n", block);
|
||||
Array.Copy(originalLines, begin, block, 0, end - begin);
|
||||
var originalString = "";
|
||||
for (var i = block.Length - 1; i >= 0; i--)
|
||||
{
|
||||
if(block[i].Replace(" ", "").Replace("\t", "").Length > 0)
|
||||
{
|
||||
originalString = string.Join("\r\n", block, 0, i + 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (boundary.Value.BoundaryType == ItemFilterBlockBoundaryType.ItemFilterBlock)
|
||||
{
|
||||
script.ItemFilterBlocks.Add(_blockTranslator.TranslateStringToItemFilterBlock(blockString, script));
|
||||
script.ItemFilterBlocks.Add(_blockTranslator.TranslateStringToItemFilterBlock(blockString, script, originalString));
|
||||
}
|
||||
else
|
||||
{
|
||||
script.ItemFilterBlocks.Add(_blockTranslator.TranslateStringToItemFilterCommentBlock(blockString, script));
|
||||
script.ItemFilterBlocks.Add(_blockTranslator.TranslateStringToItemFilterCommentBlock(blockString, script, originalString));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user