Skip to content

Commit

Permalink
thread path through all generating functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rmgaray committed Oct 25, 2024
1 parent 9440bef commit 6a63c52
Show file tree
Hide file tree
Showing 25 changed files with 3,716 additions and 1,881 deletions.
5 changes: 3 additions & 2 deletions conway-cddl/codegen/generators/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ export class GenArray extends CodeGeneratorBase {
`;
}

generateDeserialize(reader: string): string {
generateDeserialize(reader: string, path: string): string {
return `
return new ${this.name}(
${reader}.readArray(
reader => ${this.typeUtils.readType("reader", this.item)}
(reader, idx) => ${this.typeUtils.readType("reader", this.item, `[...${path}, "Elem#" + idx]`)}
, ${path}
)
);
`;
Expand Down
6 changes: 3 additions & 3 deletions conway-cddl/codegen/generators/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ export class GenEnum extends CodeGeneratorBase {
);
}

generateDeserialize(reader: string): string {
generateDeserialize(reader: string, path: string): string {
return `
let kind = Number(${reader}.readInt());
let kind = Number(${reader}.readInt(${path}));
${this.values
.map(
(x) => `if(kind == ${x.value}) return new ${this.name}(${x.value})`,
)
.join("\n")}
throw "Unrecognized enum value: " + kind + " for " + ${this.name};
throw "Unrecognized enum value: " + kind + " for " + ${this.name} + "(at " + ${path}.join("/") + ")";
`;
}

Expand Down
10 changes: 5 additions & 5 deletions conway-cddl/codegen/generators/enum_simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ export class GenEnumSimple extends CodeGeneratorBase {
${this.values.map((x) => `${x.name} = ${x.value},`).join("\n")}
}
export function deserialize${this.name}(reader: CBORReader): ${this.name} {
let value = Number(reader.readInt());
export function deserialize${this.name}(reader: CBORReader, path: string[]): ${this.name} {
let value = Number(reader.readInt(path));
switch(value) {
${this.values.map((x) => `case ${x.value}: return ${this.name}.${x.name}`).join("\n")}
}
throw new Error("Invalid value for enum ${this.name}: " + value);
throw new Error("Invalid value for enum ${this.name}: " + value + "(at " + path.join('/') + ")");
}
export function serialize${this.name}(writer: CBORWriter, value: ${this.name}) : void {
Expand All @@ -42,8 +42,8 @@ export class GenEnumSimple extends CodeGeneratorBase {
`;
}

deserialize(reader: string) {
return `deserialize${this.name}(${reader})`;
deserialize(reader: string, path: string) {
return `deserialize${this.name}(${reader}, ${path})`;
}

serialize(writer: string, value: string) {
Expand Down
4 changes: 2 additions & 2 deletions conway-cddl/codegen/generators/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ export class GenHash extends CodeGeneratorBase {
`;
}

generateDeserialize(reader: string): string {
generateDeserialize(reader: string, path: string): string {
return `
return new ${this.name}(${reader}.readBytes());
return new ${this.name}(${reader}.readBytes(${path}));
`;
}

Expand Down
54 changes: 27 additions & 27 deletions conway-cddl/codegen/generators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export class CodeGeneratorBase {
return contents != null ? contents(newName) : newName;
}

deserialize(reader: string): string {
return `${this.name}.deserialize(${reader})`;
deserialize(reader: string, path: string): string {
return `${this.name}.deserialize(${reader}, ${path})`;
}

serialize(writer: string, value: string): string {
Expand All @@ -62,18 +62,18 @@ export class CodeGeneratorBase {
${this.renameMethod(
"from_bytes",
(from_bytes) => `
static ${from_bytes}(data: Uint8Array): ${this.name} {
static ${from_bytes}(data: Uint8Array, path: string[] = ['${this.name}']): ${this.name} {
let reader = new CBORReader(data);
return ${this.deserialize("reader")}
return ${this.deserialize("reader", "path")}
}`,
)}
${this.renameMethod(
"from_hex",
(from_hex) => `
static ${from_hex}(hex_str: string): ${this.name} {
return ${this.name}.${this.renameMethod("from_bytes")}(hexToBytes(hex_str));
static ${from_hex}(hex_str: string, path: string[] = ['${this.name}']): ${this.name} {
return ${this.name}.${this.renameMethod("from_bytes")}(hexToBytes(hex_str), path);
}`,
)}
Expand All @@ -100,8 +100,8 @@ export class CodeGeneratorBase {
${this.renameMethod(
"clone",
(clone) => `
${clone}(): ${this.name} {
return ${this.name}.${this.renameMethod("from_bytes")}(this.${this.renameMethod("to_bytes")}());
${clone}(path: string[]): ${this.name} {
return ${this.name}.${this.renameMethod("from_bytes")}(this.${this.renameMethod("to_bytes")}(), path);
}`,
)}
Expand All @@ -124,7 +124,7 @@ export class CodeGeneratorBase {
return "";
}

generateDeserialize(_reader: string): string {
generateDeserialize(_reader: string, _path: string): string {
return `throw new Error("Not Implemented");`;
}

Expand All @@ -145,9 +145,9 @@ export class CodeGeneratorBase {
let serializeInner: string;
if (this.options.tagged.bytes) {
deserializeInner = `
let innerBytes = reader.readBytes();
let innerBytes = reader.readBytes(path);
let innerReader = new CBORReader(innerBytes);
return ${this.name}.deserializeInner(innerReader);
return ${this.name}.deserializeInner(innerReader, path);
`;
serializeInner = `
let innerWriter = new CBORWriter();
Expand All @@ -156,7 +156,7 @@ export class CodeGeneratorBase {
`;
} else {
deserializeInner = `
return ${this.name}.deserializeInner(reader);
return ${this.name}.deserializeInner(reader, path);
`;
serializeInner = `
this.serializeInner(writer);
Expand All @@ -167,17 +167,17 @@ export class CodeGeneratorBase {
${this.renameMethod(
"deserialize",
(deserialize) => `
static ${deserialize}(reader: CBORReader): ${this.name} {
let taggedTag = reader.readTaggedTag();
static ${deserialize}(reader: CBORReader, path: string[]): ${this.name} {
let taggedTag = reader.readTaggedTag(path);
if (taggedTag != ${this.options.tagged!.tag}) {
throw new Error("Expected tag ${this.options.tagged!.tag}, got " + taggedTag);
throw new Error("Expected tag ${this.options.tagged!.tag}, got " + taggedTag + " (at " + path + ")");
}
${deserializeInner}
}`,
)}
static deserializeInner(reader: CBORReader): ${this.name} {
${this.generateDeserialize("reader")}
static deserializeInner(reader: CBORReader, path: string[]): ${this.name} {
${this.generateDeserialize("reader", "path")}
}`;

serialize = `
Expand All @@ -198,8 +198,8 @@ export class CodeGeneratorBase {
${this.renameMethod(
"deserialize",
(deserialize) => `
static ${deserialize}(reader: CBORReader): ${this.name} {
${this.generateDeserialize("reader")}
static ${deserialize}(reader: CBORReader, path: string[]): ${this.name} {
${this.generateDeserialize("reader", "path")}
}`,
)}
Expand Down Expand Up @@ -275,25 +275,25 @@ export class TypeUtils {
}
}

readType(reader: string, type: string): string {
readType(reader: string, type: string, path: string): string {
let codegen = this.customTypes[type];
if (codegen != null) {
return codegen.deserialize(reader);
return codegen.deserialize(reader, path);
}

switch (type) {
case "string":
return `${reader}.readString()`;
return `${reader}.readString(${path})`;
case "number":
return `Number(${reader}.readInt())`;
return `Number(${reader}.readInt(${path}))`;
case "bigint":
return `${reader}.readInt()`;
return `${reader}.readInt(${path})`;
case "boolean":
return `${reader}.readBoolean()`;
return `${reader}.readBoolean(${path})`;
case "bytes":
return `${reader}.readBytes()`;
return `${reader}.readBytes(${path})`;
case "arrayToUint32Array":
return `new Uint32Array(${reader}.readArray((reader) => Number(reader.readUint())));`;
return `new Uint32Array(${reader}.readArray((reader) => Number(reader.readUint(${path})), ${path}));`;
default:
console.error("Can't decode: " + type);
return `$$CANT_READ("${type}")`;
Expand Down
10 changes: 5 additions & 5 deletions conway-cddl/codegen/generators/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ export class GenMap extends CodeGeneratorBase {
`;
}

generateDeserialize(reader: string): string {
generateDeserialize(reader: string, path: string): string {
return `
let ret = new ${this.name}([]);
${reader}.readMap(
reader => ret.${this.renameMethod("insert")}(
${this.typeUtils.readType("reader", this.key)},
${this.typeUtils.readType("reader", this.value)}
)
(reader, idx) => ret.${this.renameMethod("insert")}(
${this.typeUtils.readType("reader", this.key, `[...${path}, "${this.key}#" + idx]`)},
${this.typeUtils.readType("reader", this.value, `[...${path}, "${this.value}#" + idx]`)}
), path
);
return ret;
`;
Expand Down
4 changes: 2 additions & 2 deletions conway-cddl/codegen/generators/newtype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ export class GenNewtype extends CodeGeneratorBase {
`;
}

generateDeserialize(reader: string): string {
generateDeserialize(reader: string, path: string): string {
return `
return new ${this.name}(${this.typeUtils.readType(reader, this.item)});
return new ${this.name}(${this.typeUtils.readType(reader, this.item, path)});
`;
}

Expand Down
8 changes: 4 additions & 4 deletions conway-cddl/codegen/generators/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ export class GenSet extends CodeGeneratorBase {
`;
}

generateDeserialize(reader: string): string {
generateDeserialize(reader: string, path: string): string {
return `
let ret = new ${this.name}();
if(${reader}.peekType() == "tagged") {
let tag = ${reader}.readTaggedTag();
if(${reader}.peekType(${path}) == "tagged") {
let tag = ${reader}.readTaggedTag(${path});
if(tag != 258) throw new Error("Expected tag 258. Got " + tag);
}
${reader}.readArray(reader => ret.add(${this.typeUtils.readType(reader, this.item)}));
${reader}.readArray((reader, idx) => ret.add(${this.typeUtils.readType(reader, this.item, `[...${path}, '${this.item}#' + idx]`)}), ${path});
return ret;
`;
}
Expand Down
11 changes: 6 additions & 5 deletions conway-cddl/codegen/generators/structured/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,22 @@ export class GenRecord extends GenStructuredBase<Field> {
super(name, customTypes, { genCSL: true, ...options });
}

generateDeserialize(reader: string): string {
generateDeserialize(reader: string, path: string): string {
return `
let len = ${reader}.readArrayTag();
let len = ${reader}.readArrayTag(${path});
if(len != null && len < ${this.getFields().length}) {
throw new Error("Insufficient number of fields in record. Expected ${this.getFields().length}. Received " + len);
throw new Error("Insufficient number of fields in record. Expected ${this.getFields().length}. Received " + len + "(at " + path.join("/"));
}
${this.getFields()
.map(
(x) => `
const ${x.name}_path = [...${path}, '${x.type}(${x.name})'];
let ${x.name} = ${
x.nullable
? `${reader}.readNullable(r => ${this.typeUtils.readType("r", x.type)})?? undefined`
: this.typeUtils.readType(reader, x.type)
? `${reader}.readNullable(r => ${this.typeUtils.readType("r", x.type, `${x.name}_path`)}, ${path})?? undefined`
: this.typeUtils.readType(reader, x.type, `${x.name}_path`)
};`,
)
.join("\n")}
Expand Down
6 changes: 3 additions & 3 deletions conway-cddl/codegen/generators/structured/record_fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ export class GenRecordFragment extends GenStructuredBase<Field> {
this.fragmentEncodeLen = this.fragmentEncodeLen;
}

generateDeserialize(reader: string): string {
generateDeserialize(reader: string, path: string): string {
return `
${this.getFields()
.map(
(x) => `
let ${x.name} = ${
x.nullable
? `${reader}.readNullable(r => ${this.typeUtils.readType("r", x.type)})?? undefined`
: this.typeUtils.readType(reader, x.type)
? `${reader}.readNullable(r => ${this.typeUtils.readType("r", x.type, `[...${path}, '${x.name}']`)}, ${path})?? undefined`
: this.typeUtils.readType(reader, x.type, `[...${path}, '${x.name}']`)
};`,
)
.join("\n")}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export class GenRecordFragmentWrapper extends GenStructuredBase<Item> {
return this.getFields()[0];
}

generateDeserialize(reader: string): string {
generateDeserialize(reader: string, path: string): string {
return `
let ${this.getItem().name} = ${this.getItem().type}.deserialize(${reader});
let ${this.getItem().name} = ${this.getItem().type}.deserialize(${reader}, [...${path}, '${this.getItem().type}']);
return new ${this.name}(${this.getItem().name})
`;
}
Expand Down
12 changes: 7 additions & 5 deletions conway-cddl/codegen/generators/structured/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,26 @@ export class GenStruct extends GenStructuredBase<Field> {
return `
let fields: any = {};
reader.readMap(r => {
let key = Number(r.readUint());
let key = Number(r.readUint(path));
switch(key) {
${this.getFields()
.map(
(x) => `
case ${x.id}:
fields.${x.name} = ${this.typeUtils.readType("r", x.type)};
case ${x.id}: {
const new_path = [...path, '${x.type}(${x.name})']
fields.${x.name} = ${this.typeUtils.readType("r", x.type, "new_path")};
break;
}
`,
)
.join("\n")}
}
});
}, path);
${this.getFields()
.flatMap((x) => [
!x.optional
? `if(fields.${x.name} === undefined) throw new Error("Value not provided for field ${x.id} (${x.name})");`
? `if(fields.${x.name} === undefined) throw new Error("Value not provided for field ${x.id} (${x.name}) (at " + path.join("/") + ")");`
: "",
`let ${x.name} = fields.${x.name};`,
])
Expand Down
Loading

0 comments on commit 6a63c52

Please sign in to comment.