Пример #1
0
 public SyntaxTreeNode RemoveChild(SyntaxTreeNode node)
 {
     _children.Remove(node);
     node.Parent = null;
     return(node);
 }
Пример #2
0
        private static SyntaxTree Run(string text)
        {
            var syntaxTree = new SyntaxTree()
            {
                Root = new SyntaxTreeNode()
                {
                    Text = "()", Weight = 6
                }
            };
            var nodeStack = new Stack <SyntaxTreeNode>();

            var currentNode = syntaxTree.Root.AppendChild(string.Empty);

            var quotationStack = new Stack <char>();

            for (var i = 0; i < text.Length; i++)
            {
                if (quotationStack.Count != 0)
                {
                    if (text[i] == '\\' && i < text.Length - 1 && (text[i + 1] == '\'' || text[i + 1] == '"'))
                    {
                        currentNode.Text += text[i + 1].ToString();
                        i++;

                        continue;
                    }

                    currentNode.Text += text[i].ToString();
                    if (quotationStack.Peek() == text[i])
                    {
                        if (i == 0 || text[i - 1] != '^')
                        {
                            quotationStack.Pop();
                        }
                    }

                    continue;
                }
                else if (text[i] == '"' || text[i] == '\'')
                {
                    quotationStack.Push(text[i]);
                    currentNode.Text += text[i].ToString();
                    continue;
                }
                else if (text[i] == '[')
                {
                    currentNode.Text = currentNode.Text.Trim();

                    var parent = currentNode.Parent;
                    while (parent.Weight < 5)
                    {
                        currentNode = parent;
                        parent      = parent.Parent;
                    }

                    var newNode = new SyntaxTreeNode
                    {
                        Text   = "[]",
                        Weight = 5,
                    };

                    parent.RemoveChild(currentNode);
                    newNode.AppendChild(currentNode);
                    parent.AppendChild(newNode);
                    currentNode = newNode.AppendChild(string.Empty);
                    nodeStack.Push(newNode);
                }
                else if (text[i] == ']')
                {
                    currentNode.Text = currentNode.Text.Trim();
                    if (string.IsNullOrEmpty(currentNode.Text))
                    {
                        currentNode.Parent.RemoveChild(currentNode);
                    }

                    currentNode = nodeStack.Pop();
                }
                else if (text[i] == '(')
                {
                    currentNode.Text = currentNode.Text.Trim();

                    if (string.IsNullOrEmpty(currentNode.Text))
                    {
                        currentNode.Text = "()";
                    }
                    else
                    {
                        currentNode.Text = "fn:" + currentNode.Text;
                    }

                    nodeStack.Push(currentNode);
                    currentNode.Weight = 5;
                    currentNode        = currentNode.AppendChild(string.Empty);
                }
                else if (text[i] == ')')
                {
                    currentNode.Text = currentNode.Text.Trim();
                    if (string.IsNullOrEmpty(currentNode.Text))
                    {
                        currentNode.Parent.RemoveChild(currentNode);
                    }

                    currentNode = nodeStack.Pop();
                }
                else if (text[i] == ',')
                {
                    currentNode.Text = currentNode.Text.Trim();
                    if (string.IsNullOrEmpty(currentNode.Text))
                    {
                        currentNode.Parent.RemoveChild(currentNode);
                    }

                    do
                    {
                        currentNode = nodeStack.Pop();
                    }while (!currentNode.Text.StartsWith("fn:"));

                    nodeStack.Push(currentNode);
                    currentNode = currentNode.AppendChild(string.Empty);
                }
                else if (_keyWorDictionary.Keys.Contains(text[i]))
                {
                    var keyWord = IdentifyKeyWord(text, ref i, _keyWorDictionary[text[i]]);
                    if (!string.IsNullOrEmpty(keyWord))
                    {
                        currentNode.Text = currentNode.Text.Trim();

                        var weight = _WeightDictionary[keyWord];
                        while (currentNode.Parent != syntaxTree.Root && currentNode.Parent.Weight < weight)
                        {
                            currentNode = currentNode.Parent;
                        }

                        var parent = currentNode.Parent;
                        var old    = parent.RemoveChild(currentNode);

                        currentNode        = parent.AppendChild(keyWord);
                        currentNode.Weight = weight;

                        if (!string.IsNullOrEmpty(old.Text))
                        {
                            currentNode.AppendChild(old);
                        }

                        currentNode = currentNode.AppendChild(string.Empty);
                    }
                    else
                    {
                        currentNode.Text += text[i].ToString();
                    }
                }
                else
                {
                    currentNode.Text += text[i].ToString();
                }
            }

            return(syntaxTree);
        }
Пример #3
0
 public SyntaxTreeNode AppendChild(SyntaxTreeNode node)
 {
     _children.Add(node);
     node.Parent = this;
     return(node);
 }