Browse Source

fix unary operator formatting

master
g2384 5 years ago
parent
commit
f0912cbe51
5 changed files with 39 additions and 16 deletions
  1. +1
    -0
      README.md
  2. +9
    -7
      VHDLFormatter.js
  3. +9
    -8
      VHDLFormatter.ts
  4. +1
    -1
      index.html
  5. +19
    -0
      tests/VHDLFormatterUnitTests.ts

+ 1
- 0
README.md View File

@ -19,6 +19,7 @@ VHDL formatter web online written in javascript
- fix exponential notation
- user can choose EOL symbols (or use system's by default)
- align comments (when user chooses "align" option)
- bugfix "extra whitespaces around unary minus or plus"
Many thanks to [@MihaiBabiac](https://github.com/MihaiBabiac)


+ 9
- 7
VHDLFormatter.js View File

@ -181,6 +181,8 @@ function beautify(input, settings) {
var arr = input.split("\r\n");
var comments = EscapeComments(arr);
var backslashes = escapeText(arr, "\\\\[^\\\\]+\\\\", ILBackslash);
let quotes = escapeText(arr, '"([^"]+)"', ILQuote);
let singleQuotes = escapeText(arr, "'[^']'", ILSingleQuote);
RemoveLeadingWhitespaces(arr);
input = arr.join("\r\n");
if (settings.RemoveComments) {
@ -188,17 +190,13 @@ function beautify(input, settings) {
input = input.replace(/@@comments[0-9]+/g, '');
comments = [];
}
input = SetKeywordCase(input, "uppercase", KeyWords, TypeNames);
input = RemoveExtraNewLines(input);
input = input.replace(/[\t ]+/g, ' ');
input = input.replace(/\([\t ]+/g, '\(');
input = input.replace(/[ ]+;/g, ';');
input = input.replace(/:[ ]*(PROCESS|ENTITY)/gi, ':$1');
arr = input.split("\r\n");
let quotes = escapeText(arr, '"([^"]+)"', ILQuote);
let singleQuotes = escapeText(arr, "'[^']'", ILSingleQuote);
input = arr.join("\r\n");
input = SetKeywordCase(input, "uppercase", KeyWords, TypeNames);
arr = input.split("\r\n");
if (settings.RemoveAsserts) {
RemoveAsserts(arr); //RemoveAsserts must be after EscapeQuotes
}
@ -212,7 +210,7 @@ function beautify(input, settings) {
input = arr.join("\r\n");
input = input.replace(/([a-zA-Z0-9\); ])\);(@@comments[0-9]+)?@@end/g, '$1\r\n);$2@@end');
input = input.replace(/[ ]?([&=:\-<>\+|\*])[ ]?/g, ' $1 ');
input = input.replace(/(\d+e) +([+\-]) +(\d+)/g, '$1$2$3'); // Fix exponential notation format broken by previous step
input = input.replace(/(\d+e) +([+\-]) +(\d+)/g, '$1$2$3'); // fix exponential notation format broken by previous step
input = input.replace(/[ ]?([,])[ ]?/g, '$1 ');
input = input.replace(/[ ]?(['"])(THEN)/g, '$1 $2');
input = input.replace(/[ ]?(\?)?[ ]?(<|:|>|\/)?[ ]+(=)?[ ]?/g, ' $1$2$3 ');
@ -226,7 +224,11 @@ function beautify(input, settings) {
input = input.replace(/\r\n\r\n\r\n/g, '\r\n');
input = input.replace(/[\r\n\s]+$/g, '');
input = input.replace(/[ \t]+\)/g, ')');
input = input.replace(/\s*\)\s+RETURN\s+([\w]+;)/g, '\r\n) RETURN $1'); //function(..\r\n)return type; -> function(..\r\n)return type;
input = input.replace(/\s*\)\s+RETURN\s+([\w]+;)/g, '\r\n) RETURN $1'); //function(..)\r\nreturn type; -> function(..\r\n)return type;
let keywordAndSignRegex = new RegExp("(\\b" + KeyWords.join("\\b|\\b") + "\\b) +([\\-+]) +(\\w)", "g");
input = input.replace(keywordAndSignRegex, "$1 $2$3"); // `WHEN - 2` -> `WHEN -2`
input = input.replace(/([,|]) +([+\-]) +(\w)/g, '$1 $2$3'); // `1, - 2)` -> `1, -2)`
input = input.replace(/(\() +([+\-]) +(\w)/g, '$1$2$3'); // `( - 2)` -> `(-2)`
arr = input.split("\r\n");
let result = [];
beautify3(arr, result, settings, 0, 0);


+ 9
- 8
VHDLFormatter.ts View File

@ -230,6 +230,8 @@ export function beautify(input: string, settings: BeautifierSettings) {
var arr = input.split("\r\n");
var comments = EscapeComments(arr);
var backslashes = escapeText(arr, "\\\\[^\\\\]+\\\\", ILBackslash);
let quotes = escapeText(arr, '"([^"]+)"', ILQuote);
let singleQuotes = escapeText(arr, "'[^']'", ILSingleQuote);
RemoveLeadingWhitespaces(arr);
input = arr.join("\r\n");
@ -239,18 +241,13 @@ export function beautify(input: string, settings: BeautifierSettings) {
comments = [];
}
input = SetKeywordCase(input, "uppercase", KeyWords, TypeNames);
input = RemoveExtraNewLines(input);
input = input.replace(/[\t ]+/g, ' ');
input = input.replace(/\([\t ]+/g, '\(');
input = input.replace(/[ ]+;/g, ';');
input = input.replace(/:[ ]*(PROCESS|ENTITY)/gi, ':$1');
arr = input.split("\r\n");
let quotes = escapeText(arr, '"([^"]+)"', ILQuote);
let singleQuotes = escapeText(arr, "'[^']'", ILSingleQuote);
input = arr.join("\r\n");
input = SetKeywordCase(input, "uppercase", KeyWords, TypeNames);
arr = input.split("\r\n");
if (settings.RemoveAsserts) {
RemoveAsserts(arr);//RemoveAsserts must be after EscapeQuotes
@ -267,7 +264,7 @@ export function beautify(input: string, settings: BeautifierSettings) {
input = input.replace(/([a-zA-Z0-9\); ])\);(@@comments[0-9]+)?@@end/g, '$1\r\n);$2@@end');
input = input.replace(/[ ]?([&=:\-<>\+|\*])[ ]?/g, ' $1 ');
input = input.replace(/(\d+e) +([+\-]) +(\d+)/g, '$1$2$3');// Fix exponential notation format broken by previous step
input = input.replace(/(\d+e) +([+\-]) +(\d+)/g, '$1$2$3');// fix exponential notation format broken by previous step
input = input.replace(/[ ]?([,])[ ]?/g, '$1 ');
input = input.replace(/[ ]?(['"])(THEN)/g, '$1 $2');
input = input.replace(/[ ]?(\?)?[ ]?(<|:|>|\/)?[ ]+(=)?[ ]?/g, ' $1$2$3 ');
@ -281,7 +278,11 @@ export function beautify(input: string, settings: BeautifierSettings) {
input = input.replace(/\r\n\r\n\r\n/g, '\r\n');
input = input.replace(/[\r\n\s]+$/g, '');
input = input.replace(/[ \t]+\)/g, ')');
input = input.replace(/\s*\)\s+RETURN\s+([\w]+;)/g, '\r\n) RETURN $1');//function(..\r\n)return type; -> function(..\r\n)return type;
input = input.replace(/\s*\)\s+RETURN\s+([\w]+;)/g, '\r\n) RETURN $1');//function(..)\r\nreturn type; -> function(..\r\n)return type;
let keywordAndSignRegex = new RegExp("(\\b" + KeyWords.join("\\b|\\b") + "\\b) +([\\-+]) +(\\w)", "g");
input = input.replace(keywordAndSignRegex, "$1 $2$3");// `WHEN - 2` -> `WHEN -2`
input = input.replace(/([,|]) +([+\-]) +(\w)/g, '$1 $2$3');// `1, - 2)` -> `1, -2)`
input = input.replace(/(\() +([+\-]) +(\w)/g, '$1$2$3');// `( - 2)` -> `(-2)`
arr = input.split("\r\n");
let result: (FormattedLine | FormattedLine[])[] = [];
beautify3(arr, result, settings, 0, 0);


+ 1
- 1
index.html View File

@ -311,7 +311,7 @@
<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>
<label for="check_alias">Check ALIAS (all long names will be replaced by ALIAS names)</label>
</div>
<div id="sign_align_in_div">
Align signs in


+ 19
- 0
tests/VHDLFormatterUnitTests.ts View File

@ -915,6 +915,8 @@ function IntegrationTest() {
IntegrationTest74();
IntegrationTest75();
IntegrationTest76();
IntegrationTest77();
IntegrationTest78();
}
function IntegrationTest23() {
@ -1339,6 +1341,23 @@ function IntegrationTest76() {
assertAndCountTest("align <= => signs", expected, actual);
}
function IntegrationTest77() {
let settings = GetDefaultSettings();
settings.SignAlignAll = true;
let input = "WHEN -2;\r\nSIGNAL +0;";
let actual = beautify(input, settings);
assertAndCountTest("negative sign and number", input, actual);
}
function IntegrationTest78() {
let settings = GetDefaultSettings();
settings.SignAlignAll = true;
let input = "sfixed(4 downto - 18);\r\nx <= to_sfixed( - 2.3, x);\r\nresize(x + 1, 4, - 18) when others;";
let expected = "sfixed(4 DOWNTO -18);\r\nx <= to_sfixed(-2.3, x);\r\nresize(x + 1, 4, -18) WHEN OTHERS;";
let actual = beautify(input, settings);
assertAndCountTest("negative sign and number", expected, actual);
}
function GetDefaultSettings(indentation: string = " "): BeautifierSettings {
let new_line_after_symbols = new NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];


Loading…
Cancel
Save