You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

946 lines
38 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.RemoveAsserts = exports.ApplyNoNewLineAfter = exports.beautify3 = exports.beautifySemicolonBlock = exports.beautifyVariableInitialiseBlock = exports.beautifyPackageIsNewBlock = exports.beautifyComponentBlock = exports.beautifyCaseBlock = exports.AlignSign = exports.AlignSigns = exports.beautifyPortGenericBlock = exports.FormattedLineToString = exports.FormattedLine = exports.beautify = exports.BeautifierSettings = exports.signAlignSettings = exports.SetNewLinesAfterSymbols = exports.NewLineSettings = void 0;
  4. let isTesting = false;
  5. const ILEscape = "@@";
  6. const ILCommentPrefix = ILEscape + "comments";
  7. const ILIndentedReturnPrefix = ILEscape;
  8. const ILQuote = "⨵";
  9. const ILSingleQuote = "⦼";
  10. const ILBackslash = "⨸";
  11. const ILSemicolon = "⨴";
  12. var FormatMode;
  13. (function (FormatMode) {
  14. FormatMode[FormatMode["Default"] = 0] = "Default";
  15. FormatMode[FormatMode["EndsWithSemicolon"] = 1] = "EndsWithSemicolon";
  16. FormatMode[FormatMode["CaseWhen"] = 2] = "CaseWhen";
  17. FormatMode[FormatMode["IfElse"] = 3] = "IfElse";
  18. FormatMode[FormatMode["PortGeneric"] = 4] = "PortGeneric";
  19. })(FormatMode || (FormatMode = {}));
  20. let Mode = FormatMode.Default;
  21. class NewLineSettings {
  22. constructor() {
  23. this.newLineAfter = [];
  24. this.noNewLineAfter = [];
  25. }
  26. newLineAfterPush(keyword) {
  27. this.newLineAfter.push(keyword);
  28. }
  29. noNewLineAfterPush(keyword) {
  30. this.noNewLineAfter.push(keyword);
  31. }
  32. push(keyword, addNewLine) {
  33. let str = addNewLine.toLowerCase();
  34. if (str == "none") {
  35. return;
  36. }
  37. else if (!str.startsWith("no")) {
  38. this.newLineAfterPush(keyword);
  39. }
  40. else {
  41. this.noNewLineAfterPush(keyword);
  42. }
  43. }
  44. }
  45. exports.NewLineSettings = NewLineSettings;
  46. function ConstructNewLineSettings(dict) {
  47. let settings = new NewLineSettings();
  48. for (let key in dict) {
  49. settings.push(key, dict[key]);
  50. }
  51. return settings;
  52. }
  53. String.prototype.regexCount = function (pattern) {
  54. if (pattern.flags.indexOf("g") < 0) {
  55. pattern = new RegExp(pattern.source, pattern.flags + "g");
  56. }
  57. return (this.match(pattern) || []).length;
  58. };
  59. String.prototype.count = function (text) {
  60. return this.split(text).length - 1;
  61. };
  62. String.prototype.regexStartsWith = function (pattern) {
  63. var searchResult = this.search(pattern);
  64. return searchResult == 0;
  65. };
  66. String.prototype.regexIndexOf = function (pattern, startIndex) {
  67. startIndex = startIndex || 0;
  68. var searchResult = this.substr(startIndex).search(pattern);
  69. return (-1 === searchResult) ? -1 : searchResult + startIndex;
  70. };
  71. String.prototype.regexLastIndexOf = function (pattern, startIndex) {
  72. pattern = (pattern.global) ? pattern :
  73. new RegExp(pattern.source, 'g' + (pattern.ignoreCase ? 'i' : '') + (pattern.multiline ? 'm' : ''));
  74. if (typeof (startIndex) === 'undefined') {
  75. startIndex = this.length;
  76. }
  77. else if (startIndex < 0) {
  78. startIndex = 0;
  79. }
  80. const stringToWorkWith = this.substring(0, startIndex + 1);
  81. let lastIndexOf = -1;
  82. let nextStop = 0;
  83. let result;
  84. while ((result = pattern.exec(stringToWorkWith)) != null) {
  85. lastIndexOf = result.index;
  86. pattern.lastIndex = ++nextStop;
  87. }
  88. return lastIndexOf;
  89. };
  90. String.prototype.reverse = function () {
  91. return this.split('').reverse().join('');
  92. };
  93. String.prototype.convertToRegexBlockWords = function () {
  94. let result = new RegExp("(" + this + ")([^\\w]|$)");
  95. return result;
  96. };
  97. Array.prototype.convertToRegexBlockWords = function () {
  98. let wordsStr = this.join("|");
  99. let result = new RegExp("(" + wordsStr + ")([^\\w]|$)");
  100. return result;
  101. };
  102. function EscapeComments(arr) {
  103. var comments = [];
  104. var count = 0;
  105. for (var i = 0; i < arr.length; i++) {
  106. var line = arr[i];
  107. var commentStartIndex = line.indexOf("--");
  108. if (commentStartIndex >= 0) {
  109. comments.push(line.substr(commentStartIndex));
  110. arr[i] = line.substr(0, commentStartIndex) + ILCommentPrefix + count;
  111. count++;
  112. }
  113. }
  114. var isInComment = false;
  115. var commentRegex = new RegExp("(?<=" + ILCommentPrefix + "[\\d]+).");
  116. for (var i = 0; i < arr.length; i++) {
  117. var commentStartIndex = 0;
  118. var hasComment = true;
  119. var commentEndInlineIndex = 0;
  120. while (hasComment) {
  121. var line = arr[i];
  122. if (!isInComment) {
  123. commentStartIndex = line.indexOf("/*");
  124. var commentEndIndex = line.indexOf("*/", commentStartIndex);
  125. if (commentStartIndex >= 0) {
  126. if (commentEndIndex >= 0) {
  127. commentEndInlineIndex = commentEndIndex + 2;
  128. isInComment = false;
  129. comments.push(line.substring(commentStartIndex, commentEndInlineIndex));
  130. arr[i] = line.substr(0, commentStartIndex) + ILCommentPrefix + count + line.substr(commentEndInlineIndex);
  131. count++;
  132. hasComment = true;
  133. if (commentStartIndex + 2 == line.length) {
  134. hasComment = false;
  135. }
  136. }
  137. else {
  138. isInComment = true;
  139. comments.push(line.substr(commentStartIndex));
  140. arr[i] = line.substr(0, commentStartIndex) + ILCommentPrefix + count;
  141. count++;
  142. hasComment = false;
  143. }
  144. }
  145. else {
  146. hasComment = false;
  147. }
  148. continue;
  149. }
  150. if (isInComment) {
  151. var lastCommentEndIndex = line.regexLastIndexOf(commentRegex, line.length);
  152. if (commentStartIndex == 0) {
  153. var commentEndIndex = line.indexOf("*/", lastCommentEndIndex);
  154. }
  155. else {
  156. var commentEndIndex = line.indexOf("*/", commentStartIndex);
  157. }
  158. if (commentEndIndex >= 0) {
  159. isInComment = false;
  160. comments.push(line.substr(0, commentEndIndex + 2));
  161. arr[i] = ILCommentPrefix + count + line.substr(commentEndIndex + 2);
  162. count++;
  163. hasComment = true;
  164. }
  165. else {
  166. comments.push(line);
  167. arr[i] = ILCommentPrefix + count;
  168. count++;
  169. hasComment = false;
  170. }
  171. }
  172. }
  173. }
  174. return comments;
  175. }
  176. function ToLowerCases(arr) {
  177. for (var i = 0; i < arr.length; i++) {
  178. arr[i] = arr[i].toLowerCase();
  179. }
  180. }
  181. function ToUpperCases(arr) {
  182. for (var i = 0; i < arr.length; i++) {
  183. arr[i] = arr[i].toUpperCase();
  184. }
  185. }
  186. function ToCamelCases(arr) {
  187. for (var i = 0; i < arr.length; i++) {
  188. arr[i] = arr[i].charAt(0) + arr[i].slice(1).toLowerCase();
  189. }
  190. }
  191. function ReplaceKeyWords(text, keywords) {
  192. for (var k = 0; k < keywords.length; k++) {
  193. text = text.replace(new RegExp("([^a-zA-Z0-9_@]|^)" + keywords[k] + "([^a-zA-Z0-9_]|$)", 'gi'), "$1" + keywords[k] + "$2");
  194. }
  195. return text;
  196. }
  197. function SetKeywordCase(input, keywordcase, keywords) {
  198. let inputcase = keywordcase.toLowerCase();
  199. switch (inputcase) {
  200. case "lowercase":
  201. ToLowerCases(keywords);
  202. break;
  203. case "defaultcase":
  204. ToCamelCases(keywords);
  205. break;
  206. case "uppercase":
  207. ToUpperCases(keywords);
  208. }
  209. input = ReplaceKeyWords(input, keywords);
  210. return input;
  211. }
  212. function SetNewLinesAfterSymbols(text, newLineSettings) {
  213. if (newLineSettings == null) {
  214. return text;
  215. }
  216. if (newLineSettings.newLineAfter != null) {
  217. newLineSettings.newLineAfter.forEach(symbol => {
  218. let upper = symbol.toUpperCase();
  219. var rexString = "(" + upper + ")[ ]?([^ \r\n@])";
  220. let regex = null;
  221. if (upper.regexStartsWith(/\w/)) {
  222. regex = new RegExp("\\b" + rexString, "g");
  223. }
  224. else {
  225. regex = new RegExp(rexString, "g");
  226. }
  227. text = text.replace(regex, '$1\r\n$2');
  228. if (upper == "PORT") {
  229. text = text.replace(/\bPORT\b\s+MAP/, "PORT MAP");
  230. }
  231. });
  232. }
  233. if (newLineSettings.noNewLineAfter != null) {
  234. newLineSettings.noNewLineAfter.forEach(symbol => {
  235. let rexString = "(" + symbol.toUpperCase() + ")[ \r\n]+([^@])";
  236. let regex = null;
  237. if (symbol.regexStartsWith(/\w/)) {
  238. regex = new RegExp("\\b" + rexString, "g");
  239. text = text.replace(regex, '$1 $2');
  240. }
  241. else {
  242. regex = new RegExp(rexString, "g");
  243. }
  244. text = text.replace(regex, '$1 $2');
  245. });
  246. }
  247. return text;
  248. }
  249. exports.SetNewLinesAfterSymbols = SetNewLinesAfterSymbols;
  250. class signAlignSettings {
  251. constructor(isRegional, isAll, mode, keyWords, alignComments = false) {
  252. this.isRegional = isRegional;
  253. this.isAll = isAll;
  254. this.mode = mode;
  255. this.keyWords = keyWords;
  256. this.alignComments = alignComments;
  257. }
  258. }
  259. exports.signAlignSettings = signAlignSettings;
  260. class BeautifierSettings {
  261. constructor(removeComments, removeReport, checkAlias, signAlignSettings, keywordCase, typeNameCase, indentation, newLineSettings, endOfLine, addNewLine) {
  262. this.RemoveComments = removeComments;
  263. this.RemoveAsserts = removeReport;
  264. this.CheckAlias = checkAlias;
  265. this.SignAlignSettings = signAlignSettings;
  266. this.KeywordCase = keywordCase;
  267. this.TypeNameCase = typeNameCase;
  268. this.Indentation = indentation;
  269. this.NewLineSettings = newLineSettings;
  270. this.EndOfLine = endOfLine;
  271. this.AddNewLine = addNewLine;
  272. }
  273. }
  274. exports.BeautifierSettings = BeautifierSettings;
  275. let KeyWords = ["ABS", "ACCESS", "AFTER", "ALIAS", "ALL", "AND", "ARCHITECTURE", "ARRAY", "ASSERT", "ATTRIBUTE", "BEGIN", "BLOCK", "BODY", "BUFFER", "BUS", "CASE", "COMPONENT", "CONFIGURATION", "CONSTANT", "CONTEXT", "COVER", "DISCONNECT", "DOWNTO", "DEFAULT", "ELSE", "ELSIF", "END", "ENTITY", "EXIT", "FAIRNESS", "FILE", "FOR", "FORCE", "FUNCTION", "GENERATE", "GENERIC", "GROUP", "GUARDED", "IF", "IMPURE", "IN", "INERTIAL", "INOUT", "IS", "LABEL", "LIBRARY", "LINKAGE", "LITERAL", "LOOP", "MAP", "MOD", "NAND", "NEW", "NEXT", "NOR", "NOT", "NULL", "OF", "ON", "OPEN", "OR", "OTHERS", "OUT", "PACKAGE", "PORT", "POSTPONED", "PROCEDURE", "PROCESS", "PROPERTY", "PROTECTED", "PURE", "RANGE", "RECORD", "REGISTER", "REJECT", "RELEASE", "REM", "REPORT", "RESTRICT", "RESTRICT_GUARANTEE", "RETURN", "ROL", "ROR", "SELECT", "SEQUENCE", "SEVERITY", "SHARED", "SIGNAL", "SLA", "SLL", "SRA", "SRL", "STRONG", "SUBTYPE", "THEN", "TO", "TRANSPORT", "TYPE", "UNAFFECTED", "UNITS", "UNTIL", "USE", "VARIABLE", "VMODE", "VPROP", "VUNIT", "WAIT", "WHEN", "WHILE", "WITH", "XNOR", "XOR"];
  276. let TypeNames = ["BOOLEAN", "BIT", "CHARACTER", "INTEGER", "TIME", "NATURAL", "POSITIVE", "STD_LOGIC", "STD_LOGIC_VECTOR", "STD_ULOGIC", "STD_ULOGIC_VECTOR", "STRING"];
  277. function beautify(input, settings) {
  278. input = input.replace(/\r\n/g, "\n");
  279. input = input.replace(/\n/g, "\r\n");
  280. var arr = input.split("\r\n");
  281. var comments = EscapeComments(arr);
  282. var backslashes = escapeText(arr, "\\\\[^\\\\]+\\\\", ILBackslash);
  283. let quotes = escapeText(arr, '"([^"]+)"', ILQuote);
  284. let singleQuotes = escapeText(arr, "'[^']'", ILSingleQuote);
  285. RemoveLeadingWhitespaces(arr);
  286. input = arr.join("\r\n");
  287. if (settings.RemoveComments) {
  288. input = input.replace(/\r\n[ \t]*@@comments[0-9]+[ \t]*\r\n/g, '\r\n');
  289. input = input.replace(/@@comments[0-9]+/g, '');
  290. comments = [];
  291. }
  292. input = SetKeywordCase(input, "uppercase", KeyWords);
  293. input = SetKeywordCase(input, "uppercase", TypeNames);
  294. input = RemoveExtraNewLines(input);
  295. input = input.replace(/[\t ]+/g, ' ');
  296. input = input.replace(/\([\t ]+/g, '\(');
  297. input = input.replace(/[ ]+;/g, ';');
  298. input = input.replace(/:[ ]*(PROCESS|ENTITY)/gi, ':$1');
  299. arr = input.split("\r\n");
  300. if (settings.RemoveAsserts) {
  301. RemoveAsserts(arr); //RemoveAsserts must be after EscapeQuotes
  302. }
  303. ReserveSemicolonInKeywords(arr);
  304. input = arr.join("\r\n");
  305. input = input.replace(/\b(PORT|GENERIC)\b\s+MAP/g, '$1 MAP');
  306. input = input.replace(/\b(PORT|PROCESS|GENERIC)\b[\s]*\(/g, '$1 (');
  307. let newLineSettings = settings.NewLineSettings;
  308. if (newLineSettings != null) {
  309. input = SetNewLinesAfterSymbols(input, newLineSettings);
  310. arr = input.split("\r\n");
  311. ApplyNoNewLineAfter(arr, newLineSettings.noNewLineAfter);
  312. input = arr.join("\r\n");
  313. }
  314. input = input.replace(/([a-zA-Z0-9\); ])\);(@@comments[0-9]+)?@@end/g, '$1\r\n);$2@@end');
  315. input = input.replace(/[ ]?([&=:\-\+|\*]|[<>]+)[ ]?/g, ' $1 ');
  316. input = input.replace(/(\d+e) +([+\-]) +(\d+)/g, '$1$2$3'); // fix exponential notation format broken by previous step
  317. input = input.replace(/[ ]?([,])[ ]?/g, '$1 ');
  318. input = input.replace(/[ ]?(['"])(THEN)/g, '$1 $2');
  319. input = input.replace(/[ ]?(\?)?[ ]?(<|:|>|\/)?[ ]+(=)?[ ]?/g, ' $1$2$3 ');
  320. input = input.replace(/(IF)[ ]?([\(\)])/g, '$1 $2');
  321. input = input.replace(/([\(\)])[ ]?(THEN)/gi, '$1 $2');
  322. input = input.replace(/(^|[\(\)])[ ]?(AND|OR|XOR|XNOR)[ ]*([\(])/g, '$1 $2 $3');
  323. input = input.replace(/ ([\-\*\/=+<>])[ ]*([\-\*\/=+<>]) /g, " $1$2 ");
  324. //input = input.replace(/\r\n[ \t]+--\r\n/g, "\r\n");
  325. input = input.replace(/[ ]+/g, ' ');
  326. input = input.replace(/[ \t]+\r\n/g, "\r\n");
  327. input = input.replace(/\r\n\r\n\r\n/g, '\r\n');
  328. input = input.replace(/[\r\n\s]+$/g, '');
  329. input = input.replace(/[ \t]+\)/g, ')');
  330. input = input.replace(/\s*\)\s+RETURN\s+([\w]+;)/g, '\r\n) RETURN $1'); //function(..)\r\nreturn type; -> function(..\r\n)return type;
  331. input = input.replace(/\)\s*(@@\w+)\r\n\s*RETURN\s+([\w]+;)/g, ') $1\r\n' + ILIndentedReturnPrefix + 'RETURN $2'); //function(..)\r\nreturn type; -> function(..\r\n)return type;
  332. let keywordAndSignRegex = new RegExp("(\\b" + KeyWords.join("\\b|\\b") + "\\b) +([\\-+]) +(\\w)", "g");
  333. input = input.replace(keywordAndSignRegex, "$1 $2$3"); // `WHEN - 2` -> `WHEN -2`
  334. input = input.replace(/([,|]) +([+\-]) +(\w)/g, '$1 $2$3'); // `1, - 2)` -> `1, -2)`
  335. input = input.replace(/(\() +([+\-]) +(\w)/g, '$1$2$3'); // `( - 2)` -> `(-2)`
  336. arr = input.split("\r\n");
  337. let result = [];
  338. beautify3(arr, result, settings, 0, 0);
  339. var alignSettings = settings.SignAlignSettings;
  340. if (alignSettings != null && alignSettings.isAll) {
  341. AlignSigns(result, 0, result.length - 1, alignSettings.mode, alignSettings.alignComments);
  342. }
  343. arr = FormattedLineToString(result, settings.Indentation);
  344. input = arr.join("\r\n");
  345. input = input.replace(/@@RETURN/g, "RETURN");
  346. input = SetKeywordCase(input, settings.KeywordCase, KeyWords);
  347. input = SetKeywordCase(input, settings.TypeNameCase, TypeNames);
  348. input = replaceEscapedWords(input, quotes, ILQuote);
  349. input = replaceEscapedWords(input, singleQuotes, ILSingleQuote);
  350. input = replaceEscapedComments(input, comments, ILCommentPrefix);
  351. input = replaceEscapedWords(input, backslashes, ILBackslash);
  352. input = input.replace(new RegExp(ILSemicolon, "g"), ";");
  353. input = input.replace(/@@[a-z]+/g, "");
  354. var escapedTexts = new RegExp("[" + ILBackslash + ILQuote + ILSingleQuote + "]", "g");
  355. input = input.replace(escapedTexts, "");
  356. input = input.replace(/\r\n/g, settings.EndOfLine);
  357. if (settings.AddNewLine && !input.endsWith(settings.EndOfLine)) {
  358. input += settings.EndOfLine;
  359. }
  360. return input;
  361. }
  362. exports.beautify = beautify;
  363. function replaceEscapedWords(input, arr, prefix) {
  364. for (var i = 0; i < arr.length; i++) {
  365. var text = arr[i];
  366. var regex = new RegExp("(" + prefix + "){" + text.length + "}");
  367. input = input.replace(regex, text);
  368. }
  369. return input;
  370. }
  371. function replaceEscapedComments(input, arr, prefix) {
  372. for (var i = 0; i < arr.length; i++) {
  373. input = input.replace(prefix + i, arr[i]);
  374. }
  375. return input;
  376. }
  377. function RemoveLeadingWhitespaces(arr) {
  378. for (var i = 0; i < arr.length; i++) {
  379. arr[i] = arr[i].replace(/^\s+/, "");
  380. }
  381. }
  382. class FormattedLine {
  383. constructor(line, indent) {
  384. this.Line = line;
  385. this.Indent = indent;
  386. }
  387. }
  388. exports.FormattedLine = FormattedLine;
  389. function FormattedLineToString(arr, indentation) {
  390. let result = [];
  391. if (arr == null) {
  392. return result;
  393. }
  394. if (indentation == null) {
  395. indentation = "";
  396. }
  397. arr.forEach(i => {
  398. if (i instanceof FormattedLine) {
  399. if (i.Line.length > 0) {
  400. result.push((Array(i.Indent + 1).join(indentation)) + i.Line);
  401. }
  402. else {
  403. result.push("");
  404. }
  405. }
  406. else {
  407. result = result.concat(FormattedLineToString(i, indentation));
  408. }
  409. });
  410. return result;
  411. }
  412. exports.FormattedLineToString = FormattedLineToString;
  413. function GetCloseparentheseEndIndex(inputs, startIndex) {
  414. let openParentheseCount = 0;
  415. let closeParentheseCount = 0;
  416. for (let i = startIndex; i < inputs.length; i++) {
  417. let input = inputs[i];
  418. openParentheseCount += input.count("(");
  419. closeParentheseCount += input.count(")");
  420. if (openParentheseCount > 0
  421. && openParentheseCount <= closeParentheseCount) {
  422. return i;
  423. }
  424. }
  425. return startIndex;
  426. }
  427. function beautifyPortGenericBlock(inputs, result, settings, startIndex, parentEndIndex, indent, mode) {
  428. let firstLine = inputs[startIndex];
  429. let regex = new RegExp("[\\w\\s:]*(" + mode + ")([\\s]|$)");
  430. if (!firstLine.regexStartsWith(regex)) {
  431. return [startIndex, parentEndIndex];
  432. }
  433. let firstLineHasParenthese = firstLine.indexOf("(") >= 0;
  434. let hasParenthese = firstLineHasParenthese;
  435. let blockBodyStartIndex = startIndex;
  436. let secondLineHasParenthese = startIndex + 1 < inputs.length && inputs[startIndex + 1].startsWith("(");
  437. if (secondLineHasParenthese) {
  438. hasParenthese = true;
  439. blockBodyStartIndex++;
  440. }
  441. let endIndex = hasParenthese ? GetCloseparentheseEndIndex(inputs, startIndex) : startIndex;
  442. if (endIndex != startIndex && firstLineHasParenthese) {
  443. inputs[startIndex] = inputs[startIndex].replace(/\b(PORT|GENERIC|PROCEDURE)\b([\w ]+)\(([\w\(\) ]+)/, '$1$2(\r\n$3');
  444. let newInputs = inputs[startIndex].split("\r\n");
  445. if (newInputs.length == 2) {
  446. inputs[startIndex] = newInputs[0];
  447. inputs.splice(startIndex + 1, 0, newInputs[1]);
  448. endIndex++;
  449. parentEndIndex++;
  450. }
  451. }
  452. else if (endIndex > startIndex + 1 && secondLineHasParenthese) {
  453. inputs[startIndex + 1] = inputs[startIndex + 1].replace(/\(([\w\(\) ]+)/, '(\r\n$1');
  454. let newInputs = inputs[startIndex + 1].split("\r\n");
  455. if (newInputs.length == 2) {
  456. inputs[startIndex + 1] = newInputs[0];
  457. inputs.splice(startIndex + 2, 0, newInputs[1]);
  458. endIndex++;
  459. parentEndIndex++;
  460. }
  461. }
  462. if (firstLineHasParenthese && inputs[startIndex].indexOf("MAP") > 0) {
  463. inputs[startIndex] = inputs[startIndex].replace(/([^\w])(MAP)\s+\(/g, '$1$2(');
  464. }
  465. result.push(new FormattedLine(inputs[startIndex], indent));
  466. if (secondLineHasParenthese) {
  467. let secondLineIndent = indent;
  468. if (endIndex == startIndex + 1) {
  469. secondLineIndent++;
  470. }
  471. result.push(new FormattedLine(inputs[startIndex + 1], secondLineIndent));
  472. }
  473. let blockBodyEndIndex = endIndex;
  474. let i = beautify3(inputs, result, settings, blockBodyStartIndex + 1, indent + 1, endIndex);
  475. if (inputs[i].startsWith(")")) {
  476. result[i].Indent--;
  477. blockBodyEndIndex--;
  478. }
  479. var alignSettings = settings.SignAlignSettings;
  480. if (alignSettings != null) {
  481. if (alignSettings.isRegional && !alignSettings.isAll
  482. && alignSettings.keyWords != null
  483. && alignSettings.keyWords.indexOf(mode) >= 0) {
  484. blockBodyStartIndex++;
  485. AlignSigns(result, blockBodyStartIndex, blockBodyEndIndex, alignSettings.mode, alignSettings.alignComments);
  486. }
  487. }
  488. return [i, parentEndIndex];
  489. }
  490. exports.beautifyPortGenericBlock = beautifyPortGenericBlock;
  491. function AlignSigns(result, startIndex, endIndex, mode, alignComments = false) {
  492. AlignSign_(result, startIndex, endIndex, ":", mode);
  493. AlignSign_(result, startIndex, endIndex, ":=", mode);
  494. AlignSign_(result, startIndex, endIndex, "<=", mode);
  495. AlignSign_(result, startIndex, endIndex, "=>", mode);
  496. AlignSign_(result, startIndex, endIndex, "direction", mode);
  497. if (alignComments) {
  498. AlignSign_(result, startIndex, endIndex, "@@comments", mode);
  499. }
  500. }
  501. exports.AlignSigns = AlignSigns;
  502. function indexOfGroup(regex, input, group) {
  503. var match = regex.exec(input);
  504. if (match == null) {
  505. return -1;
  506. }
  507. var index = match.index;
  508. for (let i = 1; i < group; i++) {
  509. index += match[i].length;
  510. }
  511. return index;
  512. }
  513. function AlignSign_(result, startIndex, endIndex, symbol, mode) {
  514. let maxSymbolIndex = -1;
  515. let symbolIndices = {};
  516. let startLine = startIndex;
  517. let labelAndKeywords = [
  518. "([\\w\\s]*:(\\s)*PROCESS)",
  519. "([\\w\\s]*:(\\s)*POSTPONED PROCESS)",
  520. "([\\w\\s]*:\\s*$)",
  521. "([\\w\\s]*:.*\\s+GENERATE)"
  522. ];
  523. let labelAndKeywordsStr = labelAndKeywords.join("|");
  524. let labelAndKeywordsRegex = new RegExp("(" + labelAndKeywordsStr + ")([^\\w]|$)");
  525. for (let i = startIndex; i <= endIndex; i++) {
  526. let line = result[i].Line;
  527. if (symbol == ":" && line.regexStartsWith(labelAndKeywordsRegex)) {
  528. continue;
  529. }
  530. let regex;
  531. if (symbol == "direction") {
  532. regex = new RegExp("(:\\s*)(IN|OUT|INOUT|BUFFER)(\\s+)(\\w)");
  533. }
  534. else {
  535. regex = new RegExp("([\\s\\w\\\\]|^)" + symbol + "([\\s\\w\\\\]|$)");
  536. }
  537. if (line.regexCount(regex) > 1) {
  538. continue;
  539. }
  540. let colonIndex;
  541. if (symbol == "direction") {
  542. colonIndex = indexOfGroup(regex, line, 4);
  543. }
  544. else {
  545. colonIndex = line.regexIndexOf(regex);
  546. }
  547. if (colonIndex > 0) {
  548. maxSymbolIndex = Math.max(maxSymbolIndex, colonIndex);
  549. symbolIndices[i] = colonIndex;
  550. }
  551. else if ((mode != "local" && !line.startsWith(ILCommentPrefix) && line.length != 0)
  552. || (mode == "local")) {
  553. if (startLine < i - 1) // if cannot find the symbol, a block of symbols ends
  554. {
  555. AlignSign(result, startLine, i - 1, symbol, maxSymbolIndex, symbolIndices);
  556. }
  557. maxSymbolIndex = -1;
  558. symbolIndices = {};
  559. startLine = i;
  560. }
  561. }
  562. if (startLine < endIndex) // if cannot find the symbol, a block of symbols ends
  563. {
  564. AlignSign(result, startLine, endIndex, symbol, maxSymbolIndex, symbolIndices);
  565. }
  566. }
  567. function AlignSign(result, startIndex, endIndex, symbol, maxSymbolIndex = -1, symbolIndices = {}) {
  568. if (maxSymbolIndex < 0) {
  569. return;
  570. }
  571. for (let lineIndex in symbolIndices) {
  572. let symbolIndex = symbolIndices[lineIndex];
  573. if (symbolIndex == maxSymbolIndex) {
  574. continue;
  575. }
  576. let line = result[lineIndex].Line;
  577. result[lineIndex].Line = line.substring(0, symbolIndex)
  578. + (Array(maxSymbolIndex - symbolIndex + 1).join(" "))
  579. + line.substring(symbolIndex);
  580. }
  581. }
  582. exports.AlignSign = AlignSign;
  583. function beautifyCaseBlock(inputs, result, settings, startIndex, indent) {
  584. if (!inputs[startIndex].regexStartsWith(/(.+:\s*)?(CASE)([\s]|$)/)) {
  585. return startIndex;
  586. }
  587. result.push(new FormattedLine(inputs[startIndex], indent));
  588. let i = beautify3(inputs, result, settings, startIndex + 1, indent + 2);
  589. result[i].Indent = indent;
  590. return i;
  591. }
  592. exports.beautifyCaseBlock = beautifyCaseBlock;
  593. function getSemicolonBlockEndIndex(inputs, settings, startIndex, parentEndIndex) {
  594. let endIndex = 0;
  595. let openBracketsCount = 0;
  596. let closeBracketsCount = 0;
  597. for (let i = startIndex; i < inputs.length; i++) {
  598. let input = inputs[i];
  599. let indexOfSemicolon = input.indexOf(";");
  600. let splitIndex = indexOfSemicolon < 0 ? input.length : indexOfSemicolon + 1;
  601. let stringBeforeSemicolon = input.substring(0, splitIndex);
  602. let stringAfterSemicolon = input.substring(splitIndex);
  603. stringAfterSemicolon = stringAfterSemicolon.replace(new RegExp(ILCommentPrefix + "[0-9]+"), "");
  604. openBracketsCount += stringBeforeSemicolon.count("(");
  605. closeBracketsCount += stringBeforeSemicolon.count(")");
  606. if (indexOfSemicolon < 0) {
  607. continue;
  608. }
  609. if (openBracketsCount == closeBracketsCount) {
  610. endIndex = i;
  611. if (stringAfterSemicolon.trim().length > 0 && settings.NewLineSettings.newLineAfter.indexOf(";") >= 0) {
  612. inputs[i] = stringBeforeSemicolon;
  613. inputs.splice(i, 0, stringAfterSemicolon);
  614. parentEndIndex++;
  615. }
  616. break;
  617. }
  618. }
  619. return [endIndex, parentEndIndex];
  620. }
  621. function beautifyComponentBlock(inputs, result, settings, startIndex, parentEndIndex, indent) {
  622. let endIndex = startIndex;
  623. for (let i = startIndex; i < inputs.length; i++) {
  624. if (inputs[i].regexStartsWith(/END(\s|$)/)) {
  625. endIndex = i;
  626. break;
  627. }
  628. }
  629. result.push(new FormattedLine(inputs[startIndex], indent));
  630. if (endIndex != startIndex) {
  631. let actualEndIndex = beautify3(inputs, result, settings, startIndex + 1, indent + 1, endIndex);
  632. let incremental = actualEndIndex - endIndex;
  633. endIndex += incremental;
  634. parentEndIndex += incremental;
  635. }
  636. return [endIndex, parentEndIndex];
  637. }
  638. exports.beautifyComponentBlock = beautifyComponentBlock;
  639. function beautifyPackageIsNewBlock(inputs, result, settings, startIndex, parentEndIndex, indent) {
  640. let endIndex = startIndex;
  641. for (let i = startIndex; i < inputs.length; i++) {
  642. if (inputs[i].regexIndexOf(/;(\s|$)/) >= 0) {
  643. endIndex = i;
  644. break;
  645. }
  646. }
  647. result.push(new FormattedLine(inputs[startIndex], indent));
  648. if (endIndex != startIndex) {
  649. let actualEndIndex = beautify3(inputs, result, settings, startIndex + 1, indent + 1, endIndex);
  650. let incremental = actualEndIndex - endIndex;
  651. endIndex += incremental;
  652. parentEndIndex += incremental;
  653. }
  654. return [endIndex, parentEndIndex];
  655. }
  656. exports.beautifyPackageIsNewBlock = beautifyPackageIsNewBlock;
  657. function beautifyVariableInitialiseBlock(inputs, result, settings, startIndex, parentEndIndex, indent) {
  658. let endIndex = startIndex;
  659. for (let i = startIndex; i < inputs.length; i++) {
  660. if (inputs[i].regexIndexOf(/;(\s|$)/) >= 0) {
  661. endIndex = i;
  662. break;
  663. }
  664. }
  665. result.push(new FormattedLine(inputs[startIndex], indent));
  666. if (endIndex != startIndex) {
  667. let actualEndIndex = beautify3(inputs, result, settings, startIndex + 1, indent + 1, endIndex);
  668. let incremental = actualEndIndex - endIndex;
  669. endIndex += incremental;
  670. parentEndIndex += incremental;
  671. }
  672. return [endIndex, parentEndIndex];
  673. }
  674. exports.beautifyVariableInitialiseBlock = beautifyVariableInitialiseBlock;
  675. function beautifySemicolonBlock(inputs, result, settings, startIndex, parentEndIndex, indent) {
  676. let endIndex = startIndex;
  677. [endIndex, parentEndIndex] = getSemicolonBlockEndIndex(inputs, settings, startIndex, parentEndIndex);
  678. result.push(new FormattedLine(inputs[startIndex], indent));
  679. if (endIndex != startIndex) {
  680. beautify3(inputs, result, settings, startIndex + 1, indent + 1, endIndex);
  681. alignSignalAssignmentBlock(settings, inputs, startIndex, endIndex, result);
  682. }
  683. return [endIndex, parentEndIndex];
  684. }
  685. exports.beautifySemicolonBlock = beautifySemicolonBlock;
  686. function alignSignalAssignmentBlock(settings, inputs, startIndex, endIndex, result) {
  687. if (settings.Indentation.replace(/ +/g, "").length == 0) {
  688. let reg = new RegExp("^([\\w\\\\]+[\\s]*<=\\s*)");
  689. let match = reg.exec(inputs[startIndex]);
  690. if (match != null) {
  691. let length = match[0].length;
  692. let prefixLength = length - settings.Indentation.length;
  693. let prefix = new Array(prefixLength + 1).join(" ");
  694. for (let i = startIndex + 1; i <= endIndex; i++) {
  695. let fl = result[i];
  696. fl.Line = prefix + fl.Line;
  697. }
  698. }
  699. }
  700. }
  701. function beautify3(inputs, result, settings, startIndex, indent, endIndex) {
  702. let i;
  703. let regexOneLineBlockKeyWords = new RegExp(/(PROCEDURE)[^\w](?!.+[^\w]IS([^\w]|$))/); //match PROCEDURE..; but not PROCEDURE .. IS;
  704. let regexFunctionMultiLineBlockKeyWords = new RegExp(/(FUNCTION|IMPURE FUNCTION)[^\w](?=.+[^\w]IS([^\w]|$))/); //match FUNCTION .. IS; but not FUNCTION
  705. let blockMidKeyWords = ["BEGIN"];
  706. let blockStartsKeyWords = [
  707. "IF",
  708. "CASE",
  709. "ARCHITECTURE",
  710. "PROCEDURE",
  711. "PACKAGE",
  712. "(([\\w\\s]*:)?(\\s)*PROCESS)",
  713. "(([\\w\\s]*:)?(\\s)*POSTPONED PROCESS)",
  714. "(.*\\s*PROTECTED)",
  715. "(COMPONENT)",
  716. "(ENTITY(?!.+;))",
  717. "FOR",
  718. "WHILE",
  719. "LOOP",
  720. "(.*\\s*GENERATE)",
  721. "(CONTEXT[\\w\\s\\\\]+IS)",
  722. "(CONFIGURATION(?!.+;))",
  723. "BLOCK",
  724. "UNITS",
  725. "\\w+\\s+\\w+\\s+IS\\s+RECORD"
  726. ];
  727. let blockEndsKeyWords = ["END", ".*\\)\\s*RETURN\\s+[\\w]+;"];
  728. let indentedEndsKeyWords = [ILIndentedReturnPrefix + "RETURN\\s+\\w+;"];
  729. let blockEndsWithSemicolon = [
  730. "(WITH\\s+[\\w\\s\\\\]+SELECT)",
  731. "([\\w\\\\]+[\\s]*<=)",
  732. "([\\w\\\\]+[\\s]*:=)",
  733. "FOR\\s+[\\w\\s,]+:\\s*\\w+\\s+USE",
  734. "REPORT"
  735. ];
  736. let newLineAfterKeyWordsStr = blockStartsKeyWords.join("|");
  737. let regexBlockMidKeyWords = blockMidKeyWords.convertToRegexBlockWords();
  738. let regexBlockStartsKeywords = new RegExp("([\\w]+\\s*:\\s*)?(" + newLineAfterKeyWordsStr + ")([^\\w]|$)");
  739. let regexBlockEndsKeyWords = blockEndsKeyWords.convertToRegexBlockWords();
  740. let regexBlockIndentedEndsKeyWords = indentedEndsKeyWords.convertToRegexBlockWords();
  741. let regexblockEndsWithSemicolon = blockEndsWithSemicolon.convertToRegexBlockWords();
  742. let regexMidKeyWhen = "WHEN".convertToRegexBlockWords();
  743. let regexMidKeyElse = "ELSE|ELSIF".convertToRegexBlockWords();
  744. if (endIndex == null) {
  745. endIndex = inputs.length - 1;
  746. }
  747. for (i = startIndex; i <= endIndex; i++) {
  748. if (indent < 0) {
  749. indent = 0;
  750. }
  751. let input = inputs[i].trim();
  752. if (input.regexStartsWith(regexBlockIndentedEndsKeyWords)) {
  753. result.push(new FormattedLine(input, indent));
  754. return i;
  755. }
  756. if (input.regexStartsWith(/COMPONENT\s/)) {
  757. let modeCache = Mode;
  758. Mode = FormatMode.EndsWithSemicolon;
  759. [i, endIndex] = beautifyComponentBlock(inputs, result, settings, i, endIndex, indent);
  760. Mode = modeCache;
  761. continue;
  762. }
  763. if (input.regexStartsWith(/PACKAGE[\s\w]+IS\s+NEW/)) {
  764. let modeCache = Mode;
  765. Mode = FormatMode.EndsWithSemicolon;
  766. [i, endIndex] = beautifyPackageIsNewBlock(inputs, result, settings, i, endIndex, indent);
  767. Mode = modeCache;
  768. continue;
  769. }
  770. if (input.regexStartsWith(/\w+\s+\w+\s*:.+:\s*=\s*\(([^;]|$)/)) { // 'variable symbol: type [:= initial_value];'
  771. let modeCache = Mode;
  772. Mode = FormatMode.EndsWithSemicolon;
  773. let endsWithBracket = input.regexIndexOf(/:\s*=\s*\(/) > 0;
  774. let startIndex = i;
  775. [i, endIndex] = beautifySemicolonBlock(inputs, result, settings, i, endIndex, indent);
  776. if (endsWithBracket && startIndex != i) {
  777. let fl = result[endIndex];
  778. if (fl.Line.regexStartsWith(/\);$/)) {
  779. fl.Indent--;
  780. }
  781. }
  782. Mode = modeCache;
  783. continue;
  784. }
  785. if (input.regexStartsWith(/\w+\s*:\s*ENTITY/)) {
  786. let modeCache = Mode;
  787. Mode = FormatMode.EndsWithSemicolon;
  788. [i, endIndex] = beautifySemicolonBlock(inputs, result, settings, i, endIndex, indent);
  789. Mode = modeCache;
  790. continue;
  791. }
  792. if (Mode != FormatMode.EndsWithSemicolon && input.regexStartsWith(regexblockEndsWithSemicolon)) {
  793. let modeCache = Mode;
  794. Mode = FormatMode.EndsWithSemicolon;
  795. [i, endIndex] = beautifySemicolonBlock(inputs, result, settings, i, endIndex, indent);
  796. Mode = modeCache;
  797. continue;
  798. }
  799. if (input.regexStartsWith(/(.+:\s*)?(CASE)([\s]|$)/)) {
  800. let modeCache = Mode;
  801. Mode = FormatMode.CaseWhen;
  802. i = beautifyCaseBlock(inputs, result, settings, i, indent);
  803. Mode = modeCache;
  804. continue;
  805. }
  806. if (input.regexStartsWith(/[\w\s:]*(:=)([\s]|$)/)) {
  807. [i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, ":=");
  808. continue;
  809. }
  810. if (input.regexStartsWith(/[\w\s:]*\bPORT\b([\s]|$)/)) {
  811. [i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, "PORT");
  812. continue;
  813. }
  814. if (input.regexStartsWith(/TYPE\s+\w+\s+IS\s+\(/)) {
  815. [i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, "IS");
  816. continue;
  817. }
  818. if (input.regexStartsWith(/[\w\s:]*GENERIC([\s]|$)/)) {
  819. [i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, "GENERIC");
  820. continue;
  821. }
  822. if (input.regexStartsWith(/[\w\s:]*PROCEDURE[\s\w]+\($/)) {
  823. [i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, "PROCEDURE");
  824. if (inputs[i].regexStartsWith(/.*\)[\s]*IS/)) {
  825. i = beautify3(inputs, result, settings, i + 1, indent + 1);
  826. }
  827. continue;
  828. }
  829. if (input.regexStartsWith(/FUNCTION[^\w]/)
  830. && input.regexIndexOf(/[^\w]RETURN[^\w]/) < 0) {
  831. [i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, "FUNCTION");
  832. if (!inputs[i].regexStartsWith(regexBlockEndsKeyWords)) {
  833. i = beautify3(inputs, result, settings, i + 1, indent + 1);
  834. }
  835. else {
  836. result[i].Indent++;
  837. }
  838. continue;
  839. }
  840. if (input.regexStartsWith(/IMPURE FUNCTION[^\w]/)
  841. && input.regexIndexOf(/[^\w]RETURN[^\w]/) < 0) {
  842. [i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, "IMPURE FUNCTION");
  843. if (!inputs[i].regexStartsWith(regexBlockEndsKeyWords)) {
  844. if (inputs[i].regexStartsWith(regexBlockIndentedEndsKeyWords)) {
  845. result[i].Indent++;
  846. }
  847. else {
  848. i = beautify3(inputs, result, settings, i + 1, indent + 1);
  849. }
  850. }
  851. else {
  852. result[i].Indent++;
  853. }
  854. continue;
  855. }
  856. result.push(new FormattedLine(input, indent));
  857. if (startIndex != 0
  858. && (input.regexStartsWith(regexBlockMidKeyWords)
  859. || (Mode != FormatMode.EndsWithSemicolon && input.regexStartsWith(regexMidKeyElse))
  860. || (Mode == FormatMode.CaseWhen && input.regexStartsWith(regexMidKeyWhen)))) {
  861. result[i].Indent--;
  862. }
  863. else if (startIndex != 0
  864. && (input.regexStartsWith(regexBlockEndsKeyWords))) {
  865. result[i].Indent--;
  866. return i;
  867. }
  868. if (input.regexStartsWith(regexOneLineBlockKeyWords)) {
  869. continue;
  870. }
  871. if (input.regexStartsWith(regexFunctionMultiLineBlockKeyWords)
  872. || input.regexStartsWith(regexBlockStartsKeywords)) {
  873. i = beautify3(inputs, result, settings, i + 1, indent + 1);
  874. }
  875. }
  876. i--;
  877. return i;
  878. }
  879. exports.beautify3 = beautify3;
  880. function ReserveSemicolonInKeywords(arr) {
  881. for (let i = 0; i < arr.length; i++) {
  882. if (arr[i].match(/FUNCTION|PROCEDURE/) != null) {
  883. arr[i] = arr[i].replace(/;/g, ILSemicolon);
  884. }
  885. }
  886. }
  887. function ApplyNoNewLineAfter(arr, noNewLineAfter) {
  888. if (noNewLineAfter == null) {
  889. return;
  890. }
  891. for (let i = 0; i < arr.length; i++) {
  892. noNewLineAfter.forEach(n => {
  893. let regex = new RegExp("(" + n.toUpperCase + ")[ a-z0-9]+[a-z0-9]+");
  894. if (arr[i].regexIndexOf(regex) >= 0) {
  895. arr[i] += "@@singleline";
  896. }
  897. });
  898. }
  899. }
  900. exports.ApplyNoNewLineAfter = ApplyNoNewLineAfter;
  901. function RemoveAsserts(arr) {
  902. let need_semi = false;
  903. let inAssert = false;
  904. let n = 0;
  905. for (let i = 0; i < arr.length; i++) {
  906. let has_semi = arr[i].indexOf(";") >= 0;
  907. if (need_semi) {
  908. arr[i] = '';
  909. }
  910. n = arr[i].indexOf("ASSERT ");
  911. if (n >= 0) {
  912. inAssert = true;
  913. arr[i] = '';
  914. }
  915. if (!has_semi) {
  916. if (inAssert) {
  917. need_semi = true;
  918. }
  919. }
  920. else {
  921. need_semi = false;
  922. }
  923. }
  924. }
  925. exports.RemoveAsserts = RemoveAsserts;
  926. function escapeText(arr, regex, escapedChar) {
  927. let quotes = [];
  928. let regexEpr = new RegExp(regex, "g");
  929. for (let i = 0; i < arr.length; i++) {
  930. let matches = arr[i].match(regexEpr);
  931. if (matches != null) {
  932. for (var j = 0; j < matches.length; j++) {
  933. var match = matches[j];
  934. arr[i] = arr[i].replace(match, escapedChar.repeat(match.length));
  935. quotes.push(match);
  936. }
  937. }
  938. }
  939. return quotes;
  940. }
  941. function RemoveExtraNewLines(input) {
  942. input = input.replace(/(?:\r\n|\r|\n)/g, '\r\n');
  943. input = input.replace(/ \r\n/g, '\r\n');
  944. input = input.replace(/\r\n\r\n\r\n/g, '\r\n');
  945. return input;
  946. }
  947. //# sourceMappingURL=VHDLFormatter.js.map