コード例 #1
0
        private void CopyCompiledSources(Runtime runtime, string workingDirectory)
        {
            RuntimeInfo runtimeInfo = RuntimeInfo.InitOrGetRuntimeInfo(runtime);
            string      extension   = runtimeInfo.Extensions[0];

            foreach (string fileName in _grammar.Files)
            {
                if (Path.GetExtension(fileName).Equals("." + extension, StringComparison.OrdinalIgnoreCase))
                {
                    string sourceFileName = Path.Combine(_grammar.Directory, fileName);

                    string shortFileName = Path.GetFileName(fileName);

                    if ((runtimeInfo.Runtime == Runtime.Java || runtimeInfo.Runtime == Runtime.Go) &&
                        !string.IsNullOrWhiteSpace(_result.ParserGeneratedState.PackageName))
                    {
                        shortFileName = Path.Combine(_result.ParserGeneratedState.PackageName, shortFileName);
                    }

                    string destFileName = Path.Combine(workingDirectory, shortFileName);

                    File.Copy(sourceFileName, destFileName, true);
                }
            }
        }
コード例 #2
0
        public void RuntimeInitialized()
        {
            var runtimes = (Runtime[])Enum.GetValues(typeof(Runtime));

            foreach (Runtime runtime in runtimes)
            {
                string message;
                if (runtime != Runtime.CPlusPlus && runtime != Runtime.Swift)
                {
                    RuntimeInfo runtimeInfo = RuntimeInfo.InitOrGetRuntimeInfo(runtime);
                    Assert.IsFalse(string.IsNullOrEmpty(runtimeInfo.Version), $"Failed to initialize {runtime} runtime");
                    message = $"{runtime}: {runtimeInfo.RuntimeToolName} {runtimeInfo.Version}";
                }
                else
                {
                    message = $"{runtime} is not supported for now";
                }
                Console.WriteLine(message);
            }
        }
コード例 #3
0
        private void Generate(Grammar grammar, GrammarCheckedState state, CancellationToken cancellationToken)
        {
            Processor processor = null;

            try
            {
                string runtimeDirectoryName = Path.Combine(HelperDirectoryName, grammar.Name, Runtime.ToString());

                if ((Runtime == Runtime.Java || Runtime == Runtime.Go) && !string.IsNullOrWhiteSpace(PackageName))
                {
                    runtimeDirectoryName = Path.Combine(runtimeDirectoryName, PackageName);
                }

                if (Directory.Exists(runtimeDirectoryName))
                {
                    Directory.Delete(runtimeDirectoryName, true);
                }

                Directory.CreateDirectory(runtimeDirectoryName);

                cancellationToken.ThrowIfCancellationRequested();

                RuntimeInfo runtimeInfo = RuntimeInfo.InitOrGetRuntimeInfo(Runtime);

                var jarGenerator = GeneratorTool ?? Path.Combine("Generators", runtimeInfo.JarGenerator);
                foreach (string grammarFileName in state.InputState.Grammar.Files)
                {
                    string extension = Path.GetExtension(grammarFileName);
                    if (extension != Grammar.AntlrDotExt)
                    {
                        continue;
                    }

                    _currentGrammarSource = state.GrammarFilesData[grammarFileName];

                    var arguments =
                        $@"-jar ""{jarGenerator}"" ""{Path.Combine(grammar.Directory, grammarFileName)}"" " +
                        $@"-o ""{runtimeDirectoryName}"" " +
                        $"-Dlanguage={runtimeInfo.DLanguage} " +
                        $"{(GenerateVisitor ? "-visitor" : "-no-visitor")} " +
                        $"{(GenerateListener ? "-listener" : "-no-listener")}";

                    if (!string.IsNullOrWhiteSpace(PackageName))
                    {
                        arguments += " -package " + PackageName;
                    }
                    else if (Runtime == Runtime.Go)
                    {
                        arguments += " -package main";
                    }

                    if (grammarFileName.Contains(Grammar.LexerPostfix) && state.LexerSuperClass != null)
                    {
                        arguments += " -DsuperClass=" + state.LexerSuperClass;
                    }

                    if (grammarFileName.Contains(Grammar.ParserPostfix) && state.ParserSuperClass != null)
                    {
                        arguments += " -DsuperClass=" + state.ParserSuperClass;
                    }

                    _result.Command               = "java " + arguments;
                    processor                     = new Processor("java", arguments, ".");
                    processor.CancellationToken   = cancellationToken;
                    processor.ErrorDataReceived  += ParserGeneration_ErrorDataReceived;
                    processor.OutputDataReceived += ParserGeneration_OutputDataReceived;

                    processor.Start();

                    cancellationToken.ThrowIfCancellationRequested();
                }
            }
            catch (Exception ex)
            {
                _result.Exception = ex;
                if (!(ex is OperationCanceledException))
                {
                    ErrorEvent?.Invoke(this, new ParsingError(ex, WorkflowStage.ParserGenerated));
                }
            }
            finally
            {
                processor?.Dispose();
            }
        }
コード例 #4
0
ファイル: MainWindowViewModel.cs プロジェクト: ag-csharp/DAGE
        public MainWindowViewModel(Window window)
        {
            _window               = window;
            _grammarTextBox       = _window.Find <TextEditor>("GrammarTextBox");
            _textTextBox          = _window.Find <TextEditor>("TextTextBox");
            _grammarErrorsListBox = _window.Find <ListBox>("GrammarErrorsListBox");
            _textErrorsListBox    = _window.Find <ListBox>("TextErrorsListBox");
            _parseTreeTextBox     = _window.Find <TextEditor>("ParseTreeTextBox");
            _tokensTextBox        = _window.Find <TextEditor>("TokensTextBox");

            _grammarTextBox.SetupHightlighting(".g4");

            _settings = Settings.Load();

            _window.WindowState = _settings.WindowState;
            if (_settings.Width > 0)
            {
                _window.Width = _settings.Width;
            }
            if (_settings.Height > 0)
            {
                _window.Height = _settings.Height;
            }
            if (_settings.Left != -1 && _settings.Top != -1)
            {
                _window.Position = new PixelPoint(_settings.Left, _settings.Top);
            }

            Grammar grammar = null;

            bool   openDefaultGrammar = false;
            string packageName        = null;
            string root = null;

            if (string.IsNullOrEmpty(_settings.GrammarFileOrDirectory))
            {
                openDefaultGrammar = true;
            }
            else
            {
                try
                {
                    grammar = GrammarFactory.Open(_settings.GrammarFileOrDirectory, out packageName, out root);
                }
                catch (Exception ex)
                {
                    ShowOpenFileErrorMessage(_settings.GrammarFileOrDirectory, ex.Message);

                    _settings.OpenedGrammarFile = "";
                    openDefaultGrammar          = true;
                }
            }

            if (openDefaultGrammar)
            {
                grammar = GrammarFactory.CreateDefault();
                GrammarFactory.FillGrammarFiles(grammar, Settings.Directory, false);
                _settings.GrammarFileOrDirectory = grammar.Directory;
                _settings.Save();
            }

            _workflow       = new Workflow(grammar);
            SelectedRuntime = RuntimeInfo.InitOrGetRuntimeInfo(_workflow.Runtime);
            PackageName     = packageName;
            Root            = root;

            InitFiles();
            if (string.IsNullOrEmpty(_settings.OpenedGrammarFile) || !grammar.Files.Contains(_settings.OpenedGrammarFile))
            {
                OpenedGrammarFile = GrammarFiles.FirstOrDefault();
            }
            else
            {
                OpenedGrammarFile = _settings.OpenedGrammarFile;
            }

            if (string.IsNullOrEmpty(_settings.OpenedTextFile))
            {
                OpenedTextFile = TextFiles.Count > 0 ? TextFiles.First() : null;
            }
            else
            {
                OpenedTextFile = new FileName(_settings.OpenedTextFile);
            }

            SetupWindowSubscriptions();
            SetupWorkflowSubscriptions();
            SetupTextBoxSubscriptions();
            SetupCommandSubscriptions();

            AutoProcessing = _settings.Autoprocessing;
        }
コード例 #5
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);
        }
コード例 #6
0
        private void PrepareParserCode(string workingDirectory, string runtimeDir)
        {
            string  templateFile = Path.Combine(workingDirectory, _currentRuntimeInfo.MainFile);
            Runtime runtime      = _currentRuntimeInfo.Runtime;

            string code        = File.ReadAllText(Path.Combine(runtimeDir, _currentRuntimeInfo.MainFile));
            string packageName = _result.ParserGeneratedState.PackageName;

            code = code.Replace(TemplateGrammarName, _grammar.Name);

            string newPackageValue = "";

            bool isPackageNameEmpty = string.IsNullOrWhiteSpace(packageName);

            if (!isPackageNameEmpty)
            {
                if (runtime.IsCSharpRuntime())
                {
                    newPackageValue = "using " + packageName + ";";
                }
                else if (runtime == Runtime.Java)
                {
                    newPackageValue = "import " + packageName + ".*;";
                }
                else if (runtime == Runtime.Go)
                {
                    newPackageValue = "\"./" + packageName + "\"";
                }
                else if (runtime == Runtime.Php)
                {
                    newPackageValue = $"use {packageName}\\{_grammar.Name}Lexer;";
                    if (_grammar.Type != GrammarType.Lexer)
                    {
                        newPackageValue += $"{Environment.NewLine}use {packageName}\\{_grammar.Name}Parser;";
                    }
                }
            }

            code = code.Replace(PackageNamePart, newPackageValue);

            if (runtime == Runtime.Go)
            {
                code = code.Replace("/*$PackageName2$*/", isPackageNameEmpty ? "" : packageName + ".");
            }
            else if (runtime == Runtime.Php)
            {
                code = code.Replace(RuntimesPath, GetPhpAutoloadPath());
            }

            string caseInsensitiveBlockMarker = GenerateCaseInsensitiveBlockMarker();

            if (_grammar.CaseInsensitiveType != CaseInsensitiveType.None)
            {
                string antlrInputStream      = RuntimeInfo.InitOrGetRuntimeInfo(runtime).AntlrInputStream;
                string caseInsensitiveStream = "AntlrCaseInsensitiveInputStream";

                if (runtime == Runtime.Java)
                {
                    caseInsensitiveStream = "new " + caseInsensitiveStream;
                }
                else if (runtime == Runtime.Go)
                {
                    caseInsensitiveStream = "New" + caseInsensitiveStream;
                }
                if (runtime == Runtime.Php)
                {
                    antlrInputStream      = antlrInputStream + "::fromPath";
                    caseInsensitiveStream = caseInsensitiveStream + "::fromPath";
                }

                var    antlrInputStreamRegex = new Regex($@"{antlrInputStream}\(([^\)]+)\)");
                string isLowerBool           = (_grammar.CaseInsensitiveType == CaseInsensitiveType.lower).ToString();
                if (!runtime.IsPythonRuntime())
                {
                    isLowerBool = isLowerBool.ToLowerInvariant();
                }

                code = antlrInputStreamRegex.Replace(code,
                                                     m => $"{caseInsensitiveStream}({m.Groups[1].Value}, {isLowerBool})");

                if (runtime.IsPythonRuntime())
                {
                    code = code.Replace("from antlr4.InputStream import InputStream", "")
                           .Replace(caseInsensitiveBlockMarker,
                                    "from AntlrCaseInsensitiveInputStream import AntlrCaseInsensitiveInputStream");
                }
                else if (runtime == Runtime.JavaScript)
                {
                    code = code.Replace(caseInsensitiveBlockMarker,
                                        "var AntlrCaseInsensitiveInputStream = require('./AntlrCaseInsensitiveInputStream').AntlrCaseInsensitiveInputStream;");
                }
                else if (runtime == Runtime.Php)
                {
                    code = code.Replace(caseInsensitiveBlockMarker, "require_once 'AntlrCaseInsensitiveInputStream.php';");
                }
            }
            else
            {
                code = code.Replace(caseInsensitiveBlockMarker, "");
            }

            Regex parserPartStart, parserPartEnd;

            if (runtime.IsPythonRuntime())
            {
                string newValue = runtime == Runtime.Python2
                    ? "print \"Tree \" + tree.toStringTree(recog=parser);"
                    : "print(\"Tree \", tree.toStringTree(recog=parser));";
                code = code.Replace("'''PrintTree'''", newValue);

                parserPartStart = ParserPartStartPython;
                parserPartEnd   = ParserPartEndPython;

                code = RemoveCodeOrClearMarkers(code, ParserIncludeStartPython, ParserIncludeEndPython);
            }
            else
            {
                parserPartStart = ParserPartStart;
                parserPartEnd   = ParserPartEnd;

                if (runtime == Runtime.JavaScript || runtime == Runtime.Go || runtime == Runtime.Php)
                {
                    code = RemoveCodeOrClearMarkers(code, ParserIncludeStartJavaScriptGoPhp, ParserIncludeEndJavaScriptGoPhp);
                }
            }

            code = RemoveCodeOrClearMarkers(code, parserPartStart, parserPartEnd);

            File.WriteAllText(templateFile, code);
        }
コード例 #7
0
        public MainWindowViewModel(Window window)
        {
            _window               = window;
            _grammarTextBox       = _window.Find <TextEditor>("GrammarTextBox");
            _textTextBox          = _window.Find <TextEditor>("TextTextBox");
            _grammarErrorsListBox = _window.Find <ListBox>("GrammarErrorsListBox");
            _textErrorsListBox    = _window.Find <ListBox>("TextErrorsListBox");
            _parseTreeTextBox     = _window.Find <TextEditor>("ParseTreeTextBox");
            _tokensTextBox        = _window.Find <TextEditor>("TokensTextBox");

            _grammarTextBox.SetupHightlighting(".g4");

            _settings = Settings.Load();

            _window.WindowState = _settings.WindowState;
            if (_settings.Width > 0)
            {
                _window.Width = _settings.Width;
            }
            if (_settings.Height > 0)
            {
                _window.Height = _settings.Height;
            }
            if (_settings.Left > 0 && _settings.Top > 0)
            {
                _window.Position = new PixelPoint(_settings.Left, _settings.Top);
            }

            Grammar grammar = null;

            bool   openDefaultGrammar = false;
            string packageName        = null;
            string root = null;

            if (string.IsNullOrEmpty(_settings.GrammarFileOrDirectory))
            {
                openDefaultGrammar = true;
            }
            else
            {
                try
                {
                    grammar = GrammarFactory.Open(_settings.GrammarFileOrDirectory, out packageName, out root);
                }
                catch (Exception ex)
                {
                    ShowOpenFileErrorMessage(_settings.GrammarFileOrDirectory, ex.Message);

                    _settings.OpenedGrammarFile = "";
                    openDefaultGrammar          = true;
                }
            }

            if (openDefaultGrammar)
            {
                grammar = GrammarFactory.CreateDefault();
                GrammarFactory.FillGrammarFiles(grammar, Settings.Directory, false);
                _settings.GrammarFileOrDirectory = grammar.Directory;
                _settings.Save();
            }

            _workflow = new Workflow(grammar);

            var availableRuntimes = new[]
            {
                Runtime.Java, Runtime.CSharpStandard, Runtime.CSharpOptimized, Runtime.Python2, Runtime.Python3,
                Runtime.Go, Runtime.Php
            };

            _runtimeInfoWrappers = new Dictionary <Runtime, RuntimeInfoWrapper>();

            foreach (Runtime runtime in availableRuntimes)
            {
                _runtimeInfoWrappers.Add(runtime, new RuntimeInfoWrapper(RuntimeInfo.InitOrGetRuntimeInfo(runtime)));
            }

            var list = new List <RuntimeInfoWrapper> {
                AutodetectRuntime
            };

            list.AddRange(_runtimeInfoWrappers.Values);

            Runtimes = new ObservableCollection <RuntimeInfoWrapper>(list);

            SelectedRuntime = GetAutoOrSelectedRuntime();
            PackageName     = packageName;
            Root            = root;

            InitFiles();
            if (string.IsNullOrEmpty(_settings.OpenedGrammarFile) || !grammar.Files.Contains(_settings.OpenedGrammarFile))
            {
                OpenedGrammarFile = GrammarFiles.FirstOrDefault();
            }
            else
            {
                OpenedGrammarFile = _settings.OpenedGrammarFile;
            }

            if (string.IsNullOrEmpty(_settings.OpenedTextFile))
            {
                OpenedTextFile = TextFiles.Count > 0 ? TextFiles.First() : null;
            }
            else
            {
                OpenedTextFile = new FileName(_settings.OpenedTextFile);
            }

            SetupWindowSubscriptions();
            SetupWorkflowSubscriptions();
            SetupTextBoxSubscriptions();
            SetupCommandSubscriptions();

            AutoProcessing = _settings.Autoprocessing;
        }