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.

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