static void ExecuteCommand_Delete(CKey inParent, CTokenLine line, ITreeBuildSupport inSupport) { if (line.CommandParams.Length == 0 || string.IsNullOrEmpty(line.CommandParams[0])) { inSupport.GetLogger().LogError(EErrorCode.PathEmpty, line); return; } string key_path = line.CommandParams[0]; CKey start_key = inParent; int i = 0; while (i < key_path.Length && key_path[i] == '<' && start_key.Parent != null) { start_key = start_key.Parent; i++; } key_path = key_path.Substring(i); string[] path = key_path.Split(new char[] { '\\', '/' }); if (!RemoveKeysByPath(start_key, path)) { inSupport.GetLogger().LogError(EErrorCode.CantFindKey, line, $"Start key: {inParent.GetPath()}"); } }
static void ExecuteCommand_Insert(CKey arr_key, CTokenLine line, ITreeBuildSupport inSupport) { string file_name = line.CommandParams["file"]; string key_path = line.CommandParams["key"]; if (string.IsNullOrEmpty(file_name) && string.IsNullOrEmpty(key_path)) { inSupport.GetLogger().LogError(EErrorCode.PathEmpty, line); } CKey root = null; if (!string.IsNullOrEmpty(file_name)) { root = (CKey)inSupport.GetTree(file_name); } else { root = arr_key.GetRoot(); } if (root == null) { inSupport.GetLogger().LogError(EErrorCode.CantFindInsertFile, line); return; } if (root.KeyCount == 1 && root.GetKey(0).IsArrayKey()) { root = root.GetKey(0); } CKey key = root; if (!string.IsNullOrEmpty(key_path)) { key = (CKey)root.FindKey(key_path); if (key == null) { inSupport.GetLogger().LogError(EErrorCode.CantFindKey, line); return; } } bool insert_parent = line.CommandParams.ContainsKey("parent"); CKey copy_key = key.GetCopy() as CKey; if (insert_parent) { copy_key.SetParent(arr_key); } else { arr_key.TakeAllElements(copy_key, false); } arr_key.CheckOnOneArray(); }
static void OnClosingKey(CKey key, EKeyAddingMode inKeyAddMode, ITreeBuildSupport inSupport) { if (key == null) { return; } if (key.IsEmpty) { inSupport.GetLogger().LogError(EErrorCode.HeadWithoutValues, key); return; } if (inKeyAddMode == EKeyAddingMode.AddUnique) { return; } CKey parent = key.Parent; if (parent == null) { inSupport.GetLogger().LogError(EErrorCode.KeyMustHaveParent, key); return; } key.SetParent(null); if (inKeyAddMode == EKeyAddingMode.Override) { var pathes = new List <List <string> >(); key.GetTerminalPathes(pathes, new List <string>()); //for correct deleting array elems for (int i = pathes.Count - 1; i >= 0; --i) { var path = pathes[i]; RemoveKeysByPath(parent, path); } inKeyAddMode = EKeyAddingMode.Add; } if (inKeyAddMode == EKeyAddingMode.Add) { CKey child_key = parent.FindChildKey(key.Name); if (child_key != null) { child_key.MergeKey(key); } else { key.SetParent(parent); } } }
static void ExecuteCommand_Delete(CKey arr_key, CTokenLine line, ITreeBuildSupport inSupport) { if (line.CommandParams.Length == 0 || string.IsNullOrEmpty(line.CommandParams[0])) { inSupport.GetLogger().LogError(EErrorCode.PathEmpty, line); return; } string key_path = line.CommandParams[0]; string[] path = key_path.Split(new char[] { '\\', '/' }); if (!RemoveKeysByPath(arr_key, path)) { inSupport.GetLogger().LogError(EErrorCode.CantFindKey, line); } }
//inStartLine - next of parent line static void CollectByDivs(CKey inParent, int inParentRank, int inStartLine, int inEndLine, List <CTokenLine> inLines, ITreeBuildSupport inSupport, CKey inRoot, EKeyAddingMode inKeyAddingMode) { int curr_rank = inParentRank + 1; List <Tuple <int, int> > recs_by_divs = new List <Tuple <int, int> >(); CollectDividers(inStartLine, inEndLine, curr_rank, inLines, inSupport, recs_by_divs); if (recs_by_divs.Count > 1) { if (inKeyAddingMode == EKeyAddingMode.Override) { inParent.ClearAllArrayKeys(); } for (int i = 0; i < recs_by_divs.Count; i++) { int first_line = recs_by_divs[i].Item1; int exlude_last_line = recs_by_divs[i].Item2; if (first_line < exlude_last_line) { CKey arr_key = CKey.CreateArrayKey(inParent, inLines[first_line].Position); if (IsLinePresent(curr_rank, first_line, exlude_last_line, inLines)) { Collect(arr_key, curr_rank, first_line, exlude_last_line, inLines, inSupport, inRoot, EKeyAddingMode.AddUnique); } else { CollectByDivs(arr_key, curr_rank, first_line, exlude_last_line, inLines, inSupport, inRoot, EKeyAddingMode.AddUnique); } if (arr_key.IsEmpty) { arr_key.SetParent(null); } } } } else { Collect(inParent, inParentRank, recs_by_divs[0].Item1, recs_by_divs[0].Item2, inLines, inSupport, inRoot, inKeyAddingMode); } }
public static CKey Build(string inRootName, List <CTokenLine> inLines, ITreeBuildSupport inSupport) { var root = CKey.CreateRoot(inRootName); if (inLines.Count == 0) { return(root); } CollectByDivs(root, -1, 0, inLines.Count, inLines, inSupport, root, EKeyAddingMode.AddUnique); if (root.KeyCount == 1 && root.GetKey(0).IsArrayKey() && root.GetKey(0).KeyCount == 0) { root.TakeAllElements(root.GetKey(0), false); root.GetKey(0).SetParent(null); } return(root); }
static CKey ExecuteCommand(CKey inParent, CKey inArrKey, CTokenLine line, ITreeBuildSupport inSupport, CBuildCommands inCommands) { CKey arr_key = inArrKey; if (line.Command == ECommands.Name) { if (line.CommandParams.Length < 1) { inSupport.GetLogger().LogError(EErrorCode.EmptyCommand, line); } else { inCommands.SetNextArrayKeyName(line.CommandParams[0], line.Position.Line, inParent); } } else if (line.Command == ECommands.Insert) { if (arr_key == null) { arr_key = CreateNewArrayKey(inParent, line, inCommands); } ExecuteCommand_Insert(arr_key, line, inSupport); } else if (line.Command == ECommands.Delete) { if (arr_key != null) { ExecuteCommand_Delete(arr_key, line, inSupport); } } else if (line.Command == ECommands.ChangeValue) { if (arr_key != null) { arr_key.ChangeValues(line.CommandParams.GetDictionary()); } } return(arr_key); }
public static CKey Build(string inRootName, List <CTokenLine> inLines, ITreeBuildSupport inSupport) { var root = CKey.CreateRoot(inRootName); CBuildCommands commands = new CBuildCommands(inSupport.GetLogger()); SCollectResult collect_res = Collect(root, -1, inLines, 0, inSupport, commands); if (!collect_res.WasRecordDivider) { root.CheckOnOneArray(); } //if(root.ValuesCount == 0 && root.KeyCount == 1 && string.IsNullOrEmpty(root.Name)) //{ // CKey k = root.GetKey(0); // k.SetParent(null); // root = k; //} return(root); }
static SCollectResult Collect(CKey inParent, int inParentRank, List <CTokenLine> inLines, int inStartIndex, ITreeBuildSupport inSupport, CBuildCommands inCommands) { SAddLineResult current_state = new SAddLineResult(); int curr_rank = inParentRank + 1; bool rec_divider_was = false; int i = inStartIndex; while (i < inLines.Count) { int t = i; CTokenLine line = inLines[i]; if (!line.IsEmpty() || line.Comments != null) { if (line.Rank < curr_rank) { OnClosingKey(current_state.last_record_key, current_state.last_key_add_mode, inSupport); return(new SCollectResult { CurrentLineIndex = i, WasRecordDivider = rec_divider_was }); } else if (line.Rank > curr_rank) { if (current_state.last_record_key == null) { current_state.last_record_key = CreateNewArrayKey(inParent, line, inCommands); } SCollectResult collect_res = Collect(current_state.last_record_key, curr_rank, inLines, i, inSupport, inCommands); if (!collect_res.WasRecordDivider) { current_state.last_record_key.CheckOnOneArray(); } i = collect_res.CurrentLineIndex; } else { OnClosingKey(current_state.last_record_key, current_state.last_key_add_mode, inSupport); current_state = AddLine(inParent, current_state.current_array_key, line, inSupport, inCommands); if (current_state.WasRecordDivider) { rec_divider_was = true; } } } if (t == i) { i++; } } //if (!rec_divider_was && current_state.current_array_key != null) // current_state.current_array_key.CheckOnOneArray(); OnClosingKey(current_state.last_record_key, current_state.last_key_add_mode, inSupport); return(new SCollectResult { CurrentLineIndex = i, WasRecordDivider = rec_divider_was }); }
static SAddLineResult AddLine(CKey inParent, CKey inCurrentArrayKey, CTokenLine line, ITreeBuildSupport inSupport, CBuildCommands inCommands) { SAddLineResult result = new SAddLineResult(); if (line.IsEmpty()) { if (line.Comments != null) { inCommands.SetNextLineComment(line.Comments.Text, line.Position.Line, inParent); } result.current_array_key = inCurrentArrayKey; } else if (line.IsRecordDivider()) { result.current_array_key = null; result.WasRecordDivider = true; } else if (line.IsCommandLine()) { result.current_array_key = ExecuteCommand(inParent, inCurrentArrayKey, line, inSupport, inCommands); } else if (line.Head != null) { result.current_array_key = inCurrentArrayKey; if (result.current_array_key == null) { result.current_array_key = CreateNewArrayKey(inParent, line, inCommands); } if (line.AdditionMode == EKeyAddingMode.AddUnique && result.current_array_key.IsKeyWithNamePresent(line.Head.Text)) { inSupport.GetLogger().LogError(EErrorCode.ElementWithNameAlreadyPresent, line); } result.last_key_add_mode = line.AdditionMode; result.last_record_key = CKey.Create(result.current_array_key, line, inSupport.GetLogger()); if (inCommands.IsNextLineCommentPresent) { result.last_record_key.AddComments(inCommands.PopNextLineComments(inParent)); } } else if (!line.IsTailEmpty) { //if (!line.IsNewArrayLine && line.TailLength == 1 && !inParent.IsArray) // inParent.AddTokenTail(line, true, inSupport.GetLogger()); //else { result.current_array_key = CreateNewArrayKey(inParent, line, inCommands); result.current_array_key.AddTokenTail(line, inSupport.GetLogger()); } } return(result); }
static void ExecuteCommand_Insert(CKey inParent, CTokenLine line, ITreeBuildSupport inSupport, CKey inRoot) { string file_name = line.CommandParams["file"]; string key_path = line.CommandParams["key"]; if (string.IsNullOrEmpty(file_name) && string.IsNullOrEmpty(key_path)) { inSupport.GetLogger().LogError(EErrorCode.PathEmpty, line); } CKey root = inRoot; if (!string.IsNullOrEmpty(file_name)) { root = (CKey)inSupport.GetTree(file_name); } //else // root = inParent.GetRoot(); if (root == null) { inSupport.GetLogger().LogError(EErrorCode.CantFindInsertFile, line); return; } if (root.KeyCount == 1 && root.GetKey(0).IsArrayKey()) { root = root.GetKey(0); } CKey key = root; if (!string.IsNullOrEmpty(key_path)) { key = (CKey)root.FindKey(key_path); if (key == null) { inSupport.GetLogger().LogError(EErrorCode.CantFindKey, line); return; } } CKey destination_key = inParent; bool insert_in_parent = line.CommandParams.ContainsKey("insert_in_parent"); if (insert_in_parent) { if (inParent.Parent != null) { destination_key = inParent.Parent; } else { inSupport.GetLogger().LogError(EErrorCode.KeyMustHaveParent, line); } } bool add_key = line.CommandParams.ContainsKey("add_key"); CKey copy_key = key.GetCopy() as CKey; if (add_key) { copy_key.SetParent(destination_key); } else { destination_key.TakeAllElements(copy_key, false); } destination_key.CheckOnOneArray(); }
static void CollectDividers(int inStartLine, int inEndLine, int inNeedRank, List <CTokenLine> inLines, ITreeBuildSupport inSupport, List <Tuple <int, int> > outList) { int start = inStartLine; int end; do { end = FindDivider(start, inEndLine, inNeedRank, inLines); outList.Add(new Tuple <int, int>(start, end)); start = end + 1; }while (start < inEndLine); }
static Tuple <CKey, int> CreateKey(int inStartLine, int inEndLine, List <CTokenLine> inLines, ITreeBuildSupport inSupport, CKey inRoot, CKey inParent, EKeyAddingMode inKeyAddingMode) { CTokenLine line = inLines[inStartLine]; int key_rank = line.Rank; if (line.IsHeadEmpty) { CKey arr_key = CKey.CreateArrayKey(inParent, line.Position); arr_key.AddTokenTail(line, inSupport.GetLogger()); return(new Tuple <CKey, int>(arr_key, inStartLine + 1)); } else { CKey key = null; if (inKeyAddingMode != EKeyAddingMode.AddUnique) { key = inParent.FindChildKey(line.Head.Text); if (key == null) { inSupport.GetLogger().LogError(EErrorCode.CantFindKey, line, $"Key name: {line.Head.Text}"); } else { if (inKeyAddingMode == EKeyAddingMode.Override) { key.ClearComments(); key.ClearValues(); } if (inKeyAddingMode == EKeyAddingMode.Override || inKeyAddingMode == EKeyAddingMode.Add) { key.AddTokenTail(line, inSupport.GetLogger()); } } } else if (inParent.IsKeyWithNamePresent(line.Head.Text)) { inSupport.GetLogger().LogError(EErrorCode.ElementWithNameAlreadyPresent, line); } if (key == null) { key = CKey.Create(inParent, line, inSupport.GetLogger()); } int last_line = FindNextSameRankLine(inStartLine, inEndLine, key_rank, inLines); if (last_line > inStartLine + 1) { EKeyAddingMode next_add_mode = inKeyAddingMode; if (next_add_mode == EKeyAddingMode.Add) { next_add_mode = EKeyAddingMode.AddUnique; } CollectByDivs(key, key_rank, inStartLine + 1, last_line, inLines, inSupport, inRoot, next_add_mode); } if (key.KeyCount == 0 && key.ValuesCount == 0) { inSupport.GetLogger().LogError(EErrorCode.HeadWithoutValues, line); } return(new Tuple <CKey, int>(key, last_line)); } }
//inStartLine - next of parent line static void Collect(CKey inParent, int inParentRank, int inStartLine, int inEndLine, List <CTokenLine> inLines, ITreeBuildSupport inSupport, CKey inRoot, EKeyAddingMode inKeyAddingMode) { CBuildCommands command_for_next_string = null; int start = inStartLine; do { int old_start = start; CTokenLine line = inLines[start]; if (line.IsCommandLine()) { if (line.Command == ECommands.Name) { if (line.CommandParams.Length < 1) { inSupport.GetLogger().LogError(EErrorCode.EmptyCommand, line); } else { if (inParent.IsArray && line.Rank == inParentRank) { if (!inParent.SetName(line.CommandParams[0])) { inSupport.GetLogger().LogError(EErrorCode.DublicateKeyName, line); } } else { //inSupport.GetLogger().LogError(EErrorCode.NextArrayKeyNameMissParent, line); if (command_for_next_string == null) { command_for_next_string = new CBuildCommands(inSupport.GetLogger()); } command_for_next_string.SetNextArrayKeyName(line.CommandParams[0], line.Position.Line, inParent); } } } else if (line.Command == ECommands.Insert) { ExecuteCommand_Insert(inParent, line, inSupport, inRoot); } else if (line.Command == ECommands.Delete) { ExecuteCommand_Delete(inParent, line, inSupport); } else if (line.Command == ECommands.ChangeValue) { inParent.ChangeValues(line.CommandParams.GetDictionary()); } } else if (line.IsEmpty() && line.Comments != null) { if (command_for_next_string == null) { command_for_next_string = new CBuildCommands(inSupport.GetLogger()); } command_for_next_string.SetNextLineComment(line.Comments.Text, line.Position.Line, inParent); } else if (!line.IsEmpty()) { EKeyAddingMode adding_mode = inKeyAddingMode; if (adding_mode == EKeyAddingMode.AddUnique && line.AdditionMode != EKeyAddingMode.AddUnique) { adding_mode = line.AdditionMode; } Tuple <CKey, int> new_key_new_line = CreateKey(start, inEndLine, inLines, inSupport, inRoot, inParent, adding_mode); CKey new_key = new_key_new_line.Item1; if (command_for_next_string != null && command_for_next_string.IsNextArrayKeyNamePresent) { if (!new_key.SetName(command_for_next_string.PopNextArrayKeyName(inParent))) { inSupport.GetLogger().LogError(EErrorCode.DublicateKeyName, line); } } if (command_for_next_string != null && command_for_next_string.IsNextLineCommentPresent) { new_key.AddComments(command_for_next_string.PopNextLineComments(inParent)); } //if (!new_key.IsArray && line.AdditionMode == EKeyAddingMode.AddUnique && inParent.IsKeyWithNamePresent(new_key.Name)) // inSupport.GetLogger().LogError(EErrorCode.ElementWithNameAlreadyPresent, inLines[start]); //if(line.AdditionMode == EKeyAddingMode.AddUnique) // new_key.SetParent(inParent); //else if (line.AdditionMode == EKeyAddingMode.Override || line.AdditionMode == EKeyAddingMode.Add) //{ // if (line.AdditionMode == EKeyAddingMode.Override) // { // var pathes = new List<List<string>>(); // new_key.GetTerminalPathes(pathes, new List<string>()); // //for correct deleting array elems // for (int i = pathes.Count - 1; i >= 0; --i) // { // var path = pathes[i]; // RemoveKeysByPath(inParent, path); // } // } // CKey child_key = inParent.FindChildKey(new_key.Name); // if (child_key != null) // child_key.MergeKey(new_key); // else // new_key.SetParent(inParent); //} start = new_key_new_line.Item2; } if (old_start == start) { start++; } }while (start < inEndLine); }