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.

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