Skip to content

Commit d5f800e

Browse files
authored
Merge pull request #234 from Stavegu/fix/parser_module_with_import
Fix/parser module with import
2 parents 16ae1fa + af2fdcf commit d5f800e

File tree

6 files changed

+175
-15
lines changed

6 files changed

+175
-15
lines changed

src/parser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export class SystemVerilogParser {
1616
/(?:automatic\s+)?/,
1717
')',
1818
/(?<name>\w+)/,
19+
/(?<import>\s*import\s*\w+::[\w*]+;)?/,
1920
/(?<params>\s*#\s*\([\w\W]*?\))?/,
2021
/(?<ports>\s*\([\W\w]*?\))?/,
2122
/\s*;/,

src/providers/ModuleInstantiator.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ function isModuleParameterized(symbol: string, container: string): boolean {
108108
// Remove new lines
109109
container = container.replace(/\r\n|\n|\r/g, ' ');
110110
// Surround '#(' with space
111-
container = container.replace(/#\(/g, ' #( ');
111+
container = container.replace(/#\s*\(/g, ' #( ');
112112
// Replace multiple white spaces with a single whitespace
113113
container = container.replace(/\t+/g, ' ');
114114
container = container.replace(/ +/g, ' ');
@@ -118,7 +118,13 @@ function isModuleParameterized(symbol: string, container: string): boolean {
118118
return false;
119119
}
120120

121-
if (keys[0] === symbol && keys[1] === '#(') {
121+
// Get only indexes related to module header
122+
const subkeys = keys.slice(
123+
0,
124+
keys.findIndex((element) => element.includes(';') && !element.includes('::'))
125+
);
126+
127+
if (subkeys[0] === symbol && subkeys.find((element) => element === '#(')) {
122128
return true;
123129
}
124130

src/test/ModuleInstantiator.test.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ suite('ModuleInstantiator Tests', () => {
206206

207207
let fullRange = null;
208208
// Range of the module in the document
209-
fullRange = new vscode.Range(new vscode.Position(300, 6), new vscode.Position(324, 0));
209+
fullRange = new vscode.Range(new vscode.Position(301, 6), new vscode.Position(324, 0));
210210

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

@@ -239,6 +239,46 @@ suite('ModuleInstantiator Tests', () => {
239239

240240
compareInstantiation('azzer', container, instance);
241241
});
242+
243+
test('test #10: formatInstance without parameters and with import', async () => {
244+
let uri = vscode.Uri.file(path.join(__dirname, testFolderLocation, 'test-files', 'ModuleInstantiator.test.1.v')); // prettier-ignore
245+
let document = await vscode.workspace.openTextDocument(uri);
246+
247+
// Range of the module in the document
248+
let fullRange = null;
249+
fullRange = new vscode.Range(new vscode.Position(358, 6), new vscode.Position(392, 0));
250+
251+
const container = document.getText(fullRange).replace(/^\s+|\s+$/g, '');
252+
uri = vscode.Uri.file(path.join(__dirname, testFolderLocation, 'test-files', 'ModuleInstantiator.test.2.v'));
253+
document = await vscode.workspace.openTextDocument(uri);
254+
fullRange = new vscode.Range(new vscode.Position(168, 0), new vscode.Position(179, 0));
255+
256+
let instance = document.getText(fullRange);
257+
258+
compareInstantiation('abber', container, instance);
259+
});
260+
261+
test('test #11: formatInstance with parameters and specific import', async () => {
262+
let uri = vscode.Uri.file(path.join(__dirname, testFolderLocation, 'test-files', 'ModuleInstantiator.test.1.v')); // prettier-ignore
263+
let document = await vscode.workspace.openTextDocument(uri);
264+
265+
let fullRange = null;
266+
// Range of the module in the document
267+
fullRange = new vscode.Range(new vscode.Position(395, 6), new vscode.Position(431, 0));
268+
269+
let container = document.getText(fullRange).replace(/^\s+|\s+$/g, '');
270+
271+
console.log('Container \n\r' + container);
272+
273+
uri = vscode.Uri.file(path.join(__dirname, testFolderLocation, 'test-files', 'ModuleInstantiator.test.2.v'));
274+
document = await vscode.workspace.openTextDocument(uri);
275+
276+
fullRange = new vscode.Range(new vscode.Position(184, 0), new vscode.Position(198, 0));
277+
278+
let instance = document.getText(fullRange);
279+
280+
compareInstantiation('affer', container, instance);
281+
});
242282
});
243283

244284
function compareInstantiation(instance_name, container_name, expected): void {

src/test/indexer_map.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ let symbols: Map<string, Array<SystemVerilogSymbol>>;
1717
const testFolderLocation = '../../src/test';
1818

1919
const uri = Uri.file(path.join(__dirname, testFolderLocation, 'test-files', 'ModuleInstantiator.test.1.v'));
20-
const documentSymbols = ['adder', 'bar', 'akker', 'accer', 'anner', 'atter', 'apper', 'golden'];
20+
const documentSymbols = ['adder', 'bar', 'akker', 'accer', 'anner', 'atter', 'apper', 'golden', 'abber', 'affer'];
2121

2222
const nonSVUri = Uri.file(path.join(__dirname, testFolderLocation, 'test-files', 'foo.txt'));
2323

@@ -31,10 +31,10 @@ suite('indexer_map Tests', () => {
3131
assert.strictEqual(symbols.size, 4);
3232
let count = await indexer.addDocumentSymbols(sVDocument, symbols);
3333

34-
assert.strictEqual(count, 10);
34+
assert.strictEqual(count, 12);
3535
assert.strictEqual(symbols.size, 5);
36-
assert.strictEqual(symbols.get(uri.fsPath).length, 10);
37-
assert.strictEqual(getSymbolsCount(), 23);
36+
assert.strictEqual(symbols.get(uri.fsPath).length, 12);
37+
assert.strictEqual(getSymbolsCount(), 25);
3838

3939
documentSymbols.forEach((symbolName) => {
4040
if (!symbolExists(symbolName)) {
@@ -47,28 +47,28 @@ suite('indexer_map Tests', () => {
4747
assert.strictEqual(count, 0);
4848
assert.strictEqual(symbols.size, 5);
4949
assert.strictEqual(symbols.get(nonSVUri.fsPath), undefined);
50-
assert.strictEqual(getSymbolsCount(), 23);
50+
assert.strictEqual(getSymbolsCount(), 25);
5151

5252
// undefined/null document
5353
count = await indexer.addDocumentSymbols(undefined, symbols);
5454
assert.strictEqual(count, 0);
5555
assert.strictEqual(symbols.size, 5);
56-
assert.strictEqual(getSymbolsCount(), 23);
56+
assert.strictEqual(getSymbolsCount(), 25);
5757

5858
count = await indexer.addDocumentSymbols(sVDocument, undefined);
5959
assert.strictEqual(count, 0);
6060
assert.strictEqual(symbols.size, 5);
61-
assert.strictEqual(getSymbolsCount(), 23);
61+
assert.strictEqual(getSymbolsCount(), 25);
6262

6363
count = await indexer.addDocumentSymbols(undefined, undefined);
6464
assert.strictEqual(count, 0);
6565
assert.strictEqual(symbols.size, 5);
66-
assert.strictEqual(getSymbolsCount(), 23);
66+
assert.strictEqual(getSymbolsCount(), 25);
6767

6868
count = await indexer.addDocumentSymbols(null, symbols);
6969
assert.strictEqual(count, 0);
7070
assert.strictEqual(symbols.size, 5);
71-
assert.strictEqual(getSymbolsCount(), 23);
71+
assert.strictEqual(getSymbolsCount(), 25);
7272
});
7373

7474
test('test #2: removeDocumentSymbols', async () => {
@@ -80,9 +80,9 @@ suite('indexer_map Tests', () => {
8080
assert.strictEqual(symbols.size, 4);
8181
let count = await indexer.addDocumentSymbols(sVDocument, symbols);
8282

83-
assert.strictEqual(count, 10);
83+
assert.strictEqual(count, 12);
8484
assert.strictEqual(symbols.size, 5);
85-
assert.strictEqual(getSymbolsCount(), 23);
85+
assert.strictEqual(getSymbolsCount(), 25);
8686

8787
count = indexer.removeDocumentSymbols(sVDocument.uri.fsPath, symbols);
8888

@@ -92,7 +92,7 @@ suite('indexer_map Tests', () => {
9292
}
9393
});
9494

95-
assert.strictEqual(count, -10);
95+
assert.strictEqual(count, -12);
9696
assert.strictEqual(symbols.size, 4);
9797
assert.strictEqual(getSymbolsCount(), 13);
9898

src/test/test-files/ModuleInstantiator.test.1.v

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,80 @@ module azzer #(parameter SIZE = (2*1)+1,
351351
assign c = tmp_c;
352352

353353
endmodule
354+
355+
// -------------------------------------------------------
356+
// -- Example without parameters and with import
357+
// -------------------------------------------------------
358+
359+
module abber import pa_Package::*; (
360+
input clk,
361+
input reset,
362+
input [3:0] a,
363+
// keep this single comment
364+
input [3:0] b,
365+
/* multiline comment should
366+
be kept*/
367+
input valid,
368+
output [6:0] c
369+
);
370+
371+
reg [6:0] tmp_c;
372+
373+
//Reset
374+
always_ff @(posedge reset)
375+
tmp_c <= pa_adder::RV_C;
376+
377+
`ifdef VERBOSE_RESET
378+
always @(posedge reset) begin
379+
wait(posedge reset);
380+
$display("Reset asserted!")
381+
end
382+
`endif
383+
384+
// Waddition operation
385+
always @(posedge clk)
386+
if(valid) tmp_c <= a + b;
387+
388+
assign c = tmp_c;
389+
390+
endmodule
391+
392+
// -------------------------------------------------------
393+
// -- Example with parameters and specific import
394+
// -------------------------------------------------------
395+
396+
module affer import pa_Package::PARAMETER1; #(
397+
parameter SIZE = PARAMETER1,
398+
parameter SIZE_TWO
399+
)(
400+
input clk,
401+
input reset,
402+
input [3:0] a,
403+
// keep this single comment
404+
input [3:0] b,
405+
/* multiline comment should
406+
be kept*/
407+
input valid,
408+
output [6:0] c
409+
);
410+
411+
reg [6:0] tmp_c;
412+
413+
//Reset
414+
always_ff @(posedge reset)
415+
tmp_c <= pa_adder::RV_C;
416+
417+
`ifdef VERBOSE_RESET
418+
always @(posedge reset) begin
419+
wait(posedge reset);
420+
$display("Reset asserted!")
421+
end
422+
`endif
423+
424+
// Waddition operation
425+
always @(posedge clk)
426+
if(valid) tmp_c <= a + b;
427+
428+
assign c = tmp_c;
429+
430+
endmodule

src/test/test-files/ModuleInstantiator.test.2.v

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,42 @@ azzer #(
162162
.c (c)
163163
);
164164

165+
// -------------------------------------------------------
166+
// -- Example without parameters and with import
167+
// -------------------------------------------------------
168+
169+
abber u_abber (
170+
.clk (clk),
171+
.reset (reset),
172+
.a (a),
173+
// keep this single comment
174+
.b (b),
175+
/* multiline comment should
176+
be kept*/
177+
.valid (valid),
178+
.c (c)
179+
);
180+
181+
// -------------------------------------------------------
182+
// -- Example with parameters and specific import
183+
// -------------------------------------------------------
184+
185+
affer #(
186+
.SIZE (PARAMETER1),
187+
.SIZE_TWO (SIZE_TWO)
188+
) u_affer (
189+
.clk (clk),
190+
.reset (reset),
191+
.a (a),
192+
// keep this single comment
193+
.b (b),
194+
/* multiline comment should
195+
be kept*/
196+
.valid (valid),
197+
.c (c)
198+
);
199+
200+
165201
// -------------------------------------------------------
166202
// -- End file
167203
// -------------------------------------------------------

0 commit comments

Comments
 (0)