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.

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