Beispiel #1
0
 public static TokenTree Parse(TextReader textReader, string defaultDirectory = null, string selector = null, bool ignoreErrors = false)
 {
     Parser parser = new Parser();
     Reader reader = new Reader(textReader) {Options = {DefaultDirectory = defaultDirectory}};
     if (!string.IsNullOrWhiteSpace(selector))
         reader.Options.Selector = selector;
     foreach (Line line in reader)
         parser.AddLine(line, ignoreErrors);
     return parser.ParsedTree;
 }
Beispiel #2
0
 public void SubstituteString(int length, string str)
 {
     Parser parser = new Parser();
     ICollection<ISentenceItem> newIitems = parser.ParseIntoSentenceItems(str, Line);
     ICollection<ISentenceItem> itemsToReplace = SentenceItems.ToList().FindAll(x => x.Length == length);
     foreach (var item in itemsToReplace)
     {
         int insertIndex = SentenceItems.ToList().IndexOf(SentenceItems.ToList().Find(x => x.Value == item.Value));
         SentenceItems = SentenceItems.Take(insertIndex).Concat(newIitems).Concat(SentenceItems.Skip(insertIndex + 1)).ToList();
     }
 }
        private void Execute(object command)
        {
            string input = command.ToString();

            try
            {
                if (input.StartsWith("#"))
                {
                    string text = input.Substring(1).Trim();
                    if (text == "print" || text == "p")
                    {
                        PrintStructure();
                    }
                    else if (text.StartsWith("print "))
                    {
                        PrintExpression(text.Substring(5).Trim());
                    }
                    else if (text.StartsWith("p "))
                    {
                        PrintExpression(text.Substring(1).Trim());
                    }
                    else if (text.StartsWith("delete "))
                    {
                        DeleteItem(text.Substring(6).Trim());
                    }
                    else if (text.StartsWith("d "))
                    {
                        DeleteItem(text.Substring(1).Trim());
                    }
                    else if (text.StartsWith("deleteall "))
                    {
                        DeleteItems(text.Substring(9).Trim());
                    }
                    else if (text.StartsWith("da "))
                    {
                        DeleteItems(text.Substring(2).Trim());
                    }
                    else if (text.StartsWith("insert "))
                    {
                        InsertItem(text.Substring(6).Trim());
                    }
                    else if (text.StartsWith("i "))
                    {
                        InsertItem(text.Substring(1).Trim());
                    }
                    else if (text.StartsWith("save "))
                    {
                        SaveStructure(text.Substring(4).Trim());
                    }
                    else if (text.StartsWith("s "))
                    {
                        SaveStructure(text.Substring(1).Trim());
                    }
                    else if (text.StartsWith("read "))
                    {
                        ReadStructure(text.Substring(4).Trim());
                    }
                    else if (text.StartsWith("r "))
                    {
                        ReadStructure(text.Substring(1).Trim());
                    }
                    else if (text == "exit" || text == "x")
                    {
                        Application.Current.Shutdown();
                    }
                    else if (text == "clear" || text == "c")
                    {
                        _parser = new Parser();
                    }
                    else if (text == "clearhistory" || text == "ch")
                    {
                        ClearHistory();
                    }
                    else if (text == "help" || text == "h")
                    {
                        ShowHelp();
                    }
                    else
                    {
                        int count;
                        if (int.TryParse(text, out count) && count <= _historyList.Count && count > 0)
                        {
                            Execute(_historyList[count - 1]);
                            return;
                        }
                        UpdateOutput($"Invalid command: {text}");
                    }
                }
                else
                {
                    TokenTree tree = _parser.AddLine(new Line(input));
                    UpdateOutput($"{tree.Key}: {tree.Value.Evaluate(new TokenTreeList(Tree), false)}");
                }
                Input = "";
                UpdateHistory(input);
            }
            catch (Exception ex)
            {
                UpdateOutput(ex.Message);
            }
            CommandEntered = true;
        }
Beispiel #4
0
        private void UpdateTextColor(FilterName[] filterNames)
        {
            if(parser == null) {
                parser = new Parser(new ExpressionFilter());
            }
            else {
                parser.FieldFilters.Clear();
            }

            int position = ExpressionText.SelectionStart;
            ExpressionText.HideSelection = true;
            ExpressionText.SuspendLayout();

            // add filter names
            Dictionary<string, FilterName> filterDictionary = new Dictionary<string, FilterName>();

            for(int i = 0; i < filterNames.Length; i++) {
                parser.FieldFilters.Add(filterNames[i].Name);

                if(filterDictionary.ContainsKey(filterNames[i].Name) == false) {
                    filterDictionary.Add(filterNames[i].Name, filterNames[i]);
                }
            }

            parser.Text = ExpressionText.Text + "  ";
            parser.InitParsing();
            TextBlock? block = parser.GetNextBlock();

            StringBuilder builder = new StringBuilder();
            StringBuilder textBuilder = new StringBuilder(ExpressionText.Text);
            int diff = 0;
            builder.Append("{\\rtf1\\ansi\\deff0");

            // color table
            //
            //           1      2    3       4         5
            // colors: black, blue, red, dark violet, gray
            builder.Append("{\\colortbl;\\red0\\green0\\blue0;\\red0\\green0\\blue255;\\red255\\green0\\blue0;\\red148\\green0\\blue211;\\red128\\green128\\blue128;}");

            // parse the text
            while(block.HasValue) {
                TextBlock b = block.Value;

                if(b.Type == TextBlockType.Keyword) {
                    /*ExpressionText.SelectionStart = b.StartIndex;
                    ExpressionText.SelectionLength = b.EndIndex - b.StartIndex + 1;
                    ExpressionText.SelectionColor = Color.DarkViolet;*/

                    if(b.EndIndex - b.StartIndex > 0) {
                        textBuilder.Replace(b.Text, "\\highlight0\\cf4" + b.Text, diff + b.StartIndex, b.EndIndex - b.StartIndex);
                        diff += 15;
                    }
                }
                else if(b.Type == TextBlockType.Field) {
                    int color = 5;
                    /*ExpressionText.SelectionStart = b.StartIndex;
                    ExpressionText.SelectionLength = b.EndIndex - b.StartIndex;
                    */
                    // set color
                    if(filterDictionary.ContainsKey(b.Text) == false ||
                      (filterDictionary.ContainsKey(b.Text) && filterDictionary[b.Text].Enabled == false)) {

                        //ExpressionText.SelectionColor = Color.Gray;
                        color = 5;
                    }
                    else {
                        //ExpressionText.SelectionColor = Color.Blue;
                        color = 2;
                    }

                    // append
                    if(b.EndIndex - b.StartIndex > 0) {
                        textBuilder.Replace(b.Text, "\\highlight0\\cf" + color.ToString() + b.Text, diff + b.StartIndex, b.EndIndex - b.StartIndex);
                        diff += 15;
                    }
                }
                else if(ContainsParantesis(b.Text) == false &&
                   ContainsName(b.Text, filterNames) == false) {
                    /*// mark as error
                    ExpressionText.SelectionStart = b.StartIndex;
                    ExpressionText.SelectionLength = b.EndIndex - b.StartIndex;
                    ExpressionText.SelectionBackColor = Color.LightCoral;*/

                    if(b.EndIndex - b.StartIndex > 0) {
                        textBuilder.Replace(b.Text, "\\highlight0\\cf3" + b.Text, diff + b.StartIndex, b.EndIndex - b.StartIndex);
                        diff += 15;
                    }
                }
                else {
                    // unknown
                    if(b.EndIndex - b.StartIndex > 0) {
                        textBuilder.Replace(b.Text, "\\highlight0\\cf1" + b.Text, diff + b.StartIndex, b.EndIndex - b.StartIndex);
                        diff += 15;
                    }
                }

                block = parser.GetNextBlock();
            }

            updatingColor = true;
            builder.Append(textBuilder.ToString());
            ExpressionText.Rtf = builder.ToString();
            ExpressionText.SelectionStart = position;
            ExpressionText.SelectionLength = 0;
            updatingColor = false;
        }