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.

967 lines
39 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
  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.CodeBlock = 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. let block = new CodeBlock(arr);
  339. beautify3(block, result, settings, 0);
  340. var alignSettings = settings.SignAlignSettings;
  341. if (alignSettings != null && alignSettings.isAll) {
  342. AlignSigns(result, 0, result.length - 1, alignSettings.mode, alignSettings.alignComments);
  343. }
  344. arr = FormattedLineToString(result, settings.Indentation);
  345. input = arr.join("\r\n");
  346. input = input.replace(/@@RETURN/g, "RETURN");
  347. input = SetKeywordCase(input, settings.KeywordCase, KeyWords);
  348. input = SetKeywordCase(input, settings.TypeNameCase, TypeNames);
  349. input = replaceEscapedWords(input, quotes, ILQuote);
  350. input = replaceEscapedWords(input, singleQuotes, ILSingleQuote);
  351. input = replaceEscapedComments(input, comments, ILCommentPrefix);
  352. input = replaceEscapedWords(input, backslashes, ILBackslash);
  353. input = input.replace(new RegExp(ILSemicolon, "g"), ";");
  354. input = input.replace(/@@[a-z]+/g, "");
  355. var escapedTexts = new RegExp("[" + ILBackslash + ILQuote + ILSingleQuote + "]", "g");
  356. input = input.replace(escapedTexts, "");
  357. input = input.replace(/\r\n/g, settings.EndOfLine);
  358. if (settings.AddNewLine && !input.endsWith(settings.EndOfLine)) {
  359. input += settings.EndOfLine;
  360. }
  361. return input;
  362. }
  363. exports.beautify = beautify;
  364. function replaceEscapedWords(input, arr, prefix) {
  365. for (var i = 0; i < arr.length; i++) {
  366. var text = arr[i];
  367. var regex = new RegExp("(" + prefix + "){" + text.length + "}");
  368. input = input.replace(regex, text);
  369. }
  370. return input;
  371. }
  372. function replaceEscapedComments(input, arr, prefix) {
  373. for (var i = 0; i < arr.length; i++) {
  374. input = input.replace(prefix + i, arr[i]);
  375. }
  376. return input;
  377. }
  378. function RemoveLeadingWhitespaces(arr) {
  379. for (var i = 0; i < arr.length; i++) {
  380. arr[i] = arr[i].replace(/^\s+/, "");
  381. }
  382. }
  383. class FormattedLine {
  384. constructor(line, indent) {
  385. this.Line = line;
  386. this.Indent = indent;
  387. }
  388. }
  389. exports.FormattedLine = FormattedLine;
  390. class CodeBlock {
  391. constructor(lines, start = 0, end = lines.length - 1) {
  392. this.lines = lines;
  393. this.start = start;
  394. this.end = end;
  395. this.parent = null;
  396. this.cursor = start;
  397. }
  398. _notifySplit(atLine) {
  399. if (this.start > atLine)
  400. this.start++;
  401. if (this.end >= atLine)
  402. this.end++;
  403. if (this.cursor >= atLine)
  404. this.cursor++;
  405. if (this.parent)
  406. this.parent._notifySplit(atLine);
  407. }
  408. splitLine(atLine, firstText, secondText) {
  409. this.lines[atLine] = firstText;
  410. this.lines.splice(atLine + 1, 0, secondText);
  411. this._notifySplit(atLine);
  412. }
  413. subBlock(start, end) {
  414. let newBlock = new CodeBlock(this.lines, start, end);
  415. newBlock.parent = this;
  416. return newBlock;
  417. }
  418. }
  419. exports.CodeBlock = CodeBlock;
  420. function FormattedLineToString(arr, indentation) {
  421. let result = [];
  422. if (arr == null) {
  423. return result;
  424. }
  425. if (indentation == null) {
  426. indentation = "";
  427. }
  428. arr.forEach(i => {
  429. if (i instanceof FormattedLine) {
  430. if (i.Line.length > 0) {
  431. result.push((Array(i.Indent + 1).join(indentation)) + i.Line);
  432. }
  433. else {
  434. result.push("");
  435. }
  436. }
  437. else {
  438. result = result.concat(FormattedLineToString(i, indentation));
  439. }
  440. });
  441. return result;
  442. }
  443. exports.FormattedLineToString = FormattedLineToString;
  444. function GetCloseparentheseEndIndex(block) {
  445. let openParentheseCount = 0;
  446. let closeParentheseCount = 0;
  447. let startIndex = block.cursor;
  448. for (; block.cursor <= block.end; block.cursor++) {
  449. let input = block.lines[block.cursor];
  450. openParentheseCount += input.count("(");
  451. closeParentheseCount += input.count(")");
  452. if (openParentheseCount > 0
  453. && openParentheseCount <= closeParentheseCount) {
  454. return;
  455. }
  456. }
  457. block.cursor = startIndex;
  458. }
  459. function beautifyPortGenericBlock(block, result, settings, indent, mode) {
  460. let startIndex = block.cursor;
  461. let firstLine = block.lines[startIndex];
  462. let regex = new RegExp("[\\w\\s:]*(" + mode + ")([\\s]|$)");
  463. if (!firstLine.regexStartsWith(regex)) {
  464. return;
  465. }
  466. let firstLineHasParenthese = firstLine.indexOf("(") >= 0;
  467. let secondLineHasParenthese = startIndex + 1 <= block.end && block.lines[startIndex + 1].startsWith("(");
  468. let hasParenthese = firstLineHasParenthese || secondLineHasParenthese;
  469. let blockBodyStartIndex = startIndex + (secondLineHasParenthese ? 1 : 0);
  470. if (hasParenthese) {
  471. GetCloseparentheseEndIndex(block);
  472. }
  473. let endIndex = block.cursor;
  474. let bodyBlock = block.subBlock(blockBodyStartIndex, endIndex);
  475. if (endIndex != startIndex && firstLineHasParenthese) {
  476. block.lines[startIndex] = block.lines[startIndex].replace(/\b(PORT|GENERIC|PROCEDURE)\b([\w ]+)\(([\w\(\) ]+)/, '$1$2(\r\n$3');
  477. let newInputs = block.lines[startIndex].split("\r\n");
  478. if (newInputs.length == 2) {
  479. bodyBlock.splitLine(startIndex, newInputs[0], newInputs[1]);
  480. }
  481. }
  482. else if (endIndex > startIndex + 1 && secondLineHasParenthese) {
  483. block.lines[startIndex + 1] = block.lines[startIndex + 1].replace(/\(([\w\(\) ]+)/, '(\r\n$1');
  484. let newInputs = block.lines[startIndex + 1].split("\r\n");
  485. if (newInputs.length == 2) {
  486. bodyBlock.splitLine(startIndex + 1, newInputs[0], newInputs[1]);
  487. }
  488. }
  489. if (firstLineHasParenthese && block.lines[startIndex].indexOf("MAP") > 0) {
  490. block.lines[startIndex] = block.lines[startIndex].replace(/([^\w])(MAP)\s+\(/g, '$1$2(');
  491. }
  492. result.push(new FormattedLine(block.lines[startIndex], indent));
  493. if (secondLineHasParenthese) {
  494. let secondLineIndent = indent;
  495. if (endIndex == startIndex + 1) {
  496. secondLineIndent++;
  497. }
  498. result.push(new FormattedLine(block.lines[startIndex + 1], secondLineIndent));
  499. }
  500. beautify3(bodyBlock.subBlock(bodyBlock.start + 1, bodyBlock.end), result, settings, indent + 1);
  501. if (block.lines[block.cursor].startsWith(")")) {
  502. result[block.cursor].Indent--;
  503. bodyBlock.end--;
  504. }
  505. var alignSettings = settings.SignAlignSettings;
  506. if (alignSettings != null) {
  507. if (alignSettings.isRegional && !alignSettings.isAll
  508. && alignSettings.keyWords != null
  509. && alignSettings.keyWords.indexOf(mode) >= 0) {
  510. AlignSigns(result, bodyBlock.start + 1, bodyBlock.end, alignSettings.mode, alignSettings.alignComments);
  511. }
  512. }
  513. }
  514. exports.beautifyPortGenericBlock = beautifyPortGenericBlock;
  515. function AlignSigns(result, startIndex, endIndex, mode, alignComments = false) {
  516. AlignSign_(result, startIndex, endIndex, ":", mode);
  517. AlignSign_(result, startIndex, endIndex, ":=", mode);
  518. AlignSign_(result, startIndex, endIndex, "<=", mode);
  519. AlignSign_(result, startIndex, endIndex, "=>", mode);
  520. AlignSign_(result, startIndex, endIndex, "direction", mode);
  521. if (alignComments) {
  522. AlignSign_(result, startIndex, endIndex, "@@comments", mode);
  523. }
  524. }
  525. exports.AlignSigns = AlignSigns;
  526. function indexOfGroup(regex, input, group) {
  527. var match = regex.exec(input);
  528. if (match == null) {
  529. return -1;
  530. }
  531. var index = match.index;
  532. for (let i = 1; i < group; i++) {
  533. index += match[i].length;
  534. }
  535. return index;
  536. }
  537. function AlignSign_(result, startIndex, endIndex, symbol, mode) {
  538. let maxSymbolIndex = -1;
  539. let symbolIndices = {};
  540. let startLine = startIndex;
  541. let labelAndKeywords = [
  542. "([\\w\\s]*:(\\s)*PROCESS)",
  543. "([\\w\\s]*:(\\s)*POSTPONED PROCESS)",
  544. "([\\w\\s]*:\\s*$)",
  545. "([\\w\\s]*:.*\\s+GENERATE)"
  546. ];
  547. let labelAndKeywordsStr = labelAndKeywords.join("|");
  548. let labelAndKeywordsRegex = new RegExp("(" + labelAndKeywordsStr + ")([^\\w]|$)");
  549. for (let i = startIndex; i <= endIndex; i++) {
  550. let line = result[i].Line;
  551. if (symbol == ":" && line.regexStartsWith(labelAndKeywordsRegex)) {
  552. continue;
  553. }
  554. let regex;
  555. if (symbol == "direction") {
  556. regex = new RegExp("(:\\s*)(IN|OUT|INOUT|BUFFER)(\\s+)(\\w)");
  557. }
  558. else {
  559. regex = new RegExp("([\\s\\w\\\\]|^)" + symbol + "([\\s\\w\\\\]|$)");
  560. }
  561. if (line.regexCount(regex) > 1) {
  562. continue;
  563. }
  564. let colonIndex;
  565. if (symbol == "direction") {
  566. colonIndex = indexOfGroup(regex, line, 4);
  567. }
  568. else {
  569. colonIndex = line.regexIndexOf(regex);
  570. }
  571. if (colonIndex > 0) {
  572. maxSymbolIndex = Math.max(maxSymbolIndex, colonIndex);
  573. symbolIndices[i] = colonIndex;
  574. }
  575. else if ((mode != "local" && !line.startsWith(ILCommentPrefix) && line.length != 0)
  576. || (mode == "local")) {
  577. if (startLine < i - 1) // if cannot find the symbol, a block of symbols ends
  578. {
  579. AlignSign(result, startLine, i - 1, symbol, maxSymbolIndex, symbolIndices);
  580. }
  581. maxSymbolIndex = -1;
  582. symbolIndices = {};
  583. startLine = i;
  584. }
  585. }
  586. if (startLine < endIndex) // if cannot find the symbol, a block of symbols ends
  587. {
  588. AlignSign(result, startLine, endIndex, symbol, maxSymbolIndex, symbolIndices);
  589. }
  590. }
  591. function AlignSign(result, startIndex, endIndex, symbol, maxSymbolIndex = -1, symbolIndices = {}) {
  592. if (maxSymbolIndex < 0) {
  593. return;
  594. }
  595. for (let lineIndex in symbolIndices) {
  596. let symbolIndex = symbolIndices[lineIndex];
  597. if (symbolIndex == maxSymbolIndex) {
  598. continue;
  599. }
  600. let line = result[lineIndex].Line;
  601. result[lineIndex].Line = line.substring(0, symbolIndex)
  602. + (Array(maxSymbolIndex - symbolIndex + 1).join(" "))
  603. + line.substring(symbolIndex);
  604. }
  605. }
  606. exports.AlignSign = AlignSign;
  607. function beautifyCaseBlock(block, result, settings, indent) {
  608. if (!block.lines[block.cursor].regexStartsWith(/(.+:\s*)?(CASE)([\s]|$)/)) {
  609. return;
  610. }
  611. result.push(new FormattedLine(block.lines[block.cursor], indent));
  612. block.cursor++;
  613. beautify3(block, result, settings, indent + 2);
  614. result[block.cursor].Indent = indent;
  615. }
  616. exports.beautifyCaseBlock = beautifyCaseBlock;
  617. function getSemicolonBlockEndIndex(block, settings) {
  618. let endIndex = block.cursor;
  619. let openBracketsCount = 0;
  620. let closeBracketsCount = 0;
  621. for (; block.cursor <= block.end; block.cursor++) {
  622. let input = block.lines[block.cursor];
  623. let indexOfSemicolon = input.indexOf(";");
  624. let splitIndex = indexOfSemicolon < 0 ? input.length : indexOfSemicolon + 1;
  625. let stringBeforeSemicolon = input.substring(0, splitIndex);
  626. let stringAfterSemicolon = input.substring(splitIndex);
  627. stringAfterSemicolon = stringAfterSemicolon.replace(new RegExp(ILCommentPrefix + "[0-9]+"), "");
  628. openBracketsCount += stringBeforeSemicolon.count("(");
  629. closeBracketsCount += stringBeforeSemicolon.count(")");
  630. if (indexOfSemicolon < 0) {
  631. continue;
  632. }
  633. if (openBracketsCount == closeBracketsCount) {
  634. endIndex = block.cursor;
  635. if (stringAfterSemicolon.trim().length > 0 && settings.NewLineSettings.newLineAfter.indexOf(";") >= 0) {
  636. block.splitLine(block.cursor, stringBeforeSemicolon, stringAfterSemicolon);
  637. }
  638. break;
  639. }
  640. }
  641. block.cursor = endIndex;
  642. }
  643. function beautifyComponentBlock(block, result, settings, indent) {
  644. let startIndex = block.cursor;
  645. for (; block.cursor <= block.end; block.cursor++) {
  646. if (block.lines[block.cursor].regexStartsWith(/END(\s|$)/)) {
  647. break;
  648. }
  649. }
  650. result.push(new FormattedLine(block.lines[startIndex], indent));
  651. if (block.cursor != startIndex) {
  652. beautify3(block.subBlock(startIndex + 1, block.cursor), result, settings, indent + 1);
  653. }
  654. }
  655. exports.beautifyComponentBlock = beautifyComponentBlock;
  656. function beautifyPackageIsNewBlock(block, result, settings, indent) {
  657. let startIndex = block.cursor;
  658. for (; block.cursor <= block.end; block.cursor++) {
  659. if (block.lines[block.cursor].regexIndexOf(/;(\s|$)/) >= 0) {
  660. break;
  661. }
  662. }
  663. result.push(new FormattedLine(block.lines[startIndex], indent));
  664. if (block.cursor != startIndex) {
  665. beautify3(block.subBlock(startIndex + 1, block.cursor), result, settings, indent + 1);
  666. }
  667. }
  668. exports.beautifyPackageIsNewBlock = beautifyPackageIsNewBlock;
  669. function beautifyVariableInitialiseBlock(block, result, settings, indent) {
  670. let startIndex = block.cursor;
  671. for (; block.cursor <= block.end; block.cursor++) {
  672. if (block.lines[block.cursor].regexIndexOf(/;(\s|$)/) >= 0) {
  673. break;
  674. }
  675. }
  676. result.push(new FormattedLine(block.lines[startIndex], indent));
  677. if (block.cursor != startIndex) {
  678. beautify3(block.subBlock(startIndex + 1, block.cursor), result, settings, indent + 1);
  679. }
  680. }
  681. exports.beautifyVariableInitialiseBlock = beautifyVariableInitialiseBlock;
  682. function beautifySemicolonBlock(block, result, settings, indent) {
  683. let startIndex = block.cursor;
  684. getSemicolonBlockEndIndex(block, settings);
  685. result.push(new FormattedLine(block.lines[startIndex], indent));
  686. if (block.cursor != startIndex) {
  687. beautify3(block.subBlock(startIndex + 1, block.cursor), result, settings, indent + 1);
  688. alignSignalAssignmentBlock(settings, block.lines, startIndex, block.cursor, result);
  689. }
  690. }
  691. exports.beautifySemicolonBlock = beautifySemicolonBlock;
  692. function alignSignalAssignmentBlock(settings, inputs, startIndex, endIndex, result) {
  693. if (settings.Indentation.replace(/ +/g, "").length == 0) {
  694. let reg = new RegExp("^([\\w\\\\]+[\\s]*<=\\s*)");
  695. let match = reg.exec(inputs[startIndex]);
  696. if (match != null) {
  697. let length = match[0].length;
  698. let prefixLength = length - settings.Indentation.length;
  699. let prefix = new Array(prefixLength + 1).join(" ");
  700. for (let i = startIndex + 1; i <= endIndex; i++) {
  701. let fl = result[i];
  702. fl.Line = prefix + fl.Line;
  703. }
  704. }
  705. }
  706. }
  707. function beautify3(block, result, settings, indent) {
  708. let regexOneLineBlockKeyWords = new RegExp(/(PROCEDURE)[^\w](?!.+[^\w]IS([^\w]|$))/); //match PROCEDURE..; but not PROCEDURE .. IS;
  709. let regexFunctionMultiLineBlockKeyWords = new RegExp(/(FUNCTION|IMPURE FUNCTION)[^\w](?=.+[^\w]IS([^\w]|$))/); //match FUNCTION .. IS; but not FUNCTION
  710. let blockMidKeyWords = ["BEGIN"];
  711. let blockStartsKeyWords = [
  712. "IF",
  713. "CASE",
  714. "ARCHITECTURE",
  715. "PROCEDURE",
  716. "PACKAGE",
  717. "(([\\w\\s]*:)?(\\s)*PROCESS)",
  718. "(([\\w\\s]*:)?(\\s)*POSTPONED PROCESS)",
  719. "(.*\\s*PROTECTED)",
  720. "(COMPONENT)",
  721. "(ENTITY(?!.+;))",
  722. "FOR",
  723. "WHILE",
  724. "LOOP",
  725. "(.*\\s*GENERATE)",
  726. "(CONTEXT[\\w\\s\\\\]+IS)",
  727. "(CONFIGURATION(?!.+;))",
  728. "BLOCK",
  729. "UNITS",
  730. "\\w+\\s+\\w+\\s+IS\\s+RECORD"
  731. ];
  732. let blockEndsKeyWords = ["END", ".*\\)\\s*RETURN\\s+[\\w]+;"];
  733. let indentedEndsKeyWords = [ILIndentedReturnPrefix + "RETURN\\s+\\w+;"];
  734. let blockEndsWithSemicolon = [
  735. "(WITH\\s+[\\w\\s\\\\]+SELECT)",
  736. "([\\w\\\\]+[\\s]*<=)",
  737. "([\\w\\\\]+[\\s]*:=)",
  738. "FOR\\s+[\\w\\s,]+:\\s*\\w+\\s+USE",
  739. "REPORT"
  740. ];
  741. let newLineAfterKeyWordsStr = blockStartsKeyWords.join("|");
  742. let regexBlockMidKeyWords = blockMidKeyWords.convertToRegexBlockWords();
  743. let regexBlockStartsKeywords = new RegExp("([\\w]+\\s*:\\s*)?(" + newLineAfterKeyWordsStr + ")([^\\w]|$)");
  744. let regexBlockEndsKeyWords = blockEndsKeyWords.convertToRegexBlockWords();
  745. let regexBlockIndentedEndsKeyWords = indentedEndsKeyWords.convertToRegexBlockWords();
  746. let regexblockEndsWithSemicolon = blockEndsWithSemicolon.convertToRegexBlockWords();
  747. let regexMidKeyWhen = "WHEN".convertToRegexBlockWords();
  748. let regexMidKeyElse = "THEN|ELSE|ELSIF".convertToRegexBlockWords();
  749. for (; block.cursor <= block.end; block.cursor++) {
  750. if (indent < 0) {
  751. indent = 0;
  752. }
  753. let input = block.lines[block.cursor].trim();
  754. if (input.regexStartsWith(regexBlockIndentedEndsKeyWords)) {
  755. result.push(new FormattedLine(input, indent));
  756. return;
  757. }
  758. if (input.regexStartsWith(/COMPONENT\s/)) {
  759. let modeCache = Mode;
  760. Mode = FormatMode.EndsWithSemicolon;
  761. beautifyComponentBlock(block, result, settings, indent);
  762. Mode = modeCache;
  763. continue;
  764. }
  765. if (input.regexStartsWith(/PACKAGE[\s\w]+IS\s+NEW/)) {
  766. let modeCache = Mode;
  767. Mode = FormatMode.EndsWithSemicolon;
  768. beautifyPackageIsNewBlock(block, result, settings, indent);
  769. Mode = modeCache;
  770. continue;
  771. }
  772. if (input.regexStartsWith(/\w+\s+\w+\s*:.+:\s*=\s*\(([^;]|$)/)) { // 'variable symbol: type [:= initial_value];'
  773. let modeCache = Mode;
  774. Mode = FormatMode.EndsWithSemicolon;
  775. let endsWithBracket = input.regexIndexOf(/:\s*=\s*\(/) > 0;
  776. let startIndex = block.cursor;
  777. beautifySemicolonBlock(block, result, settings, indent);
  778. if (endsWithBracket && startIndex != block.cursor) {
  779. let fl = result[block.end];
  780. if (fl.Line.regexStartsWith(/\);$/)) {
  781. fl.Indent--;
  782. }
  783. }
  784. Mode = modeCache;
  785. continue;
  786. }
  787. if (input.regexIndexOf(/:=(\s*@@comments\d+\s*)?$/) > 0) {
  788. let modeCache = Mode;
  789. Mode = FormatMode.EndsWithSemicolon;
  790. beautifySemicolonBlock(block, result, settings, indent);
  791. Mode = modeCache;
  792. continue;
  793. }
  794. if (input.regexStartsWith(/\w+\s*:\s*ENTITY/)) {
  795. let modeCache = Mode;
  796. Mode = FormatMode.EndsWithSemicolon;
  797. beautifySemicolonBlock(block, result, settings, indent);
  798. Mode = modeCache;
  799. continue;
  800. }
  801. if (Mode != FormatMode.EndsWithSemicolon && input.regexStartsWith(regexblockEndsWithSemicolon)) {
  802. let modeCache = Mode;
  803. Mode = FormatMode.EndsWithSemicolon;
  804. beautifySemicolonBlock(block, result, settings, indent);
  805. Mode = modeCache;
  806. continue;
  807. }
  808. if (input.regexStartsWith(/(.+:\s*)?(CASE)([\s]|$)/)) {
  809. let modeCache = Mode;
  810. Mode = FormatMode.CaseWhen;
  811. beautifyCaseBlock(block, result, settings, indent);
  812. Mode = modeCache;
  813. continue;
  814. }
  815. if (input.regexStartsWith(/[\w\s:]*(:=)([\s]|$)/)) {
  816. beautifyPortGenericBlock(block, result, settings, indent, ":=");
  817. continue;
  818. }
  819. if (input.regexStartsWith(/[\w\s:]*\bPORT\b([\s]|$)/)) {
  820. var preCursor = block.cursor;
  821. beautifyPortGenericBlock(block, result, settings, indent, "PORT");
  822. var preLine = preCursor - 1;
  823. if (preLine >= 0) {
  824. var preL = block.lines[preLine];
  825. if (preL.regexIndexOf(/:\s+(COMPONENT|ENTITY)/) >= 0) {
  826. indent--;
  827. }
  828. }
  829. continue;
  830. }
  831. if (input.regexStartsWith(/TYPE\s+\w+\s+IS\s+\(/)) {
  832. beautifyPortGenericBlock(block, result, settings, indent, "IS");
  833. continue;
  834. }
  835. if (input.regexStartsWith(/[\w\s:]*GENERIC([\s]|$)/)) {
  836. beautifyPortGenericBlock(block, result, settings, indent, "GENERIC");
  837. continue;
  838. }
  839. if (input.regexStartsWith(/[\w\s:]*PROCEDURE[\s\w]+\($/)) {
  840. beautifyPortGenericBlock(block, result, settings, indent, "PROCEDURE");
  841. if (block.lines[block.cursor].regexStartsWith(/.*\)[\s]*IS/)) {
  842. block.cursor++;
  843. beautify3(block, result, settings, indent + 1);
  844. }
  845. continue;
  846. }
  847. if (input.regexStartsWith(/FUNCTION[^\w]/)
  848. && input.regexIndexOf(/[^\w]RETURN[^\w]/) < 0) {
  849. beautifyPortGenericBlock(block, result, settings, indent, "FUNCTION");
  850. if (!block.lines[block.cursor].regexStartsWith(regexBlockEndsKeyWords)) {
  851. block.cursor++;
  852. beautify3(block, result, settings, indent + 1);
  853. }
  854. else {
  855. result[block.cursor].Indent++;
  856. }
  857. continue;
  858. }
  859. if (input.regexStartsWith(/IMPURE FUNCTION[^\w]/)
  860. && input.regexIndexOf(/[^\w]RETURN[^\w]/) < 0) {
  861. beautifyPortGenericBlock(block, result, settings, indent, "IMPURE FUNCTION");
  862. if (!block.lines[block.cursor].regexStartsWith(regexBlockEndsKeyWords)) {
  863. if (block.lines[block.cursor].regexStartsWith(regexBlockIndentedEndsKeyWords)) {
  864. result[block.cursor].Indent++;
  865. }
  866. else {
  867. block.cursor++;
  868. beautify3(block, result, settings, indent + 1);
  869. }
  870. }
  871. else {
  872. result[block.cursor].Indent++;
  873. }
  874. continue;
  875. }
  876. result.push(new FormattedLine(input, indent));
  877. if (indent > 0
  878. && (input.regexStartsWith(regexBlockMidKeyWords)
  879. || (Mode != FormatMode.EndsWithSemicolon && input.regexStartsWith(regexMidKeyElse))
  880. || (Mode == FormatMode.CaseWhen && input.regexStartsWith(regexMidKeyWhen)))) {
  881. result[block.cursor].Indent--;
  882. }
  883. else if (indent > 0
  884. && (input.regexStartsWith(regexBlockEndsKeyWords))) {
  885. result[block.cursor].Indent--;
  886. return;
  887. }
  888. if (input.regexStartsWith(regexOneLineBlockKeyWords)) {
  889. continue;
  890. }
  891. if (input.regexStartsWith(regexFunctionMultiLineBlockKeyWords)
  892. || input.regexStartsWith(regexBlockStartsKeywords)) {
  893. block.cursor++;
  894. beautify3(block, result, settings, indent + 1);
  895. }
  896. }
  897. block.cursor--;
  898. }
  899. exports.beautify3 = beautify3;
  900. function ReserveSemicolonInKeywords(arr) {
  901. for (let i = 0; i < arr.length; i++) {
  902. if (arr[i].match(/FUNCTION|PROCEDURE/) != null) {
  903. arr[i] = arr[i].replace(/;/g, ILSemicolon);
  904. }
  905. }
  906. }
  907. function ApplyNoNewLineAfter(arr, noNewLineAfter) {
  908. if (noNewLineAfter == null) {
  909. return;
  910. }
  911. for (let i = 0; i < arr.length; i++) {
  912. noNewLineAfter.forEach(n => {
  913. let regex = new RegExp("(" + n.toUpperCase + ")[ a-z0-9]+[a-z0-9]+");
  914. if (arr[i].regexIndexOf(regex) >= 0) {
  915. arr[i] += "@@singleline";
  916. }
  917. });
  918. }
  919. }
  920. exports.ApplyNoNewLineAfter = ApplyNoNewLineAfter;
  921. function RemoveAsserts(arr) {
  922. let need_semi = false;
  923. let inAssert = false;
  924. let n = 0;
  925. for (let i = 0; i < arr.length; i++) {
  926. let has_semi = arr[i].indexOf(";") >= 0;
  927. if (need_semi) {
  928. arr[i] = '';
  929. }
  930. n = arr[i].indexOf("ASSERT ");
  931. if (n >= 0) {
  932. inAssert = true;
  933. arr[i] = '';
  934. }
  935. if (!has_semi) {
  936. if (inAssert) {
  937. need_semi = true;
  938. }
  939. }
  940. else {
  941. need_semi = false;
  942. }
  943. }
  944. }
  945. exports.RemoveAsserts = RemoveAsserts;
  946. function escapeText(arr, regex, escapedChar) {
  947. let quotes = [];
  948. let regexEpr = new RegExp(regex, "g");
  949. for (let i = 0; i < arr.length; i++) {
  950. let matches = arr[i].match(regexEpr);
  951. if (matches != null) {
  952. for (var j = 0; j < matches.length; j++) {
  953. var match = matches[j];
  954. arr[i] = arr[i].replace(match, escapedChar.repeat(match.length));
  955. quotes.push(match);
  956. }
  957. }
  958. }
  959. return quotes;
  960. }
  961. function RemoveExtraNewLines(input) {
  962. input = input.replace(/(?:\r\n|\r|\n)/g, '\r\n');
  963. input = input.replace(/ \r\n/g, '\r\n');
  964. input = input.replace(/\r\n\r\n\r\n/g, '\r\n');
  965. return input;
  966. }
  967. //# sourceMappingURL=VHDLFormatter.js.map