Ejemplo n.º 1
0
        public IKey Parse(string inFileName, string inText, ILogPrinter inLogger, object inContextData)
        {
            if (!string.IsNullOrEmpty(inFileName))
            {
                _parsed.RemoveAll(p => string.Equals(p.FileName, inFileName));
            }

            var supporter = new CSupportOwner(this, inLogger, inContextData, inFileName);

            _sentenser.ParseText(inText, supporter.GetLogger());

            List <CTokenLine> lines = new List <CTokenLine>();

            for (int i = 0; i < _sentenser.SentenseCount; i++)
            {
                CSentense  sentense = _sentenser[i];
                CTokenLine tl       = new CTokenLine();
                tl.Init(sentense, supporter.GetLogger());
                lines.Add(tl);
            }

            string root_name = Path.GetFileNameWithoutExtension(inFileName);
            CKey   root      = CTreeBuilder.Build(root_name, lines, supporter);

            _parsed.Add(new CParsed(root, lines, inFileName));

            return(root);
        }
Ejemplo n.º 2
0
        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()}");
            }
        }
Ejemplo n.º 3
0
        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();
        }
Ejemplo n.º 4
0
        static CKey CreateNewArrayKey(CKey inParent, CTokenLine line, CBuildCommands inCommands)
        {
            var res_arr_key = CKey.CreateArrayKey(inParent, line.Position);

            if (inCommands.IsNextArrayKeyNamePresent)
            {
                res_arr_key.SetName(inCommands.PopNextArrayKeyName(inParent));
            }
            return(res_arr_key);
        }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
        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);
            }
        }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
        internal void AddTokenTail(CTokenLine line, ILogger inLoger)
        {
            if (line.Comments != null)
            {
                AddComments(line.Comments.Text);
            }

            if (line.IsTailEmpty)
            {
                return;
            }

            for (int i = 0; i < line.TailLength; i++)
            {
                CToken t = line.Tail[i];

                CBaseElement be = CBaseValue.CreateBaseValue(this, t);

                if (be == null)
                {
                    inLoger.LogError(EErrorCode.WrongTokenInTail, t);
                }
            }
        }
Ejemplo n.º 9
0
        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
            });
        }
Ejemplo n.º 10
0
 internal static CKey Create(CKey parent, CTokenLine line, ILogger inLoger)
 {
     return(new CKey(parent, line, inLoger));
 }
Ejemplo n.º 11
0
 private CKey(CKey parent, CTokenLine line, ILogger inLoger) : base(parent, line.Head.Position)
 {
     _name = line.Head.Text;
     AddTokenTail(line, inLoger);
 }
Ejemplo n.º 12
0
        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();
        }
Ejemplo n.º 13
0
        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));
            }
        }
Ejemplo n.º 14
0
        //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);
        }