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.

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