Implements loading of tutorials.
Beispiel #1
0
        public void Compile_ForAllFoundTutorials_Succeeds(Tutorial tutorial)
        {
            var grammar = new PegParser().Parse(tutorial.GrammarText);

            var result = PegCompiler.Compile(grammar);

            Assert.That(result.Errors, Is.Empty);
        }
Beispiel #2
0
        public void Parse_ForAllFoundTutorials_Succeeds(Tutorial tutorial)
        {
            var grammar = new PegParser().Parse(tutorial.GrammarText);
            var compiled = PegCompiler.Compile(grammar);
            var parser = CodeCompiler.Compile<object>(compiled.Code);

            var result = parser.Parse(tutorial.TestText);

            Assert.That(result, Is.Not.Null);
        }
Beispiel #3
0
        public AppViewModel()
        {
            this.Tutorials = Tutorial.FindAll();

            var grammarTextChanges = this.WhenAny(x => x.GrammarText, grammarText => grammarText.GetValue());
            var testTextChanges    = this.WhenAny(x => x.TestText, testText => testText.GetValue());

            var pegParser   = new Pipeline.PegParser(grammarTextChanges);
            var pegCompiler = new Pipeline.PegCompiler(pegParser.Grammars);
            var csCompiler  = new Pipeline.CsCompiler(pegCompiler.Codes, pegParser.Grammars);

            this.RuleSelector = new Pipeline.RuleSelector(csCompiler.Parsers, pegParser.Grammars);
            var testParser  = new Pipeline.TestParser(this.RuleSelector.SelectedEntrypoints, testTextChanges);
            var testResults = testParser.Results.Select(r =>
            {
                return(r is string s ? s : JsonConvert.SerializeObject(r, Formatting.Indented));
            });

            var comparer  = new CompilerErrorListEqualityComparer();
            var allErrors = Observable.CombineLatest(
                pegParser.Errors.DistinctUntilChanged(comparer),
                pegCompiler.Errors.DistinctUntilChanged(comparer),
                csCompiler.Errors.DistinctUntilChanged(comparer),
                testParser.Errors.DistinctUntilChanged(comparer))
                            .Select(errorLists => errorLists.SelectMany(e => e));

            var grammarNameChanges = this.WhenAny(x => x.GrammarFileName, grammarFileName => grammarFileName.GetValue());
            var testNameChanges    = this.WhenAny(x => x.TestFileName, testFileName => testFileName.GetValue());

            var compileErrors = Observable.CombineLatest(
                allErrors,
                grammarNameChanges,
                testNameChanges,
                (errors, grammarName, testName) =>
            {
                return(errors.Select(e =>
                {
                    switch (e.FileName)
                    {
                    case Pipeline.PegParser.SentinelFileName:
                        return new CompilerError(grammarName, e.Line, e.Column, e.ErrorNumber, e.ErrorText)
                        {
                            IsWarning = e.IsWarning
                        };

                    case Pipeline.CsCompiler.SentinelFileName:
                        return new CompilerError(grammarName + ".g.cs", e.Line, e.Column, e.ErrorNumber, e.ErrorText)
                        {
                            IsWarning = e.IsWarning
                        };

                    case Pipeline.TestParser.SentinelFileName:
                        return new CompilerError(testName, e.Line, e.Column, e.ErrorNumber, e.ErrorText)
                        {
                            IsWarning = e.IsWarning
                        };

                    default:
                        return e;
                    }
                }).ToList());
            });

            this.pipeline = new CompositeDisposable(
                this.RuleSelector,
                testResults.BindTo(this, x => x.TestResults),
                compileErrors.BindTo(this, x => x.CompileErrors));

            this.Save = ReactiveCommand.CreateAsyncTask(grammarNameChanges.Select(n => n != "Untitled.peg"), async _ =>
            {
                Directory.CreateDirectory(Path.GetDirectoryName(this.grammarFileName));
                await FileUtilities.WriteAllTextAsync(this.grammarFileName, this.grammarText);
                this.GrammarChanged = false;
            });

            this.SaveAs = ReactiveCommand.CreateAsyncTask(async f =>
            {
                var fileName = (string)f;
                Directory.CreateDirectory(Path.GetDirectoryName(fileName));
                await FileUtilities.WriteAllTextAsync(fileName, this.grammarText);
                this.GrammarFileName = fileName;
                this.GrammarChanged  = false;
                return(true);
            });

            this.Load = ReactiveCommand.CreateAsyncTask(async f =>
            {
                var fileName         = (string)f;
                this.GrammarText     = await FileUtilities.ReadAllTextAsync(fileName);
                this.GrammarFileName = fileName;
                this.GrammarChanged  = false;
                this.TestText        = string.Empty;
                return(true);
            });

            this.LoadTutorial = ReactiveCommand.CreateAsyncTask(async t =>
            {
                var tutorial         = (Tutorial)t;
                this.GrammarText     = File.Exists(tutorial.FileName) ? await FileUtilities.ReadAllTextAsync(tutorial.FileName) : tutorial.GrammarText;
                this.GrammarFileName = tutorial.FileName;
                this.GrammarChanged  = false;
                this.TestText        = tutorial.TestText;
                return(true);
            });
        }