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.

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