Skip to content

Add extension coding to the C# UPER code generation #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
a8499f9
Allow a numbered list in an integer next to constraints
ssharks May 19, 2025
cf656c0
Allow size word between parenthesis
ssharks May 19, 2025
a2afa83
Correctly parse the extensibiltity of elements
ssharks May 19, 2025
0ecc307
Add some corner cases to the test
ssharks May 19, 2025
4160bb2
Move project to .NET9
ssharks May 20, 2025
decaedb
Add a script to generate ASN output for C# testing
ssharks May 22, 2025
b504fe7
Add the isextension option
ssharks May 22, 2025
8ca7d7a
Update ignore .history .vscode and the unpacked bncompiler
ssharks May 22, 2025
92f9543
Avoid duplicate file names
ssharks May 22, 2025
08c3178
Correctly write the isextension option
ssharks May 22, 2025
39d4947
Ordering in encoding of enums
ssharks May 22, 2025
a6d9b84
Make encoding and decoding of enums with extension marker work
ssharks May 23, 2025
febee12
Add sequence with extension encoding and decoding
ssharks May 23, 2025
b0e1033
Allow extended elements in the middle of a sequence
ssharks May 25, 2025
9b44fd4
Implement extensible choice encoding
ssharks May 26, 2025
063fba6
Support extensible integers and size
ssharks May 26, 2025
e09749a
Do not generate assembly attributes in the project file
ssharks May 26, 2025
ecea8a1
Update generated files
ssharks May 26, 2025
9f08993
Add unit test for extensible size
ssharks May 26, 2025
f8860a9
Fix the range for encoding standard enums
ssharks May 26, 2025
ae5d84f
Throw an exception when the index decoding of an extension field prov…
ssharks May 26, 2025
b052f35
Add unit test for CAM messages
ssharks May 26, 2025
9ed7065
Add missing test functions for extensibleSize
ssharks May 26, 2025
4b7c6ef
Use variables to shorten lines
ssharks May 26, 2025
72577d4
Use upper case first letter for enums and avoid assigning a null to a…
ssharks May 27, 2025
080960f
Support multiple input files
ssharks May 27, 2025
839cdf0
Build using gitlab ci/cd
ssharks Jun 3, 2025
4e52387
Remove MVN options
ssharks Jun 3, 2025
406c84f
Update the exmple scripts with the new command line working
ssharks Jun 3, 2025
bc99255
Update the exmple scripts with the new command line working
ssharks Jun 3, 2025
2f224e7
Merge remote-tracking branch 'gitlab/feature/asn_cam_compatibility' i…
ssharks Jun 3, 2025
5672629
Add artifact to gitlab build
ssharks Jun 3, 2025
005389d
Generate code for named integers
ssharks Jun 9, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
/BinaryNotes.NET/BinaryNotesTests/bin
/BinaryNotes.NET/BinaryNotesTests/obj
/BinaryNotes.NET/TestResults
/BinaryNotes.NET/BinaryNotesTests/bncompiler/*
/Dist/target/
nbactions.xml
nb-configuration.xml
/.history/*
/.vscode/*
19 changes: 19 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
stages:
- build

variables:
MAVEN_OPTS: ""
MAVEN_CLI_OPTS: ""

# Use Maven + OpenJDK 17 image from Docker Hub
default:
image: maven:3.9.6-eclipse-temurin-17

build:
stage: build
script:
- mvn $MAVEN_CLI_OPTS package --file pom.xml
artifacts:
paths:
- $CI_PROJECT_DIR/binarynotes/Dist/target/binarynotes-dist-1.6.zip

11 changes: 6 additions & 5 deletions BNCompiler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@
<artifactId>antlr</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>org.lineargs</groupId>
<artifactId>LineArgs</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
Expand All @@ -52,6 +47,12 @@
<version>4.0.4</version>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-cli/commons-cli -->
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.9.0</version>
</dependency>
</dependencies>

<build>
Expand Down
37 changes: 27 additions & 10 deletions BNCompiler/src/main/antlr/org/bn/compiler/parser/ASN1.g
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ embedded_type returns [Object obj]
enum_type returns [Object obj]
{AsnEnum enumtyp = new AsnEnum() ;
AsnNamedNumberList nnlst; obj = null;}
: ( ENUMERATED_KW (nnlst = namedNumber_list { enumtyp.namedNumberList = nnlst;}) )
: ( ENUMERATED_KW (nnlst = namedNumberExtensible_list { enumtyp.namedNumberList = nnlst;}) )
{obj = enumtyp ; enumtyp=null;}
;

Expand All @@ -613,8 +613,8 @@ external_type returns [Object obj]
integer_type returns [Object obj]
{AsnInteger intgr = new AsnInteger();
AsnNamedNumberList numlst; AsnConstraint cnstrnt; obj=null;}
: ( INTEGER_KW (numlst = namedNumber_list {intgr.namedNumberList = numlst;}
| cnstrnt = constraint {intgr.constraint = cnstrnt;})? )
: ( INTEGER_KW (numlst = namedNumber_list {intgr.namedNumberList = numlst;})?
(cnstrnt = constraint {intgr.constraint = cnstrnt;})? )
{obj = intgr ; numlst = null ; cnstrnt = null; intgr = null; }
;

Expand Down Expand Up @@ -661,7 +661,9 @@ sequenceof_type returns [Object obj]
{AsnSequenceOf seqof = new AsnSequenceOf();
AsnConstraint cns; obj = null; Object obj1 ; String s ;}
: ( SEQUENCE_KW {seqof.isSequenceOf = true;}
(SIZE_KW {seqof.isSizeConstraint=true;}(cns = constraint {seqof.constraint = cns ;}))? OF_KW
( (SIZE_KW {seqof.isSizeConstraint=true;}(cns = constraint {seqof.constraint = cns ;}))? |
(L_PAREN SIZE_KW {seqof.isSizeConstraint=true;}(cns = constraint {seqof.constraint = cns ;}) R_PAREN)? )
OF_KW
( obj1 = type
{ if((AsnDefinedType.class).isInstance(obj1)){
seqof.isDefinedType=true;
Expand Down Expand Up @@ -859,8 +861,14 @@ typeorvalue returns [Object obj]

elementType_list returns [AsnElementTypeList elelist]
{elelist = new AsnElementTypeList(); AsnElementType eletyp; }
: (eletyp = elementType {elelist.elements.add(eletyp); }
(COMMA (eletyp = elementType {elelist.elements.add(eletyp);}))*)
: (eletyp = elementType {elelist.addElement(eletyp, false); }
(COMMA (eletyp = elementType {elelist.addElement(eletyp, false);}))*
(
(COMMA ELLIPSIS {elelist.isExtensible=true;}
(COMMA (eletyp = elementType {elelist.addElement(eletyp, true);}))* )?
(COMMA ELLIPSIS {elelist.isExtensible=true;}
(COMMA (eletyp = elementType {elelist.addElement(eletyp, false);}))*)?
) )
;

elementType returns [AsnElementType eletyp]
Expand All @@ -882,11 +890,20 @@ Object obj; AsnTag tg; String s;}
}
}
;


namedNumberExtensible_list returns [AsnNamedNumberList nnlist]
{nnlist = new AsnNamedNumberList(true);AsnNamedNumber nnum ; }
: ( L_BRACE (nnum= namedNumber {nnlist.addNamedNumber(nnum, false); }) // first element
(COMMA (nnum = namedNumber {nnlist.addNamedNumber(nnum, false); }))* // more elements
((COMMA (ELLIPSIS {nnlist.isExtensible=true;}) ) // extension marker
(COMMA (nnum = namedNumber {nnlist.addNamedNumber(nnum, true); }))*)? // extended elements
R_BRACE )
;

namedNumber_list returns [AsnNamedNumberList nnlist]
{nnlist = new AsnNamedNumberList();AsnNamedNumber nnum ; }
: ( L_BRACE (nnum= namedNumber {nnlist.namedNumbers.add(nnum); })
(COMMA (nnum = namedNumber {nnlist.namedNumbers.add(nnum); }) )* R_BRACE )
: ( L_BRACE (nnum= namedNumber {nnlist.addNamedNumber(nnum, false); })
(COMMA (nnum = namedNumber {nnlist.addNamedNumber(nnum, false); }) )* R_BRACE )
;

namedNumber returns [AsnNamedNumber nnum]
Expand Down Expand Up @@ -922,7 +939,7 @@ element_set_specs[AsnConstraint cnstrnt]
: (elemspec=element_set_spec {
cnstrnt.elemSetSpec=elemspec; // TODO - need list.add() func
}
(COMMA ELLIPSIS {cnstrnt.isCommaDotDot=true;})?
(COMMA ELLIPSIS {cnstrnt.isExtensible=true;})?
(COMMA elemspec=element_set_spec {cnstrnt.addElemSetSpec=elemspec;cnstrnt.isAdditionalElementSpec=true;})?)
;

Expand Down
22 changes: 5 additions & 17 deletions BNCompiler/src/main/java/org/bn/compiler/CompilerArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,16 @@
*/
package org.bn.compiler;

import org.lineargs.Option;
import org.lineargs.constraints.RegexConstraint;

public class CompilerArgs {

@Option(name = "--moduleName", shortName = "-m", description = "Binding module name ('cs' or 'java')")
@RegexConstraint(mask = ".+")
private String moduleName = null;

@Option(name = "--outputDir", shortName = "-o", description = "Output directory name", isOptional = true)
@RegexConstraint(mask = ".+")
private String outputDir = "output/";

@Option(name = "--fileName", shortName = "-f", description = "Input ASN.1 filename")
@RegexConstraint(mask = ".+")
private String inputFileName = null;
private String[] inputFileNames = null;

@Option(name = "--namespace", shortName = "-ns", description = "Generate classes with specified namespace/package name", isOptional = true)
@RegexConstraint(mask = ".+")
private String namespace = null;

@Option(name = "--model-only", shortName = "-x", description = "Generate only the ASN.1 model (as XML)", isOptional = true)
private Boolean generateModelOnly = false;

public String getModuleName() {
Expand All @@ -55,12 +43,12 @@ public void setOutputDir(String outputDir) {
this.outputDir = outputDir;
}

public String getInputFileName() {
return inputFileName;
public String[] getInputFileNames() {
return inputFileNames;
}

public void setInputFileName(String inputFileName) {
this.inputFileName = inputFileName;
public void setInputFileName(String[] inputFileNames) {
this.inputFileNames = inputFileNames;
}

public String getNamespace() {
Expand Down
108 changes: 91 additions & 17 deletions BNCompiler/src/main/java/org/bn/compiler/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@
import org.bn.compiler.parser.ASNParser;
import org.bn.compiler.parser.model.ASN1Model;
import org.bn.compiler.parser.model.ASNModule;
import org.lineargs.LineArgsParser;

import org.apache.commons.cli.*;

public class Main {

private static final String VERSION = "1.6";

private CompilerArgs arguments = null;
private CompilerArgs arguments = new CompilerArgs();

public static void main(String args[]) {
try {
Expand All @@ -51,20 +52,84 @@ public static void main(String args[]) {
}

public void start(String[] args) throws Exception {
LineArgsParser parser = new LineArgsParser();
if (args.length > 0) {
arguments = parser.parse(CompilerArgs.class, args);
Options options = new Options();

Option moduleOption = Option.builder("m")
.longOpt("moduleName")
.hasArg()
.desc("Binding module name ('cs' or 'java')")
.required()
.build();
options.addOption(moduleOption);
Option outputDirOption = Option.builder("o")
.longOpt("outputDir")
.hasArg()
.desc("Output directory name")
.optionalArg(true)
.build();
options.addOption(outputDirOption);
Option namespaceOption = Option.builder("ns")
.longOpt("namespace")
.hasArg()
.desc("Generate classes with specified namespace/package name")
.optionalArg(true)
.build();
options.addOption(namespaceOption);
Option generateModelOnlyOption = Option.builder("x")
.longOpt("model-only")
.desc("Generate only the ASN.1 model (as XML)")
.optionalArg(true)
.build();
options.addOption(generateModelOnlyOption);
Option helpOption = Option.builder("h")
.longOpt("help")
.desc("Show help message")
.build();
options.addOption(helpOption);

CommandLineParser parser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter();
String executable = "bncompiler-" + VERSION + ".jar";

try {
CommandLine cmd = parser.parse(options, args);
// Show help and exit if --help is used
if (cmd.hasOption("h")) {
printHelp(formatter, options, executable);
return;
}

arguments.setModuleName( cmd.getOptionValue("m") );
arguments.setOutputDir( cmd.getOptionValue("o") );
arguments.setNamespace( cmd.getOptionValue("ns") );
arguments.setGenerateModelOnly( cmd.hasOption("x") );
arguments.setInputFileName( cmd.getArgs() ); // Remaining arguments

if (arguments.getInputFileNames() == null || arguments.getInputFileNames().length == 0) {
throw new ParseException("No input files specified.");
}

Module module = new Module(arguments.getModuleName(), arguments.getOutputDir());
startForModule(module);
} else {
parser.printHelp(CompilerArgs.class, System.out);
} catch (ParseException e) {
System.err.println("Error: " + e.getMessage());
printHelp(formatter, options, executable);
System.exit(1);
}
}

private static void printHelp(HelpFormatter formatter, Options options, String executable) {
String usage = executable + " --moduleName <cs|java> --outputDir <output_dir> -ns <namespace> <file1> [file2] ...";
String header = "\nParses a list of files for a specified animal type.\n\nOptions:";
String footer = "\nExample:\n " + executable + "--moduleName cs --outputDir output_ns -ns test_asn test.asn\n\n"
+ " " + executable + " --moduleName java --outputDir output_java -ns test_asn test.asn\n";
formatter.printHelp(usage, header, options, footer, false);
}

private void startForModule(Module module) throws TransformerException, JAXBException, IOException, ANTLRException {
if (!arguments.getGenerateModelOnly()) {
System.out.println("Current directory: " + new File(".").getCanonicalPath());
System.out.println("Compiling file: " + arguments.getInputFileName());
System.out.println("Compiling file(s): " + String.join(", ", arguments.getInputFileNames()));

ByteArrayOutputStream outputXml = new ByteArrayOutputStream(65535);
createModel(outputXml, module);
Expand All @@ -81,7 +146,12 @@ private void createModel(OutputStream outputXml, Module module) throws JAXBExcep
if (arguments.getNamespace() != null) {
model.moduleNS = arguments.getNamespace();
} else {
model.moduleNS = model.module.moduleIdentifier.name.toLowerCase();
for (ASNModule m : model.modules) {
if (m.moduleIdentifier != null && m.moduleIdentifier.name != null) {
model.moduleNS = m.moduleIdentifier.name.toLowerCase();
break;
}
}
}
}

Expand All @@ -92,15 +162,19 @@ private void createModel(OutputStream outputXml, Module module) throws JAXBExcep
}

private ASN1Model createModelFromStream() throws FileNotFoundException, ANTLRException {
InputStream stream = new FileInputStream(arguments.getInputFileName());
ASNLexer lexer = new ASNLexer(stream);
ASNParser parser = new ASNParser(lexer);

ASNModule module = new ASNModule();
parser.module_definition(module);

ASN1Model model = new ASN1Model();
model.module = module;
model.modules = new java.util.ArrayList<>();
for (String inputFile : arguments.getInputFileNames()) {
InputStream stream = new FileInputStream(inputFile);
ASNLexer lexer = new ASNLexer(stream);
ASNParser parser = new ASNParser(lexer);

ASNModule module = new ASNModule();
parser.module_definition(module);

model.modules.add(module);
}

return model;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
@XmlRootElement public class ASN1Model implements Serializable {
public String outputDirectory;
public String moduleNS;
public ASNModule module;
public java.util.ArrayList<ASNModule> modules = new java.util.ArrayList<ASNModule>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class AsnConstraint {
public ElementSetSpec elemSetSpec;
public boolean isAdditionalElementSpec;
public boolean isColonValue;
public boolean isCommaDotDot;
public boolean isExtensible;
public boolean isDefinedValue;
public boolean isElementSetSpecs;
public boolean isExceptionSpec;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class AsnElementType {
public boolean isDefault;
public boolean isDefinedType; // Element type is defined Type
public boolean isOptional;
public boolean isExtended;
public boolean isTag;
public boolean isTagDefault;
public String name; // type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
public class AsnElementTypeList {

public ArrayList<AsnElementType> elements;
public boolean isExtensible;

public AsnElementTypeList() {
elements = new ArrayList<>();
}

public void addElement(AsnElementType element, boolean extendedElement) {
element.isExtended = extendedElement;
elements.add(element);
}

@Override
public String toString() {
return "";
Expand Down
Loading