Esempio n. 1
0
        public void GrammarGeneratedCodeCorrectMapping(Runtime runtime)
        {
            Assert.Ignore("Not ready");

            var grammarText =
                @"grammar test;
                  rootRule
                      : {a==0}? tokensOrRules* EOF {a++;}
                      ;
                  tokensOrRules
                      : {a==0}? TOKEN+ {a++;}
                      ;
                  TOKEN: {b==0}? [a-z]+ {b++;};
                  DIGIT: {b==0}? [0-9]+ {b++;};";
            var grammar  = GrammarFactory.CreateDefaultCombinedAndFill(grammarText, "test", ".");
            var workflow = new Workflow(grammar);

            workflow.Runtime = runtime;

            var state = workflow.Process();

            Assert.AreEqual(WorkflowStage.ParserCompiled, state.Stage, state.Exception?.ToString());

            ParserCompiledState parserGeneratedState = state as ParserCompiledState;
            var errors = parserGeneratedState.Errors;

            Assert.AreEqual(8, errors.Count);
            Assert.AreEqual(2, errors.Where(e => e.TextSpan.GetLineColumn().BeginLine == 3).Count());
            Assert.AreEqual(2, errors.Where(e => e.TextSpan.GetLineColumn().BeginLine == 6).Count());
            Assert.AreEqual(2, errors.Where(e => e.TextSpan.GetLineColumn().BeginLine == 8).Count());
            Assert.AreEqual(2, errors.Where(e => e.TextSpan.GetLineColumn().BeginLine == 9).Count());
        }
Esempio n. 2
0
        public void ParserCompiledStageErrors(Runtime runtime)
        {
            var grammarText =
                @"grammar Test;
                start:  DIGIT+ {i^;};
                CHAR:   [a-z]+;
                DIGIT:  [0-9]+;
                WS:     [ \r\n\t]+ -> skip;";
            var grammar  = GrammarFactory.CreateDefaultCombinedAndFill(grammarText, "Test", ".");
            var workflow = new Workflow(grammar);

            workflow.Runtime = runtime;

            var state = workflow.Process();

            Assert.AreEqual(WorkflowStage.ParserCompiled, state.Stage, state.Exception?.ToString());

            ParserCompiledState parserGeneratedState = state as ParserCompiledState;

            Assert.GreaterOrEqual(parserGeneratedState.Errors.Count, 1);
            //Assert.AreEqual(runtime == Runtime.Go ? 1 : 2, parserGeneratedState.Errors[0].TextSpan.GetLineColumn().BeginLine);
        }
Esempio n. 3
0
        public ParserCompiledState Compile(ParserGeneratedState state,
                                           CancellationToken cancellationToken = default)
        {
            _grammar = state.GrammarCheckedState.InputState.Grammar;
            Runtime runtime = state.Runtime;

            _result = new ParserCompiledState(state);

            _currentRuntimeInfo = RuntimeInfo.InitOrGetRuntimeInfo(runtime);

            Processor processor = null;

            try
            {
                string runtimeSource      = runtime.GetGeneralRuntimeName();
                string runtimeDir         = Path.Combine(RuntimesDirName, runtimeSource);
                string runtimeLibraryPath = RuntimeLibrary ?? Path.Combine(runtimeDir, _currentRuntimeInfo.RuntimeLibrary);
                string workingDirectory   = Path.Combine(ParserGenerator.HelperDirectoryName, _grammar.Name, runtime.ToString());

                var generatedFiles = new List <string>();
                _grammarCodeMapping = new Dictionary <string, List <TextSpanMapping> >();

                string generatedGrammarName =
                    runtime != Runtime.Go ? _grammar.Name : _grammar.Name.ToLowerInvariant();

                if (_grammar.Type != GrammarType.Lexer)
                {
                    GetGeneratedFileNames(state.GrammarCheckedState, generatedGrammarName, workingDirectory, generatedFiles, false);
                }

                GetGeneratedFileNames(state.GrammarCheckedState, generatedGrammarName, workingDirectory, generatedFiles, true);

                CopyCompiledSources(runtime, workingDirectory);

                if (_grammar.Type != GrammarType.Lexer)
                {
                    if (state.IncludeListener)
                    {
                        GetGeneratedListenerOrVisitorFiles(generatedGrammarName, workingDirectory, generatedFiles, false);
                    }
                    if (state.IncludeVisitor)
                    {
                        GetGeneratedListenerOrVisitorFiles(generatedGrammarName, workingDirectory, generatedFiles, true);
                    }
                }

                string arguments = "";

                switch (runtime)
                {
                case Runtime.CSharpOptimized:
                case Runtime.CSharpStandard:
                    arguments = PrepareCSharpFiles(workingDirectory, runtimeDir);
                    break;

                case Runtime.Java:
                    arguments = PrepareJavaFiles(generatedFiles, runtimeDir, workingDirectory, runtimeLibraryPath);
                    break;

                case Runtime.Python2:
                case Runtime.Python3:
                    arguments = PreparePythonFiles(generatedFiles, runtimeDir, workingDirectory);
                    break;

                case Runtime.JavaScript:
                    arguments = PrepareJavaScriptFiles(generatedFiles, workingDirectory, runtimeDir);
                    break;

                case Runtime.Go:
                    arguments = PrepareGoFiles(generatedFiles, runtimeDir, workingDirectory);
                    break;

                case Runtime.Php:
                    arguments = PreparePhpFiles(generatedFiles, runtimeDir, workingDirectory);
                    break;
                }

                PrepareParserCode(workingDirectory, runtimeDir);

                _buffer            = new List <string>();
                _processedMessages = new HashSet <string>();

                _result.Command               = _currentRuntimeInfo.RuntimeToolName + " " + arguments;
                processor                     = new Processor(_currentRuntimeInfo.RuntimeToolName, arguments, workingDirectory);
                processor.CancellationToken   = cancellationToken;
                processor.ErrorDataReceived  += ParserCompilation_ErrorDataReceived;
                processor.OutputDataReceived += ParserCompilation_OutputDataReceived;

                processor.Start();

                if (_buffer.Count > 0)
                {
                    if (runtime.IsPythonRuntime())
                    {
                        AddPythonError();
                    }
                    else if (runtime == Runtime.JavaScript)
                    {
                        AddJavaScriptError();
                    }
                }

                cancellationToken.ThrowIfCancellationRequested();
            }
            catch (Exception ex)
            {
                _result.Exception = ex;
                if (!(ex is OperationCanceledException))
                {
                    AddError(new ParsingError(ex, WorkflowStage.ParserCompiled));
                }
            }
            finally
            {
                processor?.Dispose();
            }

            return(_result);
        }