コード例 #1
0
        public void Finalize()
        {
            ResetNodeConstruction();

            Stack <Node> nodes = new Stack <Node>();

            nodes.Push(this.Root);

            while (nodes.Count > 0)
            {
                Node currentNode = nodes.Pop();
                if (currentNode.GetType() == typeof(KeyCollectionNode))
                {
                    KeyCollectionNode collectionNode = (KeyCollectionNode)currentNode;
                    foreach (Node child in collectionNode.Children)
                    {
                        if (!collectionNode.IsRoot())
                        {
                            child.Depth = (byte)(collectionNode.Depth + 1);
                        }
                        nodes.Push(child);
                    }
                }
            }

            this.Root.Children.Add(new CapNode(this.File));
        }
コード例 #2
0
        public KeyCollectionNode CreateKeyCollectionNode()
        {
            KeyCollectionNode construct = new KeyCollectionNode(File, this.CurrentKey);

            construct.SetParent(this.CurrentParent);
            this.ResetNodeConstruction();
            this.CurrentParent = construct;
            return(construct);
        }
コード例 #3
0
 public void MoveUpParent()
 {
     if (this.CurrentParent == this.Root)
     {
     }
     else
     {
         this.CurrentParent = this.CurrentParent.Parent;                 // Let us move up.
     }
 }
コード例 #4
0
ファイル: Node.cs プロジェクト: Crazedseal/PdxScriptPlusPlus
 public virtual void SetParent(KeyCollectionNode parent)
 {
     if (Parent != null)
     {
         KeyCollectionNode oldParent = Parent;
         oldParent.Children.Remove(this);
     }
     this.Parent = parent;
     this.Parent.Children.Add(this);
 }
コード例 #5
0
        public override Node Clone()
        {
            KeyCollectionNode clone = new KeyCollectionNode(this.FileOrigin, this.Key);

            clone.Depth = this.Depth;
            foreach (var child in this.Children)
            {
                Node childClone = child.Clone();
                childClone.SetParent(clone);
            }

            return(clone);
        }
コード例 #6
0
        public void Print()
        {
            Stack <Node> nodeStack = new Stack <Node>();

            nodeStack.Push(this);

            Node currentNode = null;

            while (nodeStack.Count > 0)
            {
                Node previousNode = currentNode;
                currentNode = nodeStack.Pop();

                if (previousNode != null && previousNode.Depth > currentNode.Depth)
                {
                    int depthDifference = previousNode.Depth - currentNode.Depth;
                    for (int i = 0; i != depthDifference; i++)
                    {
                        for (int j = 0; j < (previousNode.Depth - 1) - i; j++)
                        {
                            Console.Write('\t');
                        }
                        Console.WriteLine("}");
                    }
                }

                for (int i = 0; i < currentNode.Depth; i++)
                {
                    Console.Write('\t');
                }

                if (currentNode.GetType() == typeof(KeyCollectionNode))
                {
                    KeyCollectionNode keyCollectionNode = (KeyCollectionNode)currentNode;
                    List <Node>       reverse           = new List <Node>(keyCollectionNode.Children);
                    reverse.Reverse();
                    foreach (Node child in reverse)
                    {
                        nodeStack.Push(child);
                    }
                    if (!keyCollectionNode.IsRoot())
                    {
                        Console.WriteLine(currentNode.GetString());
                    }
                }
                else
                {
                    Console.WriteLine(currentNode.GetString());
                }
            }
        }
コード例 #7
0
        public static ParseContext ConstructContext(KeyCollectionNode root)
        {
            ParseContext construct = new ParseContext();

            if (root.SetContext(construct))
            {
                construct.Root          = root;
                construct.CurrentParent = construct.Root;
                return(construct);
            }
            else
            {
                return(null);
            }
        }
コード例 #8
0
 public Boolean IsRootNode(KeyCollectionNode root)
 {
     return(this.Root == root);
 }
コード例 #9
0
        public String PrintString()
        {
            StringBuilder stringBuilder = new StringBuilder();
            Stack <Node>  nodeStack     = new Stack <Node>();

            CapNode cap = new CapNode(this.FileOrigin);

            cap.Depth = this.Depth;

            if (!this.IsRoot())
            {
                nodeStack.Push(cap);
            }

            nodeStack.Push(this);
            int startingDepth = this.Depth;

            Node currentNode = null;

            while (nodeStack.Count > 0)
            {
                Node previousNode = currentNode;
                currentNode = nodeStack.Pop();

                if (previousNode != null && previousNode.Depth > currentNode.Depth)
                {
                    int depthDifference = previousNode.Depth - currentNode.Depth;
                    for (int i = 0; i != depthDifference; i++)
                    {
                        for (int j = startingDepth; j < (previousNode.Depth - 1) - i; j++)
                        {
                            stringBuilder.Append('\t');
                        }
                        stringBuilder.AppendLine("}");
                    }
                }
                if (currentNode.GetType() == typeof(CapNode))
                {
                    continue;
                }
                for (int i = startingDepth; i < currentNode.Depth; i++)
                {
                    stringBuilder.Append('\t');
                }

                if (currentNode.GetType() == typeof(KeyCollectionNode))
                {
                    KeyCollectionNode keyCollectionNode = (KeyCollectionNode)currentNode;
                    List <Node>       reverse           = new List <Node>(keyCollectionNode.Children);
                    reverse.Reverse();
                    foreach (Node child in reverse)
                    {
                        nodeStack.Push(child);
                    }
                    if (!keyCollectionNode.IsRoot())
                    {
                        stringBuilder.AppendLine(currentNode.GetString());
                    }
                }
                else
                {
                    stringBuilder.AppendLine(currentNode.GetString());
                }
            }

            String built = stringBuilder.ToString();

            return(built);
        }
コード例 #10
0
        public static ParsePDXScriptResult ParseFile(String file)
        {
            ParsePDXScriptResult result = new ParsePDXScriptResult();
            // Open the file.
            StreamReader fileRead;

            try
            {
                fileRead = File.OpenText(file);
            }
            catch (Exception except)
            {
                result.Reason  = except.Message;
                result.Success = false;
                result.Context = null;
                return(result);
            }

            // Read the contents, close and dispose.
            String fileContents = fileRead.ReadToEnd();

            fileRead.Close();
            fileRead.Dispose();

            ParseContext      parseContext;
            KeyCollectionNode root = new KeyCollectionNode(file, "root");

            parseContext   = ParseContext.ConstructContext(root);
            result.Context = parseContext;

            Boolean UnixNewLine = (Environment.NewLine == "\n");

            HashSet <int> EscapedCharacters = new HashSet <int>();

            for (int currentPosition = 0; currentPosition < fileContents.Length; currentPosition++)
            {
                Char CurrentCharacter = fileContents[currentPosition];

                switch (parseContext.State)
                {
                case ParseContext.ContextState.Unknown:
                    if (CurrentCharacter == '#')
                    {
                        parseContext.State    = ParseContext.ContextState.CommentBuilding;
                        parseContext.Comment += CurrentCharacter;
                    }
                    else if (CurrentCharacter == '}')
                    {
                        parseContext.MoveUpParent();
                    }
                    else if (Char.IsWhiteSpace(CurrentCharacter) == false)
                    {
                        parseContext.State       = ParseContext.ContextState.KeyBuilding;
                        parseContext.CurrentKey += CurrentCharacter;
                    }
                    break;

                case ParseContext.ContextState.KeyBuilding:
                    if (CurrentCharacter == '#')
                    {
                        parseContext.CreateSingleNode();                                 // Move this current value into a single value line.
                        parseContext.State = ParseContext.ContextState.CommentBuilding;
                    }
                    else if (ValidOperators.Contains(CurrentCharacter))
                    {
                        parseContext.State           = ParseContext.ContextState.AfterOperator;
                        parseContext.CurrentOperator = CurrentCharacter;
                    }
                    else if (CurrentCharacter == '}')
                    {
                        parseContext.CreateSingleNode();
                        parseContext.MoveUpParent();
                        parseContext.State = ParseContext.ContextState.Unknown;
                    }
                    else if (!Char.IsWhiteSpace(CurrentCharacter))
                    {
                        parseContext.CurrentKey += CurrentCharacter;
                    }
                    else
                    {
                        parseContext.State = ParseContext.ContextState.StateFinding;
                    }
                    break;

                case ParseContext.ContextState.StringBuilding:
                    if (CurrentCharacter == '\\' && !EscapedCharacters.Contains(currentPosition))
                    {
                        if (currentPosition + 1 < fileContents.Length)
                        {
                            EscapedCharacters.Add(currentPosition + 1);
                        }

                        parseContext.CurrentValue += CurrentCharacter;
                    }
                    else if (CurrentCharacter == '\\' && EscapedCharacters.Contains(currentPosition))
                    {
                        parseContext.CurrentValue += CurrentCharacter;
                    }
                    else if (CurrentCharacter == '"' && !EscapedCharacters.Contains(currentPosition))
                    {
                        parseContext.CurrentValue += CurrentCharacter;                                 // Finish the end of the String.
                        parseContext.CreateKeyValueNode();
                        parseContext.State             = ParseContext.ContextState.Unknown;
                        parseContext.BuildingNodeState = ParseContext.NodeState.Unknown;
                    }
                    else if (CurrentCharacter == '\n' || CurrentCharacter == '\r')
                    {
                        parseContext.CurrentValue += '"';
                        parseContext.CreateKeyValueNode();
                        parseContext.State             = ParseContext.ContextState.Unknown;
                        parseContext.BuildingNodeState = ParseContext.NodeState.Unknown;
                    }
                    else
                    {
                        parseContext.CurrentValue += CurrentCharacter;
                    }
                    break;

                case ParseContext.ContextState.ValueBuilding:
                    if (CurrentCharacter == '#')
                    {
                        parseContext.CreateKeyValueNode();
                        parseContext.State             = ParseContext.ContextState.CommentBuilding;
                        parseContext.BuildingNodeState = ParseContext.NodeState.Comment;
                    }
                    else if (Char.IsWhiteSpace(CurrentCharacter))
                    {
                        parseContext.CreateKeyValueNode();
                        parseContext.State = ParseContext.ContextState.Unknown;
                    }
                    else if (CurrentCharacter == '}')
                    {
                        parseContext.CreateKeyValueNode();
                        parseContext.State = ParseContext.ContextState.Unknown;
                        parseContext.MoveUpParent();
                    }
                    else
                    {
                        parseContext.CurrentValue += CurrentCharacter;
                    }
                    break;

                case ParseContext.ContextState.StateFinding:
                    if (CurrentCharacter == '#')
                    {
                        parseContext.CreateSingleNode();
                        parseContext.State             = ParseContext.ContextState.CommentBuilding;
                        parseContext.BuildingNodeState = ParseContext.NodeState.Comment;
                    }
                    else if (CurrentCharacter == '}')
                    {
                        parseContext.CreateSingleNode();
                        parseContext.MoveUpParent();
                        parseContext.State = ParseContext.ContextState.Unknown;
                    }
                    else if (ValidOperators.Contains(CurrentCharacter))
                    {
                        parseContext.CurrentOperator = CurrentCharacter;
                        parseContext.State           = ParseContext.ContextState.AfterOperator;
                    }
                    else if (Char.IsWhiteSpace(CurrentCharacter) == false)
                    {
                        parseContext.CreateSingleNode();
                        parseContext.State       = ParseContext.ContextState.Unknown;
                        parseContext.CurrentKey += CurrentCharacter;
                    }
                    break;

                case ParseContext.ContextState.AfterOperator:
                    if (CurrentCharacter == '{')
                    {
                        parseContext.CreateKeyCollectionNode();
                        parseContext.State = ParseContext.ContextState.Unknown;
                    }
                    else if (CurrentCharacter == '"')
                    {
                        parseContext.CurrentValue += CurrentCharacter;
                        parseContext.State         = ParseContext.ContextState.StringBuilding;
                    }
                    else if (CurrentCharacter == '#')
                    {
                        // Discard current node.
                        parseContext.ForceDiscardNode();
                        parseContext.State = ParseContext.ContextState.CommentBuilding;
                    }
                    else if (Char.IsWhiteSpace(CurrentCharacter) == false)
                    {
                        parseContext.CurrentValue += CurrentCharacter;
                        parseContext.State         = ParseContext.ContextState.ValueBuilding;
                    }
                    break;

                case ParseContext.ContextState.CommentBuilding:
                    if (UnixNewLine)
                    {
                        if (CurrentCharacter == '\n')
                        {
                            parseContext.CreateCommentNode();
                            parseContext.State = ParseContext.ContextState.Unknown;
                        }
                        else
                        {
                            parseContext.Comment += CurrentCharacter;
                        }
                    }
                    else
                    {
                        int NextCharacterPosition = currentPosition + 1;

                        if (NextCharacterPosition < fileContents.Length &&
                            CurrentCharacter == '\r' &&
                            fileContents[NextCharacterPosition] == '\n')
                        {
                            parseContext.CreateCommentNode();
                            parseContext.State = ParseContext.ContextState.Unknown;
                            currentPosition    = NextCharacterPosition;
                        }
                        else
                        {
                            parseContext.Comment += CurrentCharacter;
                        }
                    }
                    break;
                }
            }

            // Ensuring we do not drop the last node.
            switch (parseContext.State)
            {
            case ParseContext.ContextState.AfterOperator:
                parseContext.CreateKeyValueNode();
                break;

            case ParseContext.ContextState.Unknown:                     // Literally do nothing.
                break;

            case ParseContext.ContextState.CommentBuilding:
                parseContext.CreateCommentNode();
                break;

            case ParseContext.ContextState.StateFinding:
                parseContext.CreateSingleNode();
                break;

            case ParseContext.ContextState.StringBuilding:
                parseContext.CurrentValue += '"';                         // Close the String.
                parseContext.CreateKeyValueNode();
                break;

            case ParseContext.ContextState.ValueBuilding:
                parseContext.CreateKeyValueNode();
                break;

            case ParseContext.ContextState.KeyBuilding:
                parseContext.CreateSingleNode();
                break;
            }


            parseContext.Finalize();
            return(result);
        }