You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by "TomNewChao (via GitHub)" <gi...@apache.org> on 2026/09/28 14:51:35 UTC

[PR] feat(plc4net): port the mspec code generator to C#

TomNewChao opened a new pull request, #2783:
URL: https://github.com/apache/plc4x/pull/2783

   Third slice of the plc4net revival tracked in #2656, following the merged
   #2761, #2771 and #2774.
   
   This ports the Java mspec code generator (protocol-base-mspec) to C# as
   plc4net/tools/code-gen, so driver slices can generate their read/write model
   without a JDK.
   
   - The generator shares the mspec language with the Java toolchain.
     Expression.g4 is adapted from the Unlicense-licensed original grammar
     (noted in LICENSE); the ANTLR C# output is checked in, so day-to-day work
     needs no JDK, and every checked-in artifact carries the ASF header.
   - It consumes the same ParserSerializerTestsuite XML that plc4j and plc4go
     use.
   - The C# emitter covers enums with parameter accessors and reverse lookups,
     discriminated types, dataIo value shapes, and the string/array/padding/
     checksum/manual/reserved/virtual/optional/implicit field kinds.
   - Generation fails loudly instead of emitting silently wrong drivers:
     duplicate type names, little-endian byteOrder (not implemented yet), enum
     fields keyed on an accessor the enum does not declare, unknown base types,
     and an empty mspec file set are rejected; unparseable expressions are
     reported on stderr.
   - A member-keyed enum accessor keeps every arm even where underlying
     constants collide (S7's TransportSize has COUNTER and DATE_AND_TIME both
     at 0x1C).
   
   Nullable-reference clean under the repo's Nullable=enable +
   TreatWarningsAsErrors baseline. 249 tests pass, including full-model
   generation from the in-tree modbus, s7 and knxnetip mspecs and the shared
   testsuite runner. Refreshing generated sources is a manual step for now; the
   CI wiring lands with the driver slices that consume them.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@plc4x.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(plc4net): add the generated C# parsers for the mspec and expression grammars

Posted by "sruehl (via GitHub)" <gi...@apache.org>.
sruehl merged PR #2783:
URL: https://github.com/apache/plc4x/pull/2783


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@plc4x.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(plc4net): add the generated C# parsers for the mspec and expression grammars

Posted by "TomNewChao (via GitHub)" <gi...@apache.org>.
TomNewChao commented on code in PR #2783:
URL: https://github.com/apache/plc4x/pull/2783#discussion_r4131685956


##########
plc4net/tools/code-gen/code-gen.csproj:
##########
@@ -0,0 +1,55 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+      https://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing,
+  software distributed under the License is distributed on an
+  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  KIND, either express or implied.  See the License for the
+  specific language governing permissions and limitations
+  under the License.
+-->
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Library</OutputType>
+    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
+    <AssemblyName>plc4net-code-gen</AssemblyName>
+    <Title>PLC4Net: Code Generation</Title>
+    <RootNamespace>org.apache.plc4net.tools.codegen</RootNamespace>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <!--
+      CS3021: ANTLR emits [CLSCompliant(false)] on every generated lexer, parser
+      and listener, but this assembly declared no [assembly: CLSCompliant] at
+      all, which made every one of those attributes redundant and raised one
+      warning per generated type. Declaring the assembly non-CLS-compliant
+      (false) states exactly what the generated code already claims and removes
+      the redundancy at its source: no generated file is hand-edited and no
+      diagnostic is suppressed.
+    -->
+    <AssemblyAttribute Include="System.CLSCompliantAttribute">
+      <_Parameter1>false</_Parameter1>
+      <_Parameter1_IsLiteral>true</_Parameter1_IsLiteral>
+    </AssemblyAttribute>
+  </ItemGroup>
+
+  <ItemGroup>
+    <!--
+      The checked-in parsers were generated by the ANTLR 4.13.2 tool;
+      4.13.1 is the latest Antlr4.Runtime.Standard package published on
+      NuGet.
+    -->
+    <PackageReference Include="Antlr4.Runtime.Standard" Version="4.13.1" />

Review Comment:
   Addressed in 66e590a24. `generate_parsers.py` pins ANTLR 4.13.2 and its SHA-256, invokes both shared grammars with the C# namespace/listener options, inserts the ASF headers into `.cs` and `.tokens` artifacts, removes `.interp` files, and provides `--check` for reproducibility verification. The check regenerates every artifact in a temporary directory and currently passes against the checked-in files.



##########
plc4net/tools/code-gen/src/MSpecLexer.Predicates.cs:
##########
@@ -0,0 +1,34 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//      https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+namespace org.apache.plc4net.tools.codegen.grammar
+{
+    /// <summary>
+    /// The mspec lexer's <c>EmptyLine</c> rule starts with the semantic predicate
+    /// <c>{getCharPositionInLine() == 0}?</c> in the grammar under
+    /// <c>code-generation/protocol-base-mspec</c>. ANTLR copies predicate text into
+    /// the generated code unchanged, so this part of the (partial) lexer supplies
+    /// that method - the Java runtime's name for the C# runtime's <c>Column</c> -
+    /// and the grammar can be used exactly as it is upstream.
+    /// </summary>
+    public partial class MSpecLexer
+    {
+        private int getCharPositionInLine() => Column;

Review Comment:
   Addressed in 66e590a24. The new `code-gen-test` project references `code-gen` and parses all three existing MSpec fixtures plus representative expressions. A dedicated indented-empty-line test asserts that the compatibility predicate emits `EmptyLine`; the parsing helpers assert zero lexer/parser errors and EOF consumption. All 8 new tests pass, together with the existing 184 PLC4Net tests.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@plc4x.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(plc4net): add the mspec and expression grammars with their generated C# parsers

Posted by "TomNewChao (via GitHub)" <gi...@apache.org>.
TomNewChao commented on PR #2783:
URL: https://github.com/apache/plc4x/pull/2783#issuecomment-5883049807

   @sruehl Could you take a look at this when you get a chance? It adds the mspec and expression grammars with their generated C# parsers, the front-end for the .NET code generator port. Happy to address any feedback.
   
   The dotnet CI workflow on this PR is waiting for a committer's approval before it can run.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@plc4x.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(plc4net): add the generated C# parsers for the mspec and expression grammars

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #2783:
URL: https://github.com/apache/plc4x/pull/2783#discussion_r4131515055


##########
plc4net/tools/code-gen/code-gen.csproj:
##########
@@ -0,0 +1,55 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+      https://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing,
+  software distributed under the License is distributed on an
+  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  KIND, either express or implied.  See the License for the
+  specific language governing permissions and limitations
+  under the License.
+-->
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Library</OutputType>
+    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
+    <AssemblyName>plc4net-code-gen</AssemblyName>
+    <Title>PLC4Net: Code Generation</Title>
+    <RootNamespace>org.apache.plc4net.tools.codegen</RootNamespace>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <!--
+      CS3021: ANTLR emits [CLSCompliant(false)] on every generated lexer, parser
+      and listener, but this assembly declared no [assembly: CLSCompliant] at
+      all, which made every one of those attributes redundant and raised one
+      warning per generated type. Declaring the assembly non-CLS-compliant
+      (false) states exactly what the generated code already claims and removes
+      the redundancy at its source: no generated file is hand-edited and no
+      diagnostic is suppressed.
+    -->
+    <AssemblyAttribute Include="System.CLSCompliantAttribute">
+      <_Parameter1>false</_Parameter1>
+      <_Parameter1_IsLiteral>true</_Parameter1_IsLiteral>
+    </AssemblyAttribute>
+  </ItemGroup>
+
+  <ItemGroup>
+    <!--
+      The checked-in parsers were generated by the ANTLR 4.13.2 tool;
+      4.13.1 is the latest Antlr4.Runtime.Standard package published on
+      NuGet.
+    -->
+    <PackageReference Include="Antlr4.Runtime.Standard" Version="4.13.1" />

Review Comment:
   The tool and runtime versions are recorded, but the repository has no command or target that reproduces these checked-in artifacts. A raw ANTLR run does not encode the required C# namespace or the post-generated ASF headers, so future grammar updates cannot reliably regenerate the same files. Please add a pinned generation script/target that captures both grammar invocations, output/package options, header insertion, and `.interp` cleanup.



##########
plc4net/tools/code-gen/src/MSpecLexer.Predicates.cs:
##########
@@ -0,0 +1,34 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//      https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+namespace org.apache.plc4net.tools.codegen.grammar
+{
+    /// <summary>
+    /// The mspec lexer's <c>EmptyLine</c> rule starts with the semantic predicate
+    /// <c>{getCharPositionInLine() == 0}?</c> in the grammar under
+    /// <c>code-generation/protocol-base-mspec</c>. ANTLR copies predicate text into
+    /// the generated code unchanged, so this part of the (partial) lexer supplies
+    /// that method - the Java runtime's name for the C# runtime's <c>Column</c> -
+    /// and the grammar can be used exactly as it is upstream.
+    /// </summary>
+    public partial class MSpecLexer
+    {
+        private int getCharPositionInLine() => Column;

Review Comment:
   This handwritten runtime shim is not exercised by any PLC4Net test: no test project references `code-gen`. A generation or runtime change could therefore leave the project compiling while whitespace-only lines are tokenized incorrectly. Please add parser smoke tests that parse the existing MSpec fixtures and representative expression strings, including an indented empty line that reaches this predicate, and assert both EOF consumption and zero lexer/parser errors.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@plc4x.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(plc4net): add the generated C# parsers for the mspec and expression grammars

Posted by "TomNewChao (via GitHub)" <gi...@apache.org>.
TomNewChao commented on PR #2783:
URL: https://github.com/apache/plc4x/pull/2783#issuecomment-5886129088

   @sruehl Thanks for the review, and you are right: we need one shared MSpec.g4, not copies.
   
   I would like to explain why I made a copy in the first place: I wanted every language maintainer to have their own MSpec.g4, so that the languages could be split apart more easily later. Looking further ahead, though, I think MSpec.g4 should be independent of every programming language.
   
   As things stand, the approach you suggested is the best one, and I have changed the PR accordingly: it now references the MSpec.g4 in the Java module (together with the Expression.g4 next to it) and generates the parsers from it, unchanged: https://github.com/apache/plc4x/blob/develop/code-generation/protocol-base-mspec/src/main/antlr4/org/apache/plc4x/plugins/codegenerator/language/mspec/MSpec.g4
   
   Thank you for the suggestion.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@plc4x.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org