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.

767 lines
31 KiB

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