Change type of language field in syntax tree to be string (#3119)

* Change syntax tree language field to be string

Тип LanguageId заменен на string

* Fix unit language name assigning

* Update lexer and parser

* Add checks to avoid NullReferenceException

Проверки на null для SyntaxTree. (К падению компилятора они не приводили, т.к. они связаны с другими ошибками, например синтаксическими), но мешали отладке.

* Return initial lexer and parser versions

* Revert language name assigning
This commit is contained in:
AlexanderZemlyak 2024-05-28 14:16:49 +03:00 committed by GitHub
parent 8acf536300
commit c2111cf375
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 69 additions and 82 deletions

View file

@ -3291,7 +3291,7 @@ namespace PascalABCCompiler
private void SemanticCheckNoIncludeDirectivesInPascalUnit(CompilationUnit compilationUnit)
{
if (HasIncludeNamespaceDirective(compilationUnit) && compilationUnit.SyntaxTree is SyntaxTree.unit_module unitModule
if (compilationUnit.SyntaxTree is SyntaxTree.unit_module unitModule && HasIncludeNamespaceDirective(compilationUnit)
&& unitModule.unit_name.HeaderKeyword != SyntaxTree.UnitHeaderKeyword.Library)
{
throw new IncludeNamespaceInUnitError(currentCompilationUnit.SyntaxTree.file_name, currentCompilationUnit.SyntaxTree.source_context);
@ -3584,15 +3584,19 @@ namespace PascalABCCompiler
/// </summary>
public static bool IsDll(SyntaxTree.compilation_unit unitSyntaxTree, out SyntaxTree.compiler_directive dllDirective)
{
foreach (SyntaxTree.compiler_directive directive in unitSyntaxTree.compiler_directives)
if (unitSyntaxTree != null)
{
if (string.Equals(directive.Name.text, "apptype", StringComparison.CurrentCultureIgnoreCase)
&& string.Equals(directive.Directive.text, "dll", StringComparison.CurrentCultureIgnoreCase))
foreach (SyntaxTree.compiler_directive directive in unitSyntaxTree.compiler_directives)
{
dllDirective = directive;
return true;
if (string.Equals(directive.Name.text, "apptype", StringComparison.CurrentCultureIgnoreCase)
&& string.Equals(directive.Directive.text, "dll", StringComparison.CurrentCultureIgnoreCase))
{
dllDirective = directive;
return true;
}
}
}
dllDirective = null;
return false;
}

View file

@ -2,7 +2,7 @@
// This CSharp output file generated by Gardens Point LEX
// Version: 1.1.3.301
// Machine: DESKTOP-V3E9T2U
// DateTime: 19.05.2024 23:53:52
// DateTime: 25.05.2024 22:13:44
// UserName: alex
// GPLEX input file <ABCPascal.lex>
// GPLEX frame file <embedded resource>

View file

@ -423,7 +423,8 @@ unit_file
unit_header interface_part implementation_part initialization_part tkPoint
{
$$ = new unit_module($1 as unit_name, $2 as interface_node, $3 as implementation_node,
($4 as initfinal_part).initialization_sect, ($4 as initfinal_part).finalization_sect, /*$1 as attribute_list*/ null, @$);
($4 as initfinal_part).initialization_sect, ($4 as initfinal_part).finalization_sect, /*$1 as attribute_list*/ null, @$);
($$ as compilation_unit).Language = PascalABCCompiler.StringConstants.pascalLanguageName;
}
|
//attribute_declarations
@ -431,6 +432,7 @@ unit_file
{
$$ = new unit_module($1 as unit_name, $2 as interface_node, null,
($3 as initfinal_part).initialization_sect, ($3 as initfinal_part).finalization_sect, /*$1 as attribute_list*/ null, @$);
($$ as compilation_unit).Language = PascalABCCompiler.StringConstants.pascalLanguageName;
}
;

View file

@ -2,7 +2,7 @@
// GPPG version 1.3.6
// Machine: DESKTOP-V3E9T2U
// DateTime: 19.05.2024 23:53:53
// DateTime: 25.05.2024 22:13:45
// UserName: alex
// Input file <ABCPascal.y>
@ -3135,13 +3135,15 @@ public partial class GPPGParser: ShiftReduceParser<PascalABCCompiler.ParserTools
// initialization_part, tkPoint
{
CurrentSemanticValue.stn = new unit_module(ValueStack[ValueStack.Depth-5].stn as unit_name, ValueStack[ValueStack.Depth-4].stn as interface_node, ValueStack[ValueStack.Depth-3].stn as implementation_node,
(ValueStack[ValueStack.Depth-2].stn as initfinal_part).initialization_sect, (ValueStack[ValueStack.Depth-2].stn as initfinal_part).finalization_sect, /*$1 as attribute_list*/ null, CurrentLocationSpan);
(ValueStack[ValueStack.Depth-2].stn as initfinal_part).initialization_sect, (ValueStack[ValueStack.Depth-2].stn as initfinal_part).finalization_sect, /*$1 as attribute_list*/ null, CurrentLocationSpan);
(CurrentSemanticValue.stn as compilation_unit).Language = PascalABCCompiler.StringConstants.pascalLanguageName;
}
break;
case 48: // unit_file -> unit_header, abc_interface_part, initialization_part, tkPoint
{
CurrentSemanticValue.stn = new unit_module(ValueStack[ValueStack.Depth-4].stn as unit_name, ValueStack[ValueStack.Depth-3].stn as interface_node, null,
(ValueStack[ValueStack.Depth-2].stn as initfinal_part).initialization_sect, (ValueStack[ValueStack.Depth-2].stn as initfinal_part).finalization_sect, /*$1 as attribute_list*/ null, CurrentLocationSpan);
(CurrentSemanticValue.stn as compilation_unit).Language = PascalABCCompiler.StringConstants.pascalLanguageName;
}
break;
case 49: // unit_header -> unit_key_word, unit_name, tkSemiColon,

View file

@ -45,7 +45,7 @@ namespace Languages.Pascal.Frontend.Core
public program_module NewProgramModule(program_name progName, Object optHeadCompDirs, uses_list mainUsesClose, syntax_tree_node progBlock, Object optPoint, LexLocation loc)
{
var progModule = new program_module(progName, mainUsesClose, progBlock as block, null, loc);
progModule.Language = LanguageId.PascalABCNET;
progModule.Language = PascalABCCompiler.StringConstants.pascalLanguageName;
if (optPoint == null && progBlock != null)
{
var fp = progBlock.source_context.end_position;

View file

@ -24,7 +24,7 @@ namespace PascalABCCompiler.VBNETParser
if (td.Type == ICSharpCode.NRefactory.Ast.ClassType.Module)
{
mod = new unit_module();
mod.Language = LanguageId.VBNET;
//mod.Language = "..."; // нужно заполнять !!!
mod.file_name = FileName;
mod.unit_name = new unit_name();
mod.unit_name.idunit_name = new ident(td.Name);

View file

@ -148,6 +148,12 @@
<Install>true</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\StringConstants\StringConstants.csproj">
<Project>{e8aefbf9-0113-4fa4-be45-6cda555498b7}</Project>
<Name>StringConstants</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PreBuildEvent>

View file

@ -1474,7 +1474,14 @@ namespace PascalABCCompiler.SyntaxTree
_compilation_unit.compiler_directives.Add(_read_node() as compiler_directive);
}
}
_compilation_unit.Language = (LanguageId)br.ReadByte();
if (br.ReadByte() == 0)
{
_compilation_unit.Language = null;
}
else
{
_compilation_unit.Language = br.ReadString();
}
}

View file

@ -1582,7 +1582,15 @@ namespace PascalABCCompiler.SyntaxTree
}
}
}
bw.Write((byte)_compilation_unit.Language);
if (_compilation_unit.Language == null)
{
bw.Write((byte)0);
}
else
{
bw.Write((byte)1);
bw.Write(_compilation_unit.Language);
}
}

View file

@ -1,6 +1,6 @@
// 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;
using System.Collections.Generic;
namespace PascalABCCompiler.SyntaxTree
@ -12157,7 +12157,7 @@ namespace PascalABCCompiler.SyntaxTree
///<summary>
///Конструктор с параметрами.
///</summary>
public compilation_unit(string _file_name,List<compiler_directive> _compiler_directives,LanguageId _Language)
public compilation_unit(string _file_name,List<compiler_directive> _compiler_directives,string _Language)
{
this._file_name=_file_name;
this._compiler_directives=_compiler_directives;
@ -12168,7 +12168,7 @@ namespace PascalABCCompiler.SyntaxTree
///<summary>
///Конструктор с параметрами.
///</summary>
public compilation_unit(string _file_name,List<compiler_directive> _compiler_directives,LanguageId _Language,SourceContext sc)
public compilation_unit(string _file_name,List<compiler_directive> _compiler_directives,string _Language,SourceContext sc)
{
this._file_name=_file_name;
this._compiler_directives=_compiler_directives;
@ -12184,7 +12184,7 @@ namespace PascalABCCompiler.SyntaxTree
protected string _file_name;
protected List<compiler_directive> _compiler_directives=new List<compiler_directive>();
protected LanguageId _Language;
protected string _Language;
///<summary>
///
@ -12219,7 +12219,7 @@ namespace PascalABCCompiler.SyntaxTree
///<summary>
///
///</summary>
public LanguageId Language
public string Language
{
get
{
@ -12547,7 +12547,7 @@ namespace PascalABCCompiler.SyntaxTree
///<summary>
///Конструктор с параметрами.
///</summary>
public unit_module(string _file_name,List<compiler_directive> _compiler_directives,LanguageId _Language,unit_name _unit_name,interface_node _interface_part,implementation_node _implementation_part,statement_list _initialization_part,statement_list _finalization_part,attribute_list _attributes)
public unit_module(string _file_name,List<compiler_directive> _compiler_directives,string _Language,unit_name _unit_name,interface_node _interface_part,implementation_node _implementation_part,statement_list _initialization_part,statement_list _finalization_part,attribute_list _attributes)
{
this._file_name=_file_name;
this._compiler_directives=_compiler_directives;
@ -12564,7 +12564,7 @@ namespace PascalABCCompiler.SyntaxTree
///<summary>
///Конструктор с параметрами.
///</summary>
public unit_module(string _file_name,List<compiler_directive> _compiler_directives,LanguageId _Language,unit_name _unit_name,interface_node _interface_part,implementation_node _implementation_part,statement_list _initialization_part,statement_list _finalization_part,attribute_list _attributes,SourceContext sc)
public unit_module(string _file_name,List<compiler_directive> _compiler_directives,string _Language,unit_name _unit_name,interface_node _interface_part,implementation_node _implementation_part,statement_list _initialization_part,statement_list _finalization_part,attribute_list _attributes,SourceContext sc)
{
this._file_name=_file_name;
this._compiler_directives=_compiler_directives;
@ -12935,7 +12935,7 @@ namespace PascalABCCompiler.SyntaxTree
///<summary>
///Конструктор с параметрами.
///</summary>
public program_module(string _file_name,List<compiler_directive> _compiler_directives,LanguageId _Language,program_name _program_name,uses_list _used_units,block _program_block,using_list _using_namespaces)
public program_module(string _file_name,List<compiler_directive> _compiler_directives,string _Language,program_name _program_name,uses_list _used_units,block _program_block,using_list _using_namespaces)
{
this._file_name=_file_name;
this._compiler_directives=_compiler_directives;
@ -12950,7 +12950,7 @@ namespace PascalABCCompiler.SyntaxTree
///<summary>
///Конструктор с параметрами.
///</summary>
public program_module(string _file_name,List<compiler_directive> _compiler_directives,LanguageId _Language,program_name _program_name,uses_list _used_units,block _program_block,using_list _using_namespaces,SourceContext sc)
public program_module(string _file_name,List<compiler_directive> _compiler_directives,string _Language,program_name _program_name,uses_list _used_units,block _program_block,using_list _using_namespaces,SourceContext sc)
{
this._file_name=_file_name;
this._compiler_directives=_compiler_directives;
@ -34484,7 +34484,7 @@ namespace PascalABCCompiler.SyntaxTree
///<summary>
///Конструктор с параметрами.
///</summary>
public c_module(string _file_name,List<compiler_directive> _compiler_directives,LanguageId _Language,declarations _defs,uses_list _used_units)
public c_module(string _file_name,List<compiler_directive> _compiler_directives,string _Language,declarations _defs,uses_list _used_units)
{
this._file_name=_file_name;
this._compiler_directives=_compiler_directives;
@ -34497,7 +34497,7 @@ namespace PascalABCCompiler.SyntaxTree
///<summary>
///Конструктор с параметрами.
///</summary>
public c_module(string _file_name,List<compiler_directive> _compiler_directives,LanguageId _Language,declarations _defs,uses_list _used_units,SourceContext sc)
public c_module(string _file_name,List<compiler_directive> _compiler_directives,string _Language,declarations _defs,uses_list _used_units,SourceContext sc)
{
this._file_name=_file_name;
this._compiler_directives=_compiler_directives;

View file

@ -202,7 +202,7 @@ program_body->syntax_tree_node
compilation_unit->syntax_tree_node
string file_name
List<compiler_directive> compiler_directives
LanguageId Language
string Language
unit_module->compilation_unit
unit_name unit_name

View file

@ -1,9 +1,10 @@
using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
// TODO: рефакторинг для многоязычности EVA
namespace PascalABCCompiler.SyntaxTree
{
/// <summary>
@ -382,7 +383,7 @@ namespace PascalABCCompiler.SyntaxTree
{ }
public override string ToString()
{
return string.Format("{0} {1} {2}", to, OperatorServices.ToString(operator_type, LanguageId.PascalABCNET), from);
return string.Format("{0} {1} {2}", to, OperatorServices.ToString(operator_type, StringConstants.pascalLanguageName), from);
}
public bool first_assignment_defines_type = false;
}
@ -418,7 +419,7 @@ namespace PascalABCCompiler.SyntaxTree
public override string ToString()
{
return string.Format("{0} {2} {1}", left, right, OperatorServices.ToString(operation_type, LanguageId.PascalABCNET));
return string.Format("{0} {2} {1}", left, right, OperatorServices.ToString(operation_type, StringConstants.pascalLanguageName));
}
}
@ -430,7 +431,7 @@ namespace PascalABCCompiler.SyntaxTree
}
public override string ToString()
{
return string.Format("{0} {1}", OperatorServices.ToString(operation_type, LanguageId.PascalABCNET), this.subnode);
return string.Format("{0} {1}", OperatorServices.ToString(operation_type, StringConstants.pascalLanguageName), this.subnode);
}
}
@ -969,7 +970,7 @@ namespace PascalABCCompiler.SyntaxTree
public partial class unit_module
{
public unit_module(LanguageId _Language, unit_name _unit_name, interface_node _interface_part, implementation_node _implementation_part, statement_list _initialization_part, statement_list _finalization_part, SourceContext sc)
public unit_module(string _Language, unit_name _unit_name, interface_node _interface_part, implementation_node _implementation_part, statement_list _initialization_part, statement_list _finalization_part, SourceContext sc)
{
this._Language = _Language;
this._unit_name = _unit_name;
@ -981,7 +982,7 @@ namespace PascalABCCompiler.SyntaxTree
}
}
public partial class program_module
/*public partial class program_module
{
public static program_module create(ident id, uses_list _used_units, block _program_block, using_list _using_namespaces, SourceContext sc = null)
{
@ -1001,7 +1002,7 @@ namespace PascalABCCompiler.SyntaxTree
r.Language = LanguageId.CommonLanguage;
return r;
}
}
}*/
public partial class method_name
{

View file

@ -3,7 +3,6 @@ using System.Collections.Generic;
namespace PascalABCCompiler.SyntaxTree
{
public enum LanguageId { CommonLanguage=32, PascalABCNET=2, C=4, VBNET=8, Oberon00 = 16}
public enum oberon_export_marker {export,export_readonly};
@ -93,9 +92,11 @@ namespace PascalABCCompiler.SyntaxTree
}
return false;
}
public static string ToString(Operators Operator, LanguageId Language)
// TODO: рефакторинг для многоязычности EVA
public static string ToString(Operators Operator, string Language)
{
if (Language == LanguageId.PascalABCNET)
if (Language == StringConstants.pascalLanguageName)
{
switch (Operator)
{
@ -128,48 +129,6 @@ namespace PascalABCCompiler.SyntaxTree
case Operators.Power: return "**";
}
}
else
if (Language == LanguageId.C)
{
switch (Operator)
{
case Operators.Plus: return "+";
case Operators.Minus: return "-";
case Operators.Multiplication: return "*";
case Operators.Division: return "/";
case Operators.ModulusRemainder: return "%";
case Operators.AssignmentAddition: return "+=";
case Operators.AssignmentSubtraction: return "-=";
case Operators.AssignmentMultiplication: return "*=";
case Operators.AssignmentDivision: return "/=";
case Operators.AssignmentModulus: return "%=";
case Operators.PrefixIncrement: return "++";
case Operators.PostfixIncrement: return "++";
case Operators.PrefixDecrement: return "--";
case Operators.PostfixDecrement: return "--";
case Operators.LogicalAND: return "%%";
case Operators.LogicalOR: return "||";
case Operators.Less: return "<";
case Operators.Greater: return ">";
case Operators.LessEqual: return "<=";
case Operators.GreaterEqual: return ">=";
case Operators.Equal: return "==";
case Operators.NotEqual: return "!=";
case Operators.LogicalNOT: return "!";
case Operators.BitwiseLeftShift: return "<<";
case Operators.BitwiseRightShift: return ">>";
case Operators.BitwiseAND: return "&";
case Operators.BitwiseOR: return "|";
case Operators.BitwiseXOR: return "^";
case Operators.BitwiseNOT: return "~";
case Operators.AssignmentBitwiseLeftShift: return "<<=";
case Operators.AssignmentBitwiseRightShift: return ">>=";
case Operators.AssignmentBitwiseAND: return "&=";
case Operators.AssignmentBitwiseOR: return "|=";
case Operators.AssignmentBitwiseXOR: return "^=";
case Operators.Assignment: return "=";
}
}
return null;
}
}

View file

@ -761,7 +761,7 @@
<Fields>
<ExtendedField Name="file_name" Type="string" CreateVariable="false" DeleteVariable="false" />
<ExtendedField Name="compiler_directives" Type="List&lt;compiler_directive&gt;" CreateVariable="true" DeleteVariable="false" />
<ExtendedField Name="Language" Type="LanguageId" CreateVariable="false" DeleteVariable="false" />
<ExtendedField Name="Language" Type="string" CreateVariable="false" DeleteVariable="false" />
</Fields>
<Methods />
<Tags>

View file

@ -1,2 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<ClassDiagram />