From 3527285963d75603afcc75e509e0f75f9b50a769 Mon Sep 17 00:00:00 2001 From: PTKu <61538034+PTKu@users.noreply.github.com> Date: Wed, 1 May 2024 08:45:13 +0200 Subject: [PATCH 1/2] The most significant changes involve the addition of a foreach loop in the `CsOnlinerSourceBuilder` and `CsPlainSourceBuilder` classes, which iterates over the `UsingDirectives` of the `fileSyntax` object and adds each `UsingDirective` to the source code with the appropriate namespace. Additionally, the `class_extended_by_known_type.g.cs` and `file_with_usings.g.cs` files have been updated to include more `using` directives, importing namespaces related to `Simatic.Ax.Stateframework`, `Simatic.Ax.StatePattern`, and various `FileWithUsings` namespaces. 1. The `CsOnlinerSourceBuilder` and `CsPlainSourceBuilder` classes have been updated to include a foreach loop that iterates over the `UsingDirectives` of the `fileSyntax` object. This loop adds each `UsingDirective` to the source code with the appropriate namespace. The `CsOnlinerSourceBuilder` adds the namespace as is, while the `CsPlainSourceBuilder` prepends "Pocos." to the namespace. 2. The `class_extended_by_known_type.g.cs` and `file_with_usings.g.cs` files have been updated to include additional `using` directives. These directives import namespaces related to the `Simatic.Ax.Stateframework`, `Simatic.Ax.StatePattern`, and various `FileWithUsings` namespaces. 3. The `class_extended_by_known_type.g.cs` and `file_with_usings.g.cs` files have also been updated to include additional `using` directives under the `Pocos` namespace. These directives import the same namespaces as above, but under the `Pocos` namespace. --- .../AXSharp.Cs.Compiler/Onliner/CsOnlinerSourceBuilder.cs | 5 +++++ .../src/AXSharp.Cs.Compiler/Plain/CsPlainSourceBuilder.cs | 6 ++++++ .../expected/.g/Onliners/class_extended_by_known_type.g.cs | 2 ++ .../units/expected/.g/Onliners/file_with_usings.g.cs | 3 +++ .../expected/.g/POCO/class_extended_by_known_type.g.cs | 2 ++ .../samples/units/expected/.g/POCO/file_with_usings.g.cs | 3 +++ 6 files changed, 21 insertions(+) diff --git a/src/AXSharp.compiler/src/AXSharp.Cs.Compiler/Onliner/CsOnlinerSourceBuilder.cs b/src/AXSharp.compiler/src/AXSharp.Cs.Compiler/Onliner/CsOnlinerSourceBuilder.cs index 2e571f30..964e6ce3 100644 --- a/src/AXSharp.compiler/src/AXSharp.Cs.Compiler/Onliner/CsOnlinerSourceBuilder.cs +++ b/src/AXSharp.compiler/src/AXSharp.Cs.Compiler/Onliner/CsOnlinerSourceBuilder.cs @@ -55,6 +55,11 @@ public void CreateFile(IFileSyntax fileSyntax, IxNodeVisitor visitor) AddToSource("using System.Collections.Generic;"); AddToSource("using AXSharp.Connector.Localizations;"); + foreach (var fileSyntaxUsingDirective in fileSyntax.UsingDirectives) + { + AddToSource($"using {fileSyntaxUsingDirective.QualifiedIdentifierList.GetText()};"); + } + fileSyntax.Declarations.ToList().ForEach(p => p.Visit(visitor, this)); } diff --git a/src/AXSharp.compiler/src/AXSharp.Cs.Compiler/Plain/CsPlainSourceBuilder.cs b/src/AXSharp.compiler/src/AXSharp.Cs.Compiler/Plain/CsPlainSourceBuilder.cs index fb532a3c..0ed3172a 100644 --- a/src/AXSharp.compiler/src/AXSharp.Cs.Compiler/Plain/CsPlainSourceBuilder.cs +++ b/src/AXSharp.compiler/src/AXSharp.Cs.Compiler/Plain/CsPlainSourceBuilder.cs @@ -179,6 +179,12 @@ public virtual void CreateNamedValueTypeDeclaration(INamedValueTypeDeclaration n public void CreateFile(IFileSyntax fileSyntax, IxNodeVisitor visitor) { AddToSource("using System;"); + + foreach (var fileSyntaxUsingDirective in fileSyntax.UsingDirectives) + { + AddToSource($"using Pocos.{fileSyntaxUsingDirective.QualifiedIdentifierList.GetText()};"); + } + AddToSource("namespace Pocos {"); fileSyntax.Declarations.ToList().ForEach(p => p.Visit(visitor, this)); AddToSource("}"); diff --git a/src/AXSharp.compiler/tests/AXSharp.Compiler.CsTests/samples/units/expected/.g/Onliners/class_extended_by_known_type.g.cs b/src/AXSharp.compiler/tests/AXSharp.Compiler.CsTests/samples/units/expected/.g/Onliners/class_extended_by_known_type.g.cs index e296c211..da3314b6 100644 --- a/src/AXSharp.compiler/tests/AXSharp.Compiler.CsTests/samples/units/expected/.g/Onliners/class_extended_by_known_type.g.cs +++ b/src/AXSharp.compiler/tests/AXSharp.Compiler.CsTests/samples/units/expected/.g/Onliners/class_extended_by_known_type.g.cs @@ -3,6 +3,8 @@ using AXSharp.Connector.ValueTypes; using System.Collections.Generic; using AXSharp.Connector.Localizations; +using Simatic.Ax.Stateframework; +using Simatic.Ax.StatePattern; namespace Simatic.Ax.StateFramework { diff --git a/src/AXSharp.compiler/tests/AXSharp.Compiler.CsTests/samples/units/expected/.g/Onliners/file_with_usings.g.cs b/src/AXSharp.compiler/tests/AXSharp.Compiler.CsTests/samples/units/expected/.g/Onliners/file_with_usings.g.cs index 1f3ba8e0..7960acb2 100644 --- a/src/AXSharp.compiler/tests/AXSharp.Compiler.CsTests/samples/units/expected/.g/Onliners/file_with_usings.g.cs +++ b/src/AXSharp.compiler/tests/AXSharp.Compiler.CsTests/samples/units/expected/.g/Onliners/file_with_usings.g.cs @@ -3,6 +3,9 @@ using AXSharp.Connector.ValueTypes; using System.Collections.Generic; using AXSharp.Connector.Localizations; +using FileWithUsingsSimpleFirstLevelNamespace; +using FileWithUsingsSimpleQualifiedNamespace.Qualified; +using FileWithUsingsHelloLevelOne.FileWithUsingsHelloLevelTwo; namespace FileWithUsingsSimpleFirstLevelNamespace { diff --git a/src/AXSharp.compiler/tests/AXSharp.Compiler.CsTests/samples/units/expected/.g/POCO/class_extended_by_known_type.g.cs b/src/AXSharp.compiler/tests/AXSharp.Compiler.CsTests/samples/units/expected/.g/POCO/class_extended_by_known_type.g.cs index d0264b84..c2efaa77 100644 --- a/src/AXSharp.compiler/tests/AXSharp.Compiler.CsTests/samples/units/expected/.g/POCO/class_extended_by_known_type.g.cs +++ b/src/AXSharp.compiler/tests/AXSharp.Compiler.CsTests/samples/units/expected/.g/POCO/class_extended_by_known_type.g.cs @@ -1,4 +1,6 @@ using System; +using Pocos.Simatic.Ax.Stateframework; +using Pocos.Simatic.Ax.StatePattern; namespace Pocos { diff --git a/src/AXSharp.compiler/tests/AXSharp.Compiler.CsTests/samples/units/expected/.g/POCO/file_with_usings.g.cs b/src/AXSharp.compiler/tests/AXSharp.Compiler.CsTests/samples/units/expected/.g/POCO/file_with_usings.g.cs index cd26fc5e..0d64ca0c 100644 --- a/src/AXSharp.compiler/tests/AXSharp.Compiler.CsTests/samples/units/expected/.g/POCO/file_with_usings.g.cs +++ b/src/AXSharp.compiler/tests/AXSharp.Compiler.CsTests/samples/units/expected/.g/POCO/file_with_usings.g.cs @@ -1,4 +1,7 @@ using System; +using Pocos.FileWithUsingsSimpleFirstLevelNamespace; +using Pocos.FileWithUsingsSimpleQualifiedNamespace.Qualified; +using Pocos.FileWithUsingsHelloLevelOne.FileWithUsingsHelloLevelTwo; namespace Pocos { From 820e5177bc97612f0e684eddefaa09d68cc96b8b Mon Sep 17 00:00:00 2001 From: PTKu <61538034+PTKu@users.noreply.github.com> Date: Wed, 1 May 2024 08:56:17 +0200 Subject: [PATCH 2/2] The most significant changes involve the modification of several methods to change the parameter type from `ISyntaxToken` to `PragmaSyntax`. This includes the `AddToDictionaryIfLocalizedString`, `IsPragmaToken`, and `IsStringToken` methods. Additionally, the `IterateSyntaxTreeForPragmas` method has been uncommented and modified, and the `IterateSyntaxTreeForStringLiterals` method has been commented out. The `IsStringToken` method now checks for different `SyntaxKind` types. 1. The `IterateSyntaxTreeForStringLiterals` method has been commented out, disabling its execution. 2. The `IterateSyntaxTreeForPragmas` method has been uncommented and modified. The `token` variable now refers to `pragmaSyntax` and the `IsAttributeNamePragmaToken` method checks the `PragmaContent` of the `token`. 3. The `AddToDictionaryIfLocalizedString` method's parameter type has been changed from `ISyntaxToken` to `PragmaSyntax`. The `TryToGetLocalizedStrings` method now checks the `PragmaContent` of the `token`. 4. The `pos` variable in the `AddToDictionaryIfLocalizedString` method now gets its value from `token.SourceText.GetLineSpan(token.Span).StartLinePosition`. 5. The `IsPragmaToken` and `IsStringToken` methods' parameter type has been changed from `ISyntaxToken` to `PragmaSyntax`. The check for `SyntaxKind.PragmaToken` in `IsPragmaToken` has been commented out. 6. The `IsStringToken` method now checks if the `SyntaxKind` of the `token` is `TypedStringDToken`, `TypedStringSToken`, `UntypedStringDToken`, or `UntypedStringSToken`. --- src/AXSharp.compiler/src/ixr/Program.cs | 38 ++++++++++++------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/AXSharp.compiler/src/ixr/Program.cs b/src/AXSharp.compiler/src/ixr/Program.cs index c9cc0794..a0129977 100644 --- a/src/AXSharp.compiler/src/ixr/Program.cs +++ b/src/AXSharp.compiler/src/ixr/Program.cs @@ -93,35 +93,35 @@ void Generate(Options o) void IterateSyntaxTreeForStringLiterals(ISyntaxNode root, LocalizedStringWrapper lw, string fileName) { - foreach (var literalSyntax in GetChildNodesRecursive(root).OfType()) - { - var token = literalSyntax.Tokens.First(); - //literalSyntax.Location - AddToDictionaryIfLocalizedString(token,lw,fileName); - } + //foreach (var literalSyntax in GetChildNodesRecursive(root).OfType()) + //{ + // var token = literalSyntax.Tokens.First(); + // //literalSyntax.Location + // AddToDictionaryIfLocalizedString(token,lw,fileName); + //} } void IterateSyntaxTreeForPragmas(ISyntaxNode root, LocalizedStringWrapper lw, string fileName) { - //foreach (var pragmaSyntax in GetChildNodesRecursive(root).OfType()) - //{ - // var token = pragmaSyntax.PragmaToken; - // if(lw.IsAttributeNamePragmaToken(token.Text)) - // { - // AddToDictionaryIfLocalizedString(token,lw,fileName); - // } - //} + foreach (var pragmaSyntax in GetChildNodesRecursive(root).OfType()) + { + var token = pragmaSyntax; + if (lw.IsAttributeNamePragmaToken(token.PragmaContent)) + { + AddToDictionaryIfLocalizedString(token, lw, fileName); + } + } } -void AddToDictionaryIfLocalizedString(ISyntaxToken token, LocalizedStringWrapper lw, string fileName) +void AddToDictionaryIfLocalizedString(PragmaSyntax token, LocalizedStringWrapper lw, string fileName) { // if is valid token if(IsStringToken(token) || IsPragmaToken(token)) { // try to acquire localized string - var localizedStringList = lw.TryToGetLocalizedStrings(token.Text); + var localizedStringList = lw.TryToGetLocalizedStrings(token.PragmaContent); if(localizedStringList == null) { @@ -139,7 +139,7 @@ void AddToDictionaryIfLocalizedString(ISyntaxToken token, LocalizedStringWrapper //check if identifier is valid if(lw.IsValidId(id)) { - var pos = token.Location.GetLineSpan().StartLinePosition; + var pos = token.SourceText.GetLineSpan(token.Span).StartLinePosition; var wrapper = new StringValueWrapper(rawText, fileName, pos.Line); // add id and wrapper to dictionary lw.LocalizedStringsDictionary.TryAdd(id, wrapper); @@ -147,7 +147,7 @@ void AddToDictionaryIfLocalizedString(ISyntaxToken token, LocalizedStringWrapper } } } -bool IsPragmaToken(ISyntaxToken token) +bool IsPragmaToken(PragmaSyntax token) { //if(token.SyntaxKind == SyntaxKind.PragmaToken) //{ @@ -156,7 +156,7 @@ bool IsPragmaToken(ISyntaxToken token) return false; } -bool IsStringToken(ISyntaxToken token) +bool IsStringToken(PragmaSyntax token) { if(token.SyntaxKind == SyntaxKind.TypedStringDToken || token.SyntaxKind == SyntaxKind.TypedStringSToken ||