Exemple #1
0
        public ParserCompiliedState Compile(ParserGeneratedState state,
                                            CancellationToken cancellationToken = default)
        {
            _grammar = state.GrammarCheckedState.InputState.Grammar;
            Runtime runtime = state.Runtime;

            _result = new ParserCompiliedState(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;
                }

                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.ParserCompilied));
                }
            }
            finally
            {
                processor?.Dispose();
            }

            return(_result);
        }
Exemple #2
0
 public TextParsedState(ParserCompiliedState parserCompiliedState, CodeSource text)
 {
     ParserCompiliedState =
         parserCompiliedState ?? throw new ArgumentNullException(nameof(parserCompiliedState));
     Text = text ?? throw new ArgumentNullException(nameof(text));
 }