Exemplo n.º 1
0
        private void CalculateClick(object sender, RoutedEventArgs e)
        {
            try
            {
                if (InputBox.Text == string.Empty || PatternBox.Text == string.Empty)
                {
                    SetStatusLabelMessage("Следует заполнить все поля формы", StatusLabelColor.Red);
                }

                var expression = new RoseMarkExpression(PatternBox.Text);
                var encodeDataHolder = new Dictionary<string, object>();
                var res = new Dictionary<string, object>();
                var stopwatch = new Stopwatch();
                stopwatch.Start();;
                var compileInfo = new CompileInfo
                                  {
                                      Input = InputBox.Text,
                                      PrecompiledDataHolder = res,
                                      ShouldCheckLenght = true,
                                      EncodeDataHolder = encodeDataHolder
                                  };
                var result = expression.Compile(compileInfo);

                if (result.IsCompiled)
                {
                    SetStatusLabelMessage("Cоответствие", StatusLabelColor.Green);
                }
                else
                {
                    SetStatusLabelMessage("Несоответствие", StatusLabelColor.Red);
                }

                    var builder = new StringBuilder();

                    builder.AppendLine("Затрачено времени: " + stopwatch.Elapsed);

                    var token = ExpressionToken.Parse(PatternBox.Text);
                    builder.AppendLine("Количество элементов токена: " + token.Elements.Count);

                foreach (var el in token.Elements)
                {
                    builder.AppendLine("Элемент '" + el.Text + "': Тип:" + el.GetType());
                }
                    
                    builder.AppendLine("Количество данных кодировщика: " + encodeDataHolder.Count);
                    foreach (var o in encodeDataHolder)
                    {
                        builder.AppendLine(o.Key + ": " + o.Value);
                    }
                    ResultOutput.Text = builder.ToString();


            }
            catch (Exception exception)
            {
                SetStatusLabelMessage("Ошибка во время выполнения", StatusLabelColor.Red);
            }

        }
        public CompileResult Compile(CompileInfo compileInfo)
        {
            try
            {
                var token = ExpressionToken.Parse(ExpressionString);
                var res = new CompileResult();
                var startIndex = 0;
                var provider = new FunctionExecutionProvider();

                for (var i = 0; i < token.Elements.Count; i++)
                {
                    if (token.Elements[i] is ExpressionTextElement)
                    {
                        token.Elements[i] = new ExpressionFunctionElement("[text(\"" + token.Elements[i].Text + "\")]");
                    }
                }

                var tokenIndex = 0;

                foreach (var e in token.Elements)
                {
                    var f = e as ExpressionFunctionElement;
                    foreach (var functionExecutor in executors)
                    {

                        if (functionExecutor.FunctionName == f.Function.FunctionName)
                        {
                            var toCheck = compileInfo.Input.Substring(startIndex);

                            var info = new FunctionExecuteArgs(f.Function.FunctionArgs, f.Function.FunctionAttributes,
                                provider, token, tokenIndex, toCheck, compileInfo.PrecompiledDataHolder);

                            FunctionExecuteResult result;

                            try
                            {
                                result = functionExecutor.Execute(info);
                            }
                            catch 
                            {
                                return new CompileResult
                                       {
                                           IsCompiled = false,
                                           Lenght = 0
                                       };
                            }

                            compileInfo.EncodeDataHolder.Clear();

                            foreach (var kv in provider.ComponentsData)
                            {
                                compileInfo.EncodeDataHolder.Add(kv.Key, kv.Value);
                            }

                            if (result.IsMatch == false)
                                return new CompileResult
                                       {
                                           IsCompiled = false
                                       };
                            startIndex += result.SkipLenght;

                        }
                    }
                    tokenIndex++;
                }

                if (startIndex != compileInfo.Input.Length && compileInfo.ShouldCheckLenght)
                {
                    Console.WriteLine(startIndex + " => " + compileInfo.Input.Length);
                    return new CompileResult
                           {
                               IsCompiled = false,
                               Lenght = 0
                           };
                }

                return new CompileResult
                       {
                           IsCompiled = true,
                           Lenght = startIndex
                       };
            }
            catch (Exception re)
            {
                Console.WriteLine(re);
                return new CompileResult()
                       {
                           IsCompiled = false
                       };
            }
        }