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.

840 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) {
  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. }
  270. }
  271. exports.BeautifierSettings = BeautifierSettings;
  272. 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"];
  273. let TypeNames = ["BOOLEAN", "BIT", "CHARACTER", "INTEGER", "TIME", "NATURAL", "POSITIVE", "STRING"];
  274. function beautify(input, settings) {
  275. input = input.replace(/\r\n/g, "\n");
  276. input = input.replace(/\n/g, "\r\n");
  277. var arr = input.split("\r\n");
  278. var comments = EscapeComments(arr);
  279. var backslashes = escapeText(arr, "\\\\[^\\\\]+\\\\", ILBackslash);
  280. let quotes = escapeText(arr, '"([^"]+)"', ILQuote);
  281. let singleQuotes = escapeText(arr, "'[^']'", ILSingleQuote);
  282. RemoveLeadingWhitespaces(arr);
  283. input = arr.join("\r\n");
  284. if (settings.RemoveComments) {
  285. input = input.replace(/\r\n[ \t]*@@comments[0-9]+[ \t]*\r\n/g, '\r\n');
  286. input = input.replace(/@@comments[0-9]+/g, '');
  287. comments = [];
  288. }
  289. input = SetKeywordCase(input, "uppercase", KeyWords);
  290. input = SetKeywordCase(input, "uppercase", TypeNames);
  291. input = RemoveExtraNewLines(input);
  292. input = input.replace(/[\t ]+/g, ' ');
  293. input = input.replace(/\([\t ]+/g, '\(');
  294. input = input.replace(/[ ]+;/g, ';');
  295. input = input.replace(/:[ ]*(PROCESS|ENTITY)/gi, ':$1');
  296. arr = input.split("\r\n");
  297. if (settings.RemoveAsserts) {
  298. RemoveAsserts(arr); //RemoveAsserts must be after EscapeQuotes
  299. }
  300. ReserveSemicolonInKeywords(arr);
  301. input = arr.join("\r\n");
  302. input = input.replace(/\b(PORT|GENERIC)\b\s+MAP/g, '$1 MAP');
  303. input = input.replace(/\b(PORT|PROCESS|GENERIC)\b[\s]*\(/g, '$1 (');
  304. let newLineSettings = settings.NewLineSettings;
  305. if (newLineSettings != null) {
  306. input = SetNewLinesAfterSymbols(input, newLineSettings);
  307. arr = input.split("\r\n");
  308. ApplyNoNewLineAfter(arr, newLineSettings.noNewLineAfter);
  309. input = arr.join("\r\n");
  310. }
  311. input = input.replace(/([a-zA-Z0-9\); ])\);(@@comments[0-9]+)?@@end/g, '$1\r\n);$2@@end');
  312. input = input.replace(/[ ]?([&=:\-\+|\*]|[<>]+)[ ]?/g, ' $1 ');
  313. input = input.replace(/(\d+e) +([+\-]) +(\d+)/g, '$1$2$3'); // fix exponential notation format broken by previous step
  314. input = input.replace(/[ ]?([,])[ ]?/g, '$1 ');
  315. input = input.replace(/[ ]?(['"])(THEN)/g, '$1 $2');
  316. input = input.replace(/[ ]?(\?)?[ ]?(<|:|>|\/)?[ ]+(=)?[ ]?/g, ' $1$2$3 ');
  317. input = input.replace(/(IF)[ ]?([\(\)])/g, '$1 $2');
  318. input = input.replace(/([\(\)])[ ]?(THEN)/gi, '$1 $2');
  319. input = input.replace(/(^|[\(\)])[ ]?(AND|OR|XOR|XNOR)[ ]*([\(])/g, '$1 $2 $3');
  320. input = input.replace(/ ([\-\*\/=+<>])[ ]*([\-\*\/=+<>]) /g, " $1$2 ");
  321. //input = input.replace(/\r\n[ \t]+--\r\n/g, "\r\n");
  322. input = input.replace(/[ ]+/g, ' ');
  323. input = input.replace(/[ \t]+\r\n/g, "\r\n");
  324. input = input.replace(/\r\n\r\n\r\n/g, '\r\n');
  325. input = input.replace(/[\r\n\s]+$/g, '');
  326. input = input.replace(/[ \t]+\)/g, ')');
  327. input = input.replace(/\s*\)\s+RETURN\s+([\w]+;)/g, '\r\n) RETURN $1'); //function(..)\r\nreturn type; -> function(..\r\n)return type;
  328. 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;
  329. let keywordAndSignRegex = new RegExp("(\\b" + KeyWords.join("\\b|\\b") + "\\b) +([\\-+]) +(\\w)", "g");
  330. input = input.replace(keywordAndSignRegex, "$1 $2$3"); // `WHEN - 2` -> `WHEN -2`
  331. input = input.replace(/([,|]) +([+\-]) +(\w)/g, '$1 $2$3'); // `1, - 2)` -> `1, -2)`
  332. input = input.replace(/(\() +([+\-]) +(\w)/g, '$1$2$3'); // `( - 2)` -> `(-2)`
  333. arr = input.split("\r\n");
  334. let result = [];
  335. beautify3(arr, result, settings, 0, 0);
  336. var alignSettings = settings.SignAlignSettings;
  337. if (alignSettings != null && alignSettings.isAll) {
  338. AlignSigns(result, 0, result.length - 1, alignSettings.mode);
  339. }
  340. arr = FormattedLineToString(result, settings.Indentation);
  341. input = arr.join("\r\n");
  342. input = input.replace(/@@RETURN/g, "RETURN");
  343. input = SetKeywordCase(input, settings.KeywordCase, KeyWords);
  344. input = SetKeywordCase(input, settings.TypeNameCase, TypeNames);
  345. input = replaceEscapedWords(input, quotes, ILQuote);
  346. input = replaceEscapedWords(input, singleQuotes, ILSingleQuote);
  347. input = replaceEscapedComments(input, comments, ILCommentPrefix);
  348. input = replaceEscapedWords(input, backslashes, ILBackslash);
  349. input = input.replace(new RegExp(ILSemicolon, "g"), ";");
  350. input = input.replace(/@@[a-z]+/g, "");
  351. var escapedTexts = new RegExp("[" + ILBackslash + ILQuote + ILSingleQuote + "]", "g");
  352. input = input.replace(escapedTexts, "");
  353. input = input.replace(/\r\n/g, settings.EndOfLine);
  354. return input;
  355. }
  356. exports.beautify = beautify;
  357. function replaceEscapedWords(input, arr, prefix) {
  358. for (var i = 0; i < arr.length; i++) {
  359. var text = arr[i];
  360. var regex = new RegExp("(" + prefix + "){" + text.length + "}");
  361. input = input.replace(regex, text);
  362. }
  363. return input;
  364. }
  365. function replaceEscapedComments(input, arr, prefix) {
  366. for (var i = 0; i < arr.length; i++) {
  367. input = input.replace(prefix + i, arr[i]);
  368. }
  369. return input;
  370. }
  371. function RemoveLeadingWhitespaces(arr) {
  372. for (var i = 0; i < arr.length; i++) {
  373. arr[i] = arr[i].replace(/^\s+/, "");
  374. }
  375. }
  376. class FormattedLine {
  377. constructor(line, indent) {
  378. this.Line = line;
  379. this.Indent = indent;
  380. }
  381. }
  382. exports.FormattedLine = FormattedLine;
  383. function FormattedLineToString(arr, indentation) {
  384. let result = [];
  385. if (arr == null) {
  386. return result;
  387. }
  388. if (indentation == null) {
  389. indentation = "";
  390. }
  391. arr.forEach(i => {
  392. if (i instanceof FormattedLine) {
  393. if (i.Line.length > 0) {
  394. result.push((Array(i.Indent + 1).join(indentation)) + i.Line);
  395. }
  396. else {
  397. result.push("");
  398. }
  399. }
  400. else {
  401. result = result.concat(FormattedLineToString(i, indentation));
  402. }
  403. });
  404. return result;
  405. }
  406. exports.FormattedLineToString = FormattedLineToString;
  407. function GetCloseparentheseEndIndex(inputs, startIndex) {
  408. let openParentheseCount = 0;
  409. let closeParentheseCount = 0;
  410. for (let i = startIndex; i < inputs.length; i++) {
  411. let input = inputs[i];
  412. openParentheseCount += input.count("(");
  413. closeParentheseCount += input.count(")");
  414. if (openParentheseCount > 0
  415. && openParentheseCount <= closeParentheseCount) {
  416. return i;
  417. }
  418. }
  419. return startIndex;
  420. }
  421. function beautifyPortGenericBlock(inputs, result, settings, startIndex, parentEndIndex, indent, mode) {
  422. let firstLine = inputs[startIndex];
  423. let regex = new RegExp("[\\w\\s:]*(" + mode + ")([\\s]|$)");
  424. if (!firstLine.regexStartsWith(regex)) {
  425. return [startIndex, parentEndIndex];
  426. }
  427. let firstLineHasParenthese = firstLine.indexOf("(") >= 0;
  428. let hasParenthese = firstLineHasParenthese;
  429. let blockBodyStartIndex = startIndex;
  430. let secondLineHasParenthese = startIndex + 1 < inputs.length && inputs[startIndex + 1].startsWith("(");
  431. if (secondLineHasParenthese) {
  432. hasParenthese = true;
  433. blockBodyStartIndex++;
  434. }
  435. let endIndex = hasParenthese ? GetCloseparentheseEndIndex(inputs, startIndex) : startIndex;
  436. if (endIndex != startIndex && firstLineHasParenthese) {
  437. inputs[startIndex] = inputs[startIndex].replace(/\b(PORT|GENERIC|PROCEDURE)\b([\w ]+)\(([\w\(\) ]+)/, '$1$2(\r\n$3');
  438. let newInputs = inputs[startIndex].split("\r\n");
  439. if (newInputs.length == 2) {
  440. inputs[startIndex] = newInputs[0];
  441. inputs.splice(startIndex + 1, 0, newInputs[1]);
  442. endIndex++;
  443. parentEndIndex++;
  444. }
  445. }
  446. else if (endIndex > startIndex + 1 && secondLineHasParenthese) {
  447. inputs[startIndex + 1] = inputs[startIndex + 1].replace(/\(([\w\(\) ]+)/, '(\r\n$1');
  448. let newInputs = inputs[startIndex + 1].split("\r\n");
  449. if (newInputs.length == 2) {
  450. inputs[startIndex + 1] = newInputs[0];
  451. inputs.splice(startIndex + 2, 0, newInputs[1]);
  452. endIndex++;
  453. parentEndIndex++;
  454. }
  455. }
  456. if (firstLineHasParenthese && inputs[startIndex].indexOf("MAP") > 0) {
  457. inputs[startIndex] = inputs[startIndex].replace(/([^\w])(MAP)\s+\(/g, '$1$2(');
  458. }
  459. result.push(new FormattedLine(inputs[startIndex], indent));
  460. if (secondLineHasParenthese) {
  461. let secondLineIndent = indent;
  462. if (endIndex == startIndex + 1) {
  463. secondLineIndent++;
  464. }
  465. result.push(new FormattedLine(inputs[startIndex + 1], secondLineIndent));
  466. }
  467. let blockBodyEndIndex = endIndex;
  468. let i = beautify3(inputs, result, settings, blockBodyStartIndex + 1, indent + 1, endIndex);
  469. if (inputs[i].startsWith(")")) {
  470. result[i].Indent--;
  471. blockBodyEndIndex--;
  472. }
  473. var alignSettings = settings.SignAlignSettings;
  474. if (alignSettings != null) {
  475. if (alignSettings.isRegional && !alignSettings.isAll
  476. && alignSettings.keyWords != null
  477. && alignSettings.keyWords.indexOf(mode) >= 0) {
  478. blockBodyStartIndex++;
  479. AlignSigns(result, blockBodyStartIndex, blockBodyEndIndex, alignSettings.mode);
  480. }
  481. }
  482. return [i, parentEndIndex];
  483. }
  484. exports.beautifyPortGenericBlock = beautifyPortGenericBlock;
  485. function AlignSigns(result, startIndex, endIndex, mode) {
  486. AlignSign_(result, startIndex, endIndex, ":", mode);
  487. AlignSign_(result, startIndex, endIndex, ":=", mode);
  488. AlignSign_(result, startIndex, endIndex, "<=", mode);
  489. AlignSign_(result, startIndex, endIndex, "=>", mode);
  490. AlignSign_(result, startIndex, endIndex, "@@comments", mode);
  491. }
  492. exports.AlignSigns = AlignSigns;
  493. function AlignSign_(result, startIndex, endIndex, symbol, mode) {
  494. let maxSymbolIndex = -1;
  495. let symbolIndices = {};
  496. let startLine = startIndex;
  497. let labelAndKeywords = [
  498. "([\\w\\s]*:(\\s)*PROCESS)",
  499. "([\\w\\s]*:(\\s)*POSTPONED PROCESS)",
  500. "([\\w\\s]*:\\s*$)",
  501. "([\\w\\s]*:.*\\s+GENERATE)"
  502. ];
  503. let labelAndKeywordsStr = labelAndKeywords.join("|");
  504. let labelAndKeywordsRegex = new RegExp("(" + labelAndKeywordsStr + ")([^\\w]|$)");
  505. for (let i = startIndex; i <= endIndex; i++) {
  506. let line = result[i].Line;
  507. if (symbol == ":" && line.regexStartsWith(labelAndKeywordsRegex)) {
  508. continue;
  509. }
  510. let regex = new RegExp("([\\s\\w\\\\]|^)" + symbol + "([\\s\\w\\\\]|$)");
  511. if (line.regexCount(regex) > 1) {
  512. continue;
  513. }
  514. let colonIndex = line.regexIndexOf(regex);
  515. if (colonIndex > 0) {
  516. maxSymbolIndex = Math.max(maxSymbolIndex, colonIndex);
  517. symbolIndices[i] = colonIndex;
  518. }
  519. else if ((mode != "local" && !line.startsWith(ILCommentPrefix) && line.length != 0)
  520. || (mode == "local")) {
  521. if (startLine < i - 1) // if cannot find the symbol, a block of symbols ends
  522. {
  523. AlignSign(result, startLine, i - 1, symbol, maxSymbolIndex, symbolIndices);
  524. }
  525. maxSymbolIndex = -1;
  526. symbolIndices = {};
  527. startLine = i;
  528. }
  529. }
  530. if (startLine < endIndex) // if cannot find the symbol, a block of symbols ends
  531. {
  532. AlignSign(result, startLine, endIndex, symbol, maxSymbolIndex, symbolIndices);
  533. }
  534. }
  535. function AlignSign(result, startIndex, endIndex, symbol, maxSymbolIndex = -1, symbolIndices = {}) {
  536. if (maxSymbolIndex < 0) {
  537. return;
  538. }
  539. for (let lineIndex in symbolIndices) {
  540. let symbolIndex = symbolIndices[lineIndex];
  541. if (symbolIndex == maxSymbolIndex) {
  542. continue;
  543. }
  544. let line = result[lineIndex].Line;
  545. result[lineIndex].Line = line.substring(0, symbolIndex)
  546. + (Array(maxSymbolIndex - symbolIndex + 1).join(" "))
  547. + line.substring(symbolIndex);
  548. }
  549. }
  550. exports.AlignSign = AlignSign;
  551. function beautifyCaseBlock(inputs, result, settings, startIndex, indent) {
  552. if (!inputs[startIndex].regexStartsWith(/(.+:\s*)?(CASE)([\s]|$)/)) {
  553. return startIndex;
  554. }
  555. result.push(new FormattedLine(inputs[startIndex], indent));
  556. let i = beautify3(inputs, result, settings, startIndex + 1, indent + 2);
  557. result[i].Indent = indent;
  558. return i;
  559. }
  560. exports.beautifyCaseBlock = beautifyCaseBlock;
  561. function getSemicolonBlockEndIndex(inputs, settings, startIndex, parentEndIndex) {
  562. let endIndex = 0;
  563. let openBracketsCount = 0;
  564. let closeBracketsCount = 0;
  565. for (let i = startIndex; i < inputs.length; i++) {
  566. let input = inputs[i];
  567. let indexOfSemicolon = input.indexOf(";");
  568. let splitIndex = indexOfSemicolon < 0 ? input.length : indexOfSemicolon + 1;
  569. let stringBeforeSemicolon = input.substring(0, splitIndex);
  570. let stringAfterSemicolon = input.substring(splitIndex);
  571. stringAfterSemicolon = stringAfterSemicolon.replace(new RegExp(ILCommentPrefix + "[0-9]+"), "");
  572. openBracketsCount += stringBeforeSemicolon.count("(");
  573. closeBracketsCount += stringBeforeSemicolon.count(")");
  574. if (indexOfSemicolon < 0) {
  575. continue;
  576. }
  577. if (openBracketsCount == closeBracketsCount) {
  578. endIndex = i;
  579. if (stringAfterSemicolon.trim().length > 0 && settings.NewLineSettings.newLineAfter.indexOf(";") >= 0) {
  580. inputs[i] = stringBeforeSemicolon;
  581. inputs.splice(i, 0, stringAfterSemicolon);
  582. parentEndIndex++;
  583. }
  584. break;
  585. }
  586. }
  587. return [endIndex, parentEndIndex];
  588. }
  589. function beautifyComponentBlock(inputs, result, settings, startIndex, parentEndIndex, indent) {
  590. let endIndex = startIndex;
  591. for (let i = startIndex; i < inputs.length; i++) {
  592. if (inputs[i].regexStartsWith(/END(\s|$)/)) {
  593. endIndex = i;
  594. break;
  595. }
  596. }
  597. result.push(new FormattedLine(inputs[startIndex], indent));
  598. if (endIndex != startIndex) {
  599. let actualEndIndex = beautify3(inputs, result, settings, startIndex + 1, indent + 1, endIndex);
  600. let incremental = actualEndIndex - endIndex;
  601. endIndex += incremental;
  602. parentEndIndex += incremental;
  603. }
  604. return [endIndex, parentEndIndex];
  605. }
  606. exports.beautifyComponentBlock = beautifyComponentBlock;
  607. function beautifySemicolonBlock(inputs, result, settings, startIndex, parentEndIndex, indent) {
  608. let endIndex = startIndex;
  609. [endIndex, parentEndIndex] = getSemicolonBlockEndIndex(inputs, settings, startIndex, parentEndIndex);
  610. result.push(new FormattedLine(inputs[startIndex], indent));
  611. if (endIndex != startIndex) {
  612. let i = beautify3(inputs, result, settings, startIndex + 1, indent + 1, endIndex);
  613. }
  614. return [endIndex, parentEndIndex];
  615. }
  616. exports.beautifySemicolonBlock = beautifySemicolonBlock;
  617. function beautify3(inputs, result, settings, startIndex, indent, endIndex) {
  618. let i;
  619. let regexOneLineBlockKeyWords = new RegExp(/(PROCEDURE)[^\w](?!.+[^\w]IS([^\w]|$))/); //match PROCEDURE..; but not PROCEDURE .. IS;
  620. let regexFunctionMultiLineBlockKeyWords = new RegExp(/(FUNCTION|IMPURE FUNCTION)[^\w](?=.+[^\w]IS([^\w]|$))/); //match FUNCTION .. IS; but not FUNCTION
  621. let blockMidKeyWords = ["BEGIN"];
  622. let blockStartsKeyWords = [
  623. "IF",
  624. "CASE",
  625. "ARCHITECTURE",
  626. "PROCEDURE",
  627. "PACKAGE",
  628. "(([\\w\\s]*:)?(\\s)*PROCESS)",
  629. "(([\\w\\s]*:)?(\\s)*POSTPONED PROCESS)",
  630. "(.*\\s*PROTECTED)",
  631. "(COMPONENT)",
  632. "(ENTITY(?!.+;))",
  633. "FOR",
  634. "WHILE",
  635. "LOOP",
  636. "(.*\\s*GENERATE)",
  637. "(CONTEXT[\\w\\s\\\\]+IS)",
  638. "(CONFIGURATION(?!.+;))",
  639. "BLOCK",
  640. "UNITS",
  641. "\\w+\\s+\\w+\\s+IS\\s+RECORD"
  642. ];
  643. let blockEndsKeyWords = ["END", ".*\\)\\s*RETURN\\s+[\\w]+;"];
  644. let indentedEndsKeyWords = [ILIndentedReturnPrefix + "RETURN\\s+\\w+;"];
  645. let blockEndsWithSemicolon = [
  646. "(WITH\\s+[\\w\\s\\\\]+SELECT)",
  647. "([\\w\\\\]+[\\s]*<=)",
  648. "([\\w\\\\]+[\\s]*:=)",
  649. "FOR\\s+[\\w\\s,]+:\\s*\\w+\\s+USE",
  650. "REPORT"
  651. ];
  652. let newLineAfterKeyWordsStr = blockStartsKeyWords.join("|");
  653. let regexBlockMidKeyWords = blockMidKeyWords.convertToRegexBlockWords();
  654. let regexBlockStartsKeywords = new RegExp("([\\w]+\\s*:\\s*)?(" + newLineAfterKeyWordsStr + ")([^\\w]|$)");
  655. let regexBlockEndsKeyWords = blockEndsKeyWords.convertToRegexBlockWords();
  656. let regexBlockIndentedEndsKeyWords = indentedEndsKeyWords.convertToRegexBlockWords();
  657. let regexblockEndsWithSemicolon = blockEndsWithSemicolon.convertToRegexBlockWords();
  658. let regexMidKeyWhen = "WHEN".convertToRegexBlockWords();
  659. let regexMidKeyElse = "ELSE|ELSIF".convertToRegexBlockWords();
  660. if (endIndex == null) {
  661. endIndex = inputs.length - 1;
  662. }
  663. for (i = startIndex; i <= endIndex; i++) {
  664. if (indent < 0) {
  665. indent = 0;
  666. }
  667. let input = inputs[i].trim();
  668. if (input.regexStartsWith(regexBlockIndentedEndsKeyWords)) {
  669. result.push(new FormattedLine(input, indent));
  670. return i;
  671. }
  672. if (input.regexStartsWith(/COMPONENT\s/)) {
  673. let modeCache = Mode;
  674. Mode = FormatMode.EndsWithSemicolon;
  675. [i, endIndex] = beautifyComponentBlock(inputs, result, settings, i, endIndex, indent);
  676. Mode = modeCache;
  677. continue;
  678. }
  679. if (input.regexStartsWith(/\w+\s*:\s*ENTITY/)) {
  680. let modeCache = Mode;
  681. Mode = FormatMode.EndsWithSemicolon;
  682. [i, endIndex] = beautifySemicolonBlock(inputs, result, settings, i, endIndex, indent);
  683. Mode = modeCache;
  684. continue;
  685. }
  686. if (Mode != FormatMode.EndsWithSemicolon && input.regexStartsWith(regexblockEndsWithSemicolon)) {
  687. let modeCache = Mode;
  688. Mode = FormatMode.EndsWithSemicolon;
  689. [i, endIndex] = beautifySemicolonBlock(inputs, result, settings, i, endIndex, indent);
  690. Mode = modeCache;
  691. continue;
  692. }
  693. if (input.regexStartsWith(/(.+:\s*)?(CASE)([\s]|$)/)) {
  694. let modeCache = Mode;
  695. Mode = FormatMode.CaseWhen;
  696. i = beautifyCaseBlock(inputs, result, settings, i, indent);
  697. Mode = modeCache;
  698. continue;
  699. }
  700. if (input.regexStartsWith(/.*?\:\=\s*\($/)) {
  701. [i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, ":=");
  702. continue;
  703. }
  704. if (input.regexStartsWith(/[\w\s:]*\bPORT\b([\s]|$)/)) {
  705. [i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, "PORT");
  706. continue;
  707. }
  708. if (input.regexStartsWith(/TYPE\s+\w+\s+IS\s+\(/)) {
  709. [i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, "IS");
  710. continue;
  711. }
  712. if (input.regexStartsWith(/[\w\s:]*GENERIC([\s]|$)/)) {
  713. [i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, "GENERIC");
  714. continue;
  715. }
  716. if (input.regexStartsWith(/[\w\s:]*PROCEDURE[\s\w]+\($/)) {
  717. [i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, "PROCEDURE");
  718. if (inputs[i].regexStartsWith(/.*\)[\s]*IS/)) {
  719. i = beautify3(inputs, result, settings, i + 1, indent + 1);
  720. }
  721. continue;
  722. }
  723. if (input.regexStartsWith(/FUNCTION[^\w]/)
  724. && input.regexIndexOf(/[^\w]RETURN[^\w]/) < 0) {
  725. [i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, "FUNCTION");
  726. if (!inputs[i].regexStartsWith(regexBlockEndsKeyWords)) {
  727. i = beautify3(inputs, result, settings, i + 1, indent + 1);
  728. }
  729. else {
  730. result[i].Indent++;
  731. }
  732. continue;
  733. }
  734. if (input.regexStartsWith(/IMPURE FUNCTION[^\w]/)
  735. && input.regexIndexOf(/[^\w]RETURN[^\w]/) < 0) {
  736. [i, endIndex] = beautifyPortGenericBlock(inputs, result, settings, i, endIndex, indent, "IMPURE FUNCTION");
  737. if (!inputs[i].regexStartsWith(regexBlockEndsKeyWords)) {
  738. if (inputs[i].regexStartsWith(regexBlockIndentedEndsKeyWords)) {
  739. result[i].Indent++;
  740. }
  741. else {
  742. i = beautify3(inputs, result, settings, i + 1, indent + 1);
  743. }
  744. }
  745. else {
  746. result[i].Indent++;
  747. }
  748. continue;
  749. }
  750. result.push(new FormattedLine(input, indent));
  751. if (startIndex != 0
  752. && (input.regexStartsWith(regexBlockMidKeyWords)
  753. || (Mode != FormatMode.EndsWithSemicolon && input.regexStartsWith(regexMidKeyElse))
  754. || (Mode == FormatMode.CaseWhen && input.regexStartsWith(regexMidKeyWhen)))) {
  755. result[i].Indent--;
  756. }
  757. else if (startIndex != 0
  758. && (input.regexStartsWith(regexBlockEndsKeyWords))) {
  759. result[i].Indent--;
  760. return i;
  761. }
  762. if (input.regexStartsWith(regexOneLineBlockKeyWords)) {
  763. continue;
  764. }
  765. if (input.regexStartsWith(regexFunctionMultiLineBlockKeyWords)
  766. || input.regexStartsWith(regexBlockStartsKeywords)) {
  767. i = beautify3(inputs, result, settings, i + 1, indent + 1);
  768. }
  769. }
  770. i--;
  771. return i;
  772. }
  773. exports.beautify3 = beautify3;
  774. function ReserveSemicolonInKeywords(arr) {
  775. for (let i = 0; i < arr.length; i++) {
  776. if (arr[i].match(/FUNCTION|PROCEDURE/) != null) {
  777. arr[i] = arr[i].replace(/;/g, ILSemicolon);
  778. }
  779. }
  780. }
  781. function ApplyNoNewLineAfter(arr, noNewLineAfter) {
  782. if (noNewLineAfter == null) {
  783. return;
  784. }
  785. for (let i = 0; i < arr.length; i++) {
  786. noNewLineAfter.forEach(n => {
  787. let regex = new RegExp("(" + n.toUpperCase + ")[ a-z0-9]+[a-z0-9]+");
  788. if (arr[i].regexIndexOf(regex) >= 0) {
  789. arr[i] += "@@singleline";
  790. }
  791. });
  792. }
  793. }
  794. exports.ApplyNoNewLineAfter = ApplyNoNewLineAfter;
  795. function RemoveAsserts(arr) {
  796. let need_semi = false;
  797. let inAssert = false;
  798. let n = 0;
  799. for (let i = 0; i < arr.length; i++) {
  800. let has_semi = arr[i].indexOf(";") >= 0;
  801. if (need_semi) {
  802. arr[i] = '';
  803. }
  804. n = arr[i].indexOf("ASSERT ");
  805. if (n >= 0) {
  806. inAssert = true;
  807. arr[i] = '';
  808. }
  809. if (!has_semi) {
  810. if (inAssert) {
  811. need_semi = true;
  812. }
  813. }
  814. else {
  815. need_semi = false;
  816. }
  817. }
  818. }
  819. exports.RemoveAsserts = RemoveAsserts;
  820. function escapeText(arr, regex, escapedChar) {
  821. let quotes = [];
  822. let regexEpr = new RegExp(regex, "g");
  823. for (let i = 0; i < arr.length; i++) {
  824. let matches = arr[i].match(regexEpr);
  825. if (matches != null) {
  826. for (var j = 0; j < matches.length; j++) {
  827. var match = matches[j];
  828. arr[i] = arr[i].replace(match, escapedChar.repeat(match.length));
  829. quotes.push(match);
  830. }
  831. }
  832. }
  833. return quotes;
  834. }
  835. function RemoveExtraNewLines(input) {
  836. input = input.replace(/(?:\r\n|\r|\n)/g, '\r\n');
  837. input = input.replace(/ \r\n/g, '\r\n');
  838. input = input.replace(/\r\n\r\n\r\n/g, '\r\n');
  839. return input;
  840. }
  841. //# sourceMappingURL=VHDLFormatter.js.map