From f77a00c628749372176b0818a1d952b27ec02a2b Mon Sep 17 00:00:00 2001 From: Peter Kurhajec <61538034+PTKu@users.noreply.github.com> Date: Wed, 28 Jan 2026 14:17:48 +0100 Subject: [PATCH] Refactor pragma processing for localized strings Improve detection of localized strings by iterating over leading pragmas in both variable and general declarations. Rename and clarify helper methods, adjust logic to check all tokens, and simplify IsPragmaToken. Also, clean up and reorganize using directives. --- src/AXSharp.compiler/src/ixr/Program.cs | 68 ++++++++++--------- .../src/ixr/Properties/launchSettings.json | 2 +- 2 files changed, 38 insertions(+), 32 deletions(-) diff --git a/src/AXSharp.compiler/src/ixr/Program.cs b/src/AXSharp.compiler/src/ixr/Program.cs index c01e2824..faade798 100644 --- a/src/AXSharp.compiler/src/ixr/Program.cs +++ b/src/AXSharp.compiler/src/ixr/Program.cs @@ -1,24 +1,26 @@ // See https://aka.ms/new-console-template for more information using AX.ST.Semantic; +using AX.ST.Semantic.Model; +using AX.ST.Semantic.Model.Declarations; +using AX.ST.Semantic.Pragmas; using AX.ST.Syntax.Parser; using AX.ST.Syntax.Tree; using AX.Text; using AX.Text.Diagnostics; using AXSharp.Compiler; using AXSharp.ixc_doc; -using System; -using CommandLine; -using System.Reflection; -using System.Text; -using CliWrap; -using System.Runtime.InteropServices; -using System.Runtime.CompilerServices; -using AX.ST.Semantic.Model; using AXSharp.ixr_doc; +using CliWrap; +using CommandLine; using Microsoft.CodeAnalysis; using Serilog.Parsing; -using static System.Net.Mime.MediaTypeNames; +using System; +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Text; using System.Text.RegularExpressions; +using static System.Net.Mime.MediaTypeNames; const string Logo = @@ -94,30 +96,39 @@ void IterateSyntaxTreeForStringLiterals(ISyntaxNode root, LocalizedStringWrapper { foreach (var literalSyntax in GetChildNodesRecursive(root).OfType()) { - var token = literalSyntax.Tokens.First(); AddToDictionaryIfLocalizedStringInLiterals(literalSyntax, lw, fileName); } } - - void IterateSyntaxTreeForPragmas(ISyntaxNode root, LocalizedStringWrapper lw, string fileName) { - foreach (var pragmaSyntax in GetChildNodesRecursive(root).OfType()) + foreach (var storage in GetChildNodesRecursive(root).OfType()) { - var token = pragmaSyntax; - if (lw.IsAttributeNamePragmaToken(token.PragmaContent)) + storage.GetLeadingPragmas().ToList().ForEach(pragmaSyntax => { - AddToDictionaryIfLocalizedString(token, lw, fileName); - } + var token = pragmaSyntax as PragmaSyntax; + if (lw.IsAttributeNamePragmaToken(pragmaSyntax.Content)) + { + AddToDictionaryIfLocalizedStringInPragmas(token, lw, fileName); + } + }); + } + foreach (var storage in GetChildNodesRecursive(root).OfType()) + { + storage.GetLeadingPragmas().ToList().ForEach(pragmaSyntax => + { + var token = pragmaSyntax; + if (lw.IsAttributeNamePragmaToken(pragmaSyntax.Content)) + { + AddToDictionaryIfLocalizedStringInPragmas(token, lw, fileName); + } + }); } } -void AddToDictionaryIfLocalizedString(PragmaSyntax token, LocalizedStringWrapper lw, string fileName) +void AddToDictionaryIfLocalizedStringInPragmas(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.PragmaContent); @@ -142,8 +153,7 @@ void AddToDictionaryIfLocalizedString(PragmaSyntax token, LocalizedStringWrapper // add id and wrapper to dictionary lw.LocalizedStringsDictionary.TryAdd(id, wrapper); } - } - } + } } void AddToDictionaryIfLocalizedStringInLiterals(ILiteralSyntax literal, LocalizedStringWrapper lw, string fileName) @@ -158,9 +168,9 @@ void AddToDictionaryIfLocalizedStringInLiterals(ILiteralSyntax literal, Localize // try to acquire localized string var localizedStringList = lw.TryToGetLocalizedStrings(token.FullText); - if (localizedStringList == null) + if(localizedStringList == null) { - return; + continue; // ✓ Skip to next token } foreach (string localizedString in localizedStringList) @@ -184,12 +194,8 @@ void AddToDictionaryIfLocalizedStringInLiterals(ILiteralSyntax literal, Localize } } bool IsPragmaToken(PragmaSyntax token) -{ - //if(token.SyntaxKind == SyntaxKind.PragmaToken) - //{ - // return true; - //} - return false; +{ + return true; } bool IsStringToken(PragmaSyntax token) diff --git a/src/AXSharp.compiler/src/ixr/Properties/launchSettings.json b/src/AXSharp.compiler/src/ixr/Properties/launchSettings.json index ede5cf37..03000cb5 100644 --- a/src/AXSharp.compiler/src/ixr/Properties/launchSettings.json +++ b/src/AXSharp.compiler/src/ixr/Properties/launchSettings.json @@ -9,7 +9,7 @@ }, "app-withref": { "commandName": "Project", - "workingDirectory": "c:\\W\\Develop\\gh\\inxton\\simatic-ax\\axopen.templates\\axopen.template.simple\\ax\\" + "workingDirectory": "C:\\W\\Develop\\gh\\inxton\\simatic-ax\\axopen.templates\\axopen\\src\\inspectors\\ctrl" } } } \ No newline at end of file