Browse Source

support VHDL-2008 block comment /* */

master
g2384 4 years ago
parent
commit
d83221ffa8
5 changed files with 210 additions and 15 deletions
  1. +82
    -8
      VHDLFormatter.js
  2. +76
    -4
      VHDLFormatter.ts
  3. +52
    -0
      tests/VHDLFormatter.test.ts
  4. +0
    -2
      tests/VHDLFormatterUnitTests.ts
  5. +0
    -1
      tsconfig.json

+ 82
- 8
VHDLFormatter.js View File

@ -68,9 +68,23 @@ String.prototype.regexIndexOf = function (pattern, startIndex) {
return (-1 === searchResult) ? -1 : searchResult + startIndex; return (-1 === searchResult) ? -1 : searchResult + startIndex;
}; };
String.prototype.regexLastIndexOf = function (pattern, startIndex) { String.prototype.regexLastIndexOf = function (pattern, startIndex) {
startIndex = startIndex === undefined ? this.length : startIndex;
var searchResult = this.substr(0, startIndex).reverse().regexIndexOf(pattern, 0);
return (-1 === searchResult) ? -1 : this.length - ++searchResult;
pattern = (pattern.global) ? pattern :
new RegExp(pattern.source, 'g' + (pattern.ignoreCase ? 'i' : '') + (pattern.multiline ? 'm' : ''));
if (typeof (startIndex) === 'undefined') {
startIndex = this.length;
}
else if (startIndex < 0) {
startIndex = 0;
}
const stringToWorkWith = this.substring(0, startIndex + 1);
let lastIndexOf = -1;
let nextStop = 0;
let result;
while ((result = pattern.exec(stringToWorkWith)) != null) {
lastIndexOf = result.index;
pattern.lastIndex = ++nextStop;
}
return lastIndexOf;
}; };
String.prototype.reverse = function () { String.prototype.reverse = function () {
return this.split('').reverse().join(''); return this.split('').reverse().join('');
@ -96,6 +110,66 @@ function EscapeComments(arr) {
count++; count++;
} }
} }
var isInComment = false;
var commentRegex = new RegExp("(?<=" + ILCommentPrefix + "[\\d]+).");
for (var i = 0; i < arr.length; i++) {
var commentStartIndex = 0;
var hasComment = true;
var commentEndInlineIndex = 0;
while (hasComment) {
var line = arr[i];
if (!isInComment) {
commentStartIndex = line.indexOf("/*");
var commentEndIndex = line.indexOf("*/", commentStartIndex);
if (commentStartIndex >= 0) {
if (commentEndIndex >= 0) {
commentEndInlineIndex = commentEndIndex + 2;
isInComment = false;
comments.push(line.substring(commentStartIndex, commentEndInlineIndex));
arr[i] = line.substr(0, commentStartIndex) + ILCommentPrefix + count + line.substr(commentEndInlineIndex);
count++;
hasComment = true;
if (commentStartIndex + 2 == line.length) {
hasComment = false;
}
}
else {
isInComment = true;
comments.push(line.substr(commentStartIndex));
arr[i] = line.substr(0, commentStartIndex) + ILCommentPrefix + count;
count++;
hasComment = false;
}
}
else {
hasComment = false;
}
continue;
}
if (isInComment) {
var lastCommentEndIndex = line.regexLastIndexOf(commentRegex, line.length);
if (commentStartIndex == 0) {
var commentEndIndex = line.indexOf("*/", lastCommentEndIndex);
}
else {
var commentEndIndex = line.indexOf("*/", commentStartIndex);
}
if (commentEndIndex >= 0) {
isInComment = false;
comments.push(line.substr(0, commentEndIndex + 2));
arr[i] = ILCommentPrefix + count + line.substr(commentEndIndex + 2);
count++;
hasComment = true;
}
else {
comments.push(line);
arr[i] = ILCommentPrefix + count;
count++;
hasComment = false;
}
}
}
}
return comments; return comments;
} }
function ToLowerCases(arr) { function ToLowerCases(arr) {
@ -623,6 +697,10 @@ function beautify3(inputs, result, settings, startIndex, indent, endIndex) {
Mode = modeCache; Mode = modeCache;
continue; continue;
} }
if (input.regexStartsWith(/.*?\:\=\s*\($/)) {
[i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, ":=");
continue;
}
if (input.regexStartsWith(/[\w\s:]*\bPORT\b([\s]|$)/)) { if (input.regexStartsWith(/[\w\s:]*\bPORT\b([\s]|$)/)) {
[i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, "PORT"); [i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, "PORT");
continue; continue;
@ -635,10 +713,6 @@ function beautify3(inputs, result, settings, startIndex, indent, endIndex) {
[i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, "GENERIC"); [i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, "GENERIC");
continue; continue;
} }
if (input.regexStartsWith(/.*?\:\=\s*\($/)) {
[i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, ":=");
continue;
}
if (input.regexStartsWith(/[\w\s:]*PROCEDURE[\s\w]+\($/)) { if (input.regexStartsWith(/[\w\s:]*PROCEDURE[\s\w]+\($/)) {
[i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, "PROCEDURE"); [i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, "PROCEDURE");
if (inputs[i].regexStartsWith(/.*\)[\s]*IS/)) { if (inputs[i].regexStartsWith(/.*\)[\s]*IS/)) {
@ -764,4 +838,4 @@ function RemoveExtraNewLines(input) {
input = input.replace(/\r\n\r\n\r\n/g, '\r\n'); input = input.replace(/\r\n\r\n\r\n/g, '\r\n');
return input; return input;
} }
//# sourceMappingURL=VHDLFormatter.js.map
//# sourceMappingURL=VHDLFormatter.js.map

+ 76
- 4
VHDLFormatter.ts View File

@ -93,9 +93,22 @@ String.prototype.regexIndexOf = function (pattern, startIndex) {
} }
String.prototype.regexLastIndexOf = function (pattern, startIndex) { String.prototype.regexLastIndexOf = function (pattern, startIndex) {
startIndex = startIndex === undefined ? this.length : startIndex;
var searchResult = this.substr(0, startIndex).reverse().regexIndexOf(pattern, 0);
return (-1 === searchResult) ? -1 : this.length - ++searchResult;
pattern = (pattern.global) ? pattern :
new RegExp(pattern.source, 'g' + (pattern.ignoreCase ? 'i' : '') + (pattern.multiline ? 'm' : ''));
if (typeof (startIndex) === 'undefined') {
startIndex = this.length;
} else if (startIndex < 0) {
startIndex = 0;
}
const stringToWorkWith = this.substring(0, startIndex + 1);
let lastIndexOf = -1;
let nextStop = 0;
let result: RegExpExecArray;
while ((result = pattern.exec(stringToWorkWith)) != null) {
lastIndexOf = result.index;
pattern.lastIndex = ++nextStop;
}
return lastIndexOf;
} }
String.prototype.reverse = function () { String.prototype.reverse = function () {
@ -117,7 +130,7 @@ function EscapeComments(arr: Array<string>): Array<string> {
var comments = []; var comments = [];
var count = 0; var count = 0;
for (var i = 0; i < arr.length; i++) { for (var i = 0; i < arr.length; i++) {
var line: string = arr[i];
var line = arr[i];
var commentStartIndex = line.indexOf("--"); var commentStartIndex = line.indexOf("--");
if (commentStartIndex >= 0) { if (commentStartIndex >= 0) {
comments.push(line.substr(commentStartIndex)); comments.push(line.substr(commentStartIndex));
@ -125,6 +138,65 @@ function EscapeComments(arr: Array<string>): Array<string> {
count++; count++;
} }
} }
var isInComment = false;
var commentRegex = new RegExp("(?<=" + ILCommentPrefix + "[\\d]+).");
for (var i = 0; i < arr.length; i++) {
var commentStartIndex = 0;
var hasComment = true;
var commentEndInlineIndex = 0;
while (hasComment) {
var line = arr[i];
if (!isInComment) {
commentStartIndex = line.indexOf("/*");
var commentEndIndex = line.indexOf("*/", commentStartIndex);
if (commentStartIndex >= 0) {
if (commentEndIndex >= 0) {
commentEndInlineIndex = commentEndIndex + 2;
isInComment = false;
comments.push(line.substring(commentStartIndex, commentEndInlineIndex));
arr[i] = line.substr(0, commentStartIndex) + ILCommentPrefix + count + line.substr(commentEndInlineIndex);
count++;
hasComment = true;
if (commentStartIndex + 2 == line.length) {
hasComment = false;
}
}
else {
isInComment = true;
comments.push(line.substr(commentStartIndex));
arr[i] = line.substr(0, commentStartIndex) + ILCommentPrefix + count;
count++;
hasComment = false;
}
}
else {
hasComment = false;
}
continue;
}
if (isInComment) {
var lastCommentEndIndex = line.regexLastIndexOf(commentRegex, line.length);
if (commentStartIndex == 0) {
var commentEndIndex = line.indexOf("*/", lastCommentEndIndex);
}
else {
var commentEndIndex = line.indexOf("*/", commentStartIndex);
}
if (commentEndIndex >= 0) {
isInComment = false;
comments.push(line.substr(0, commentEndIndex + 2));
arr[i] = ILCommentPrefix + count + line.substr(commentEndIndex + 2);
count++;
hasComment = true;
} else {
comments.push(line);
arr[i] = ILCommentPrefix + count;
count++;
hasComment = false;
}
}
}
}
return comments return comments
} }


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

@ -0,0 +1,52 @@
import { beautify, signAlignSettings } 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";
describe('VHDLFormatter', function () {
it('do not format block comments', function () {
let settings = GetDefaultSettings();
let input = 'a;\r\n/*a; \r\n b;\r\nport ( ) ;\r\n/*\r\n*/c;';
let result = beautify(input, settings);
expect(result).toBe(input);
});
it('do not format block comments 2', function () {
let settings = GetDefaultSettings();
let input = 'a;/*a;*/b;\r\nc;';
let result = beautify(input, settings);
expect(result).toBe(input);
});
it('do not format block comments 3', function () {
let settings = GetDefaultSettings();
let input = 'a;\r\n/*a;*//*\r\n*/c;';
let result = beautify(input, settings);
expect(result).toBe(input);
});
it('do not format block comments 4', function () {
let settings = GetDefaultSettings();
let input = '/*\r\nwoof\r\n*//*\r\nwoof\r\n*//*\r\nwoof\r\n*/';
let result = beautify(input, settings);
expect(result).toBe(input);
});
});
function GetDefaultSettings(indentation: string = " "): BeautifierSettings {
let new_line_after_symbols = new NewLineSettings();
new_line_after_symbols.newLineAfter = ["then", ";"];
new_line_after_symbols.noNewLineAfter = ["generic"];
let signAligns = new signAlignSettings(false, false, "", []);
let settings = getDefaultBeautifierSettings(new_line_after_symbols, signAligns, indentation);
return settings;
}
function getDefaultBeautifierSettings(newLineSettings: NewLineSettings, signAlignSettings: signAlignSettings = null, indentation: string = " "): BeautifierSettings {
return new BeautifierSettings(false, false, false, signAlignSettings, "uppercase", "uppercase", indentation, newLineSettings, "\r\n");
}

+ 0
- 2
tests/VHDLFormatterUnitTests.ts View File

@ -9,7 +9,6 @@ import { FormattedLine } from "../VHDLFormatter";
import { FormattedLineToString } from "../VHDLFormatter"; import { FormattedLineToString } from "../VHDLFormatter";
import { CompareString } from "./assert"; import { CompareString } from "./assert";
import { assert } from "./assert"; import { assert } from "./assert";
import { testDescriptiveCounter } from "./descriptiveCounterTests";
let testCount: number = 0; let testCount: number = 0;
@ -25,7 +24,6 @@ if (showUnitTests) {
UnitTestFormattedLineToString(); UnitTestFormattedLineToString();
UnitTestbeautify3(); UnitTestbeautify3();
console.log("total tests: " + testCount); console.log("total tests: " + testCount);
testDescriptiveCounter();
} }
interface Function { interface Function {


+ 0
- 1
tsconfig.json View File

@ -10,7 +10,6 @@
"descriptiveCounter.ts", "descriptiveCounter.ts",
"VHDLFormatter.ts", "VHDLFormatter.ts",
"tests/VHDLFormatterUnitTests.ts", "tests/VHDLFormatterUnitTests.ts",
"tests/descriptiveCounterTests.ts",
"tests/assert.ts", "tests/assert.ts",
], ],
"exclude": [ "exclude": [


Loading…
Cancel
Save