Browse Source

squash commits; refactoring

master
g2384 5 years ago
parent
commit
df023d1835
16 changed files with 2692 additions and 2903 deletions
  1. +32
    -0
      .github/issue_template.md
  2. +2
    -1
      .gitignore
  3. +37
    -2
      README.md
  4. +26
    -0
      VHDLFiles/indent.vhd
  5. +49
    -0
      VHDLFiles/instance.vhd
  6. +323
    -615
      VHDLFormatter.js
  7. +342
    -627
      VHDLFormatter.ts
  8. +0
    -739
      VHDLFormatterUnitTests.js
  9. +0
    -813
      VHDLFormatterUnitTests.ts
  10. +57
    -0
      descriptiveCounter.js
  11. +57
    -0
      descriptiveCounter.ts
  12. +269
    -105
      index.html
  13. +1437
    -0
      tests/VHDLFormatterUnitTests.ts
  14. +27
    -0
      tests/assert.ts
  15. +30
    -0
      tests/descriptiveCounterTests.ts
  16. +4
    -1
      tsconfig.json

+ 32
- 0
.github/issue_template.md View File

@ -0,0 +1,32 @@
<!--=== Report a bug (start) ===-->
<!-- Remove this part if it is a bug -->
## Report a bug
### Input
<!-- Give a sample example -->
### Expected Behavior
<!--- Tell us what should happen -->
### Actual Behavior
<!--- Tell us what happens instead of the expected behavior -->
<!--=== Report a bug (end) ===-->
<!--=== Request a feature (start) ===-->
<!-- Remove this part if it is a bug -->
---
## Request a new feature
### As a user, I want to...
<!-- Describe your requirement here -->
#### Example Input:
<!-- Give a sample input -->
#### Example Output:
<!-- Give an expected output -->
<!--=== Request a feature (end) ===-->

+ 2
- 1
.gitignore View File

@ -1,2 +1,3 @@
*.map
/.vscode/*
/.vscode/*
/tests/*.js

+ 37
- 2
README.md View File

@ -1,4 +1,39 @@
# VHDLFomatter
# VHDL Fomatter
VHDL formatter web online written in javascript
[Online version http://g2384.github.io/work/VHDLformatter.html](http://g2384.github.io/work/VHDLformatter.html)
[Online version https://g2384.github.io/VHDLFormatter/](https://g2384.github.io/VHDLFormatter/)
## Release Notes
### 2.2 [2018-10-16]
- support enumerated types
### 2.1 [2018-03-22]
- fix keywords case issues
- anything in quotes will not be touched
- correct format for comments after multi-line statement
- options to align signs in parameter lists
- fix function indentation
- fix "align symbols affects process label"
- do not align multi-occurrence symbols in a line
- better styles (checkbox, button, disabled)
- support package bodies
- fix "newline after PORT affects PORT MAP"
### 2.0 [2018-02-16]
- rewrite the main algorithm (& move to a new repository)
- add more unit tests
- fix all known issues
### 1.1 [2016-11]
- fix some bugs reported by users
- add unit tests
### 1.0 [2015-01]
- support most of the VHDL features

+ 26
- 0
VHDLFiles/indent.vhd View File

@ -0,0 +1,26 @@
package run_base_pkg is
signal runner : runner_sync_t := (phase => test_runner_entry,
locks => ((false, false),
(false, false),
(false, false)),
exit_without_errors => false,
exit_simulation => false);
shared variable runner_trace_logger : logger_t;
procedure runner_init;
impure function get_phase
return runner_phase_t;
procedure set_test_case_name (
constant index : in positive;
constant new_name : in string);
impure function get_test_case_name (
constant index : positive)
return string;
procedure set_num_of_test_cases (
constant new_value : in integer);
end package;

+ 49
- 0
VHDLFiles/instance.vhd View File

@ -0,0 +1,49 @@
architecture test of instance is
begin
a: foo;
b: entity work.foo;
b1: entity work.foo(goo);
c: configuration work.bar;
d: component foo;
e: entity work.foo
port map ( a, b, c );
f: entity work.foo
port map ( a, b, x => c );
g: entity work.foo
generic map ( X => 1 )
port map ( a, b );
h: entity work.foo
port map ( a => open );
i: foo port map ( x );
UUT : ENTITY work.csa_adder
PORT MAP(
osum => sum,
ocarry => carry
);
UUT_S : ENTITY work.csa_adder
PORT MAP(
osum => sums,
ocarry => carrys
);
end architecture;
ARCHITECTURE test3 OF test IS
COMPONENT comp IS PORT (a : BOOLEAN);
END COMPONENT;
SIGNAL s_ok : BOOLEAN;
BEGIN
comp PORT MAP(a => s_ok); -- unlabeled component instantiation
END ARCHITECTURE;

+ 323
- 615
VHDLFormatter.js
File diff suppressed because it is too large
View File


+ 342
- 627
VHDLFormatter.ts
File diff suppressed because it is too large
View File


+ 0
- 739
VHDLFormatterUnitTests.js View File

@ -1,739 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const VHDLFormatter_1 = require("./VHDLFormatter");
const VHDLFormatter_2 = require("./VHDLFormatter");
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");
const VHDLFormatter_8 = require("./VHDLFormatter");
const VHDLFormatter_9 = require("./VHDLFormatter");
const VHDLFormatter_10 = require("./VHDLFormatter");
let testCount = 0;
var showUnitTests = true; //window.location.href.indexOf("http") < 0;
if (showUnitTests) {
testCount = 0;
UnitTest();
UnitTestIndentDecode();
UnitTestRemoveAsserts();
UnitTestApplyNoNewLineAfter();
UnitTestSetNewLinesAfterSymbols();
UnitTestFormattedLineToString();
UnitTestbeautify3();
console.log("total tests: " + testCount);
}
function UnitTestFormattedLineToString() {
console.log("=== FormattedLineToString ===");
FormattedLineToStringCase1();
FormattedLineToStringCase2();
}
function FormattedLineToStringCase1() {
let inputs = [
new VHDLFormatter_9.FormattedLine("a;", 0),
new VHDLFormatter_9.FormattedLine("b;", 0)
];
let expected = ["a;", "b;"];
UnitTest7(VHDLFormatter_10.FormattedLineToString, "General", " ", inputs, expected);
}
function FormattedLineToStringCase2() {
let inputs = [
new VHDLFormatter_9.FormattedLine("a;", 1),
new VHDLFormatter_9.FormattedLine("b;", 2)
];
let expected = [" a;", " b;"];
UnitTest7(VHDLFormatter_10.FormattedLineToString, "General", " ", inputs, expected);
}
function UnitTestbeautify3() {
console.log("=== beautify3 ===");
Beautify3Case1();
Beautify3Case2();
Beautify3Case3();
Beautify3Case4();
Beautify3Case5();
Beautify3Case6();
Beautify3Case7();
Beautify3Case8();
Beautify3Case9();
Beautify3Case10();
Beautify3Case11();
Beautify3Case12();
Beautify3Case13();
Beautify3Case14();
}
function Beautify3Case1() {
let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings = new VHDLFormatter_4.BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
let inputs = ["a;", "b;"];
let expected = [new VHDLFormatter_9.FormattedLine("a;", 0), new VHDLFormatter_9.FormattedLine("b;", 0)];
UnitTest6(VHDLFormatter_8.beautify3, "General", settings, inputs, expected, 0, 1, 0);
}
function Beautify3Case2() {
let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings = new VHDLFormatter_4.BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
let inputs = ["IF x = '1' THEN", "RETURN 1;", "END IF;"];
let expected = [
new VHDLFormatter_9.FormattedLine("IF x = '1' THEN", 0),
new VHDLFormatter_9.FormattedLine("RETURN 1;", 1),
new VHDLFormatter_9.FormattedLine("END IF;", 0)
];
UnitTest6(VHDLFormatter_8.beautify3, "IF END", settings, inputs, expected, 0, 2, 0);
}
function Beautify3Case3() {
let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings = new VHDLFormatter_4.BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
let inputs = [
"IF x = '1' THEN",
"RETURN 1;",
"ELSIF x = '0' THEN",
"RETURN 0;",
"ELSE",
"RETURN -1;",
"END IF;"
];
let expected = [
new VHDLFormatter_9.FormattedLine("IF x = '1' THEN", 0),
new VHDLFormatter_9.FormattedLine("RETURN 1;", 1),
new VHDLFormatter_9.FormattedLine("ELSIF x = '0' THEN", 0),
new VHDLFormatter_9.FormattedLine("RETURN 0;", 1),
new VHDLFormatter_9.FormattedLine("ELSE", 0),
new VHDLFormatter_9.FormattedLine("RETURN -1;", 1),
new VHDLFormatter_9.FormattedLine("END IF;", 0)
];
UnitTest6(VHDLFormatter_8.beautify3, "if elsif else end", settings, inputs, expected, 0, 6, 0);
}
function Beautify3Case4() {
let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings = new VHDLFormatter_4.BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
let inputs = ["END"];
let expected = [new VHDLFormatter_9.FormattedLine("END", 0)];
UnitTest6(VHDLFormatter_8.beautify3, "one line END", settings, inputs, expected, 0, 0, 0);
}
function Beautify3Case5() {
let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings = new VHDLFormatter_4.BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
let inputs = [
"CASE b",
"WHEN 1 =>",
"c <= d;",
"WHEN 2 =>",
"d <= f;",
"END CASE;"
];
let expected = [
new VHDLFormatter_9.FormattedLine("CASE b", 0),
new VHDLFormatter_9.FormattedLine("WHEN 1 =>", 1),
new VHDLFormatter_9.FormattedLine("c <= d;", 2),
new VHDLFormatter_9.FormattedLine("WHEN 2 =>", 1),
new VHDLFormatter_9.FormattedLine("d <= f;", 2),
new VHDLFormatter_9.FormattedLine("END CASE;", 0)
];
UnitTest6(VHDLFormatter_8.beautify3, "case when when end", settings, inputs, expected, 0, 5, 0);
}
function Beautify3Case6() {
let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings = new VHDLFormatter_4.BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
let inputs = [
"CASE b",
"WHEN 1 =>",
"c <= d;",
"CASE b",
"WHEN 1 =>",
"c <= d;",
"WHEN 2 =>",
"d <= f;",
"END CASE;",
"WHEN 2 =>",
"d <= f;",
"END CASE;"
];
let expected = [
new VHDLFormatter_9.FormattedLine("CASE b", 0),
new VHDLFormatter_9.FormattedLine("WHEN 1 =>", 1),
new VHDLFormatter_9.FormattedLine("c <= d;", 2),
new VHDLFormatter_9.FormattedLine("CASE b", 2),
new VHDLFormatter_9.FormattedLine("WHEN 1 =>", 3),
new VHDLFormatter_9.FormattedLine("c <= d;", 4),
new VHDLFormatter_9.FormattedLine("WHEN 2 =>", 3),
new VHDLFormatter_9.FormattedLine("d <= f;", 4),
new VHDLFormatter_9.FormattedLine("END CASE;", 2),
new VHDLFormatter_9.FormattedLine("WHEN 2 =>", 1),
new VHDLFormatter_9.FormattedLine("d <= f;", 2),
new VHDLFormatter_9.FormattedLine("END CASE;", 0)
];
UnitTest6(VHDLFormatter_8.beautify3, "case & case end", settings, inputs, expected, 0, 11, 0);
}
function Beautify3Case7() {
let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings = new VHDLFormatter_4.BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
let inputs = [
"ARCHITECTURE a OF one IS",
"SIGNAL x : INTEGER;",
"BEGIN",
"-- architecture",
"END ARCHITECTURE;"
];
let expected = [
new VHDLFormatter_9.FormattedLine("ARCHITECTURE a OF one IS", 0),
new VHDLFormatter_9.FormattedLine("SIGNAL x : INTEGER;", 1),
new VHDLFormatter_9.FormattedLine("BEGIN", 0),
new VHDLFormatter_9.FormattedLine("-- architecture", 1),
new VHDLFormatter_9.FormattedLine("END ARCHITECTURE;", 0),
];
UnitTest6(VHDLFormatter_8.beautify3, "architecture", settings, inputs, expected, 0, 4, 0);
}
function Beautify3Case8() {
let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings = new VHDLFormatter_4.BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
let inputs = [
"ARCHITECTURE a OF one IS",
"SIGNAL x : INTEGER;",
"BEGIN",
"-- architecture",
"END ARCHITECTURE;",
"ARCHITECTURE b OF one IS",
"SIGNAL x : INTEGER;",
"BEGIN",
"-- architecture",
"END ARCHITECTURE;"
];
let expected = [
new VHDLFormatter_9.FormattedLine("ARCHITECTURE a OF one IS", 0),
new VHDLFormatter_9.FormattedLine("SIGNAL x : INTEGER;", 1),
new VHDLFormatter_9.FormattedLine("BEGIN", 0),
new VHDLFormatter_9.FormattedLine("-- architecture", 1),
new VHDLFormatter_9.FormattedLine("END ARCHITECTURE;", 0),
new VHDLFormatter_9.FormattedLine("ARCHITECTURE b OF one IS", 0),
new VHDLFormatter_9.FormattedLine("SIGNAL x : INTEGER;", 1),
new VHDLFormatter_9.FormattedLine("BEGIN", 0),
new VHDLFormatter_9.FormattedLine("-- architecture", 1),
new VHDLFormatter_9.FormattedLine("END ARCHITECTURE;", 0),
];
UnitTest6(VHDLFormatter_8.beautify3, "architecture 2", settings, inputs, expected, 0, 9, 0);
}
function Beautify3Case9() {
let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings = new VHDLFormatter_4.BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
let inputs = [
"PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER) IS",
"VARIABLE i : INTEGER;",
"BEGIN",
"y := x + 1;",
"END PROCEDURE;"
];
let expected = [
new VHDLFormatter_9.FormattedLine("PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER) IS", 0),
new VHDLFormatter_9.FormattedLine("VARIABLE i : INTEGER;", 1),
new VHDLFormatter_9.FormattedLine("BEGIN", 0),
new VHDLFormatter_9.FormattedLine("y := x + 1;", 1),
new VHDLFormatter_9.FormattedLine("END PROCEDURE;", 0)
];
UnitTest6(VHDLFormatter_8.beautify3, "procedure", settings, inputs, expected, 0, 4, 0);
}
function Beautify3Case10() {
let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings = new VHDLFormatter_4.BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
let inputs = [
"PACKAGE three IS",
"SIGNAL s : INTEGER;",
"ALIAS sa IS s;",
"END PACKAGE;"
];
let expected = [
new VHDLFormatter_9.FormattedLine("PACKAGE three IS", 0),
new VHDLFormatter_9.FormattedLine("SIGNAL s : INTEGER;", 1),
new VHDLFormatter_9.FormattedLine("ALIAS sa IS s;", 1),
new VHDLFormatter_9.FormattedLine("END PACKAGE;", 0)
];
UnitTest6(VHDLFormatter_8.beautify3, "package", settings, inputs, expected, 0, 3, 0);
}
function Beautify3Case11() {
let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings = new VHDLFormatter_4.BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
let inputs = [
"PACKAGE p IS",
"PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER);",
"END PACKAGE;",
"PACKAGE BODY p IS",
"PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER) IS",
"VARIABLE i : INTEGER;",
"BEGIN",
"y := x + 1;",
"END PROCEDURE;",
"PROCEDURE bar(FILE x : text);",
"PROCEDURE baz IS",
"TYPE foo;",
"ALIAS x IS y;",
"BEGIN",
"END PROCEDURE;",
"PROCEDURE tralala IS",
"USE work.foo;",
"BEGIN",
"END PROCEDURE;",
"END PACKAGE BODY;"
];
let expected = [
new VHDLFormatter_9.FormattedLine("PACKAGE p IS", 0),
new VHDLFormatter_9.FormattedLine("PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER);", 1),
new VHDLFormatter_9.FormattedLine("END PACKAGE;", 0),
new VHDLFormatter_9.FormattedLine("PACKAGE BODY p IS", 0),
new VHDLFormatter_9.FormattedLine("PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER) IS", 1),
new VHDLFormatter_9.FormattedLine("VARIABLE i : INTEGER;", 2),
new VHDLFormatter_9.FormattedLine("BEGIN", 1),
new VHDLFormatter_9.FormattedLine("y := x + 1;", 2),
new VHDLFormatter_9.FormattedLine("END PROCEDURE;", 1),
new VHDLFormatter_9.FormattedLine("PROCEDURE bar(FILE x : text);", 1),
new VHDLFormatter_9.FormattedLine("PROCEDURE baz IS", 1),
new VHDLFormatter_9.FormattedLine("TYPE foo;", 2),
new VHDLFormatter_9.FormattedLine("ALIAS x IS y;", 2),
new VHDLFormatter_9.FormattedLine("BEGIN", 1),
new VHDLFormatter_9.FormattedLine("END PROCEDURE;", 1),
new VHDLFormatter_9.FormattedLine("PROCEDURE tralala IS", 1),
new VHDLFormatter_9.FormattedLine("USE work.foo;", 2),
new VHDLFormatter_9.FormattedLine("BEGIN", 1),
new VHDLFormatter_9.FormattedLine("END PROCEDURE;", 1),
new VHDLFormatter_9.FormattedLine("END PACKAGE BODY;", 0)
];
UnitTest6(VHDLFormatter_8.beautify3, "package procedure", settings, inputs, expected, 0, expected.length - 1, 0);
}
function Beautify3Case12() {
let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings = new VHDLFormatter_4.BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
let inputs = [
"ARCHITECTURE a OF b IS",
"SIGNAL x : INTEGER := 0;",
"BEGIN",
"p: PROCESS IS",
"BEGIN",
"END PROCESS;",
"PROCESS",
"VARIABLE y : INTEGER := 5;",
"BEGIN",
"x <= y;",
"END PROCESS;",
"PROCESS (x) IS",
"BEGIN",
"x <= x + 1;",
"END PROCESS;",
"POSTPONED PROCESS IS",
"BEGIN",
"END PROCESS;",
"POSTPONED assert x = 1;",
"END ARCHITECTURE;"
];
let expected = [
new VHDLFormatter_9.FormattedLine("ARCHITECTURE a OF b IS", 0),
new VHDLFormatter_9.FormattedLine("SIGNAL x : INTEGER := 0;", 1),
new VHDLFormatter_9.FormattedLine("BEGIN", 0),
new VHDLFormatter_9.FormattedLine("p: PROCESS IS", 1),
new VHDLFormatter_9.FormattedLine("BEGIN", 1),
new VHDLFormatter_9.FormattedLine("END PROCESS;", 1),
new VHDLFormatter_9.FormattedLine("PROCESS", 1),
new VHDLFormatter_9.FormattedLine("VARIABLE y : INTEGER := 5;", 2),
new VHDLFormatter_9.FormattedLine("BEGIN", 1),
new VHDLFormatter_9.FormattedLine("x <= y;", 2),
new VHDLFormatter_9.FormattedLine("END PROCESS;", 1),
new VHDLFormatter_9.FormattedLine("PROCESS (x) IS", 1),
new VHDLFormatter_9.FormattedLine("BEGIN", 1),
new VHDLFormatter_9.FormattedLine("x <= x + 1;", 2),
new VHDLFormatter_9.FormattedLine("END PROCESS;", 1),
new VHDLFormatter_9.FormattedLine("POSTPONED PROCESS IS", 1),
new VHDLFormatter_9.FormattedLine("BEGIN", 1),
new VHDLFormatter_9.FormattedLine("END PROCESS;", 1),
new VHDLFormatter_9.FormattedLine("POSTPONED assert x = 1;", 1),
new VHDLFormatter_9.FormattedLine("END ARCHITECTURE;", 0)
];
UnitTest6(VHDLFormatter_8.beautify3, "package postponed procedure", settings, inputs, expected, 0, expected.length - 1, 0);
}
function Beautify3Case13() {
let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings = new VHDLFormatter_4.BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
let inputs = [
"TYPE SharedCounter IS PROTECTED",
"PROCEDURE increment (N : INTEGER := 1);",
"IMPURE FUNCTION value RETURN INTEGER;",
"END PROTECTED SharedCounter;"
];
let expected = [
new VHDLFormatter_9.FormattedLine("TYPE SharedCounter IS PROTECTED", 0),
new VHDLFormatter_9.FormattedLine("PROCEDURE increment (N : INTEGER := 1);", 1),
new VHDLFormatter_9.FormattedLine("IMPURE FUNCTION value RETURN INTEGER;", 1),
new VHDLFormatter_9.FormattedLine("END PROTECTED SharedCounter;", 0)
];
UnitTest6(VHDLFormatter_8.beautify3, "type projected", settings, inputs, expected, 0, expected.length - 1, 0);
}
function Beautify3Case14() {
let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings = new VHDLFormatter_4.BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
let inputs = [
"PACKAGE p IS",
"TYPE SharedCounter IS PROTECTED",
"PROCEDURE increment (N : INTEGER := 1);",
"IMPURE FUNCTION value RETURN INTEGER;",
"END PROTECTED SharedCounter;",
"TYPE SharedCounter IS PROTECTED BODY"
];
let expected = [
new VHDLFormatter_9.FormattedLine("PACKAGE p IS", 0),
new VHDLFormatter_9.FormattedLine("TYPE SharedCounter IS PROTECTED", 1),
new VHDLFormatter_9.FormattedLine("PROCEDURE increment (N : INTEGER := 1);", 2),
new VHDLFormatter_9.FormattedLine("IMPURE FUNCTION value RETURN INTEGER;", 2),
new VHDLFormatter_9.FormattedLine("END PROTECTED SharedCounter;", 1),
new VHDLFormatter_9.FormattedLine("TYPE SharedCounter IS PROTECTED BODY", 1)
];
UnitTest6(VHDLFormatter_8.beautify3, "type projected", settings, inputs, expected, 0, expected.length - 1, 0);
}
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 ===");
let input = ["a;", "b;"];
let expected = ["a;@@singleline", "b;@@singleline"];
let parameters = [";"];
UnitTest4(VHDLFormatter_6.ApplyNoNewLineAfter, "one blankspace", parameters, input, expected);
input = ["a;", "b THEN", "c"];
expected = ["a;@@singleline", "b THEN@@singleline", "c"];
parameters = [";", "then"];
UnitTest4(VHDLFormatter_6.ApplyNoNewLineAfter, "one blankspace", parameters, input, expected);
}
function UnitTestRemoveAsserts() {
console.log("=== RemoveAsserts ===");
let input = ["ASSERT a;"];
let expected = [""];
UnitTest3(VHDLFormatter_5.RemoveAsserts, "one assert", input, expected);
input = ["ASSERT a", "b;", "c"];
expected = ["", "", "c"];
UnitTest3(VHDLFormatter_5.RemoveAsserts, "multiline assert", input, expected);
}
function UnitTestIndentDecode() {
console.log("=== IndentDecode ===");
UnitTest2(VHDLFormatter_2.indentDecode, "one blankspace", " ", "one blankspace");
UnitTest2(VHDLFormatter_2.indentDecode, "mixed chars", " A ", "one blankspace & one A & one blankspace");
UnitTest2(VHDLFormatter_2.indentDecode, "4 blankspaces", " ", "four blankspace");
UnitTest2(VHDLFormatter_2.indentDecode, "9 blankspaces", " ", "many blankspace");
}
function compareFormattedLines(expected, actual, message) {
var l = Math.min(actual.length, expected.length);
let result = "";
for (var i = 0; i < l; i++) {
if (actual[i] instanceof VHDLFormatter_9.FormattedLine) {
if (expected[i] instanceof VHDLFormatter_9.FormattedLine) {
let compareResult = compareFormattedLine((expected[i]), (actual[i]), message, false);
if (compareResult.length > 0) {
result += "index " + i + "\n" + compareResult;
}
}
else {
result += "index " + i + "\nexpected FormatLine[], actual FormattedLine. actual:" + (actual[i]).Line;
}
}
else {
if (expected[i] instanceof VHDLFormatter_9.FormattedLine) {
result += "index " + i + "\nexpected FormatLine, actual FormattedLine[]. expected:" + (expected[i]).Line;
}
else {
let compareResult = compareFormattedLines((actual[i]), (expected[i]), message);
if (compareResult.length > 0) {
result += "index " + i + "\n" + compareResult;
}
}
}
}
if (actual.length > expected.length) {
result += "actual has more items";
for (var i = expected.length; i < actual.length; i++) {
result += "actual[" + i + "] = " + actual[i];
}
}
else if (actual.length < expected.length) {
result += "expected has more items";
for (var i = actual.length; i < expected.length; i++) {
result += "expected[" + i + "] = " + expected[i];
}
}
return result;
}
function assertFormattedLines(testName, expected, actual, message) {
let result = compareFormattedLines(expected, actual, message);
if (result.length > 0) {
console.log(testName + " failed:\n" + result);
}
testCount++;
}
function compareFormattedLine(expected, actual, message, cumulateTestCount) {
let result = "";
if (expected.Indent != actual.Indent) {
result += 'indents are not equal;\nexpected: "' + expected.Line + '", ' + expected.Indent
+ ';\nactual: "' + actual.Line + '", ' + actual.Indent + "\n";
}
let compareResult = CompareString(actual.Line, expected.Line);
if (compareResult != true) {
result += compareResult;
}
return result;
}
function assert(testName, expected, actual, message) {
var result = CompareString(actual, expected);
if (result != true) {
console.log(testName + " failed: \n" + result);
}
else {
//console.log(testName + " pass");
}
testCount++;
}
function assertArray(testName, expected, actual, message) {
var result = CompareArray(actual, expected);
if (result != true) {
console.log(testName + " failed: " + result);
}
else {
//console.log(testName + " pass");
}
testCount++;
}
function UnitTest7(func, testName, indentation, inputs, expected) {
let actual = func(inputs, indentation);
assertArray(testName, expected, actual);
}
function UnitTest6(func, testName, parameters, inputs, expected, startIndex, expectedEndIndex, indent) {
let actual = [];
let endIndex = func(inputs, actual, parameters, startIndex, indent);
if (endIndex != expectedEndIndex) {
console.log(testName + " failed;\nend index, actual: " + endIndex + "; expected: " + expectedEndIndex);
}
assertFormattedLines(testName, expected, actual);
}
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));
func(actual, parameters);
assertArray(testName, expected, actual);
}
function UnitTest3(func, testName, inputs, expected) {
let actual = JSON.parse(JSON.stringify(inputs));
func(actual);
assertArray(testName, expected, actual);
}
function UnitTest2(func, testName, inputs, expected) {
let actual = func(inputs);
assert(testName, expected, actual);
}
function deepCopy(objectToCopy) {
return (JSON.parse(JSON.stringify(objectToCopy)));
}
function UnitTest() {
let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings = new VHDLFormatter_4.BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
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);
assert("General", expected, actual);
IntegrationTest2();
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"];
let newSettings = deepCopy(settings);
newSettings.NewLineSettings = new_line_after_symbols_2;
expected = "a; b; c;";
input = "a; \r\nb;\r\n c;";
actual = VHDLFormatter_1.beautify(input, newSettings);
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);
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);
assert("ENTITY ARCHITECTURE", expected, actual);
IntegrationTest5();
IntegrationTest6();
IntegrationTest7();
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);
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);
assert("IF ELSE END IF case 1", expected, actual);
newSettings = deepCopy(settings);
newSettings.NewLineSettings.newLineAfter.push("ELSE");
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, newSettings);
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);
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);
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 PORT MAP(\r\n long => a,\r\n b => b\r\n );\r\nEND;";
actual = VHDLFormatter_1.beautify(input, settings);
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 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;";
actual = VHDLFormatter_1.beautify(input, settings);
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();
new_line_after_symbols_2.newLineAfter = ["then", ";", "generic", "port"];
newSettings = deepCopy(settings);
newSettings.NewLineSettings = new_line_after_symbols_2;
actual = VHDLFormatter_1.beautify(input, newSettings);
assert("New line after PORT", expected, actual);
newSettings = deepCopy(settings);
newSettings.NewLineSettings.newLineAfter = [];
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);
assert("New line after 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);
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);
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);
assert("Align signs in all places", expected, actual);
}
function IntegrationTest5() {
let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["generic"];
let settings = new VHDLFormatter_4.BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
settings.SignAlignRegional = true;
let input = "port map(\r\ninput_1 => input_1_sig,\r\ninput_2 => input_2_sig,\r\noutput => output_sig\r\n);";
let expected = "PORT MAP(\r\n input_1 => input_1_sig,\r\n input_2 => input_2_sig,\r\n output => output_sig\r\n);";
let actual = VHDLFormatter_1.beautify(input, settings);
assert("Sign align in PORT", expected, actual);
}
function IntegrationTest6() {
let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";", "port map"];
new_line_after_symbols.noNewLineAfter = ["generic"];
let settings = new VHDLFormatter_4.BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
settings.SignAlignRegional = true;
let input = "port map(\r\ninput_1 => input_1_sig,\r\ninput_2 => input_2_sig,\r\noutput => output_sig\r\n);";
let expected = "PORT MAP\r\n(\r\n input_1 => input_1_sig,\r\n input_2 => input_2_sig,\r\n output => output_sig\r\n);";
let actual = VHDLFormatter_1.beautify(input, settings);
assert("Sign align in PORT & new line after MAP", expected, actual);
}
function IntegrationTest7() {
let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
let settings = new VHDLFormatter_4.BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
settings.SignAlignRegional = true;
let input = "entity p is\r\n generic\r\n (\r\n -- INCLK\r\n INCLK0_INPUT_FREQUENCY : natural;\r\n\r\n -- CLK1\r\n CLK1_DIVIDE_BY : natural := 1;\r\n CLK1_MULTIPLY_BY : unnatural:= 1;\r\n CLK1_PHASE_SHIFT : string := \"0\"\r\n );\r\n port\r\n (\r\n inclk0 : in std_logic := '0';\r\n c0 : out std_logic ;\r\n c1 : out std_logic \r\n );\r\nEND pll;";
let expected = "ENTITY p IS\r\n GENERIC (\r\n -- INCLK\r\n INCLK0_INPUT_FREQUENCY : NATURAL;\r\n\r\n -- CLK1\r\n CLK1_DIVIDE_BY : NATURAL := 1;\r\n CLK1_MULTIPLY_BY : unnatural := 1;\r\n CLK1_PHASE_SHIFT : STRING := \"0\"\r\n );\r\n PORT (\r\n inclk0 : IN std_logic := '0';\r\n c0 : OUT std_logic;\r\n c1 : OUT std_logic\r\n );\r\nEND pll;";
let actual = VHDLFormatter_1.beautify(input, settings);
assert("Sign align in PORT & GENERIC", expected, actual);
}
function IntegrationTest2() {
let new_line_after_symbols = new VHDLFormatter_3.NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["generic"];
let settings = new VHDLFormatter_4.BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
settings.RemoveComments = true;
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\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;";
let actual = VHDLFormatter_1.beautify(input, settings);
assert("Remove comments", expected, actual);
}
function CompareString(actual, expected) {
var l = Math.min(actual.length, expected.length);
for (var i = 0; i < l; i++) {
if (actual[i] != expected[i]) {
var toEnd = Math.min(i + 50, l);
return '\ndifferent at ' + i.toString() +
'\nactual: "\n' + actual.substring(i, toEnd) +
'\nexpected: "\n' + expected.substring(i, toEnd) + '"\n---' +
"\nactual (full): \n" + actual + "\n---" +
"\nexpected (full): \n" + expected + "\n====\n";
}
}
if (actual != expected) {
return 'actual: \n"' + actual + '"\nexpected: \n"' + expected + '"';
}
return true;
}
function CompareArray(actual, expected) {
var l = Math.min(actual.length, expected.length);
let result = "";
for (var i = 0; i < l; i++) {
if (actual[i] != expected[i]) {
result += CompareString(actual[i], expected[i]) + "\n";
}
}
if (actual.length > expected.length) {
result += "actual has more items";
for (var i = expected.length; i < actual.length; i++) {
result += "actual[" + i + "] = " + actual[i];
}
}
else if (actual.length < expected.length) {
result += "expected has more items";
for (var i = actual.length; i < expected.length; i++) {
result += "expected[" + i + "] = " + expected[i];
}
}
return true;
}
//# sourceMappingURL=VHDLFormatterUnitTests.js.map

+ 0
- 813
VHDLFormatterUnitTests.ts View File

@ -1,813 +0,0 @@
import { beautify } from "./VHDLFormatter";
import { indentDecode } from "./VHDLFormatter";
import { NewLineSettings } from "./VHDLFormatter";
import { BeautifierSettings } from "./VHDLFormatter";
import { RemoveAsserts } from "./VHDLFormatter";
import { ApplyNoNewLineAfter } from "./VHDLFormatter";
import { SetNewLinesAfterSymbols } from "./VHDLFormatter";
import { beautify3 } from "./VHDLFormatter";
import { FormattedLine } from "./VHDLFormatter";
import { FormattedLineToString } from "./VHDLFormatter";
let testCount: number = 0;
var showUnitTests = true;//window.location.href.indexOf("http") < 0;
if (showUnitTests) {
testCount = 0;
UnitTest();
UnitTestIndentDecode();
UnitTestRemoveAsserts();
UnitTestApplyNoNewLineAfter();
UnitTestSetNewLinesAfterSymbols();
UnitTestFormattedLineToString();
UnitTestbeautify3();
console.log("total tests: " + testCount);
}
interface Function {
readonly name: string;
}
function UnitTestFormattedLineToString() {
console.log("=== FormattedLineToString ===");
FormattedLineToStringCase1();
FormattedLineToStringCase2();
}
function FormattedLineToStringCase1() {
let inputs: (FormattedLine | FormattedLine[])[] = [
new FormattedLine("a;", 0),
new FormattedLine("b;", 0)];
let expected: Array<string> = ["a;", "b;"];
UnitTest7(FormattedLineToString, "General", " ", inputs, expected);
}
function FormattedLineToStringCase2() {
let inputs: (FormattedLine | FormattedLine[])[] = [
new FormattedLine("a;", 1),
new FormattedLine("b;", 2)];
let expected: Array<string> = [" a;", " b;"];
UnitTest7(FormattedLineToString, "General", " ", inputs, expected);
}
function UnitTestbeautify3() {
console.log("=== beautify3 ===");
Beautify3Case1();
Beautify3Case2();
Beautify3Case3();
Beautify3Case4();
Beautify3Case5();
Beautify3Case6();
Beautify3Case7();
Beautify3Case8();
Beautify3Case9();
Beautify3Case10();
Beautify3Case11();
Beautify3Case12();
Beautify3Case13();
Beautify3Case14();
}
function Beautify3Case1() {
let new_line_after_symbols: NewLineSettings = new NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
let inputs: Array<string> = ["a;", "b;"];
let expected: (FormattedLine | FormattedLine[])[] = [new FormattedLine("a;", 0), new FormattedLine("b;", 0)];
UnitTest6(beautify3, "General", settings, inputs, expected, 0, 1, 0);
}
function Beautify3Case2() {
let new_line_after_symbols: NewLineSettings = new NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
let inputs: Array<string> = ["IF x = '1' THEN", "RETURN 1;", "END IF;"];
let expected: (FormattedLine | FormattedLine[])[] = [
new FormattedLine("IF x = '1' THEN", 0),
new FormattedLine("RETURN 1;", 1),
new FormattedLine("END IF;", 0)
];
UnitTest6(beautify3, "IF END", settings, inputs, expected, 0, 2, 0);
}
function Beautify3Case3() {
let new_line_after_symbols: NewLineSettings = new NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
let inputs: Array<string> = [
"IF x = '1' THEN",
"RETURN 1;",
"ELSIF x = '0' THEN",
"RETURN 0;",
"ELSE",
"RETURN -1;",
"END IF;"];
let expected: (FormattedLine | FormattedLine[])[] = [
new FormattedLine("IF x = '1' THEN", 0),
new FormattedLine("RETURN 1;", 1),
new FormattedLine("ELSIF x = '0' THEN", 0),
new FormattedLine("RETURN 0;", 1),
new FormattedLine("ELSE", 0),
new FormattedLine("RETURN -1;", 1),
new FormattedLine("END IF;", 0)
];
UnitTest6(beautify3, "if elsif else end", settings, inputs, expected, 0, 6, 0);
}
function Beautify3Case4() {
let new_line_after_symbols: NewLineSettings = new NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
let inputs: Array<string> = ["END"];
let expected: (FormattedLine | FormattedLine[])[] = [new FormattedLine("END", 0)];
UnitTest6(beautify3, "one line END", settings, inputs, expected, 0, 0, 0);
}
function Beautify3Case5() {
let new_line_after_symbols: NewLineSettings = new NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
let inputs: Array<string> = [
"CASE b",
"WHEN 1 =>",
"c <= d;",
"WHEN 2 =>",
"d <= f;",
"END CASE;"
];
let expected: (FormattedLine | FormattedLine[])[] = [
new FormattedLine("CASE b", 0),
new FormattedLine("WHEN 1 =>", 1),
new FormattedLine("c <= d;", 2),
new FormattedLine("WHEN 2 =>", 1),
new FormattedLine("d <= f;", 2),
new FormattedLine("END CASE;", 0)
];
UnitTest6(beautify3, "case when when end", settings, inputs, expected, 0, 5, 0);
}
function Beautify3Case6() {
let new_line_after_symbols: NewLineSettings = new NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
let inputs: Array<string> = [
"CASE b",
"WHEN 1 =>",
"c <= d;",
"CASE b",
"WHEN 1 =>",
"c <= d;",
"WHEN 2 =>",
"d <= f;",
"END CASE;",
"WHEN 2 =>",
"d <= f;",
"END CASE;"
];
let expected: (FormattedLine | FormattedLine[])[] = [
new FormattedLine("CASE b", 0),
new FormattedLine("WHEN 1 =>", 1),
new FormattedLine("c <= d;", 2),
new FormattedLine("CASE b", 2),
new FormattedLine("WHEN 1 =>", 3),
new FormattedLine("c <= d;", 4),
new FormattedLine("WHEN 2 =>", 3),
new FormattedLine("d <= f;", 4),
new FormattedLine("END CASE;", 2),
new FormattedLine("WHEN 2 =>", 1),
new FormattedLine("d <= f;", 2),
new FormattedLine("END CASE;", 0)
];
UnitTest6(beautify3, "case & case end", settings, inputs, expected, 0, 11, 0);
}
function Beautify3Case7() {
let new_line_after_symbols: NewLineSettings = new NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
let inputs: Array<string> = [
"ARCHITECTURE a OF one IS",
"SIGNAL x : INTEGER;",
"BEGIN",
"-- architecture",
"END ARCHITECTURE;"
];
let expected: (FormattedLine | FormattedLine[])[] = [
new FormattedLine("ARCHITECTURE a OF one IS", 0),
new FormattedLine("SIGNAL x : INTEGER;", 1),
new FormattedLine("BEGIN", 0),
new FormattedLine("-- architecture", 1),
new FormattedLine("END ARCHITECTURE;", 0),
];
UnitTest6(beautify3, "architecture", settings, inputs, expected, 0, 4, 0);
}
function Beautify3Case8() {
let new_line_after_symbols: NewLineSettings = new NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
let inputs: Array<string> = [
"ARCHITECTURE a OF one IS",
"SIGNAL x : INTEGER;",
"BEGIN",
"-- architecture",
"END ARCHITECTURE;",
"ARCHITECTURE b OF one IS",
"SIGNAL x : INTEGER;",
"BEGIN",
"-- architecture",
"END ARCHITECTURE;"
];
let expected: (FormattedLine | FormattedLine[])[] = [
new FormattedLine("ARCHITECTURE a OF one IS", 0),
new FormattedLine("SIGNAL x : INTEGER;", 1),
new FormattedLine("BEGIN", 0),
new FormattedLine("-- architecture", 1),
new FormattedLine("END ARCHITECTURE;", 0),
new FormattedLine("ARCHITECTURE b OF one IS", 0),
new FormattedLine("SIGNAL x : INTEGER;", 1),
new FormattedLine("BEGIN", 0),
new FormattedLine("-- architecture", 1),
new FormattedLine("END ARCHITECTURE;", 0),
];
UnitTest6(beautify3, "architecture 2", settings, inputs, expected, 0, 9, 0);
}
function Beautify3Case9() {
let new_line_after_symbols: NewLineSettings = new NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
let inputs: Array<string> = [
"PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER) IS",
"VARIABLE i : INTEGER;",
"BEGIN",
"y := x + 1;",
"END PROCEDURE;"
];
let expected: (FormattedLine | FormattedLine[])[] = [
new FormattedLine("PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER) IS", 0),
new FormattedLine("VARIABLE i : INTEGER;", 1),
new FormattedLine("BEGIN", 0),
new FormattedLine("y := x + 1;", 1),
new FormattedLine("END PROCEDURE;", 0)
];
UnitTest6(beautify3, "procedure", settings, inputs, expected, 0, 4, 0);
}
function Beautify3Case10() {
let new_line_after_symbols: NewLineSettings = new NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
let inputs: Array<string> = [
"PACKAGE three IS",
"SIGNAL s : INTEGER;",
"ALIAS sa IS s;",
"END PACKAGE;"
];
let expected: (FormattedLine | FormattedLine[])[] = [
new FormattedLine("PACKAGE three IS", 0),
new FormattedLine("SIGNAL s : INTEGER;", 1),
new FormattedLine("ALIAS sa IS s;", 1),
new FormattedLine("END PACKAGE;", 0)
];
UnitTest6(beautify3, "package", settings, inputs, expected, 0, 3, 0);
}
function Beautify3Case11() {
let new_line_after_symbols: NewLineSettings = new NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
let inputs: Array<string> = [
"PACKAGE p IS",
"PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER);",
"END PACKAGE;",
"PACKAGE BODY p IS",
"PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER) IS",
"VARIABLE i : INTEGER;",
"BEGIN",
"y := x + 1;",
"END PROCEDURE;",
"PROCEDURE bar(FILE x : text);",
"PROCEDURE baz IS",
"TYPE foo;",
"ALIAS x IS y;",
"BEGIN",
"END PROCEDURE;",
"PROCEDURE tralala IS",
"USE work.foo;",
"BEGIN",
"END PROCEDURE;",
"END PACKAGE BODY;"
];
let expected: (FormattedLine | FormattedLine[])[] = [
new FormattedLine("PACKAGE p IS", 0),
new FormattedLine("PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER);", 1),
new FormattedLine("END PACKAGE;", 0),
new FormattedLine("PACKAGE BODY p IS", 0),
new FormattedLine("PROCEDURE foo(x : IN INTEGER; y : OUT INTEGER) IS", 1),
new FormattedLine("VARIABLE i : INTEGER;", 2),
new FormattedLine("BEGIN", 1),
new FormattedLine("y := x + 1;", 2),
new FormattedLine("END PROCEDURE;", 1),
new FormattedLine("PROCEDURE bar(FILE x : text);", 1),
new FormattedLine("PROCEDURE baz IS", 1),
new FormattedLine("TYPE foo;", 2),
new FormattedLine("ALIAS x IS y;", 2),
new FormattedLine("BEGIN", 1),
new FormattedLine("END PROCEDURE;", 1),
new FormattedLine("PROCEDURE tralala IS", 1),
new FormattedLine("USE work.foo;", 2),
new FormattedLine("BEGIN", 1),
new FormattedLine("END PROCEDURE;", 1),
new FormattedLine("END PACKAGE BODY;", 0)
];
UnitTest6(beautify3, "package procedure", settings, inputs, expected, 0, expected.length - 1, 0);
}
function Beautify3Case12() {
let new_line_after_symbols: NewLineSettings = new NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
let inputs: Array<string> = [
"ARCHITECTURE a OF b IS",
"SIGNAL x : INTEGER := 0;",
"BEGIN",
"p: PROCESS IS",
"BEGIN",
"END PROCESS;",
"PROCESS",
"VARIABLE y : INTEGER := 5;",
"BEGIN",
"x <= y;",
"END PROCESS;",
"PROCESS (x) IS",
"BEGIN",
"x <= x + 1;",
"END PROCESS;",
"POSTPONED PROCESS IS",
"BEGIN",
"END PROCESS;",
"POSTPONED assert x = 1;",
"END ARCHITECTURE;"
];
let expected: (FormattedLine | FormattedLine[])[] = [
new FormattedLine("ARCHITECTURE a OF b IS", 0),
new FormattedLine("SIGNAL x : INTEGER := 0;", 1),
new FormattedLine("BEGIN", 0),
new FormattedLine("p: PROCESS IS", 1),
new FormattedLine("BEGIN", 1),
new FormattedLine("END PROCESS;", 1),
new FormattedLine("PROCESS", 1),
new FormattedLine("VARIABLE y : INTEGER := 5;", 2),
new FormattedLine("BEGIN", 1),
new FormattedLine("x <= y;", 2),
new FormattedLine("END PROCESS;", 1),
new FormattedLine("PROCESS (x) IS", 1),
new FormattedLine("BEGIN", 1),
new FormattedLine("x <= x + 1;", 2),
new FormattedLine("END PROCESS;", 1),
new FormattedLine("POSTPONED PROCESS IS", 1),
new FormattedLine("BEGIN", 1),
new FormattedLine("END PROCESS;", 1),
new FormattedLine("POSTPONED assert x = 1;", 1),
new FormattedLine("END ARCHITECTURE;", 0)
];
UnitTest6(beautify3, "package postponed procedure", settings, inputs, expected, 0, expected.length - 1, 0);
}
function Beautify3Case13() {
let new_line_after_symbols: NewLineSettings = new NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
let inputs: Array<string> = [
"TYPE SharedCounter IS PROTECTED",
"PROCEDURE increment (N : INTEGER := 1);",
"IMPURE FUNCTION value RETURN INTEGER;",
"END PROTECTED SharedCounter;"
];
let expected: (FormattedLine | FormattedLine[])[] = [
new FormattedLine("TYPE SharedCounter IS PROTECTED", 0),
new FormattedLine("PROCEDURE increment (N : INTEGER := 1);", 1),
new FormattedLine("IMPURE FUNCTION value RETURN INTEGER;", 1),
new FormattedLine("END PROTECTED SharedCounter;", 0)
];
UnitTest6(beautify3, "type projected", settings, inputs, expected, 0, expected.length - 1, 0);
}
function Beautify3Case14() {
let new_line_after_symbols: NewLineSettings = new NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
let inputs: Array<string> = [
"PACKAGE p IS",
"TYPE SharedCounter IS PROTECTED",
"PROCEDURE increment (N : INTEGER := 1);",
"IMPURE FUNCTION value RETURN INTEGER;",
"END PROTECTED SharedCounter;",
"TYPE SharedCounter IS PROTECTED BODY"
];
let expected: (FormattedLine | FormattedLine[])[] = [
new FormattedLine("PACKAGE p IS", 0),
new FormattedLine("TYPE SharedCounter IS PROTECTED", 1),
new FormattedLine("PROCEDURE increment (N : INTEGER := 1);", 2),
new FormattedLine("IMPURE FUNCTION value RETURN INTEGER;", 2),
new FormattedLine("END PROTECTED SharedCounter;", 1),
new FormattedLine("TYPE SharedCounter IS PROTECTED BODY", 1)
];
UnitTest6(beautify3, "type projected", settings, inputs, expected, 0, expected.length - 1, 0);
}
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<string> = ["a;", "b;"];
let expected: Array<string> = ["a;@@singleline", "b;@@singleline"];
let parameters: Array<string> = [";"];
UnitTest4(ApplyNoNewLineAfter, "one blankspace", parameters, input, expected);
input = ["a;", "b THEN", "c"];
expected = ["a;@@singleline", "b THEN@@singleline", "c"];
parameters = [";", "then"];
UnitTest4(ApplyNoNewLineAfter, "one blankspace", parameters, input, expected);
}
function UnitTestRemoveAsserts() {
console.log("=== RemoveAsserts ===");
let input: Array<string> = ["ASSERT a;"];
let expected: Array<string> = [""];
UnitTest3(RemoveAsserts, "one assert", input, expected);
input = ["ASSERT a", "b;", "c"];
expected = ["", "", "c"];
UnitTest3(RemoveAsserts, "multiline assert", input, expected);
}
function UnitTestIndentDecode() {
console.log("=== IndentDecode ===");
UnitTest2(indentDecode, "one blankspace", " ", "one blankspace");
UnitTest2(indentDecode, "mixed chars", " A ", "one blankspace & one A & one blankspace");
UnitTest2(indentDecode, "4 blankspaces", " ", "four blankspace");
UnitTest2(indentDecode, "9 blankspaces", " ", "many blankspace");
}
function compareFormattedLines(expected: (FormattedLine | FormattedLine[])[], actual: (FormattedLine | FormattedLine[])[], message?): string {
var l = Math.min(actual.length, expected.length);
let result: string = "";
for (var i = 0; i < l; i++) {
if (actual[i] instanceof FormattedLine) {
if (expected[i] instanceof FormattedLine) {
let compareResult = compareFormattedLine(<FormattedLine>(expected[i]), <FormattedLine>(actual[i]), message, false);
if (compareResult.length > 0) {
result += "index " + i + "\n" + compareResult;
}
}
else {
result += "index " + i + "\nexpected FormatLine[], actual FormattedLine. actual:" + (<FormattedLine>(actual[i])).Line;
}
}
else {
if (expected[i] instanceof FormattedLine) {
result += "index " + i + "\nexpected FormatLine, actual FormattedLine[]. expected:" + (<FormattedLine>(expected[i])).Line;
}
else {
let compareResult = compareFormattedLines(<FormattedLine[]>(actual[i]), <FormattedLine[]>(expected[i]), message);
if (compareResult.length > 0) {
result += "index " + i + "\n" + compareResult;
}
}
}
}
if (actual.length > expected.length) {
result += "actual has more items";
for (var i = expected.length; i < actual.length; i++) {
result += "actual[" + i + "] = " + actual[i];
}
}
else if (actual.length < expected.length) {
result += "expected has more items";
for (var i = actual.length; i < expected.length; i++) {
result += "expected[" + i + "] = " + expected[i];
}
}
return result;
}
function assertFormattedLines(testName, expected: (FormattedLine | FormattedLine[])[], actual: (FormattedLine | FormattedLine[])[], message?) {
let result = compareFormattedLines(expected, actual, message);
if (result.length > 0) {
console.log(testName + " failed:\n" + result);
}
testCount++;
}
function compareFormattedLine(expected: FormattedLine, actual: FormattedLine, message?, cumulateTestCount?: boolean) {
let result = "";
if (expected.Indent != actual.Indent) {
result += 'indents are not equal;\nexpected: "' + expected.Line + '", ' + expected.Indent
+ ';\nactual: "' + actual.Line + '", ' + actual.Indent + "\n";
}
let compareResult = CompareString(actual.Line, expected.Line);
if (compareResult != true) {
result += compareResult;
}
return result;
}
function assert(testName, expected: string, actual: string, message?) {
var result = CompareString(actual, expected);
if (result != true) {
console.log(testName + " failed: \n" + result);
}
else {
//console.log(testName + " pass");
}
testCount++;
}
function assertArray(testName, expected, actual, message?) {
var result = CompareArray(actual, expected);
if (result != true) {
console.log(testName + " failed: " + result);
}
else {
//console.log(testName + " pass");
}
testCount++;
}
type StringCallback = (text: string) => string;
type ArrayCallback = (arr: Array<string>) => void;
type Array2Callback = (arr: Array<string>, parameters: Array<string>) => void;
type String2Callback = (text: string, parameters: NewLineSettings) => string;
type BeautifyCallback = (inputs: Array<string>, result: (FormattedLine | FormattedLine[])[], settings: BeautifierSettings, startIndex: number, indent: number) => number;
type FormattedLinesCallback = (inputs: (FormattedLine | FormattedLine[])[], indentation: string) => Array<string>;
function UnitTest7(func: FormattedLinesCallback, testName: string, indentation: string, inputs: (FormattedLine | FormattedLine[])[], expected: Array<string>) {
let actual = func(inputs, indentation);
assertArray(testName, expected, actual);
}
function UnitTest6(func: BeautifyCallback, testName: string, parameters: BeautifierSettings, inputs: Array<string>, expected: (FormattedLine | FormattedLine[])[], startIndex: number, expectedEndIndex: number, indent: number) {
let actual: (FormattedLine | FormattedLine[])[] = []
let endIndex: number = func(inputs, actual, parameters, startIndex, indent);
if (endIndex != expectedEndIndex) {
console.log(testName + " failed;\nend index, actual: " + endIndex + "; expected: " + expectedEndIndex)
}
assertFormattedLines(testName, expected, actual);
}
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<string>, inputs: Array<string>, expected: Array<string>) {
let actual = JSON.parse(JSON.stringify(inputs));
func(actual, parameters);
assertArray(testName, expected, actual);
}
function UnitTest3(func: ArrayCallback, testName: string, inputs: Array<string>, expected: Array<string>) {
let actual = JSON.parse(JSON.stringify(inputs));
func(actual);
assertArray(testName, expected, actual);
}
function UnitTest2(func: StringCallback, testName: string, inputs, expected: string) {
let actual: string = func(inputs);
assert(testName, expected, actual);
}
function deepCopy(objectToCopy: BeautifierSettings): BeautifierSettings {
return (JSON.parse(JSON.stringify(objectToCopy)));
}
function UnitTest() {
let new_line_after_symbols: NewLineSettings = new NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["port", "generic"];
let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
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);
assert("General", expected, actual);
IntegrationTest2();
let new_line_after_symbols_2: NewLineSettings = new NewLineSettings();
new_line_after_symbols_2.newLineAfter = [];
new_line_after_symbols_2.noNewLineAfter = ["then", ";", "generic", "port"];
let newSettings = deepCopy(settings);
newSettings.NewLineSettings = new_line_after_symbols_2;
expected = "a; b; c;";
input = "a; \r\nb;\r\n c;"
actual = beautify(input, newSettings);
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);
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);
assert("ENTITY ARCHITECTURE", expected, actual);
IntegrationTest5();
IntegrationTest6();
IntegrationTest7();
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);
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);
assert("IF ELSE END IF case 1", expected, actual);
newSettings = deepCopy(settings);
newSettings.NewLineSettings.newLineAfter.push("ELSE");
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, newSettings);
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);
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);
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 PORT MAP(\r\n long => a,\r\n b => b\r\n );\r\nEND;";
actual = beautify(input, settings);
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 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;";
actual = beautify(input, settings);
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 NewLineSettings();
new_line_after_symbols_2.newLineAfter = ["then", ";", "generic", "port"];
newSettings = deepCopy(settings);
newSettings.NewLineSettings = new_line_after_symbols_2;
actual = beautify(input, newSettings);
assert("New line after PORT", expected, actual);
newSettings = deepCopy(settings);
newSettings.NewLineSettings.newLineAfter = [];
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);
assert("New line after 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);
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);
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);
assert("Align signs in all places", expected, actual);
}
function IntegrationTest5() {
let new_line_after_symbols: NewLineSettings = new NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["generic"];
let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
settings.SignAlignRegional = true;
let input = "port map(\r\ninput_1 => input_1_sig,\r\ninput_2 => input_2_sig,\r\noutput => output_sig\r\n);";
let expected = "PORT MAP(\r\n input_1 => input_1_sig,\r\n input_2 => input_2_sig,\r\n output => output_sig\r\n);";
let actual = beautify(input, settings);
assert("Sign align in PORT", expected, actual);
}
function IntegrationTest6() {
let new_line_after_symbols: NewLineSettings = new NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";","port map"];
new_line_after_symbols.noNewLineAfter = ["generic"];
let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
settings.SignAlignRegional = true;
let input = "port map(\r\ninput_1 => input_1_sig,\r\ninput_2 => input_2_sig,\r\noutput => output_sig\r\n);";
let expected = "PORT MAP\r\n(\r\n input_1 => input_1_sig,\r\n input_2 => input_2_sig,\r\n output => output_sig\r\n);";
let actual = beautify(input, settings);
assert("Sign align in PORT & new line after MAP", expected, actual);
}
function IntegrationTest7() {
let new_line_after_symbols: NewLineSettings = new NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
settings.SignAlignRegional = true;
let input = "entity p is\r\n generic\r\n (\r\n -- INCLK\r\n INCLK0_INPUT_FREQUENCY : natural;\r\n\r\n -- CLK1\r\n CLK1_DIVIDE_BY : natural := 1;\r\n CLK1_MULTIPLY_BY : unnatural:= 1;\r\n CLK1_PHASE_SHIFT : string := \"0\"\r\n );\r\n port\r\n (\r\n inclk0 : in std_logic := '0';\r\n c0 : out std_logic ;\r\n c1 : out std_logic \r\n );\r\nEND pll;";
let expected = "ENTITY p IS\r\n GENERIC (\r\n -- INCLK\r\n INCLK0_INPUT_FREQUENCY : NATURAL;\r\n\r\n -- CLK1\r\n CLK1_DIVIDE_BY : NATURAL := 1;\r\n CLK1_MULTIPLY_BY : unnatural := 1;\r\n CLK1_PHASE_SHIFT : STRING := \"0\"\r\n );\r\n PORT (\r\n inclk0 : IN std_logic := '0';\r\n c0 : OUT std_logic;\r\n c1 : OUT std_logic\r\n );\r\nEND pll;";
let actual = beautify(input, settings);
assert("Sign align in PORT & GENERIC", expected, actual);
}
function IntegrationTest2() {
let new_line_after_symbols: NewLineSettings = new NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["generic"];
let settings: BeautifierSettings = new BeautifierSettings(false, false, false, false, false, "uppercase", " ", new_line_after_symbols);
settings.RemoveComments = true;
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\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;";
let actual = beautify(input, settings);
assert("Remove comments", expected, actual);
}
function CompareString(actual: string, expected: string) {
var l = Math.min(actual.length, expected.length);
for (var i = 0; i < l; i++) {
if (actual[i] != expected[i]) {
var toEnd = Math.min(i + 50, l);
return '\ndifferent at ' + i.toString() +
'\nactual: "\n' + actual.substring(i, toEnd) +
'\nexpected: "\n' + expected.substring(i, toEnd) + '"\n---' +
"\nactual (full): \n" + actual + "\n---" +
"\nexpected (full): \n" + expected + "\n====\n";
}
}
if (actual != expected) {
return 'actual: \n"' + actual + '"\nexpected: \n"' + expected + '"';
}
return true;
}
function CompareArray(actual: Array<string>, expected: Array<string>) {
var l = Math.min(actual.length, expected.length);
let result: string = "";
for (var i = 0; i < l; i++) {
if (actual[i] != expected[i]) {
result += CompareString(actual[i], expected[i]) + "\n";
}
}
if (actual.length > expected.length) {
result += "actual has more items";
for (var i = expected.length; i < actual.length; i++) {
result += "actual[" + i + "] = " + actual[i];
}
}
else if (actual.length < expected.length) {
result += "expected has more items";
for (var i = actual.length; i < expected.length; i++) {
result += "expected[" + i + "] = " + expected[i];
}
}
return true;
}

+ 57
- 0
descriptiveCounter.js View File

@ -0,0 +1,57 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function indent_decode() {
var custom_indent = document.getElementById("cust_indent").value;
var result = descriptiveCounter(custom_indent);
document.getElementById("indent_s").innerHTML = result;
}
function descriptiveCounter(input) {
input = input.replace(/\\t/g, " ");
var tokens = input.split("");
var result = "";
var repeatedCharCount = 0;
for (var i = 0; i < tokens.length; i++) {
var char = input.substr(i, 1);
if (char == input.substr(i + 1, 1)) {
repeatedCharCount++;
}
else {
switch (char) {
case " ":
char = "blankspace";
break;
case "\t":
char = "tab";
break;
default:
char = "'" + char + "'";
}
repeatedCharCount = repeatedCharCount > 8 ? 8 : repeatedCharCount;
if (repeatedCharCount > 0) {
char += "s";
}
result += getCountText(repeatedCharCount, char);
repeatedCharCount = 0;
}
}
if (result.length < 0) {
switch (char) {
case " ":
char = "blankspace";
break;
case "\t":
char = "tab";
}
repeatedCharCount = repeatedCharCount > 8 ? 8 : repeatedCharCount;
result = getCountText(repeatedCharCount, char);
}
result = result.replace(/^ & /, "");
return result;
}
exports.descriptiveCounter = descriptiveCounter;
function getCountText(count, char) {
const dict = ["one", "two", "three", "four", "five", "six", "seven", "eight", "many"];
const ampersand = " & ";
return ampersand + dict[count] + " " + char;
}
//# sourceMappingURL=descriptiveCounter.js.map

+ 57
- 0
descriptiveCounter.ts View File

@ -0,0 +1,57 @@
function indent_decode() {
var custom_indent: string = (<HTMLInputElement>document.getElementById("cust_indent")).value;
var result: string = descriptiveCounter(custom_indent);
document.getElementById("indent_s").innerHTML = result;
}
export function descriptiveCounter(input: string): string {
input = input.replace(/\\t/g, " ");
var tokens: Array<string> = input.split("");
var result = "";
var repeatedCharCount = 0;
for (var i = 0; i < tokens.length; i++) {
var char = input.substr(i, 1);
if (char == input.substr(i + 1, 1)) {
repeatedCharCount++;
} else {
switch (char) {
case " ":
char = "blankspace";
break;
case "\t":
char = "tab";
break;
default:
char = "'" + char + "'";
}
repeatedCharCount = repeatedCharCount > 8 ? 8 : repeatedCharCount;
if (repeatedCharCount > 0) {
char += "s";
}
result += getCountText(repeatedCharCount, char);
repeatedCharCount = 0;
}
}
if (result.length < 0) {
switch (char) {
case " ":
char = "blankspace";
break;
case "\t":
char = "tab";
}
repeatedCharCount = repeatedCharCount > 8 ? 8 : repeatedCharCount;
result = getCountText(repeatedCharCount, char);
}
result = result.replace(/^ & /, "")
return result;
}
function getCountText(count: number, char: string): string {
const dict = ["one", "two", "three", "four", "five", "six", "seven", "eight", "many"];
const ampersand = " & ";
return ampersand + dict[count] + " " + char;
}

VHDLFormatter.html → index.html View File

@ -7,14 +7,33 @@
margin: 0 auto;
max-width: 960px;
tab-size: 4;
font-family: Helvetica, arial;
font-family: arial, sans-serif;
}
textarea {
width: 100%;
font-family: Consolas, Courier;
}
fieldset {
width: fit-content;
border: 1px solid #9a9a9a;
margin: 25px 0 5px 0;
padding-left: 0;
}
legend {
padding: 2px 5px;
background-color: #e0e0e0;
border-radius: 2px;
margin: -27px 0 0 -1px;
position: absolute;
}
.subtitle {
font-weight: normal;
}
.btn {
margin: 10px 10px;
padding: 5px 8px;
@ -27,23 +46,17 @@
text-align: center;
white-space: nowrap;
border: 1px solid rgb(194, 194, 194);
background-color: #fff;
}
.btn:hover {
background-color: #afd9ee;
background-color: #eee;
}
.show {
display: none;
}
input[type="checkbox"]+label {
display: inline-block;
width: 19px;
height: 19px;
margin: -1px 4px 0 0;
}
.wordwrap {
white-space: pre-wrap;
white-space: -moz-pre-wrap;
@ -51,34 +64,142 @@
white-space: -o-pre-wrap;
word-wrap: break-word;
}
label {
cursor: pointer;
line-height: 1.5em;
}
label:hover {
color: #555;
text-decoration: underline;
}
form {
margin-bottom: 0px;
}
form span {
width: 170px;
display: inline-block;
text-align: right;
}
a {
color: #4b9aff;
text-decoration: none;
background: #f0f8ff;
padding: 2px 5px;
border-radius: 3px;
font-size: 0.8em;
}
a:hover {
text-decoration: underline;
text-decoration: none;
background: #d6e8ff;
}
p {
line-height: 1em;
margin: 0.5em 0em;
}
.code {
font-family: 'Consolas', Courier, monospace;
}
.inline {
display: inline-block;
}
.disabled {
color: #ccc;
cursor: default;
}
.disabled label:hover {
color: #ccc;
text-decoration: none;
cursor: default;
}
.checkbox input[type="checkbox"] {
opacity: 0;
display: none;
}
.checkbox label {
position: relative;
display: inline-block;
/*16px width of fake checkbox + 6px distance between fake checkbox and text*/
padding-left: 22px;
}
.checkbox label::before,
.checkbox label::after {
position: absolute;
content: "";
/*Needed for the line-height to take effect*/
display: inline-block;
}
/*Outer box of the fake checkbox*/
.checkbox label::before {
height: 16px;
width: 16px;
border: 1px solid #9A9A9A;
left: 0px;
/*(24px line-height - 16px height of fake checkbox) / 2 - 1px for the border to vertically center it.*/
top: 3px;
}
/*Checkmark of the fake checkbox*/
.checkbox label::after {
height: 5px;
width: 9px;
border-left: 2px solid;
border-bottom: 2px solid;
transform: rotate(-45deg);
left: 4px;
top: 7px;
color: #333;
}
/*Hide the checkmark by default*/
.checkbox input[type="checkbox"]+label::after {
content: none;
}
/*Unhide on the checked state*/
.checkbox input[type="checkbox"]:checked+label::after {
content: "";
}
/*Adding focus styles on the outer-box of the fake checkbox*/
.checkbox input[type="checkbox"]:hover+label::after {
border-color: #888;
outline: #ccc;
}
.checkbox input[type="checkbox"]:hover+label::before {
outline: #888;
border-color: #888;
background-color: #eee;
}
.checkbox input[type="checkbox"]:disabled+label::before {
outline: #ccc;
border-color: #ccc;
background-color: #fff;
color: #ccc;
}
.checkbox input[type="checkbox"]:disabled+label::after {
color: #888;
}
.checkbox input[type="checkbox"]:focus+label::before {
outline: #333 auto 5px;
}
</style>
<link href="highlight.css" rel="stylesheet" />
@ -105,111 +226,139 @@
<body>
<h2>VHDL Beautifier, Formatter</h2>
<h4>Beautify and format your VHDL code online</h4>
<p>
Proper formatting makes code easier to read and understand.
<br> Please make a backup before you replace your code!
<br>
<a href="https://github.com/g2384/g2384.github.io/issues" target="_blank">Report bugs if your code is not properly formatted, please provide sample code which causes the failure</a>
<h4 class="subtitle">Beautify and format your VHDL code online</h4>
<p>Proper formatting makes code easier to read and understand.</p>
<p>Please make a backup before you replace your code!</p>
<p style="color:#888">
<a href="https://github.com/g2384/VHDLFormatter#release-notes">Release notes</a>
<a href="https://github.com/g2384/VHDLFormatter/issues/new" target="_blank">Report <span>&#x1f41e;</span> bug</a>
<a href="https://github.com/g2384/VHDLFormatter">Source code</a>
</p>
<div id="lastModified"></div>
<textarea id="in" rows="10" wrap="off"></textarea>
<br>
<div class="checkbox">
<input type="checkbox" id="no_format" onclick="noFormat()">
<label for="no_format">Only highlight, don't format</label>
</div>
<form id="keyword">Keyword Case:
<label>
<input type="radio" name="keywordcase" value="UpperCase" checked="checked">UPPERCASE</label>
|
<input type="radio" name="keywordcase" value="UpperCase" checked="checked">UPPERCASE</label> |
<label>
<input type="radio" name="keywordcase" value="LowerCase">lowercase</label>
|
<input type="radio" name="keywordcase" value="LowerCase">lowercase</label> |
<label>
<input type="radio" name="keywordcase" value="DefaultCase">Default</label>
</form>
<label>
<input type="checkbox" id="no_format" onclick="noFormat()">Only highlight, don't format</label>
<br> New line after
<br>
<form id="new_line_after_then">
<span>THEN</span>
<label>
<fieldset id="new_line_after_div">
<legend>New line after</legend>
<form id="new_line_after_then">
<span class="code">THEN</span>
<label>
<input type="radio" name="new_line_after_thencase" value="NewLine" checked="checked">New Line</label>
<label>
<label>
<input type="radio" name="new_line_after_thencase" value="NoNewLine">No New Line</label>
<label>
<label>
<input type="radio" name="new_line_after_thencase" value="None">None</label>
</form>
<form id="new_line_after_semicolon">
<span>semicolon ";"</span>
<label>
</form>
<form id="new_line_after_semicolon">
<span class="code">semicolon ";"</span>
<label>
<input type="radio" name="new_line_after_semicoloncase" value="NewLine" checked="checked">New Line</label>
<label>
<label>
<input type="radio" name="new_line_after_semicoloncase" value="NoNewLine">No New Line</label>
<label>
<label>
<input type="radio" name="new_line_after_semicoloncase" value="None">None</label>
</form>
<form id="new_line_after_else">
<span>ELSE</span>
<label>
<input type="radio" name="new_line_after_elsecase" value="NewLine" checked="checked">New Line</label>
<label>
</form>
<form id="new_line_after_else">
<span class="code">ELSE</span>
<label>
<input type="radio" name="new_line_after_elsecase" value="NewLine">New Line</label>
<label>
<input type="radio" name="new_line_after_elsecase" value="NoNewLine">No New Line</label>
<label>
<input type="radio" name="new_line_after_elsecase" value="None">None</label>
</form>
<form id="new_line_after_port">
<span>PORT or PORT MAP</span>
<label>
<label>
<input type="radio" name="new_line_after_elsecase" value="None" checked="checked">None</label>
</form>
<form id="new_line_after_port">
<span class="code">PORT | PORT MAP</span>
<label>
<input type="radio" name="new_line_after_portcase" value="NewLine">New Line</label>
<label>
<label>
<input type="radio" name="new_line_after_portcase" value="NoNewLine">No New Line</label>
<label>
<label>
<input type="radio" name="new_line_after_portcase" value="None" checked="checked">None</label>
</form>
<form id="new_line_after_generic">
<span>GENERIC</span>
<label>
</form>
<form id="new_line_after_generic">
<span class="code">GENERIC</span>
<label>
<input type="radio" name="new_line_after_genericcase" value="NewLine">New Line</label>
<label>
<label>
<input type="radio" name="new_line_after_genericcase" value="NoNewLine">No New Line</label>
<label>
<label>
<input type="radio" name="new_line_after_genericcase" value="None" checked="checked">None</label>
</form>
<br>
<label>
<input type="checkbox" id="remove_comments">Remove commments</label>
|
<label>
<input type="checkbox" id="remove_lines">Remove blank lines</label>
|
<label>
<input type="checkbox" id="remove_report">Remove REPORT</label>
<br>
<label>
<input type="checkbox" id="check_alias">Check ALIAS (every long name is replaced with ALIAS)</label>
<br>
<label>
<input type="checkbox" id="sign_align">Align signs in PORT() GENERIC()</label>
<br>
<label>
<input type="checkbox" id="sign_align_all">Align signs in all places</label>
<br>
<label>
<input type="checkbox" id="use_space">Customise Indentation (tab is \t): </label>
<input type="text" id="cust_indent" size="8" onKeyUp="indent_decode()" value=" " /> (
<span id="indent_s">four blankspace</span>)
<br>
<label>
<input type="checkbox" id="compress">! EVIL - compress VHDL (\r\n, comments will be removed)</label>
<br>
<label>
<input type="checkbox" id="mix_letter">! EVIL - unreadable (mix upper/lower-case letters)</label>
</form>
</fieldset>
<div class="checkbox inline" id="remove_comments_div">
<input type="checkbox" id="remove_comments">
<label for="remove_comments">Remove commments</label> |
</div>
<div class="checkbox inline" id="remove_lines_div">
<input type="checkbox" id="remove_lines">
<label for="remove_lines">Remove blank lines</label> |
</div>
<div class="checkbox inline" id="remove_report_div">
<input type="checkbox" id="remove_report">
<label for="remove_report">Remove REPORT</label>
</div>
<br>
<div class="checkbox" id="check_alias_div">
<input type="checkbox" id="check_alias">
<label for="check_alias">Check ALIAS (every long name is replaced with ALIAS)</label>
</div>
<div id="sign_align_in_div">
Align signs in
<div class="checkbox inline" id="sign_align_port_div">
<input type="checkbox" id="sign_align_port">
<label for="sign_align_port" class="code">PORT()</label>
</div>
<div class="checkbox inline" id="sign_align_generic_div">
<input type="checkbox" id="sign_align_generic">
<label for="sign_align_generic" class="code">GENERIC()</label></div>
<div class="checkbox inline" id="sign_align_procedure_div">
<input type="checkbox" id="sign_align_procedure">
<label for="sign_align_procedure" class="code">PROCEDURE()</label>
</div>
<div class="checkbox inline" id="sign_align_function_div">
<input type="checkbox" id="sign_align_function">
<label for="sign_align_function">FUNCTION()</label>
</div>
</div>
<div class="checkbox" id="sign_align_all_div">
<input type="checkbox" id="sign_align_all">
<label for="sign_align_all">Align signs in all places</label>
</div>
<div id="customise_indentation_div">
<div class="checkbox inline" id="use_space_div">
<input type="checkbox" id="use_space">
<label for="use_space">Customise Indentation: </label>
</div>(tab is \t)
<input type="text" id="cust_indent" size="8" onKeyUp="indent_decode()" value=" " /> (
<span id="indent_s">four blankspaces</span>)
</div>
<div class="checkbox" id="compress_div">
<input type="checkbox" id="compress">
<label for="compress">! EVIL - compress VHDL (\r\n, comments will be removed)</label>
</div>
<div class="checkbox" id="mix_letter_div">
<input type="checkbox" id="mix_letter">
<label for="mix_letter">! EVIL - unreadable (mix upper/lower-case letters)</label>
</div>
<input type="button" class="btn" onclick="f()" value="start" />
<span class="show">
<input type="button" class="btn " id="selectAll" value="Select All" onclick="selectText(document.getElementById('vhdl'))"
/>
<label>
<input type="checkbox" onclick="wordWrap()" id="">Word wrap</label>
<div class="checkbox inline">
<input type="checkbox" onclick="wordWrap()" id="word_wrap">
<label for="word_wrap">Word wrap</label>
</div>
</span>
<br>
<pre id="result"><code class="vhdl" id="vhdl">
@ -266,12 +415,8 @@
var exports = {};
</script>
<script src="VHDLFormatter.js"></script>
<script src="descriptiveCounter.js"></script>
<script>
let lastModifiedDate = fetchHeader(location.href, 'Last-Modified');
if (lastModifiedDate != "") {
document.getElementById("lastModified").innerHTML = "<p>Last modified: " + lastModifiedDate + "</p>";
}
function f() {
var input = document.getElementById("in").value;
var no_format = document.getElementById("no_format").checked;
@ -285,7 +430,10 @@
var remove_lines = document.getElementById("remove_lines").checked;
var remove_report = document.getElementById("remove_report").checked;
var check_alias = document.getElementById("check_alias").checked;
var sign_align = document.getElementById("sign_align").checked;
var sign_align_port = document.getElementById("sign_align_port").checked;
var sign_align_function = document.getElementById("sign_align_function").checked;
var sign_align_procedure = document.getElementById("sign_align_procedure").checked;
var sign_align_generic = document.getElementById("sign_align_generic").checked;
var sign_align_all = document.getElementById("sign_align_all").checked;
var new_line_after_port = document.getElementById("new_line_after_port").elements.namedItem("new_line_after_portcase").value;
var new_line_after_then = document.getElementById("new_line_after_then").elements.namedItem("new_line_after_thencase").value;
@ -316,10 +464,26 @@
newLineSettingsDict["then"] = new_line_after_then;
newLineSettingsDict["else"] = new_line_after_else;
newLineSettings = ConstructNewLineSettings(newLineSettingsDict);
var signAlignKeywords = [];
if (sign_align_function) {
signAlignKeywords.push("FUNCTION");
signAlignKeywords.push("IMPURE FUNCTION");
}
if (sign_align_generic) {
signAlignKeywords.push("GENERIC");
}
if (sign_align_port) {
signAlignKeywords.push("PORT");
}
if (sign_align_procedure) {
signAlignKeywords.push("PROCEDURE");
}
sign_align = signAlignKeywords.length > 0;
beautifierSettings = new BeautifierSettings(remove_comments, remove_report, check_alias, sign_align,
sign_align_all,
keywordcase, indentation, newLineSettings);
beautifierSettings.SignAlignKeyWords = signAlignKeywords;
input = beautify(input, beautifierSettings);

+ 1437
- 0
tests/VHDLFormatterUnitTests.ts
File diff suppressed because it is too large
View File


+ 27
- 0
tests/assert.ts View File

@ -0,0 +1,27 @@
export function assert(testName: string, expected: string, actual: string, message?: undefined) {
var result = CompareString(actual, expected);
if (result != true) {
console.log('"' + testName + "\" failed: \n" + result);
}
else {
//console.log(testName + " pass");
}
}
export function CompareString(actual: string, expected: string) {
var l = Math.min(actual.length, expected.length);
for (var i = 0; i < l; i++) {
if (actual[i] != expected[i]) {
var toEnd = Math.min(i + 50, l);
return '\ndifferent at ' + i.toString() +
'\nactual: "\n' + actual.substring(i, toEnd) +
'\nexpected: "\n' + expected.substring(i, toEnd) + '"\n---' +
"\nactual (full): \n" + actual + "\n---" +
"\nexpected (full): \n" + expected + "\n====\n";
}
}
if (actual != expected) {
return 'actual: \n"' + actual + '"\nexpected: \n"' + expected + '"';
}
return true;
}

+ 30
- 0
tests/descriptiveCounterTests.ts View File

@ -0,0 +1,30 @@
import { descriptiveCounter } from "../descriptiveCounter";
import { assert } from "./assert";
let testCount: number = 0;
type StringCallback = (text: string) => string;
var showUnitTests = true;//window.location.href.indexOf("http") < 0;
export function testDescriptiveCounter() {
if (showUnitTests) {
testCount = 0;
start();
console.log("total tests: " + testCount);
}
}
function start() {
console.log("=== descriptiveCounter ===");
test(descriptiveCounter, "one blankspace", " ", "one blankspace");
test(descriptiveCounter, "mixed chars", " A ", "one blankspace & one 'A' & one blankspace");
test(descriptiveCounter, "4 blankspaces", " ", "four blankspaces");
test(descriptiveCounter, "9 blankspaces", " ", "many blankspaces");
test(descriptiveCounter, "2 As", "AA", "two 'A's");
}
function test(func: StringCallback, testName: string, inputs, expected: string) {
let actual: string = func(inputs);
assert(testName, expected, actual);
testCount++;
}

+ 4
- 1
tsconfig.json View File

@ -6,8 +6,11 @@
"sourceMap": true
},
"files": [
"descriptiveCounter.ts",
"VHDLFormatter.ts",
"VHDLFormatterUnitTests.ts"
"tests/VHDLFormatterUnitTests.ts",
"tests/descriptiveCounterTests.ts",
"tests/assert.ts",
],
"exclude": [
"node_modules",


Loading…
Cancel
Save