コード例 #1
0
        public CanParseGenerics()
        {
            var lexer = LexerBuilder.Build()
                        .Match("nr", @"\d+", match => new Number {
                Value = int.Parse(match.Value)
            })
                        .Match("txt", @".+", match => new Text {
                Value = match.Value
            });

            _subject = ParserBuilder.Build(lexer)
                       .Match <Number, Text>((nr, t) => new KeyValuePair <Number, Text>(nr, t), MatchOptions.AtLeastOne)
                       .Create <KeyValuePair <Number, Text> >();
        }
コード例 #2
0
        public void EmptyCodeReturnEmptyRootElement()
        {
            var code = string.Empty;

            var parserBuilder = new ParserBuilder();

            parserBuilder.Build(code);

            var finder  = parserBuilder.GetParseElementFinder();
            var classes = finder.FindClasses();
            var isEmpty = !classes.Any();

            Assert.IsTrue(isEmpty);
        }
コード例 #3
0
        public void ShouldDetectAllClassesInCode()
        {
            string[] classNames = { "class1", "class2" };
            var      code       = PythonCodeGenerator.GenerateRoot(classNames);

            var parserBuilder = new ParserBuilder();

            parserBuilder.Build(code);

            var finder  = parserBuilder.GetParseElementFinder();
            var classes = finder.FindClasses();
            var actualDetectedClasses = classes.Count();

            Assert.AreEqual(classNames.Length, actualDetectedClasses);
        }
コード例 #4
0
        public CalculatorTest()
        {
            var lexer = LexerBuilder.Build()
                        .Match <PlusToken>("plus", @"\+")
                        .Match <MinusToken>("minus", @"-")
                        .Match <MultiToken>("multi", @"\*")
                        .Match <DivideToken>("divide", @"\/")
                        .Match("number", @"\b[0-9]+(\.[0-9]*)?\b", match => decimal.Parse(match.Value));

            _subject = ParserBuilder.Build(lexer)
                       .Match <decimal, DivideToken, decimal>((o1, by, o2) => o1 / o2)
                       .Match <decimal, MultiToken, decimal>((o1, times, o2) => o1 * o2)
                       .Match <decimal, MinusToken, decimal>((o1, minus, o2) => o1 - o2)
                       .Match <decimal, PlusToken, decimal>((o1, plus, o2) => o1 + o2)
                       .Create <decimal>();
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: RamanBut-Husaim/MA.DSP
        public static void Main(string[] args)
        {
            FluentCommandLineParser <Arguments> parser = ParserBuilder.Build();
            var parserResult = parser.Parse(args);

            if (!parserResult.HasErrors && !parserResult.EmptyArgs)
            {
                Arguments argument  = parser.Object;
                var       container = new ServiceContainer();
                container.RegisterFrom <CompositionRoot>();
                using (var scope = container.BeginScope())
                {
                    IFileParserManager fileParserManager = container.GetInstance <IFileParserManager>();
                    FileParserResult   parseResult       = fileParserManager.ParseFileAsync(argument.Path).Result;
                }
            }
        }
コード例 #6
0
        /// <summary>
        /// Executes the specified command with given parameters
        /// </summary>
        /// <param name="args">Command line arguments</param>
        /// <returns>If target method returns int, this method will return that value. Else,
        /// it will return 0 in case of success and 1 in case of unhandled exception</returns>
        public int Run(params string[] args)
        {
            try
            {
                AppCreator appCreator = new AppCreator(_settings);

                CommandLineApplication app = appCreator.CreateApplication(typeof(T), DependencyResolver);

                return(app.Execute(_parserBuilder.Build(), args));
            }
            catch (AppRunnerException e)
            {
                _settings.Error.WriteLine(e.Message + "\n");
#if DEBUG
                _settings.Error.WriteLine(e.StackTrace);
#endif
                return(1);
            }
            catch (CommandParsingException e)
            {
                var optionHelp = e.Command.OptionHelp;
                if (optionHelp != null)
                {
                    _settings.Out.WriteLine(
                        $"Specify --{optionHelp.LongName} for a list of available options and commands.");
                }

                _settings.Error.WriteLine(e.Message + "\n");
                e.Command.ShowHelp();

#if DEBUG
                _settings.Error.WriteLine(e.StackTrace);
#endif

                return(1);
            }
            catch (ValueParsingException e)
            {
                _settings.Error.WriteLine(e.Message + "\n");
                return(2);
            }
            catch (AggregateException e) when(e.InnerExceptions.Any(x => x.GetBaseException() is AppRunnerException) ||
                                              e.InnerExceptions.Any(x =>
                                                                    x.GetBaseException() is CommandParsingException))
            {
                foreach (var innerException in e.InnerExceptions)
                {
                    _settings.Error.WriteLine(innerException.GetBaseException().Message + "\n");
#if DEBUG
                    _settings.Error.WriteLine(innerException.GetBaseException().StackTrace);
                    if (e.InnerExceptions.Count > 1)
                    {
                        _settings.Error.WriteLine("-----------------------------------------------------------------");
                    }
#endif
                }

                return(1);
            }
            catch (AggregateException e) when(e.InnerExceptions.Any(x =>
                                                                    x.GetBaseException() is ArgumentValidationException))

            {
                ArgumentValidationException validationException =
                    (ArgumentValidationException)e.InnerExceptions.FirstOrDefault(x =>
                                                                                  x.GetBaseException() is ArgumentValidationException);

                foreach (var failure in validationException.ValidationResult.Errors)
                {
                    _settings.Out.WriteLine(failure.ErrorMessage);
                }

                return(2);
            }
            catch (AggregateException e) when(e.InnerExceptions.Any(x => x.GetBaseException() is ValueParsingException)
                                              )

            {
                ValueParsingException valueParsingException =
                    (ValueParsingException)e.InnerExceptions.FirstOrDefault(x =>
                                                                            x.GetBaseException() is ValueParsingException);

                _settings.Error.WriteLine(valueParsingException.Message + "\n");

                return(2);
            }
            catch (AggregateException e) when(e.InnerExceptions.Any(x => x is TargetInvocationException))
            {
                TargetInvocationException ex =
                    (TargetInvocationException)e.InnerExceptions.SingleOrDefault(x => x is TargetInvocationException);

                ExceptionDispatchInfo.Capture(ex.InnerException ?? ex).Throw();
                return(1); // this will never be called
            }
            catch (AggregateException e)
            {
                foreach (Exception innerException in e.InnerExceptions)
                {
                    ExceptionDispatchInfo.Capture(innerException).Throw();
                }

                return(1); // this will never be called if there is any inner exception
            }
            catch (ArgumentValidationException ex)
            {
                foreach (var failure in ex.ValidationResult.Errors)
                {
                    _settings.Out.WriteLine(failure.ErrorMessage);
                }

                return(2);
            }
            catch (TargetInvocationException ex)
            {
                ExceptionDispatchInfo.Capture(ex.InnerException ?? ex).Throw();
                return(1); // this will never be called
            }
        }