pascalabcnet/ParserTools/ParserTools/BaseParser.cs
Александр Земляк b23f9c96d0
Деактивация кнопок, связанных с Intellisense, для языков без Intellisense + разделение ILanguageInformation на два интерфейса (#3378)
* Refactor language information classes

* Implement SimpleLanguageInformation abstract class

* Move some properties to SimpleLanguageInformation

* Implement format and debug buttons disabling if Intellisense is not supported for current language

* Add Intellisense available check in FormsExtensions to deactivate menu items if needed

* Change function interfaces in VisibilityService

* Replace CodeCompletionController.CurrentParser null checks with IntellisenseAvailable method call

* Refactor IParser and BaseParser to deduplicate code

* Fix BuildTreeInSpecialMode in SPythonParser

* Add SimpleParser class for new languages without Intellisense

* Delete duplicate SetDebugButtonsEnabled function

* Add Intellisense available check in RunService when attaching debugger

* Delete inner check in SetDebugButtonsEnabled to improve architecture

* Change CurrentParser property in CodeCompletion to be CurrentLanguage

* Divide ILanguageInformation into two interfaces (creating new ILanguageIntellisenseSupport)

* Fix LanguageIntellisenseSupport initialization in BaseLanguage

* Fix RunService fictive_attach logic

* Disable Intellisense parsing for languages without Intellisense support

* Add DefaultLanguageInformation abstract class

* Add default implementation for SetSemanticConstants in BaseLanguage

* Simplify IParser interface

* Update developer guide documentation
2026-01-27 22:25:28 +03:00

142 lines
6.3 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.Collections.Generic;
using PascalABCCompiler.Errors;
using PascalABCCompiler.SyntaxTree;
namespace PascalABCCompiler.Parsers
{
public abstract class BaseParser : IParser
{
protected List<Error> Errors = new List<Error>();
protected List<CompilerWarning> Warnings = new List<CompilerWarning>();
protected List<compiler_directive> CompilerDirectives = new List<compiler_directive>();
public ILanguageInformation LanguageInformation { get; set; }
/// <summary>
/// Возвращеает синтаксическое дерево модуля
/// </summary>
public SyntaxTree.compilation_unit GetCompilationUnit(string FileName, string Text, List<Error> Errors, List<CompilerWarning> Warnings, ParseMode parseMode, bool compilingNotMainProgram, List<string> DefinesList = null)
{
return GetSyntaxTreeChecked<SyntaxTree.compilation_unit>(FileName, Text, Errors, Warnings, parseMode, compilingNotMainProgram, DefinesList);
}
public SyntaxTree.compilation_unit GetCompilationUnitForFormatter(string FileName, string Text, List<Error> Errors, List<CompilerWarning> Warnings)
{
return GetSyntaxTreeChecked<SyntaxTree.compilation_unit>(FileName, Text, Errors, Warnings, ParseMode.ForFormatter);
}
public SyntaxTree.expression GetExpression(string FileName, string Text, List<Error> Errors, List<CompilerWarning> Warnings)
{
try // SSM 06.09.18
{
return GetSyntaxTreeChecked<SyntaxTree.expression>(FileName, Text, Errors, Warnings, ParseMode.Expression);
}
catch
{
return null;
}
}
public SyntaxTree.expression GetTypeAsExpression(string FileName, string Text, List<Error> Errors, List<CompilerWarning> Warnings)
{
return GetSyntaxTreeChecked<SyntaxTree.expression>(FileName, Text, Errors, Warnings, ParseMode.TypeAsExpression);
}
public SyntaxTree.statement GetStatement(string FileName, string Text, List<Error> Errors, List<CompilerWarning> Warnings)
{
return GetSyntaxTreeChecked<SyntaxTree.statement>(FileName, Text, Errors, Warnings, ParseMode.Statement);
}
/// <summary>
/// Обобщенная функция для получения различных синтаксических узлов c проверкой, что узел того типа, который ожидался
/// </summary>
private T GetSyntaxTreeChecked<T>(string FileName, string Text, List<Error> Errors, List<CompilerWarning> Warnings, ParseMode parseMode, bool compilingNotMainProgram = false, List<string> DefinesList = null) where T : SyntaxTree.syntax_tree_node
{
syntax_tree_node unitNode = BuildTree(FileName, Text, Errors, Warnings, parseMode, compilingNotMainProgram, DefinesList);
if (unitNode == null)
return null;
if (unitNode is T)
return unitNode as T;
Errors.Add(new UnexpectedNodeType(FileName, unitNode.source_context, null));
return null;
}
private void InitializeBeforeParsing(List<Error> Errors, List<CompilerWarning> Warnings)
{
Errors.Clear();
Warnings.Clear();
this.Errors = Errors;
this.Warnings = Warnings;
this.CompilerDirectives = new List<compiler_directive>();
}
protected virtual syntax_tree_node BuildTree(string FileName, string Text, List<Error> Errors, List<CompilerWarning> Warnings, ParseMode ParseMode, bool compilingNotMainProgram, List<string> DefinesList = null)
{
InitializeBeforeParsing(Errors, Warnings);
syntax_tree_node root = null;
PreBuildTree(FileName);
switch (ParseMode)
{
case ParseMode.Normal:
root = BuildTreeInNormalMode(FileName, Text, compilingNotMainProgram, DefinesList);
break;
case ParseMode.Expression:
root = BuildTreeInExprMode(FileName, Text);
break;
case ParseMode.TypeAsExpression:
root = BuildTreeInTypeExprMode(FileName, Text);
break;
case ParseMode.Special:
root = BuildTreeInSpecialMode(FileName, Text, compilingNotMainProgram);
break;
case ParseMode.ForFormatter:
root = BuildTreeInFormatterMode(FileName, Text);
break;
case ParseMode.Statement:
root = BuildTreeInStatementMode(FileName, Text);
break;
default:
break;
}
if (root != null && root is compilation_unit compilationUnit)
{
compilationUnit.Language = LanguageInformation.Name;
compilationUnit.file_name = FileName;
compilationUnit.compiler_directives = CompilerDirectives;
if (root is unit_module unitModule)
if (unitModule.unit_name.HeaderKeyword == UnitHeaderKeyword.Library)
unitModule.compiler_directives.Add(new compiler_directive(new token_info("apptype"), new token_info("dll")));
}
return root;
}
protected virtual void PreBuildTree(string FileName) { }
protected abstract syntax_tree_node BuildTreeInNormalMode(string FileName, string Text, bool compilingNotMainProgram, List<string> DefinesList = null);
protected abstract syntax_tree_node BuildTreeInTypeExprMode(string FileName, string Text);
protected abstract syntax_tree_node BuildTreeInExprMode(string FileName, string Text);
protected abstract syntax_tree_node BuildTreeInSpecialMode(string FileName, string Text, bool compilingNotMainProgram);
protected abstract syntax_tree_node BuildTreeInFormatterMode(string FileName, string Text);
protected abstract syntax_tree_node BuildTreeInStatementMode(string FileName, string Text);
}
}