pascalabcnet/AdditionalLanguages/SPython/SPythonParserKrylovMovchan/SPythonParser.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

124 lines
3.9 KiB
C#

using System.IO;
using System.Collections.Generic;
using PascalABCCompiler.SyntaxTree;
using PascalABCCompiler.Parsers;
using SPythonParserYacc;
using PascalABCCompiler.SyntaxTreeConverters;
namespace SPythonParser
{
public class SPythonLanguageParser : BaseParser
{
public List<ISyntaxTreeConverter> SyntaxTreeConvertersForIntellisense { get; set; }
protected override syntax_tree_node BuildTreeInNormalMode(string FileName, string Text, bool compilingNotMainProgram, List<string> DefinesList = null)
{
syntax_tree_node root = Parse(Text, FileName, false, compilingNotMainProgram, DefinesList);
if (Errors.Count > 0)
return null;
return root;
}
public syntax_tree_node Parse(string Text, string fileName, bool buildTreeForFormatter = false, bool compilingNotMainProgram = false, List<string> definesList = null)
{
#if DEBUG
#if _ERR
FileInfo f = new FileInfo(FileName);
var sv = Path.ChangeExtension(FileName,".grmtrack1");
var sw = new StreamWriter(sv);
Console.SetError(sw);
#endif
#endif
SPythonParserTools parserTools = new SPythonParserTools(Errors, Warnings, LanguageInformation.ValidDirectives, buildTreeForFormatter, false,
Path.GetFullPath(fileName), CompilerDirectives); // контекст сканера и парсера
IndentArranger ia = new IndentArranger();
ia.ProcessSourceText(ref Text);
var scanner = new Scanner(Text, parserTools, LanguageInformation.KeywordsStorage, definesList);
SPythonGPPGParser parser = new SPythonGPPGParser(scanner, parserTools, compilingNotMainProgram);
if (!parser.Parse())
if (Errors.Count == 0)
parserTools.AddErrorFromResource("UNEXPECTED_SYNTAX_ERROR", null);
#if DEBUG
#if _ERR
sw.Close();
#endif
#endif
return parser.root;
}
protected override syntax_tree_node BuildTreeInTypeExprMode(string FileName, string Text)
{
Text = string.Concat("<<type>>", Text);
var expr = Parse(Text, FileName) as expression;
if (expr == null)
return null;
foreach (ISyntaxTreeConverter converter in SyntaxTreeConvertersForIntellisense)
{
expr = (expression)converter.Convert(expr, true);
}
return expr;
}
protected override syntax_tree_node BuildTreeInExprMode(string FileName, string Text)
{
if (Text == string.Empty)
return null;
Text = string.Concat("<<expression>>", Text);
var expr = Parse(Text, FileName) as expression;
if (expr == null)
return null;
foreach (ISyntaxTreeConverter converter in SyntaxTreeConvertersForIntellisense)
{
expr = (expression)converter.Convert(expr, true);
}
return expr;
}
protected override syntax_tree_node BuildTreeInSpecialMode(string FileName, string Text, bool compilingNotMainProgram)
{
return Parse(Text, FileName, compilingNotMainProgram: compilingNotMainProgram);
}
protected override syntax_tree_node BuildTreeInFormatterMode(string FileName, string Text)
{
return Parse(Text, FileName, true);
}
protected override syntax_tree_node BuildTreeInStatementMode(string FileName, string Text)
{
Text = string.Concat("<<statement>>", Text);
var st = Parse(Text, FileName) as statement;
if (st == null)
return null;
foreach (ISyntaxTreeConverter converter in SyntaxTreeConvertersForIntellisense)
{
st = (statement)converter.Convert(st, true);
}
return st;
}
}
}