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.

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