pascalabcnet/VisualPascalABCNET/Workbench/BuildService.cs
AlexanderZemlyak 0ae7595f0d
Implement programming languages integration functionality (#3001)
* Make standardModules Dictionary instead of LanguageId usage

* Create new common string constants for pascal language

* Add LanguagesData class template

* Move some compiler directive string constants to Compiler project

* Extract LanguagesData class to separate file

Also extracted RuntimeServiceModule (IO-modules) management to new functions.

* Fix pabcnetc crashing (KeyNotFoundException)

* Make similar change in pabcnetc_clear

* Return PABCExtensions in DomConverter.cs

* Return compiler string constants to Tree Converter

* Rename MoveSystemUnitForward method

* Rename system unit variable

* Add new parser load error (localized versions)

* Fix pascal language name constant

* Add system units property to parsers

* Add LanguageKits directory with a test file

* Implement language integrator

Был реализован загрузчик комплектов дополнительных языков (всех кроме Pascal). Был пересмотрен подход к реализации ParsersController и этот класс сделан синглтоном. Добавлена загрузка стандартных модулей языков в CompilerOptions.

* Squashed commit of the following:

commit 8f9dc7e5a0
Author: Mikhalkovich Stanislav <miks@math.sfedu.ru>
Date:   Mon Mar 11 20:33:13 2024 +0300

    fix #3050

commit 90363ced45
Author: Mikhalkovich Stanislav <miks@math.sfedu.ru>
Date:   Sun Mar 3 21:37:25 2024 +0300

    Недобитки при сортировке строк

commit c9d7ba7758
Author: Mikhalkovich Stanislav <miks@math.sfedu.ru>
Date:   Sat Mar 2 13:32:14 2024 +0300

    Order и OrderDescending для строк с Ordinal

commit 6fbc0200bf
Author: AlexanderZemlyak <92867056+AlexanderZemlyak@users.noreply.github.com>
Date:   Tue Feb 27 23:21:26 2024 +0300

    Fix interface loop dependency check (#3047)

    Tests for problematic case were added

commit e4d63bfef9
Author: bormant <bormant@mail.ru>
Date:   Tue Feb 27 23:18:25 2024 +0300

    Fix typo: C (ru) -> C (en) (#3042)

commit 0d37232d21
Author: Mikhalkovich Stanislav <miks@math.sfedu.ru>
Date:   Tue Feb 27 19:15:08 2024 +0300

    School - исправление пустого диапазона простых и IP-адресов

commit 9fd4c7f338
Merge: 3cca2a89 6027083b
Author: Ivan Bondarev <ibond84@googlemail.com>
Date:   Wed Feb 21 20:57:10 2024 +0100

    Merge branch 'master' of https://github.com/pascalabcnet/pascalabcnet

commit 3cca2a8949
Author: Ivan Bondarev <ibond84@googlemail.com>
Date:   Wed Feb 21 20:56:49 2024 +0100

    fix in formatter
2024-03-13 22:16:40 +03:00

340 lines
17 KiB
C#

// Copyright (c) Ivan Bondarev, Stanislav Mikhalkovich (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using VisualPascalABCPlugins;
namespace VisualPascalABC
{
public class WorkbenchBuildService : IWorkbenchBuildService
{
public PascalABCCompiler.CompilerOptions CompilerOptions1 = new PascalABCCompiler.CompilerOptions();
bool __ForRun;
string __RuntimeServicesModule;
bool __savePCU;
public List<PascalABCCompiler.Errors.Error> ErrorsList = new List<PascalABCCompiler.Errors.Error>();
IWorkbench Workbench;
IWorkbenchProjectService ProjectService;
IWorkbenchDesignerService DesignerService;
IWorkbenchDocumentService DocumentService;
public WorkbenchBuildService()
{
Workbench = WorkbenchServiceFactory.Workbench;
ProjectService = WorkbenchServiceFactory.ProjectService;
DesignerService = WorkbenchServiceFactory.DesignerService;
DocumentService = WorkbenchServiceFactory.DocumentService;
}
public PascalABCCompiler.CompilerOptions CompilerOptions
{
get
{
return CompilerOptions1;
}
}
List<PascalABCCompiler.Errors.Error> IWorkbenchBuildService.ErrorsList
{
get
{
return ErrorsList;
}
}
public BuildServiceActionDelegate BeforeCompile { get; set; } = null;
//kompilacija proekta
public string Compile(PascalABCCompiler.IProjectInfo project, bool rebuild, string RuntimeServicesModule, bool ForRun, bool RunWithEnvironment)
{
ProjectService.SaveProject();
Workbench.WidgetController.CompilingButtonsEnabled = false;
Workbench.CompilerConsoleWindow.ClearConsole();
CompilerOptions1.SourceFileName = project.Path;
CompilerOptions1.OutputDirectory = project.OutputDirectory;
CompilerOptions1.ProjectCompiled = true;
if (project.ProjectType == PascalABCCompiler.ProjectType.WindowsApp)
CompilerOptions1.OutputFileType = PascalABCCompiler.CompilerOptions.OutputType.WindowsApplication;
ErrorsList.Clear();
CompilerOptions1.Rebuild = rebuild;
CompilerOptions1.Locale = PascalABCCompiler.StringResourcesLanguage.CurrentTwoLetterISO;
CompilerOptions1.UseDllForSystemUnits = false;
CompilerOptions1.RunWithEnvironment = RunWithEnvironment;
bool savePCU = Workbench.VisualEnvironmentCompiler.Compiler.InternalDebug.PCUGenerate;
if (Path.GetDirectoryName(CompilerOptions1.SourceFileName).ToLower() == ((string)WorkbenchStorage.StandartDirectories[Constants.LibSourceDirectoryIdent]).ToLower())
Workbench.VisualEnvironmentCompiler.Compiler.InternalDebug.PCUGenerate = false;
if (RuntimeServicesModule != null)
{
AddRuntimeServicesModuleToStandardModules(RuntimeServicesModule);
}
string ofn = Workbench.VisualEnvironmentCompiler.Compile(CompilerOptions1);
Workbench.VisualEnvironmentCompiler.Compiler.InternalDebug.PCUGenerate = savePCU;
if (RuntimeServicesModule != null)
{
RemoveRuntimeServicesModuleFromStandardModules();
}
if (Workbench.VisualEnvironmentCompiler.Compiler.ErrorsList.Count != 0 || Workbench.VisualEnvironmentCompiler.Compiler.Warnings.Count != 0)
{
List<PascalABCCompiler.Errors.Error> ErrorsAndWarnings = new List<PascalABCCompiler.Errors.Error>();
List<PascalABCCompiler.Errors.Error> Errors = Workbench.ErrorsManager.CreateErrorsList(Workbench.VisualEnvironmentCompiler.Compiler.ErrorsList);
AddErrors(ErrorsAndWarnings, Errors);
//if (!ForRun)
AddWarnings(ErrorsAndWarnings, Workbench.VisualEnvironmentCompiler.Compiler.Warnings);
Workbench.ErrorsListWindow.ShowErrorsSync(ErrorsAndWarnings, Errors.Count != 0 || (Workbench.VisualEnvironmentCompiler.Compiler.Warnings.Count != 0 && !ForRun));
}
return ofn;
}
public string Compile(string FileName, bool rebuild, string RuntimeServicesModule, bool ForRun, bool RunWithEnvironment)
{
Workbench.WidgetController.CompilingButtonsEnabled = false;
var UserOptions = Workbench.UserOptions;
Workbench.CompilerConsoleWindow.ClearConsole();
CompilerOptions1.SourceFileName = FileName;
CompilerOptions1.Locale = PascalABCCompiler.StringResourcesLanguage.CurrentTwoLetterISO;
if (Path.GetDirectoryName(FileName) == "" && WorkbenchStorage.WorkingDirectoryExsist)
CompilerOptions1.OutputDirectory = WorkbenchStorage.WorkingDirectory;
if (Workbench.UserOptions.UseOutputDirectory && Directory.Exists(UserOptions.OutputDirectory))
CompilerOptions1.OutputDirectory = UserOptions.OutputDirectory;
CompilerOptions1.Rebuild = rebuild;
CompilerOptions1.RunWithEnvironment = RunWithEnvironment;
//string runtimeModuleFileName = CompilerOptions1.SearchDirectory + "\\" + RuntimeServicesModule;
//if (RuntimeServicesModule!=null && File.Exists(runtimeModuleFileName))
// CompilerOptions1.StandardModules.Add(new PascalABCCompiler.CompilerOptions.StandardModule(runtimeModuleFileName, PascalABCCompiler.CompilerOptions.StandartModuleAddMethod.RightToMain));
if (RuntimeServicesModule != null)
{
AddRuntimeServicesModuleToStandardModules(RuntimeServicesModule);
}
ErrorsList.Clear();
//CompilerOptions1.SavePCUInThreadPull = true;
bool savePCU = Workbench.VisualEnvironmentCompiler.Compiler.InternalDebug.PCUGenerate;
if (Path.GetDirectoryName(FileName).ToLower() == ((string)WorkbenchStorage.StandartDirectories[Constants.LibSourceDirectoryIdent]).ToLower())
Workbench.VisualEnvironmentCompiler.Compiler.InternalDebug.PCUGenerate = false;
string ofn = Workbench.VisualEnvironmentCompiler.Compile(CompilerOptions1);
Workbench.VisualEnvironmentCompiler.Compiler.InternalDebug.PCUGenerate = savePCU;
if (RuntimeServicesModule != null)
{
RemoveRuntimeServicesModuleFromStandardModules();
}
if (Workbench.VisualEnvironmentCompiler.Compiler.ErrorsList.Count != 0 || Workbench.VisualEnvironmentCompiler.Compiler.Warnings.Count != 0)
{
List<PascalABCCompiler.Errors.Error> ErrorsAndWarnings = new List<PascalABCCompiler.Errors.Error>();
List<PascalABCCompiler.Errors.Error> Errors = Workbench.ErrorsManager.CreateErrorsList(Workbench.VisualEnvironmentCompiler.Compiler.ErrorsList);
AddErrors(ErrorsAndWarnings, Errors);
//if (!ForRun)
AddWarnings(ErrorsAndWarnings, Workbench.VisualEnvironmentCompiler.Compiler.Warnings);
Workbench.ErrorsListWindow.ShowErrorsSync(ErrorsAndWarnings, Errors.Count != 0 || (Workbench.VisualEnvironmentCompiler.Compiler.Warnings.Count != 0 && !ForRun));
}
return ofn;
}
public void StartCompile(PascalABCCompiler.IProjectInfo project, bool rebuild, string RuntimeServicesModule, bool ForRun, bool RunWithEnvironment)
{
ProjectService.SaveProject();
__RuntimeServicesModule = RuntimeServicesModule;
__ForRun = ForRun;
Workbench.CompilerConsoleWindow.ClearConsole();
var UserOptions = Workbench.UserOptions;
CompilerOptions1.SourceFileName = project.Path;
CompilerOptions1.ProjectCompiled = true;
CompilerOptions1.Locale = PascalABCCompiler.StringResourcesLanguage.CurrentTwoLetterISO;
if (UserOptions.UseOutputDirectory && Directory.Exists(UserOptions.OutputDirectory))
CompilerOptions1.OutputDirectory = UserOptions.OutputDirectory;
CompilerOptions1.Rebuild = rebuild;
CompilerOptions1.RunWithEnvironment = RunWithEnvironment;
//string runtimeModuleFileName = CompilerOptions1.SearchDirectory + "\\" + RuntimeServicesModule;
//if (RuntimeServicesModule!=null && File.Exists(runtimeModuleFileName))
// CompilerOptions1.StandardModules.Add(new PascalABCCompiler.CompilerOptions.StandardModule(runtimeModuleFileName, PascalABCCompiler.CompilerOptions.StandartModuleAddMethod.RightToMain));
if (__RuntimeServicesModule != null)
{
AddRuntimeServicesModuleToStandardModules(RuntimeServicesModule);
}
ErrorsList.Clear();
//CompilerOptions1.SavePCUInThreadPull = true;
__savePCU = Workbench.VisualEnvironmentCompiler.Compiler.InternalDebug.PCUGenerate;
//if (Path.GetDirectoryName(file_name).ToLower() == ((string)StandartDirectories[Constants.LibSourceDirectoryIdent]).ToLower())
// VisualEnvironmentCompiler.Compiler.InternalDebug.PCUGenerate = false;
Workbench.VisualEnvironmentCompiler.Compiler.OnChangeCompilerState += CompilationOnChangeCompilerState;
Workbench.VisualEnvironmentCompiler.StartCompile(CompilerOptions1);
}
public void StartCompile(string FileName, bool rebuild, string RuntimeServicesModule, bool ForRun, bool RunWithEnvironment)
{
__RuntimeServicesModule = RuntimeServicesModule;
__ForRun = ForRun;
Workbench.CompilerConsoleWindow.ClearConsole();
var UserOptions = Workbench.UserOptions;
CompilerOptions1.SourceFileName = FileName;
CompilerOptions1.Locale = PascalABCCompiler.StringResourcesLanguage.CurrentTwoLetterISO;
if (Path.GetDirectoryName(FileName) == "" && WorkbenchStorage.WorkingDirectoryExsist)
CompilerOptions1.OutputDirectory = WorkbenchStorage.WorkingDirectory;
if (UserOptions.UseOutputDirectory && Directory.Exists(UserOptions.OutputDirectory))
CompilerOptions1.OutputDirectory = UserOptions.OutputDirectory;
CompilerOptions1.Rebuild = rebuild;
CompilerOptions1.RunWithEnvironment = RunWithEnvironment;
//string runtimeModuleFileName = CompilerOptions1.SearchDirectory + "\\" + RuntimeServicesModule;
//if (RuntimeServicesModule!=null && File.Exists(runtimeModuleFileName))
// CompilerOptions1.StandardModules.Add(new PascalABCCompiler.CompilerOptions.StandardModule(runtimeModuleFileName, PascalABCCompiler.CompilerOptions.StandartModuleAddMethod.RightToMain));
if (__RuntimeServicesModule != null)
{
AddRuntimeServicesModuleToStandardModules(RuntimeServicesModule);
}
ErrorsList.Clear();
//CompilerOptions1.SavePCUInThreadPull = true;
__savePCU = Workbench.VisualEnvironmentCompiler.Compiler.InternalDebug.PCUGenerate;
if (Path.GetDirectoryName(FileName).ToLower() == ((string)WorkbenchStorage.StandartDirectories[Constants.LibSourceDirectoryIdent]).ToLower())
Workbench.VisualEnvironmentCompiler.Compiler.InternalDebug.PCUGenerate = false;
Workbench.VisualEnvironmentCompiler.Compiler.OnChangeCompilerState += CompilationOnChangeCompilerState;
Workbench.VisualEnvironmentCompiler.StartCompile(CompilerOptions1);
}
/// <summary>
/// Удаляет модуль сервиса времени выполнения из списков стандартных модулей всех языков
/// </summary>
private void RemoveRuntimeServicesModuleFromStandardModules()
{
foreach (var kv in CompilerOptions1.StandardModules)
{
CompilerOptions1.RemoveStandardModuleAtIndex(kv.Key, CompilerOptions1.StandardModules[kv.Key].Count - 1);
}
}
/// <summary>
/// Добавляет модуль сервиса времени выполнения в списки стандартных модулей всех языков
/// </summary>
private void AddRuntimeServicesModuleToStandardModules(string RuntimeServicesModule)
{
foreach (var kv in CompilerOptions1.StandardModules)
{
kv.Value.Add(new PascalABCCompiler.CompilerOptions.StandardModule(RuntimeServicesModule,
PascalABCCompiler.CompilerOptions.StandardModuleAddMethod.RightToMain, kv.Key));
}
}
public bool Build()
{
Workbench.OutputWindow.ClearTextBox();
Workbench.ErrorsListWindow.ClearErrorList();
DesignerService.GenerateAllDesignersCode();
CompilerOptions1.UseDllForSystemUnits = false;
if (!ProjectFactory.Instance.ProjectLoaded)
return Compile(DocumentService.CurrentCodeFileDocument.FileName, false, null, false, false) != null;
else
return Compile(ProjectFactory.Instance.CurrentProject, false, null, false, false) != null;
}
public bool Build(string FileName)
{
Workbench.OutputWindow.ClearTextBox();
Workbench.ErrorsListWindow.ClearErrorList();
/*while (codeCompletionParserController.IsParsing())
Thread.Sleep(10);*/
CompilerOptions1.UseDllForSystemUnits = false;
return Compile(FileName, false, null, false, false) != null;
}
public bool Rebuild()
{
Workbench.OutputWindow.ClearTextBox();
Workbench.ErrorsListWindow.ClearErrorList();
DesignerService.GenerateAllDesignersCode();
CompilerOptions1.UseDllForSystemUnits = false;
if (!ProjectFactory.Instance.ProjectLoaded)
return Compile(DocumentService.CurrentCodeFileDocument.FileName, true, null, false, false) != null;
else
return Compile(ProjectFactory.Instance.CurrentProject, true, null, false, false) != null;
}
public void StartCompile(bool rebuild)
{
VisualPABCSingleton.MainForm.OutputWindow.outputTextBox.Clear();
Workbench.ErrorsListWindow.ClearErrorList();
DesignerService.GenerateAllDesignersCode();
CompilerOptions1.UseDllForSystemUnits = false;
if (!ProjectFactory.Instance.ProjectLoaded)
StartCompile(DocumentService.CurrentCodeFileDocument.FileName, rebuild, null, false, false);
else
StartCompile(ProjectFactory.Instance.CurrentProject, rebuild, null, false, false);
}
void CompilationOnChangeCompilerState(PascalABCCompiler.ICompiler sender, PascalABCCompiler.CompilerState State, string FileName)
{
switch (State)
{
case PascalABCCompiler.CompilerState.CompilationFinished:
Workbench.VisualEnvironmentCompiler.Compiler.InternalDebug.PCUGenerate = __savePCU;
if (__RuntimeServicesModule != null)
{
RemoveRuntimeServicesModuleFromStandardModules();
}
if (Workbench.VisualEnvironmentCompiler.Compiler.ErrorsList.Count != 0 || Workbench.VisualEnvironmentCompiler.Compiler.Warnings.Count != 0)
{
List<PascalABCCompiler.Errors.Error> ErrorsAndWarnings = new List<PascalABCCompiler.Errors.Error>();
List<PascalABCCompiler.Errors.Error> Errors = Workbench.ErrorsManager.CreateErrorsList(Workbench.VisualEnvironmentCompiler.Compiler.ErrorsList);
AddErrors(ErrorsAndWarnings, Errors);
//if (!ForRun)
AddWarnings(ErrorsAndWarnings, Workbench.VisualEnvironmentCompiler.Compiler.Warnings);
Workbench.ErrorsListWindow.ShowErrorsSync(ErrorsAndWarnings, Errors.Count != 0 || (Workbench.VisualEnvironmentCompiler.Compiler.Warnings.Count != 0 && !__ForRun));
}
Workbench.VisualEnvironmentCompiler.Compiler.OnChangeCompilerState -= CompilationOnChangeCompilerState;
break;
}
}
public void AddWarnings(List<PascalABCCompiler.Errors.Error> Errors, List<PascalABCCompiler.Errors.CompilerWarning> warns)
{
foreach (PascalABCCompiler.Errors.CompilerWarning w in warns)
Errors.Add(w);
}
public void AddErrors(List<PascalABCCompiler.Errors.Error> Errors, List<PascalABCCompiler.Errors.Error> ers)
{
foreach (PascalABCCompiler.Errors.Error e in ers)
Errors.Add(e);
}
}
}