From 1883c0ffdfa784429229c9c1dd6a87af55e76a61 Mon Sep 17 00:00:00 2001 From: g2384 Date: Mon, 19 Oct 2020 20:16:50 +0100 Subject: [PATCH] fix missing declaration; align values in initialisation --- VHDLFormatter.js | 37 +++++++++++++++++++++++++++++++++++-- VHDLFormatter.ts | 36 +++++++++++++++++++++++++++++++++++- tests/VHDLFormatter.test.ts | 24 ++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 3 deletions(-) diff --git a/VHDLFormatter.js b/VHDLFormatter.js index 6c84b87..4c2307c 100644 --- a/VHDLFormatter.js +++ b/VHDLFormatter.js @@ -1,6 +1,6 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.RemoveAsserts = exports.ApplyNoNewLineAfter = exports.beautify3 = exports.beautifySemicolonBlock = exports.beautifyPackageIsNewBlock = exports.beautifyComponentBlock = exports.beautifyCaseBlock = exports.AlignSign = exports.AlignSigns = exports.beautifyPortGenericBlock = exports.FormattedLineToString = exports.FormattedLine = exports.beautify = exports.BeautifierSettings = exports.signAlignSettings = exports.SetNewLinesAfterSymbols = exports.NewLineSettings = void 0; +exports.RemoveAsserts = exports.ApplyNoNewLineAfter = exports.beautify3 = exports.beautifySemicolonBlock = exports.beautifyVariableInitialiseBlock = exports.beautifyPackageIsNewBlock = exports.beautifyComponentBlock = exports.beautifyCaseBlock = exports.AlignSign = exports.AlignSigns = exports.beautifyPortGenericBlock = exports.FormattedLineToString = exports.FormattedLine = exports.beautify = exports.BeautifierSettings = exports.signAlignSettings = exports.SetNewLinesAfterSymbols = exports.NewLineSettings = void 0; let isTesting = false; const ILEscape = "@@"; const ILCommentPrefix = ILEscape + "comments"; @@ -654,6 +654,24 @@ function beautifyPackageIsNewBlock(inputs, result, settings, startIndex, parentE return [endIndex, parentEndIndex]; } exports.beautifyPackageIsNewBlock = beautifyPackageIsNewBlock; +function beautifyVariableInitialiseBlock(inputs, result, settings, startIndex, parentEndIndex, indent) { + let endIndex = startIndex; + for (let i = startIndex; i < inputs.length; i++) { + if (inputs[i].regexIndexOf(/;(\s|$)/) >= 0) { + endIndex = i; + break; + } + } + result.push(new FormattedLine(inputs[startIndex], indent)); + if (endIndex != startIndex) { + let actualEndIndex = beautify3(inputs, result, settings, startIndex + 1, indent + 1, endIndex); + let incremental = actualEndIndex - endIndex; + endIndex += incremental; + parentEndIndex += incremental; + } + return [endIndex, parentEndIndex]; +} +exports.beautifyVariableInitialiseBlock = beautifyVariableInitialiseBlock; function beautifySemicolonBlock(inputs, result, settings, startIndex, parentEndIndex, indent) { let endIndex = startIndex; [endIndex, parentEndIndex] = getSemicolonBlockEndIndex(inputs, settings, startIndex, parentEndIndex); @@ -749,6 +767,21 @@ function beautify3(inputs, result, settings, startIndex, indent, endIndex) { Mode = modeCache; continue; } + if (input.regexStartsWith(/\w+\s+\w+\s*:.+:\s*=\s*\(([^;]|$)/)) { // 'variable symbol: type [:= initial_value];' + let modeCache = Mode; + Mode = FormatMode.EndsWithSemicolon; + let endsWithBracket = input.regexIndexOf(/:\s*=\s*\(/) > 0; + let startIndex = i; + [i, endIndex] = beautifySemicolonBlock(inputs, result, settings, i, endIndex, indent); + if (endsWithBracket && startIndex != i) { + let fl = result[endIndex]; + if (fl.Line.regexStartsWith(/\);$/)) { + fl.Indent--; + } + } + Mode = modeCache; + continue; + } if (input.regexStartsWith(/\w+\s*:\s*ENTITY/)) { let modeCache = Mode; Mode = FormatMode.EndsWithSemicolon; @@ -770,7 +803,7 @@ function beautify3(inputs, result, settings, startIndex, indent, endIndex) { Mode = modeCache; continue; } - if (input.regexStartsWith(/.*?\:\=\s*\($/)) { + if (input.regexStartsWith(/[\w\s:]*(:=)([\s]|$)/)) { [i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, ":="); continue; } diff --git a/VHDLFormatter.ts b/VHDLFormatter.ts index 1017839..816815d 100644 --- a/VHDLFormatter.ts +++ b/VHDLFormatter.ts @@ -724,6 +724,25 @@ export function beautifyPackageIsNewBlock(inputs: Array, result: (Format return [endIndex, parentEndIndex]; } +export function beautifyVariableInitialiseBlock(inputs: Array, result: (FormattedLine | FormattedLine[])[], settings: BeautifierSettings, startIndex: number, parentEndIndex: number, indent: number): [number, number] { + let endIndex = startIndex; + for (let i = startIndex; i < inputs.length; i++) { + if (inputs[i].regexIndexOf(/;(\s|$)/) >= 0) { + endIndex = i; + break; + } + } + result.push(new FormattedLine(inputs[startIndex], indent)); + if (endIndex != startIndex) { + let actualEndIndex = beautify3(inputs, result, settings, startIndex + 1, indent + 1, endIndex); + let incremental = actualEndIndex - endIndex; + endIndex += incremental; + parentEndIndex += incremental; + } + + return [endIndex, parentEndIndex]; +} + export function beautifySemicolonBlock(inputs: Array, result: (FormattedLine | FormattedLine[])[], settings: BeautifierSettings, startIndex: number, parentEndIndex: number, indent: number): [number, number] { let endIndex = startIndex; [endIndex, parentEndIndex] = getSemicolonBlockEndIndex(inputs, settings, startIndex, parentEndIndex); @@ -821,6 +840,21 @@ export function beautify3(inputs: Array, result: (FormattedLine | Format Mode = modeCache; continue; } + if (input.regexStartsWith(/\w+\s+\w+\s*:.+:\s*=\s*\(([^;]|$)/)) { // 'variable symbol: type [:= initial_value];' + let modeCache = Mode; + Mode = FormatMode.EndsWithSemicolon; + let endsWithBracket = input.regexIndexOf(/:\s*=\s*\(/) > 0; + let startIndex = i; + [i, endIndex] = beautifySemicolonBlock(inputs, result, settings, i, endIndex, indent); + if (endsWithBracket && startIndex != i) { + let fl = result[endIndex] as FormattedLine; + if (fl.Line.regexStartsWith(/\);$/)) { + fl.Indent--; + } + } + Mode = modeCache; + continue; + } if (input.regexStartsWith(/\w+\s*:\s*ENTITY/)) { let modeCache = Mode; Mode = FormatMode.EndsWithSemicolon; @@ -842,7 +876,7 @@ export function beautify3(inputs: Array, result: (FormattedLine | Format Mode = modeCache; continue; } - if (input.regexStartsWith(/.*?\:\=\s*\($/)) { + if (input.regexStartsWith(/[\w\s:]*(:=)([\s]|$)/)) { [i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, ":="); continue; } diff --git a/tests/VHDLFormatter.test.ts b/tests/VHDLFormatter.test.ts index b3eac61..a225c3b 100644 --- a/tests/VHDLFormatter.test.ts +++ b/tests/VHDLFormatter.test.ts @@ -119,6 +119,30 @@ describe('VHDLFormatter', function () { let result = beautify(input, settings); expect(result).toBe(input); }); + + it('support invalid line', function () { + let settings = GetDefaultSettings(); + settings.SignAlignSettings = new signAlignSettings(false, true, "", [], false); + let input = '(5 * 32 - 1 DOWNTO 0) := (\r\n);'; + let result = beautify(input, settings); + expect(result).toBe(input); + }); + + it('align initial values for constant', function () { + let settings = GetDefaultSettings(); + settings.SignAlignSettings = new signAlignSettings(false, true, "", [], false); + let input = 'CONSTANT ADDR_MATCH : STD_LOGIC_VECTOR(5 * 32 - 1 DOWNTO 0) := (\r\n X"00000000" &\r\n X"00010000" &\r\n X"00020000" &\r\n X"00030000" &\r\n X"00040000"\r\n);'; + let result = beautify(input, settings); + expect(result).toBe(input); + }); + + it('one-line initial values for constant', function () { + let settings = GetDefaultSettings(); + settings.SignAlignSettings = new signAlignSettings(false, true, "", [], false); + let input = "CONSTANT Vcc : SIGNAL := '1'; --logic 1 constant\r\nCONSTANT zero4 : bit_vector(0 TO 3) := ('0', '0', '0', '0');\r\nCONSTANT Vcc : SIGNAL := '1'; --logic 1 constant"; + let result = beautify(input, settings); + expect(result).toBe(input); + }); }); function GetDefaultSettings(indentation: string = " "): BeautifierSettings {