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.

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