diff --git a/VHDLFormatter.js b/VHDLFormatter.js index 8189e8b..528d7fe 100644 --- a/VHDLFormatter.js +++ b/VHDLFormatter.js @@ -212,18 +212,19 @@ function SetNewLinesAfterSymbols(text, newLineSettings) { } if (newLineSettings.newLineAfter != null) { newLineSettings.newLineAfter.forEach(symbol => { - let regex = new RegExp("(" + symbol.toUpperCase() + ")[ ]?([^ \r\n])", "g"); + let regex = new RegExp("(" + symbol.toUpperCase() + ")[ ]?([^ \r\n@])", "g"); text = text.replace(regex, '$1\r\n$2'); }); } if (newLineSettings.noNewLineAfter != null) { newLineSettings.noNewLineAfter.forEach(symbol => { - let regex = new RegExp("(" + symbol.toUpperCase() + ")[ \r\n]+", "g"); - text = text.replace(regex, '$1 '); + let regex = new RegExp("(" + symbol.toUpperCase() + ")[ \r\n]+([^@])", "g"); + text = text.replace(regex, '$1 $2'); }); } return text; } +exports.SetNewLinesAfterSymbols = SetNewLinesAfterSymbols; class BeautifierSettings { constructor(removeComments, removeReport, checkAlias, signAlign, signAlignAll, keywordCase, indentation, newLineSettings) { this.RemoveComments = removeComments; @@ -240,20 +241,24 @@ exports.BeautifierSettings = BeautifierSettings; let KeyWords = ["ABS", "ACCESS", "AFTER", "ALIAS", "ALL", "AND", "ARCHITECTURE", "ARRAY", "ASSERT", "ATTRIBUTE", "BEGIN", "BLOCK", "BODY", "BUFFER", "BUS", "CASE", "COMPONENT", "CONFIGURATION", "CONSTANT", "CONTEXT", "COVER", "DISCONNECT", "DOWNTO", "DEFAULT", "ELSE", "ELSIF", "END", "ENTITY", "EXIT", "FAIRNESS", "FILE", "FOR", "FORCE", "FUNCTION", "GENERATE", "GENERIC", "GROUP", "GUARDED", "IF", "IMPURE", "IN", "INERTIAL", "INOUT", "IS", "LABEL", "LIBRARY", "LINKAGE", "LITERAL", "LOOP", "MAP", "MOD", "NAND", "NEW", "NEXT", "NOR", "NOT", "NULL", "OF", "ON", "OPEN", "OR", "OTHERS", "OUT", "PACKAGE", "PORT", "POSTPONED", "PROCEDURE", "PROCESS", "PROPERTY", "PROTECTED", "PURE", "RANGE", "RECORD", "REGISTER", "REJECT", "RELEASE", "REM", "REPORT", "RESTRICT", "RESTRICT_GUARANTEE", "RETURN", "ROL", "ROR", "SELECT", "SEQUENCE", "SEVERITY", "SHARED", "SIGNAL", "SLA", "SLL", "SRA", "SRL", "STRONG", "SUBTYPE", "THEN", "TO", "TRANSPORT", "TYPE", "UNAFFECTED", "UNITS", "UNTIL", "USE", "VARIABLE", "VMODE", "VPROP", "VUNIT", "WAIT", "WHEN", "WHILE", "WITH", "XNOR", "XOR"]; let TypeNames = ["BOOLEAN", "BIT", "CHARACTER", "INTEGER", "TIME", "NATURAL", "POSITIVE", "STRING"]; function beautify(input, settings) { - input = RemoveExtraNewLines(input); - input = input.replace(/[\t ]+/g, ' '); - input = input.replace(/\([\t ]+/g, '\('); - input = input.replace(/[ ]+;/g, ';'); - input = input.replace(/:[ ]*(PROCESS|ENTITY)/gi, ':$1'); var arr = input.split("\r\n"); var comments = [], commentsIndex = 0; commentsIndex = EscapeComments(arr, comments, commentsIndex); - input = arr.join("\r\n"); if (settings.RemoveComments) { input = input.replace(/@@comments[0-9]+/g, ''); + commentsIndex = 0; } + input = arr.join("\r\n"); + input = RemoveExtraNewLines(input); + input = input.replace(/[\t ]+/g, ' '); + input = input.replace(/\([\t ]+/g, '\('); + input = input.replace(/[ ]+;/g, ';'); + input = input.replace(/:[ ]*(PROCESS|ENTITY)/gi, ':$1'); input = ReplaceKeyWords(input, KeyWords); input = ReplaceKeyWords(input, TypeNames); + arr = input.split("\r\n"); + ReserveSemicolonInKeywords(arr); + input = arr.join("\r\n"); input = input.replace(/(PORT|PROCESS|GENERIC)[\s]*\(/g, '$1 ('); input = SetNewLinesAfterSymbols(input, settings.NewLineSettings); input = beautify2(input, settings); @@ -271,7 +276,7 @@ function beautify2(input, settings) { if (settings.RemoveAsserts) { RemoveAsserts(arr); //RemoveAsserts must be after EscapeQuotes } - var singleline = [], singlelineIndex = 0; + ApplyNoNewLineAfter(arr, settings.NewLineSettings.noNewLineAfter); var align = [], align_max = [], align_i1 = 0, align_i = 0; var str = "", str1 = ""; var p = 0; @@ -280,14 +285,10 @@ function beautify2(input, settings) { var back_tab = false, forward_tab = false, semi_pos = 0, begin_b = true, port_b = false; var before_begin = true; var l = arr.length; - ApplyNoNewLineAfter(arr, settings.NewLineSettings.noNewLineAfter); for (i = 0; i < l; i++) { if (arr[i].indexOf("BEGIN") >= 0) { before_begin = false; } - if (arr[i].match(/FUNCTION|PROCEDURE/) != null) { - arr[i] = arr[i].replace(/;/g, '@@semicolon'); - } if (port_s) { port_s += arr[i]; var k_port = port_s.split("(").length; @@ -297,7 +298,8 @@ function beautify2(input, settings) { port_b = false; } } - if ((!port_b && arr[i].regexIndexOf(/(\s|\(|^)(PORT|GENERIC|PROCESS|PROCEDURE)(\s|\(|$)/) >= 0) || (arr[i].regexIndexOf(/:[ ]?=[ ]?\(/) >= 0 && before_begin)) { + if ((!port_b && arr[i].regexIndexOf(/(\s|\(|^)(PORT|GENERIC|PROCESS|PROCEDURE)(\s|\(|$)/) >= 0) + || (arr[i].regexIndexOf(/:[ ]?=[ ]?\(/) >= 0 && before_begin)) { port_b = true; port_s = arr[i]; var k_port = port_s.split("(").length; @@ -337,7 +339,7 @@ function beautify2(input, settings) { } } input = input.replace(/(MAP)[ \r\n]+\(/g, '$1('); - input = input.replace(/(;|THEN)[ ]?(@@comments[0-9]+)([a-zA-Z])/g, '$1 $2\r\n$3'); + //input = input.replace(/(;|THEN)[ ]?(@@comments[0-9]+)([a-zA-Z])/g, '$1 $2\r\n$3'); input = input.replace(/[\r\n ]+RETURN/g, ' RETURN'); input = input.replace(/BEGIN[\r\n ]+/g, 'BEGIN\r\n'); input = input.replace(/ (PORT|GENERIC) /g, '\r\n$1 '); @@ -753,6 +755,13 @@ function beautify2(input, settings) { input = input.replace(/@@singleline[ \r\n]*/, " "); return input; } +function ReserveSemicolonInKeywords(arr) { + for (let i = 0; i < arr.length; i++) { + if (arr[i].match(/FUNCTION|PROCEDURE/) != null) { + arr[i] = arr[i].replace(/;/g, '@@semicolon'); + } + } +} function ApplyNoNewLineAfter(arr, noNewLineAfter) { if (noNewLineAfter == null) { return; diff --git a/VHDLFormatter.ts b/VHDLFormatter.ts index 2654c47..a3c247e 100644 --- a/VHDLFormatter.ts +++ b/VHDLFormatter.ts @@ -226,20 +226,20 @@ function SetKeywordCase(input: string, keywordcase: string, keywords, typenames) return input; } -function SetNewLinesAfterSymbols(text: string, newLineSettings: NewLineSettings): string { +export function SetNewLinesAfterSymbols(text: string, newLineSettings: NewLineSettings): string { if (newLineSettings == null) { return text; } if (newLineSettings.newLineAfter != null) { newLineSettings.newLineAfter.forEach(symbol => { - let regex: RegExp = new RegExp("(" + symbol.toUpperCase() + ")[ ]?([^ \r\n])", "g"); + let regex: RegExp = new RegExp("(" + symbol.toUpperCase() + ")[ ]?([^ \r\n@])", "g"); text = text.replace(regex, '$1\r\n$2'); }); } if (newLineSettings.noNewLineAfter != null) { newLineSettings.noNewLineAfter.forEach(symbol => { - let regex: RegExp = new RegExp("(" + symbol.toUpperCase() + ")[ \r\n]+", "g"); - text = text.replace(regex, '$1 '); + let regex: RegExp = new RegExp("(" + symbol.toUpperCase() + ")[ \r\n]+([^@])", "g"); + text = text.replace(regex, '$1 $2'); }); } return text; @@ -272,24 +272,28 @@ let KeyWords: Array = ["ABS", "ACCESS", "AFTER", "ALIAS", "ALL", "AND", let TypeNames: Array = ["BOOLEAN", "BIT", "CHARACTER", "INTEGER", "TIME", "NATURAL", "POSITIVE", "STRING"]; export function beautify(input: string, settings: BeautifierSettings) { - input = RemoveExtraNewLines(input); - input = input.replace(/[\t ]+/g, ' '); - input = input.replace(/\([\t ]+/g, '\('); - input = input.replace(/[ ]+;/g, ';'); - input = input.replace(/:[ ]*(PROCESS|ENTITY)/gi, ':$1'); - var arr = input.split("\r\n"); var comments = [], commentsIndex = 0; commentsIndex = EscapeComments(arr, comments, commentsIndex); - input = arr.join("\r\n"); if (settings.RemoveComments) { input = input.replace(/@@comments[0-9]+/g, ''); + commentsIndex = 0; } + input = arr.join("\r\n"); + input = RemoveExtraNewLines(input); + input = input.replace(/[\t ]+/g, ' '); + input = input.replace(/\([\t ]+/g, '\('); + input = input.replace(/[ ]+;/g, ';'); + input = input.replace(/:[ ]*(PROCESS|ENTITY)/gi, ':$1'); input = ReplaceKeyWords(input, KeyWords); input = ReplaceKeyWords(input, TypeNames); + + arr = input.split("\r\n"); + ReserveSemicolonInKeywords(arr); + input = arr.join("\r\n"); input = input.replace(/(PORT|PROCESS|GENERIC)[\s]*\(/g, '$1 ('); input = SetNewLinesAfterSymbols(input, settings.NewLineSettings); @@ -311,8 +315,8 @@ function beautify2(input, settings: BeautifierSettings): string { RemoveAsserts(arr);//RemoveAsserts must be after EscapeQuotes } - var singleline = [], - singlelineIndex = 0; + ApplyNoNewLineAfter(arr, settings.NewLineSettings.noNewLineAfter); + var align = [], align_max = [], align_i1 = 0, @@ -332,15 +336,12 @@ function beautify2(input, settings: BeautifierSettings): string { port_b = false; var before_begin = true; var l = arr.length; - ApplyNoNewLineAfter(arr, settings.NewLineSettings.noNewLineAfter); + for (i = 0; i < l; i++) { if (arr[i].indexOf("BEGIN") >= 0) { before_begin = false; } - if (arr[i].match(/FUNCTION|PROCEDURE/) != null) { - arr[i] = arr[i].replace(/;/g, '@@semicolon'); - } if (port_s) { port_s += arr[i]; var k_port = port_s.split("(").length; @@ -350,7 +351,8 @@ function beautify2(input, settings: BeautifierSettings): string { port_b = false; } } - if ((!port_b && arr[i].regexIndexOf(/(\s|\(|^)(PORT|GENERIC|PROCESS|PROCEDURE)(\s|\(|$)/) >= 0) || (arr[i].regexIndexOf(/:[ ]?=[ ]?\(/) >= 0 && before_begin)) { + if ((!port_b && arr[i].regexIndexOf(/(\s|\(|^)(PORT|GENERIC|PROCESS|PROCEDURE)(\s|\(|$)/) >= 0) + || (arr[i].regexIndexOf(/:[ ]?=[ ]?\(/) >= 0 && before_begin)) { port_b = true; port_s = arr[i]; var k_port = port_s.split("(").length; @@ -365,7 +367,6 @@ function beautify2(input, settings: BeautifierSettings): string { arr[i] = arr[i].replace(/(PORT|GENERIC|PROCEDURE)([a-z0-9A-Z_ ]+)\(([a-zA-Z0-9_\(\) ]+)/, '$1$2(\r\n$3'); } } - } input = arr.join("\r\n"); input = input.replace(/([a-zA-Z0-9\); ])\);(@@comments[0-9]+)?@@end/g, '$1\r\n);$2@@end'); @@ -390,7 +391,7 @@ function beautify2(input, settings: BeautifierSettings): string { } } input = input.replace(/(MAP)[ \r\n]+\(/g, '$1('); - input = input.replace(/(;|THEN)[ ]?(@@comments[0-9]+)([a-zA-Z])/g, '$1 $2\r\n$3'); + //input = input.replace(/(;|THEN)[ ]?(@@comments[0-9]+)([a-zA-Z])/g, '$1 $2\r\n$3'); input = input.replace(/[\r\n ]+RETURN/g, ' RETURN'); input = input.replace(/BEGIN[\r\n ]+/g, 'BEGIN\r\n'); input = input.replace(/ (PORT|GENERIC) /g, '\r\n$1 '); @@ -804,6 +805,14 @@ function beautify2(input, settings: BeautifierSettings): string { } +function ReserveSemicolonInKeywords(arr: Array) { + for (let i = 0; i < arr.length; i++) { + if (arr[i].match(/FUNCTION|PROCEDURE/) != null) { + arr[i] = arr[i].replace(/;/g, '@@semicolon'); + } + } +} + export function ApplyNoNewLineAfter(arr: Array, noNewLineAfter: Array) { if (noNewLineAfter == null) { return; diff --git a/VHDLFormatterUnitTests.js b/VHDLFormatterUnitTests.js index ccd60c1..8f82e6b 100644 --- a/VHDLFormatterUnitTests.js +++ b/VHDLFormatterUnitTests.js @@ -6,12 +6,29 @@ const VHDLFormatter_3 = require("./VHDLFormatter"); const VHDLFormatter_4 = require("./VHDLFormatter"); const VHDLFormatter_5 = require("./VHDLFormatter"); const VHDLFormatter_6 = require("./VHDLFormatter"); +const VHDLFormatter_7 = require("./VHDLFormatter"); +let testCount = 0; var showUnitTests = true; //window.location.href.indexOf("http") < 0; if (showUnitTests) { + testCount = 0; UnitTest(); UnitTestIndentDecode(); UnitTestRemoveAsserts(); UnitTestApplyNoNewLineAfter(); + UnitTestSetNewLinesAfterSymbols(); + console.log("total tests: " + testCount); +} +function UnitTestSetNewLinesAfterSymbols() { + console.log("=== SetNewLinesAfterSymbols ==="); + let input = "a; @@comments1\r\nb;"; + let expected = "a; @@comments1\r\nb;"; + let parameters = new VHDLFormatter_3.NewLineSettings(); + parameters.newLineAfter = ["then", ";"]; + parameters.noNewLineAfter = ["port", "generic"]; + UnitTest5(VHDLFormatter_7.SetNewLinesAfterSymbols, "no new line after comment", parameters, input, expected); + input = "a; b;"; + expected = "a;\r\nb;"; + UnitTest5(VHDLFormatter_7.SetNewLinesAfterSymbols, "new line after ;", parameters, input, expected); } function UnitTestApplyNoNewLineAfter() { console.log("=== ApplyNoNewLineAfter ==="); @@ -48,6 +65,7 @@ function assert(testName, expected, actual, message) { else { //console.log(testName + " pass"); } + testCount++; } function assertArray(testName, expected, actual, message) { var result = CompareArray(actual, expected); @@ -57,6 +75,11 @@ function assertArray(testName, expected, actual, message) { else { //console.log(testName + " pass"); } + testCount++; +} +function UnitTest5(func, testName, parameters, inputs, expected) { + let actual = func(inputs, parameters); + assert(testName, expected, actual); } function UnitTest4(func, testName, parameters, inputs, expected) { let actual = JSON.parse(JSON.stringify(inputs)); @@ -83,12 +106,12 @@ function UnitTest() { let input = "architecture TB of TB_CPU is\r\n component CPU_IF\r\n port -- port list\r\n end component;\r\n signal CPU_DATA_VALID: std_ulogic;\r\n signal CLK, RESET: std_ulogic := '0';\r\n constant PERIOD : time := 10 ns;\r\n constant MAX_SIM: time := 50 * PERIOD;\r\n begin\r\n -- concurrent statements\r\n end TB;"; let expected = "ARCHITECTURE TB OF TB_CPU IS\r\n COMPONENT CPU_IF\r\n PORT -- port list\r\n END COMPONENT;\r\n SIGNAL CPU_DATA_VALID : std_ulogic;\r\n SIGNAL CLK, RESET : std_ulogic := '0';\r\n CONSTANT PERIOD : TIME := 10 ns;\r\n CONSTANT MAX_SIM : TIME := 50 * PERIOD;\r\nBEGIN\r\n -- concurrent statements\r\nEND TB;"; let actual = VHDLFormatter_1.beautify(input, settings); - console.log("General", CompareString(actual, expected)); + assert("General", expected, actual); let newSettings = deepCopy(settings); newSettings.RemoveComments = true; expected = "ARCHITECTURE TB OF TB_CPU IS\r\n COMPONENT CPU_IF\r\n PORT \r\n END COMPONENT;\r\n SIGNAL CPU_DATA_VALID : std_ulogic;\r\n SIGNAL CLK, RESET : std_ulogic := '0';\r\n CONSTANT PERIOD : TIME := 10 ns;\r\n CONSTANT MAX_SIM : TIME := 50 * PERIOD;\r\nBEGIN\r\nEND TB;"; actual = VHDLFormatter_1.beautify(input, newSettings); - console.log("Remove comments", CompareString(actual, expected)); + assert("Remove comments", expected, actual); let new_line_after_symbols_2 = new VHDLFormatter_3.NewLineSettings(); new_line_after_symbols_2.newLineAfter = []; new_line_after_symbols_2.noNewLineAfter = ["then", ";", "generic", "port"]; @@ -97,51 +120,51 @@ function UnitTest() { expected = "a; b; c;"; input = "a; \r\nb;\r\n c;"; actual = VHDLFormatter_1.beautify(input, newSettings); - console.log("Remove line after ;", CompareString(actual, expected)); + assert("Remove line after ;", expected, actual); newSettings = deepCopy(settings); newSettings.RemoveAsserts = true; input = "architecture arch of ent is\r\nbegin\r\n assert False report sdfjcsdfcsdj;\r\n assert False report sdfjcsdfcsdj severity note;\r\nend architecture;"; expected = "ARCHITECTURE arch OF ent IS\r\nBEGIN\r\nEND ARCHITECTURE;"; actual = VHDLFormatter_1.beautify(input, newSettings); - console.log("Remove asserts", CompareString(actual, expected)); + assert("Remove asserts", expected, actual); input = "entity TB_DISPLAY is\r\n-- port declarations\r\nend TB_DISPLAY;\r\n\r\narchitecture TEST of TB_DISPLAY is\r\n-- signal declarations\r\nbegin\r\n-- component instance(s)\r\nend TEST;"; expected = "ENTITY TB_DISPLAY IS\r\n -- port declarations\r\nEND TB_DISPLAY;\r\n\r\nARCHITECTURE TEST OF TB_DISPLAY IS\r\n -- signal declarations\r\nBEGIN\r\n -- component instance(s)\r\nEND TEST;"; actual = VHDLFormatter_1.beautify(input, settings); - console.log("ENTITY ARCHITECTURE", CompareString(actual, expected)); + assert("ENTITY ARCHITECTURE", expected, actual); newSettings = deepCopy(settings); newSettings.SignAlign = true; input = "port map(\r\ninput_1 => input_1_sig,\r\ninput_2 => input_2_sig,\r\noutput => output_sig\r\n);"; expected = "PORT MAP(\r\n input_1 => input_1_sig, \r\n input_2 => input_2_sig, \r\n output => output_sig\r\n);"; actual = VHDLFormatter_1.beautify(input, newSettings); - console.log("Sign align in PORT", actual == expected); + assert("Sign align in PORT", expected, actual); input = 'if a(3 downto 0) > "0100" then\r\na(3 downto 0) := a(3 downto 0) + "0011" ;\r\nend if ;'; expected = 'IF a(3 DOWNTO 0) > "0100" THEN\r\n a(3 DOWNTO 0) := a(3 DOWNTO 0) + "0011";\r\nEND IF;'; actual = VHDLFormatter_1.beautify(input, settings); - console.log("IF END IF case 1", CompareString(actual, expected)); + assert("IF END IF case 1", expected, actual); input = "if s = '1' then\r\no <= \"010\";\r\nelse\r\no <= \"101\";\r\nend if;"; expected = "IF s = '1' THEN\r\n o <= \"010\";\r\nELSE\r\n o <= \"101\";\r\nEND IF;"; actual = VHDLFormatter_1.beautify(input, settings); - console.log("IF ELSE END IF case 1", actual == expected); + assert("IF ELSE END IF case 1", expected, actual); input = "IF (s = r) THEN rr := '0'; ELSE rr := '1'; END IF;"; expected = "IF (s = r) THEN\r\n rr := '0';\r\nELSE\r\n rr := '1';\r\nEND IF;"; actual = VHDLFormatter_1.beautify(input, settings); - console.log("IF ELSE END IF case 2", actual == expected); + assert("IF ELSE END IF case 2", expected, actual); input = 'P1:process\r\nvariable x: Integer range 1 to 3;\r\nvariable y: BIT_VECTOR (0 to 1);\r\nbegin\r\n C1: case x is\r\n when 1 => Out_1 <= 0;\r\n when 2 => Out_1 <= 1;\r\n end case C1;\r\n C2: case y is\r\n when "00" => Out_2 <= 0;\r\n when "01" => Out_2 <= 1;\r\n end case C2;\r\nend process;'; expected = 'P1 : PROCESS\r\n VARIABLE x : INTEGER RANGE 1 TO 3;\r\n VARIABLE y : BIT_VECTOR (0 TO 1);\r\nBEGIN\r\n C1 : CASE x IS\r\n WHEN 1 => Out_1 <= 0;\r\n WHEN 2 => Out_1 <= 1;\r\n END CASE C1;\r\n C2 : CASE y IS\r\n WHEN "00" => Out_2 <= 0;\r\n WHEN "01" => Out_2 <= 1;\r\n END CASE C2;\r\nEND PROCESS;'; actual = VHDLFormatter_1.beautify(input, settings); - console.log("WHEN CASE", CompareString(actual, expected)); + assert("WHEN CASE", expected, actual); input = "case READ_CPU_STATE is\r\n when WAITING =>\r\n if CPU_DATA_VALID = '1' then\r\n CPU_DATA_READ <= '1';\r\n READ_CPU_STATE <= DATA1;\r\n end if;\r\n when DATA1 =>\r\n -- etc.\r\nend case;"; expected = "CASE READ_CPU_STATE IS\r\n WHEN WAITING => \r\n IF CPU_DATA_VALID = '1' THEN\r\n CPU_DATA_READ <= '1';\r\n READ_CPU_STATE <= DATA1;\r\n END IF;\r\n WHEN DATA1 => \r\n -- etc.\r\nEND CASE;"; actual = VHDLFormatter_1.beautify(input, settings); - console.log("WHEN CASE & IF", CompareString(actual, expected)); + assert("WHEN CASE & IF", expected, actual); input = "entity aa is\r\n port (a : in std_logic;\r\n b : in std_logic;\r\n );\r\nend aa;\r\narchitecture bb of aa is\r\n component cc\r\n port(\r\n a : in std_logic;\r\n b : in std_logic;\r\n );\r\n end cc;\r\n\r\nbegin\r\n C : cc port map (\r\n long => a,\r\n b => b\r\n );\r\nend;"; expected = "ENTITY aa IS\r\n PORT (\r\n a : IN std_logic;\r\n b : IN std_logic;\r\n );\r\nEND aa;\r\nARCHITECTURE bb OF aa IS\r\n COMPONENT cc\r\n PORT (\r\n a : IN std_logic;\r\n b : IN std_logic;\r\n );\r\n END cc;\r\n\r\nBEGIN\r\n C : cc\r\n PORT MAP(\r\n long => a, \r\n b => b\r\n );\r\nEND;"; actual = VHDLFormatter_1.beautify(input, settings); - console.log("PORT MAP", CompareString(actual, expected)); + assert("PORT MAP", expected, actual); input = "entity aa is\r\n port (a : in std_logic;\r\n b : in std_logic;\r\n );\r\n port (a : in std_logic;\r\n b : in std_logic;\r\n );\r\nend aa;\r\narchitecture bb of aa is\r\n component cc\r\n port(\r\n a : in std_logic;\r\n b : in std_logic;\r\n );\r\n port(\r\n a : in std_logic;\r\n b : in std_logic;\r\n );\r\n end cc;\r\n\r\nbegin\r\n C : cc port map (\r\n long => a,\r\n b => b\r\n );\r\n D : cc port map (\r\n long => a,\r\n b => b\r\n );\r\nend;"; expected = "ENTITY aa IS\r\n PORT (\r\n a : IN std_logic;\r\n b : IN std_logic;\r\n );\r\n PORT (\r\n a : IN std_logic;\r\n b : IN std_logic;\r\n );\r\nEND aa;\r\nARCHITECTURE bb OF aa IS\r\n COMPONENT cc\r\n PORT (\r\n a : IN std_logic;\r\n b : IN std_logic;\r\n );\r\n PORT (\r\n a : IN std_logic;\r\n b : IN std_logic;\r\n );\r\n END cc;\r\n\r\nBEGIN\r\n C : cc\r\n PORT MAP(\r\n long => a, \r\n b => b\r\n );\r\n D : cc\r\n PORT MAP(\r\n long => a, \r\n b => b\r\n );\r\nEND;"; actual = VHDLFormatter_1.beautify(input, settings); - console.log("Multiple PORT MAPs", CompareString(actual, expected)); + assert("Multiple PORT MAPs", expected, actual); input = "port (a : in std_logic;\r\n b : in std_logic;\r\n);"; expected = "PORT \r\n(\r\n a : IN std_logic;\r\n b : IN std_logic;\r\n);"; new_line_after_symbols_2 = new VHDLFormatter_3.NewLineSettings(); @@ -149,29 +172,29 @@ function UnitTest() { newSettings = deepCopy(settings); newSettings.NewLineSettings = new_line_after_symbols_2; actual = VHDLFormatter_1.beautify(input, newSettings); - console.log("New line after PORT", CompareString(actual, expected)); + assert("New line after PORT", expected, actual); input = "component a is\r\nport( Data : inout Std_Logic_Vector(7 downto 0););\r\nend component a;"; expected = "COMPONENT a IS\r\n PORT (Data : INOUT Std_Logic_Vector(7 DOWNTO 0););\r\nEND COMPONENT a;"; actual = VHDLFormatter_1.beautify(input, newSettings); - console.log("New line aster PORT (single line)", CompareString(actual, expected)); + assert("New line aster PORT (single line)", expected, actual); input = "process xyx (vf,fr,\r\nde -- comment\r\n)"; expected = "PROCESS xyx (vf, fr, \r\n de -- comment\r\n )"; actual = VHDLFormatter_1.beautify(input, newSettings); - console.log("Align parameters in PROCESS", CompareString(actual, expected)); + assert("Align parameters in PROCESS", expected, actual); input = "architecture a of b is\r\nbegin\r\n process (w)\r\n variable t : std_logic_vector (4 downto 0) ;\r\nbegin\r\n a := (others => '0') ;\r\nend process ;\r\nend a;"; expected = "ARCHITECTURE a OF b IS\r\nBEGIN\r\n PROCESS (w)\r\n VARIABLE t : std_logic_vector (4 DOWNTO 0);\r\n BEGIN\r\n a := (OTHERS => '0');\r\n END PROCESS;\r\nEND a;"; actual = VHDLFormatter_1.beautify(input, newSettings); - console.log("Double BEGIN", CompareString(actual, expected)); + assert("Double BEGIN", expected, actual); let newSettings2 = deepCopy(newSettings); newSettings2.SignAlignAll = true; input = "entity a is\r\n port ( w : in std_logic_vector (7 downto 0) ;\r\n w_s : out std_logic_vector (3 downto 0) ; ) ;\r\nend a ;\r\narchitecture b of a is\r\nbegin\r\n process ( w )\r\n variable t : std_logic_vector (4 downto 0) ;\r\n variable bcd : std_logic_vector (11 downto 0) ;\r\nbegin\r\n b(2 downto 0) := w(7 downto 5) ;\r\n t := w(4 downto 0) ;\r\n w_s <= b(11 downto 8) ;\r\n w <= b(3 downto 0) ;\r\nend process ;\r\nend b ;"; expected = "ENTITY a IS\r\n PORT \r\n (\r\n w : IN std_logic_vector (7 DOWNTO 0);\r\n w_s : OUT std_logic_vector (3 DOWNTO 0); \r\n );\r\nEND a;\r\nARCHITECTURE b OF a IS\r\nBEGIN\r\n PROCESS (w)\r\n VARIABLE t : std_logic_vector (4 DOWNTO 0);\r\n VARIABLE bcd : std_logic_vector (11 DOWNTO 0);\r\n BEGIN\r\n b(2 DOWNTO 0) := w(7 DOWNTO 5);\r\n t := w(4 DOWNTO 0);\r\n w_s <= b(11 DOWNTO 8);\r\n w <= b(3 DOWNTO 0);\r\n END PROCESS;\r\nEND b;"; actual = VHDLFormatter_1.beautify(input, newSettings2); - console.log("Align signs in all places", CompareString(actual, expected)); + assert("Align signs in all places", expected, actual); input = "begin\r\n P0 : process(input)\r\n variable value: Integer;\r\n begin\r\n result(i) := '0';\r\n end process P0;\r\nend behavior;"; expected = "BEGIN\r\n P0 : PROCESS (input)\r\n VARIABLE value : INTEGER;\r\n BEGIN\r\n result(i) := '0';\r\n END PROCESS P0;\r\nEND behavior;"; actual = VHDLFormatter_1.beautify(input, newSettings); - console.log("Indent after Begin", CompareString(actual, expected)); + assert("Indent after Begin", expected, actual); } function CompareString(actual, expected) { var l = Math.min(actual.length, expected.length); diff --git a/VHDLFormatterUnitTests.ts b/VHDLFormatterUnitTests.ts index 55cd12b..8211b9f 100644 --- a/VHDLFormatterUnitTests.ts +++ b/VHDLFormatterUnitTests.ts @@ -4,23 +4,43 @@ import { NewLineSettings } from "./VHDLFormatter"; import { BeautifierSettings } from "./VHDLFormatter"; import { RemoveAsserts } from "./VHDLFormatter"; import { ApplyNoNewLineAfter } from "./VHDLFormatter"; +import { SetNewLinesAfterSymbols } from "./VHDLFormatter"; + +let testCount: number = 0; var showUnitTests = true;//window.location.href.indexOf("http") < 0; if (showUnitTests) { + testCount = 0; UnitTest(); UnitTestIndentDecode(); UnitTestRemoveAsserts(); UnitTestApplyNoNewLineAfter(); + UnitTestSetNewLinesAfterSymbols(); + console.log("total tests: " + testCount); } interface Function { readonly name: string; } +function UnitTestSetNewLinesAfterSymbols() { + console.log("=== SetNewLinesAfterSymbols ==="); + let input = "a; @@comments1\r\nb;" + let expected = "a; @@comments1\r\nb;"; + let parameters: NewLineSettings = new NewLineSettings(); + parameters.newLineAfter = ["then", ";"]; + parameters.noNewLineAfter = ["port", "generic"]; + UnitTest5(SetNewLinesAfterSymbols, "no new line after comment", parameters, input, expected); + + input = "a; b;" + expected = "a;\r\nb;"; + UnitTest5(SetNewLinesAfterSymbols, "new line after ;", parameters, input, expected); +} + function UnitTestApplyNoNewLineAfter() { console.log("=== ApplyNoNewLineAfter ==="); let input: Array = ["a;", "b;"]; - let expected: Array = ["a;@@singleline","b;@@singleline"]; + let expected: Array = ["a;@@singleline", "b;@@singleline"]; let parameters: Array = [";"]; UnitTest4(ApplyNoNewLineAfter, "one blankspace", parameters, input, expected); @@ -57,6 +77,7 @@ function assert(testName, expected, actual, message?) { else { //console.log(testName + " pass"); } + testCount++; } function assertArray(testName, expected, actual, message?) { @@ -67,6 +88,7 @@ function assertArray(testName, expected, actual, message?) { else { //console.log(testName + " pass"); } + testCount++; } type StringCallback = (text: string) => string; @@ -75,6 +97,13 @@ type ArrayCallback = (arr: Array) => void; type Array2Callback = (arr: Array, parameters: Array) => void; +type String2Callback = (text: string, parameters: NewLineSettings) => string; + +function UnitTest5(func: String2Callback, testName: string, parameters: NewLineSettings, inputs, expected: string) { + let actual: string = func(inputs, parameters); + assert(testName, expected, actual); +} + function UnitTest4(func: Array2Callback, testName: string, parameters: Array, inputs: Array, expected: Array) { let actual = JSON.parse(JSON.stringify(inputs)); func(actual, parameters); @@ -104,13 +133,13 @@ function UnitTest() { let input = "architecture TB of TB_CPU is\r\n component CPU_IF\r\n port -- port list\r\n end component;\r\n signal CPU_DATA_VALID: std_ulogic;\r\n signal CLK, RESET: std_ulogic := '0';\r\n constant PERIOD : time := 10 ns;\r\n constant MAX_SIM: time := 50 * PERIOD;\r\n begin\r\n -- concurrent statements\r\n end TB;" let expected = "ARCHITECTURE TB OF TB_CPU IS\r\n COMPONENT CPU_IF\r\n PORT -- port list\r\n END COMPONENT;\r\n SIGNAL CPU_DATA_VALID : std_ulogic;\r\n SIGNAL CLK, RESET : std_ulogic := '0';\r\n CONSTANT PERIOD : TIME := 10 ns;\r\n CONSTANT MAX_SIM : TIME := 50 * PERIOD;\r\nBEGIN\r\n -- concurrent statements\r\nEND TB;"; let actual = beautify(input, settings); - console.log("General", CompareString(actual, expected)); + assert("General", expected, actual); let newSettings = deepCopy(settings); newSettings.RemoveComments = true; expected = "ARCHITECTURE TB OF TB_CPU IS\r\n COMPONENT CPU_IF\r\n PORT \r\n END COMPONENT;\r\n SIGNAL CPU_DATA_VALID : std_ulogic;\r\n SIGNAL CLK, RESET : std_ulogic := '0';\r\n CONSTANT PERIOD : TIME := 10 ns;\r\n CONSTANT MAX_SIM : TIME := 50 * PERIOD;\r\nBEGIN\r\nEND TB;"; actual = beautify(input, newSettings); - console.log("Remove comments", CompareString(actual, expected)); + assert("Remove comments", expected, actual); let new_line_after_symbols_2: NewLineSettings = new NewLineSettings(); new_line_after_symbols_2.newLineAfter = []; @@ -120,61 +149,61 @@ function UnitTest() { expected = "a; b; c;"; input = "a; \r\nb;\r\n c;" actual = beautify(input, newSettings); - console.log("Remove line after ;", CompareString(actual, expected)); + assert("Remove line after ;", expected, actual); newSettings = deepCopy(settings); newSettings.RemoveAsserts = true; input = "architecture arch of ent is\r\nbegin\r\n assert False report sdfjcsdfcsdj;\r\n assert False report sdfjcsdfcsdj severity note;\r\nend architecture;"; expected = "ARCHITECTURE arch OF ent IS\r\nBEGIN\r\nEND ARCHITECTURE;" actual = beautify(input, newSettings); - console.log("Remove asserts", CompareString(actual, expected)); + assert("Remove asserts", expected, actual); input = "entity TB_DISPLAY is\r\n-- port declarations\r\nend TB_DISPLAY;\r\n\r\narchitecture TEST of TB_DISPLAY is\r\n-- signal declarations\r\nbegin\r\n-- component instance(s)\r\nend TEST;"; expected = "ENTITY TB_DISPLAY IS\r\n -- port declarations\r\nEND TB_DISPLAY;\r\n\r\nARCHITECTURE TEST OF TB_DISPLAY IS\r\n -- signal declarations\r\nBEGIN\r\n -- component instance(s)\r\nEND TEST;"; actual = beautify(input, settings); - console.log("ENTITY ARCHITECTURE", CompareString(actual, expected)); + assert("ENTITY ARCHITECTURE", expected, actual); newSettings = deepCopy(settings); newSettings.SignAlign = true; input = "port map(\r\ninput_1 => input_1_sig,\r\ninput_2 => input_2_sig,\r\noutput => output_sig\r\n);"; expected = "PORT MAP(\r\n input_1 => input_1_sig, \r\n input_2 => input_2_sig, \r\n output => output_sig\r\n);"; actual = beautify(input, newSettings); - console.log("Sign align in PORT", actual == expected); + assert("Sign align in PORT", expected, actual); input = 'if a(3 downto 0) > "0100" then\r\na(3 downto 0) := a(3 downto 0) + "0011" ;\r\nend if ;'; expected = 'IF a(3 DOWNTO 0) > "0100" THEN\r\n a(3 DOWNTO 0) := a(3 DOWNTO 0) + "0011";\r\nEND IF;'; actual = beautify(input, settings); - console.log("IF END IF case 1", CompareString(actual, expected)); + assert("IF END IF case 1", expected, actual); input = "if s = '1' then\r\no <= \"010\";\r\nelse\r\no <= \"101\";\r\nend if;"; expected = "IF s = '1' THEN\r\n o <= \"010\";\r\nELSE\r\n o <= \"101\";\r\nEND IF;"; actual = beautify(input, settings); - console.log("IF ELSE END IF case 1", actual == expected); + assert("IF ELSE END IF case 1", expected, actual); input = "IF (s = r) THEN rr := '0'; ELSE rr := '1'; END IF;"; expected = "IF (s = r) THEN\r\n rr := '0';\r\nELSE\r\n rr := '1';\r\nEND IF;"; actual = beautify(input, settings); - console.log("IF ELSE END IF case 2", actual == expected); + assert("IF ELSE END IF case 2", expected, actual); input = 'P1:process\r\nvariable x: Integer range 1 to 3;\r\nvariable y: BIT_VECTOR (0 to 1);\r\nbegin\r\n C1: case x is\r\n when 1 => Out_1 <= 0;\r\n when 2 => Out_1 <= 1;\r\n end case C1;\r\n C2: case y is\r\n when "00" => Out_2 <= 0;\r\n when "01" => Out_2 <= 1;\r\n end case C2;\r\nend process;'; expected = 'P1 : PROCESS\r\n VARIABLE x : INTEGER RANGE 1 TO 3;\r\n VARIABLE y : BIT_VECTOR (0 TO 1);\r\nBEGIN\r\n C1 : CASE x IS\r\n WHEN 1 => Out_1 <= 0;\r\n WHEN 2 => Out_1 <= 1;\r\n END CASE C1;\r\n C2 : CASE y IS\r\n WHEN "00" => Out_2 <= 0;\r\n WHEN "01" => Out_2 <= 1;\r\n END CASE C2;\r\nEND PROCESS;'; actual = beautify(input, settings); - console.log("WHEN CASE", CompareString(actual, expected)); + assert("WHEN CASE", expected, actual); input = "case READ_CPU_STATE is\r\n when WAITING =>\r\n if CPU_DATA_VALID = '1' then\r\n CPU_DATA_READ <= '1';\r\n READ_CPU_STATE <= DATA1;\r\n end if;\r\n when DATA1 =>\r\n -- etc.\r\nend case;"; expected = "CASE READ_CPU_STATE IS\r\n WHEN WAITING => \r\n IF CPU_DATA_VALID = '1' THEN\r\n CPU_DATA_READ <= '1';\r\n READ_CPU_STATE <= DATA1;\r\n END IF;\r\n WHEN DATA1 => \r\n -- etc.\r\nEND CASE;"; actual = beautify(input, settings); - console.log("WHEN CASE & IF", CompareString(actual, expected)); + assert("WHEN CASE & IF", expected, actual); input = "entity aa is\r\n port (a : in std_logic;\r\n b : in std_logic;\r\n );\r\nend aa;\r\narchitecture bb of aa is\r\n component cc\r\n port(\r\n a : in std_logic;\r\n b : in std_logic;\r\n );\r\n end cc;\r\n\r\nbegin\r\n C : cc port map (\r\n long => a,\r\n b => b\r\n );\r\nend;"; expected = "ENTITY aa IS\r\n PORT (\r\n a : IN std_logic;\r\n b : IN std_logic;\r\n );\r\nEND aa;\r\nARCHITECTURE bb OF aa IS\r\n COMPONENT cc\r\n PORT (\r\n a : IN std_logic;\r\n b : IN std_logic;\r\n );\r\n END cc;\r\n\r\nBEGIN\r\n C : cc\r\n PORT MAP(\r\n long => a, \r\n b => b\r\n );\r\nEND;"; actual = beautify(input, settings); - console.log("PORT MAP", CompareString(actual, expected)); + assert("PORT MAP", expected, actual); input = "entity aa is\r\n port (a : in std_logic;\r\n b : in std_logic;\r\n );\r\n port (a : in std_logic;\r\n b : in std_logic;\r\n );\r\nend aa;\r\narchitecture bb of aa is\r\n component cc\r\n port(\r\n a : in std_logic;\r\n b : in std_logic;\r\n );\r\n port(\r\n a : in std_logic;\r\n b : in std_logic;\r\n );\r\n end cc;\r\n\r\nbegin\r\n C : cc port map (\r\n long => a,\r\n b => b\r\n );\r\n D : cc port map (\r\n long => a,\r\n b => b\r\n );\r\nend;"; expected = "ENTITY aa IS\r\n PORT (\r\n a : IN std_logic;\r\n b : IN std_logic;\r\n );\r\n PORT (\r\n a : IN std_logic;\r\n b : IN std_logic;\r\n );\r\nEND aa;\r\nARCHITECTURE bb OF aa IS\r\n COMPONENT cc\r\n PORT (\r\n a : IN std_logic;\r\n b : IN std_logic;\r\n );\r\n PORT (\r\n a : IN std_logic;\r\n b : IN std_logic;\r\n );\r\n END cc;\r\n\r\nBEGIN\r\n C : cc\r\n PORT MAP(\r\n long => a, \r\n b => b\r\n );\r\n D : cc\r\n PORT MAP(\r\n long => a, \r\n b => b\r\n );\r\nEND;"; actual = beautify(input, settings); - console.log("Multiple PORT MAPs", CompareString(actual, expected)); + assert("Multiple PORT MAPs", expected, actual); input = "port (a : in std_logic;\r\n b : in std_logic;\r\n);"; expected = "PORT \r\n(\r\n a : IN std_logic;\r\n b : IN std_logic;\r\n);"; @@ -183,34 +212,34 @@ function UnitTest() { newSettings = deepCopy(settings); newSettings.NewLineSettings = new_line_after_symbols_2; actual = beautify(input, newSettings); - console.log("New line after PORT", CompareString(actual, expected)); + assert("New line after PORT", expected, actual); input = "component a is\r\nport( Data : inout Std_Logic_Vector(7 downto 0););\r\nend component a;"; expected = "COMPONENT a IS\r\n PORT (Data : INOUT Std_Logic_Vector(7 DOWNTO 0););\r\nEND COMPONENT a;"; actual = beautify(input, newSettings); - console.log("New line aster PORT (single line)", CompareString(actual, expected)); + assert("New line aster PORT (single line)", expected, actual); input = "process xyx (vf,fr,\r\nde -- comment\r\n)"; expected = "PROCESS xyx (vf, fr, \r\n de -- comment\r\n )"; actual = beautify(input, newSettings); - console.log("Align parameters in PROCESS", CompareString(actual, expected)); + assert("Align parameters in PROCESS", expected, actual); input = "architecture a of b is\r\nbegin\r\n process (w)\r\n variable t : std_logic_vector (4 downto 0) ;\r\nbegin\r\n a := (others => '0') ;\r\nend process ;\r\nend a;"; expected = "ARCHITECTURE a OF b IS\r\nBEGIN\r\n PROCESS (w)\r\n VARIABLE t : std_logic_vector (4 DOWNTO 0);\r\n BEGIN\r\n a := (OTHERS => '0');\r\n END PROCESS;\r\nEND a;"; actual = beautify(input, newSettings); - console.log("Double BEGIN", CompareString(actual, expected)); + assert("Double BEGIN", expected, actual); let newSettings2 = deepCopy(newSettings); newSettings2.SignAlignAll = true; input = "entity a is\r\n port ( w : in std_logic_vector (7 downto 0) ;\r\n w_s : out std_logic_vector (3 downto 0) ; ) ;\r\nend a ;\r\narchitecture b of a is\r\nbegin\r\n process ( w )\r\n variable t : std_logic_vector (4 downto 0) ;\r\n variable bcd : std_logic_vector (11 downto 0) ;\r\nbegin\r\n b(2 downto 0) := w(7 downto 5) ;\r\n t := w(4 downto 0) ;\r\n w_s <= b(11 downto 8) ;\r\n w <= b(3 downto 0) ;\r\nend process ;\r\nend b ;"; expected = "ENTITY a IS\r\n PORT \r\n (\r\n w : IN std_logic_vector (7 DOWNTO 0);\r\n w_s : OUT std_logic_vector (3 DOWNTO 0); \r\n );\r\nEND a;\r\nARCHITECTURE b OF a IS\r\nBEGIN\r\n PROCESS (w)\r\n VARIABLE t : std_logic_vector (4 DOWNTO 0);\r\n VARIABLE bcd : std_logic_vector (11 DOWNTO 0);\r\n BEGIN\r\n b(2 DOWNTO 0) := w(7 DOWNTO 5);\r\n t := w(4 DOWNTO 0);\r\n w_s <= b(11 DOWNTO 8);\r\n w <= b(3 DOWNTO 0);\r\n END PROCESS;\r\nEND b;"; actual = beautify(input, newSettings2); - console.log("Align signs in all places", CompareString(actual, expected)); + assert("Align signs in all places", expected, actual); input = "begin\r\n P0 : process(input)\r\n variable value: Integer;\r\n begin\r\n result(i) := '0';\r\n end process P0;\r\nend behavior;"; expected = "BEGIN\r\n P0 : PROCESS (input)\r\n VARIABLE value : INTEGER;\r\n BEGIN\r\n result(i) := '0';\r\n END PROCESS P0;\r\nEND behavior;"; actual = beautify(input, newSettings); - console.log("Indent after Begin", CompareString(actual, expected)); + assert("Indent after Begin", expected, actual); } function CompareString(actual: string, expected: string) {