Browse Source

fix missing declaration; align values in initialisation

master
g2384 4 years ago
parent
commit
1883c0ffdf
3 changed files with 94 additions and 3 deletions
  1. +35
    -2
      VHDLFormatter.js
  2. +35
    -1
      VHDLFormatter.ts
  3. +24
    -0
      tests/VHDLFormatter.test.ts

+ 35
- 2
VHDLFormatter.js View File

@ -1,6 +1,6 @@
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); 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; let isTesting = false;
const ILEscape = "@@"; const ILEscape = "@@";
const ILCommentPrefix = ILEscape + "comments"; const ILCommentPrefix = ILEscape + "comments";
@ -654,6 +654,24 @@ function beautifyPackageIsNewBlock(inputs, result, settings, startIndex, parentE
return [endIndex, parentEndIndex]; return [endIndex, parentEndIndex];
} }
exports.beautifyPackageIsNewBlock = beautifyPackageIsNewBlock; 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) { function beautifySemicolonBlock(inputs, result, settings, startIndex, parentEndIndex, indent) {
let endIndex = startIndex; let endIndex = startIndex;
[endIndex, parentEndIndex] = getSemicolonBlockEndIndex(inputs, settings, startIndex, parentEndIndex); [endIndex, parentEndIndex] = getSemicolonBlockEndIndex(inputs, settings, startIndex, parentEndIndex);
@ -749,6 +767,21 @@ function beautify3(inputs, result, settings, startIndex, indent, endIndex) {
Mode = modeCache; Mode = modeCache;
continue; 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/)) { if (input.regexStartsWith(/\w+\s*:\s*ENTITY/)) {
let modeCache = Mode; let modeCache = Mode;
Mode = FormatMode.EndsWithSemicolon; Mode = FormatMode.EndsWithSemicolon;
@ -770,7 +803,7 @@ function beautify3(inputs, result, settings, startIndex, indent, endIndex) {
Mode = modeCache; Mode = modeCache;
continue; continue;
} }
if (input.regexStartsWith(/.*?\:\=\s*\($/)) {
if (input.regexStartsWith(/[\w\s:]*(:=)([\s]|$)/)) {
[i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, ":="); [i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, ":=");
continue; continue;
} }


+ 35
- 1
VHDLFormatter.ts View File

@ -724,6 +724,25 @@ export function beautifyPackageIsNewBlock(inputs: Array<string>, result: (Format
return [endIndex, parentEndIndex]; return [endIndex, parentEndIndex];
} }
export function beautifyVariableInitialiseBlock(inputs: Array<string>, 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<string>, result: (FormattedLine | FormattedLine[])[], settings: BeautifierSettings, startIndex: number, parentEndIndex: number, indent: number): [number, number] { export function beautifySemicolonBlock(inputs: Array<string>, result: (FormattedLine | FormattedLine[])[], settings: BeautifierSettings, startIndex: number, parentEndIndex: number, indent: number): [number, number] {
let endIndex = startIndex; let endIndex = startIndex;
[endIndex, parentEndIndex] = getSemicolonBlockEndIndex(inputs, settings, startIndex, parentEndIndex); [endIndex, parentEndIndex] = getSemicolonBlockEndIndex(inputs, settings, startIndex, parentEndIndex);
@ -821,6 +840,21 @@ export function beautify3(inputs: Array<string>, result: (FormattedLine | Format
Mode = modeCache; Mode = modeCache;
continue; 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/)) { if (input.regexStartsWith(/\w+\s*:\s*ENTITY/)) {
let modeCache = Mode; let modeCache = Mode;
Mode = FormatMode.EndsWithSemicolon; Mode = FormatMode.EndsWithSemicolon;
@ -842,7 +876,7 @@ export function beautify3(inputs: Array<string>, result: (FormattedLine | Format
Mode = modeCache; Mode = modeCache;
continue; continue;
} }
if (input.regexStartsWith(/.*?\:\=\s*\($/)) {
if (input.regexStartsWith(/[\w\s:]*(:=)([\s]|$)/)) {
[i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, ":="); [i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, ":=");
continue; continue;
} }


+ 24
- 0
tests/VHDLFormatter.test.ts View File

@ -119,6 +119,30 @@ describe('VHDLFormatter', function () {
let result = beautify(input, settings); let result = beautify(input, settings);
expect(result).toBe(input); 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 { function GetDefaultSettings(indentation: string = " "): BeautifierSettings {


Loading…
Cancel
Save