Skip to content

Commit

Permalink
Merge pull request #212 from eirikpre/revert-198-master
Browse files Browse the repository at this point in the history
Revert "Fixed autoinstantiation tripping on brackets and unpacked dimensions"
  • Loading branch information
joecrop authored Jan 6, 2024
2 parents fa3ab4d + deab5a9 commit a3d0c10
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 176 deletions.
63 changes: 13 additions & 50 deletions src/providers/ModuleInstantiator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,6 @@ function cleanUpContainer(container: string): string {
container = container.replace(/=/g, ' = ');
container = container.replace(/\(/g, ' ( ');
container = container.replace(/\)/g, ' ) ');
container = container.replace(/\[/g, ' [ ');
container = container.replace(/\]/g, ' ] ');
container = container.replace(/\/\//g, ' // ');
container = container.replace(/\/\*/g, ' /* ');

Expand Down Expand Up @@ -328,7 +326,7 @@ function findMaxLength(container: string, moduleIsParameterized: boolean): numbe
let lastPort: string = undefined; // eslint-disable-line no-undef-init
let lastParameter: string = undefined; // eslint-disable-line no-undef-init
let passedEqualSign = false;
let numBracketPast = 0;

let state = ProcessingState.INITIAL;

for (let i = 0; i < keys.length; i++) {
Expand All @@ -353,13 +351,7 @@ function findMaxLength(container: string, moduleIsParameterized: boolean): numbe
}
} else if (state === ProcessingState.PARAMETERS) {
if (keys[i] === ')') {
if (numBracketPast === 0) {
state = ProcessingState.PORTS;
} else {
numBracketPast -= 1;
}
} else if (keys[i] === '(') {
numBracketPast += 1;
state = ProcessingState.PORTS;
} else if (keys[i] === ',' && lastParameter) {
maxLength = Math.max(lastParameter.length, maxLength);
lastParameter = undefined;
Expand All @@ -377,19 +369,13 @@ function findMaxLength(container: string, moduleIsParameterized: boolean): numbe
lastParameter = undefined;
}

if (keys[i] === ')' && numBracketPast == 0) {
if (keys[i] === ')') {
state = ProcessingState.COMPLETE;
} else if (keys[i] === ',' && lastPort) {
maxLength = Math.max(lastPort.length, maxLength);
lastPort = undefined;
} else if (keys[i] === '[') {
numBracketPast += 1;
} else if (keys[i] === ']') {
numBracketPast -= 1;
} else if (!isPortSymbol(keys[i]) && !isEmptyKey(keys[i])) {
if (numBracketPast == 0) {
lastPort = keys[i].trim();
}
lastPort = keys[i].trim();
}
}

Expand Down Expand Up @@ -430,22 +416,14 @@ function parseContainer(symbol: string, container: string, moduleIsParameterized
}

const output = [];
// Filtering out keys beforehand makes it slightly less time consuming to debug
const keys = container.split(' ').filter((el) => {
if (isEmptyKey(el)) {
return false;
} else {
return true;
}
});
const keys = container.split(' ');

// This is a bit funky, but the for loop below actually checks if these varaibles
// are undefined so it is important that they are initialized as such
let lastPort: string = undefined; // eslint-disable-line no-undef-init
let lastParameter: string = undefined; // eslint-disable-line no-undef-init
let parameterDefault = [];
let lastParameterDefault: string = undefined; // eslint-disable-line no-undef-init

let numBracketPast = 0;
let passedEqualSign = false;

let state = ProcessingState.INITIAL;
Expand All @@ -472,19 +450,14 @@ function parseContainer(symbol: string, container: string, moduleIsParameterized
}
} else if (state === ProcessingState.PARAMETERS) {
if (keys[i] === ')') {
if (numBracketPast === 0) {
state = ProcessingState.PORTS;
} else {
numBracketPast -= 1;
parameterDefault.push(keys[i].trim());
}
state = ProcessingState.PORTS;
} else if (keys[i] === ',' && lastParameter) {
// Set with default value if it exists
if (passedEqualSign) {
output.push(
`${padding}.${lastParameter}${' '.repeat(maxLength - lastParameter.length)}${' '.repeat(4)}(`
);
output.push(`${parameterDefault.join('')})`); // This will butcher default values for strings with spaces included, sorry.
output.push(`${lastParameterDefault})`);

passedEqualSign = false;
} else {
Expand All @@ -499,13 +472,9 @@ function parseContainer(symbol: string, container: string, moduleIsParameterized
lastParameter = undefined;
} else if (keys[i] === '=') {
passedEqualSign = true;
parameterDefault = [];
} else if (!isParameterSymbol(keys[i]) && !isEmptyKey(keys[i])) {
if (passedEqualSign) {
if (keys[i] === '(') {
numBracketPast += 1;
}
parameterDefault.push(keys[i].trim());
lastParameterDefault = keys[i].trim();
} else {
lastParameter = keys[i].trim();
}
Expand All @@ -517,7 +486,7 @@ function parseContainer(symbol: string, container: string, moduleIsParameterized
output.push(
`${padding}.${lastParameter}${' '.repeat(maxLength - lastParameter.length)}${' '.repeat(4)}(`
);
output.push(`${parameterDefault.join('')})\n`);
output.push(`${lastParameterDefault})\n`);

passedEqualSign = false;
} else {
Expand All @@ -531,7 +500,7 @@ function parseContainer(symbol: string, container: string, moduleIsParameterized
lastParameter = undefined;
}

if (keys[i] === ')' && numBracketPast == 0) {
if (keys[i] === ')') {
state = ProcessingState.COMPLETE;
} else if (keys[i] === ',' && lastPort) {
output.push(
Expand All @@ -541,13 +510,7 @@ function parseContainer(symbol: string, container: string, moduleIsParameterized

lastPort = undefined;
} else if (!isPortSymbol(keys[i]) && !isEmptyKey(keys[i])) {
if (keys[i] === '[') {
numBracketPast += 1;
} else if (keys[i] === ']') {
numBracketPast -= 1;
} else if (numBracketPast == 0) {
lastPort = keys[i].trim();
}
lastPort = keys[i].trim();
}
}

Expand All @@ -559,7 +522,7 @@ function parseContainer(symbol: string, container: string, moduleIsParameterized
output.push(
`${padding}.${lastParameter}${' '.repeat(maxLength - lastParameter.length)}${' '.repeat(4)}(`
);
output.push(`${parameterDefault.join('')})\n`);
output.push(`${lastParameterDefault})\n`);

passedEqualSign = false;
} else {
Expand Down
40 changes: 0 additions & 40 deletions src/test/ModuleInstantiator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,46 +199,6 @@ suite('ModuleInstantiator Tests', () => {

compareInstantiation('golden', container, instance);
});

test('test #8: formatInstance with unpacked dimensions and brackets in ports', async () => {
let uri = vscode.Uri.file(path.join(__dirname, testFolderLocation, 'test-files', 'ModuleInstantiator.test.1.v')); // prettier-ignore
let document = await vscode.workspace.openTextDocument(uri);

let fullRange = null;
// Range of the module in the document
fullRange = new vscode.Range(new vscode.Position(301, 6), new vscode.Position(325, 0));

let container = document.getText(fullRange).replace(/^\s+|\s+$/g, '');

uri = vscode.Uri.file(path.join(__dirname, testFolderLocation, 'test-files', 'ModuleInstantiator.test.2.v'));
document = await vscode.workspace.openTextDocument(uri);

fullRange = new vscode.Range(new vscode.Position(138, 0), new vscode.Position(147, 0));

let instance = document.getText(fullRange);

compareInstantiation('arrer', container, instance);
});

test('test #9: formatInstance with brackets in parameter default value', async () => {
let uri = vscode.Uri.file(path.join(__dirname, testFolderLocation, 'test-files', 'ModuleInstantiator.test.1.v')); // prettier-ignore
let document = await vscode.workspace.openTextDocument(uri);

let fullRange = null;
// Range of the module in the document
fullRange = new vscode.Range(new vscode.Position(152, 6), new vscode.Position(164, 0));

let container = document.getText(fullRange).replace(/^\s+|\s+$/g, '');

uri = vscode.Uri.file(path.join(__dirname, testFolderLocation, 'test-files', 'ModuleInstantiator.test.2.v'));
document = await vscode.workspace.openTextDocument(uri);

fullRange = new vscode.Range(new vscode.Position(152, 0), new vscode.Position(164, 0));

let instance = document.getText(fullRange);

compareInstantiation('azzer', container, instance);
});
});

function compareInstantiation(instance_name, container_name, expected): void {
Expand Down
56 changes: 0 additions & 56 deletions src/test/test-files/ModuleInstantiator.test.1.v
Original file line number Diff line number Diff line change
Expand Up @@ -293,59 +293,3 @@ module golden(
assign c = tmp_c;

endmodule


// ---------------------------------------------------------------
// -- Example of ports with unpacked dimensions and brackets
// ---------------------------------------------------------------
module arrer (
input clk,
input reset,
input [(2+2)-1:0] a [2:0],
input [3:0] b [(3-1):0] ,
input valid,
output [6:0] c
);

input clk;
input reset;
input [3:0] a;
// keep this single comment
input [3:0] b;
/* multiline comment should
be kept*/
input valid;
output [6:0] c;

reg [6:0] tmp_c;
assign c = tmp_c;

endmodule

// ---------------------------------------------------------------
// -- Example of parameters with brackets
// ---------------------------------------------------------------
module azzer #(parameter SIZE = (2*1)+1,
parameter SIZE_TWO = 2*(1+1)) (
input clk,
input reset,
input [3:0] a,
input [3:0] b,
input valid,
output [6:0] c
);

input clk;
input reset;
input [3:0] a;
// keep this single comment
input [3:0] b;
/* multiline comment should
be kept*/
input valid;
output [6:0] c;

reg [6:0] tmp_c;
assign c = tmp_c;

endmodule
30 changes: 0 additions & 30 deletions src/test/test-files/ModuleInstantiator.test.2.v
Original file line number Diff line number Diff line change
Expand Up @@ -132,36 +132,6 @@ golden u_golden (
.c (c)
);

// ---------------------------------------------------------------
// -- Example of ports with unpacked dimensions and brackets
// ---------------------------------------------------------------

arrer u_arrer (
.clk (clk),
.reset (reset),
.a (a),
.b (b),
.valid (valid),
.c (c)
);


// ---------------------------------------------------------------
// -- Example of parameters with brackets
// ---------------------------------------------------------------

azzer #(
.SIZE ((2*1)+1),
.SIZE_TWO (2*(1+1))
) u_azzer (
.clk (clk),
.reset (reset),
.a (a),
.b (b),
.valid (valid),
.c (c)
);

// -------------------------------------------------------
// -- End file
// -------------------------------------------------------

0 comments on commit a3d0c10

Please sign in to comment.