Skip to content

Commit

Permalink
Allow struct to contain member with same name (#133)
Browse files Browse the repository at this point in the history
According to IDL spec, structs members consist of declarators rather
than scoped names. So it's okay for them to have the same name because
the struct member identifiers do not exist outside of the struct scope.

This also mirrors C behavior:
https://stackoverflow.com/questions/15397502/in-c-can-struct-members-use-the-same-name-as-a-type



Fixes: FG-6541 and #132

---------

Co-authored-by: Jacob Bandes-Storch <[email protected]>
  • Loading branch information
snosenzo and jtbandes authored Feb 13, 2024
1 parent e68ca22 commit 586ee40
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// -*- jsonc -*-
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.detectIndentation": false,
Expand Down
8 changes: 4 additions & 4 deletions packages/omgidl-parser/src/IDLNodes/IDLNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,18 @@ function resolveScopedOrLocalNodeReference({
// If using local un-scoped identifier, it will not be found in the definitions map
// In this case we try by building up the namespace prefix until we find a match
let referencedNode = undefined;
const namespacePrefixes = [...scopeOfUsage];
const currPrefix: string[] = [];
// start with most specific scope and work upwards
const currPrefix: string[] = [...scopeOfUsage];
for (;;) {
const identifierToTry = toScopedIdentifier([...currPrefix, usedIdentifier]);
referencedNode = definitionMap.get(identifierToTry);
if (referencedNode != undefined) {
break;
}
if (namespacePrefixes.length === 0) {
if (currPrefix.length === 0) {
break;
}
currPrefix.push(namespacePrefixes.shift()!);
currPrefix.pop();
}

return referencedNode;
Expand Down
24 changes: 23 additions & 1 deletion packages/omgidl-parser/src/parseIDL.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parseIDL as parse } from "./parseIDL";
import { parseIDL as parse, parseIDL } from "./parseIDL";

describe("omgidl parser tests", () => {
it("parses a struct", () => {
Expand Down Expand Up @@ -2597,6 +2597,28 @@ module rosidl_parser {
},
]);
});
it("can parse struct with member of the same name", () => {
const msgDef = `
struct ColorSettings {
uint8 ColorSettings;
};
`;

const ast = parseIDL(msgDef);
expect(ast).toEqual([
{
name: "ColorSettings",
aggregatedKind: "struct",
definitions: [
{
name: "ColorSettings",
isComplex: false,
type: "uint8",
},
],
},
]);
});
// **************** Not supported in our implementation yet
it("cannot compose variable size arrays (no serialization support)", () => {
const msgDef = `
Expand Down
22 changes: 22 additions & 0 deletions packages/omgidl-parser/src/parseIDLToAST.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1775,7 +1775,29 @@ module idl_parser {
},
]);
});
it("can parse struct with member of the same name", () => {
const msgDef = `
struct ColorSettings {
uint8 ColorSettings;
};
`;

const ast = parseIDLToAST(msgDef);
expect(ast).toEqual([
{
name: "ColorSettings",
declarator: "struct",
definitions: [
{
name: "ColorSettings",
isComplex: false,
declarator: "struct-member",
type: "uint8",
},
],
},
]);
});
/**************** Not supported by IDL (as far as I can tell) */
it("cannot parse constants that reference other constants", () => {
const msgDef = `
Expand Down

0 comments on commit 586ee40

Please sign in to comment.