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.

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