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.

732 lines
29 KiB

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