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.

939 lines
36 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. let isTesting = false;
  4. const ILCommentPrefix = "@@comments";
  5. const ILQuotesPrefix = "@@quotes";
  6. class NewLineSettings {
  7. constructor() {
  8. this.newLineAfter = [];
  9. this.noNewLineAfter = [];
  10. }
  11. newLineAfterPush(keyword) {
  12. this.newLineAfter.push(keyword);
  13. }
  14. noNewLineAfterPush(keyword) {
  15. this.noNewLineAfter.push(keyword);
  16. }
  17. push(keyword, addNewLine) {
  18. let str = addNewLine.toLowerCase();
  19. if (str.indexOf("none") >= 0) {
  20. return;
  21. }
  22. else if (str.indexOf("no") < 0) {
  23. this.newLineAfterPush(keyword);
  24. }
  25. else {
  26. this.noNewLineAfterPush(keyword);
  27. }
  28. }
  29. }
  30. exports.NewLineSettings = NewLineSettings;
  31. function ConstructNewLineSettings(dict) {
  32. let settings = new NewLineSettings();
  33. for (let key in dict) {
  34. settings.push(key, dict[key]);
  35. }
  36. return settings;
  37. }
  38. function fetchHeader(url, wch) {
  39. try {
  40. var req = new XMLHttpRequest();
  41. req.open("HEAD", url, false);
  42. req.send(null);
  43. if (req.status == 200) {
  44. return req.getResponseHeader(wch);
  45. }
  46. else
  47. return false;
  48. }
  49. catch (e) {
  50. return "";
  51. }
  52. }
  53. String.prototype.regexStartsWith = function (pattern) {
  54. var searchResult = this.search(pattern);
  55. return searchResult == 0;
  56. };
  57. String.prototype.regexIndexOf = function (pattern, startIndex) {
  58. startIndex = startIndex || 0;
  59. var searchResult = this.substr(startIndex).search(pattern);
  60. return (-1 === searchResult) ? -1 : searchResult + startIndex;
  61. };
  62. String.prototype.regexLastIndexOf = function (pattern, startIndex) {
  63. startIndex = startIndex === undefined ? this.length : startIndex;
  64. var searchResult = this.substr(0, startIndex).reverse().regexIndexOf(pattern, 0);
  65. return (-1 === searchResult) ? -1 : this.length - ++searchResult;
  66. };
  67. String.prototype.reverse = function () {
  68. return this.split('').reverse().join('');
  69. };
  70. function wordWrap() {
  71. var d = document.getElementById("result");
  72. if (d.className == "") {
  73. d.className = "wordwrap";
  74. }
  75. else {
  76. d.className = "";
  77. }
  78. }
  79. function getHTMLInputElement(name) {
  80. return document.getElementById(name);
  81. }
  82. function noFormat() {
  83. let elements = ["remove_comments",
  84. "remove_lines",
  85. "remove_report",
  86. "check_alias",
  87. "sign_align",
  88. "sign_align_all",
  89. "new_line_after_port",
  90. "new_line",
  91. "use_space",
  92. "compress",
  93. "mix_letter"];
  94. var t = !(getHTMLInputElement("remove_comments").disabled);
  95. elements.forEach(element => {
  96. getHTMLInputElement(element).disabled = t;
  97. });
  98. let keyword = document.getElementById("keyword");
  99. for (let i = 0; i < keyword.elements.length; i++) {
  100. keyword.elements[i].disabled = t;
  101. }
  102. }
  103. function indent_decode() {
  104. var custom_indent = getHTMLInputElement("cust_indent").value;
  105. var result = indentDecode(custom_indent);
  106. document.getElementById("indent_s").innerHTML = result;
  107. }
  108. function indentDecode(input) {
  109. input = input.replace(/\\t/g, " ");
  110. var count = [" & one ", " & two ", " & three ", " & four ", " & five ", " & six ", " & seven ", " & eight ", " & many "];
  111. var tokens = input.split("");
  112. var result = "";
  113. var repeatedCharCount = 0;
  114. for (var i = 0; i < tokens.length; i++) {
  115. var char = input.substr(i, 1);
  116. if (char == input.substr(i + 1, 1)) {
  117. repeatedCharCount++;
  118. }
  119. else {
  120. switch (char) {
  121. case " ":
  122. char = "blankspace";
  123. break;
  124. case "\t":
  125. char = "tab";
  126. }
  127. repeatedCharCount = repeatedCharCount > 8 ? 8 : repeatedCharCount;
  128. result += count[repeatedCharCount] + char;
  129. repeatedCharCount = 0;
  130. }
  131. }
  132. if (result.length < 0) {
  133. switch (char) {
  134. case " ":
  135. char = "blankspace";
  136. break;
  137. case "\t":
  138. char = "tab";
  139. }
  140. repeatedCharCount = repeatedCharCount > 8 ? 8 : repeatedCharCount;
  141. result = count[repeatedCharCount] + char;
  142. }
  143. result = result.replace(/^ & /, "");
  144. return result;
  145. }
  146. exports.indentDecode = indentDecode;
  147. function Compress(input) {
  148. input = input.replace(/\r\n/g, '');
  149. input = input.replace(/[\t ]+/g, ' ');
  150. input = input.replace(/[ ]?([&=:\-<>\+|])[ ]?/g, '$1');
  151. return input;
  152. }
  153. function MixLetters(input) {
  154. let arr = input.split("");
  155. for (var k = 0; k < arr.length; k++) {
  156. if (arr[k] === arr[k].toUpperCase() && Math.random() > 0.5) {
  157. arr[k] = arr[k].toLowerCase();
  158. }
  159. else if (Math.random() > 0.5) {
  160. arr[k] = arr[k].toUpperCase();
  161. }
  162. }
  163. return arr.join("");
  164. }
  165. function EscapeComments(arr, comments, commentIndex) {
  166. for (var i = 0; i < arr.length; i++) {
  167. let line = arr[i];
  168. var firstCharIndex = line.regexIndexOf(/[a-zA-Z0-9\(\&\)%_\+'"|]/);
  169. var commentStartIndex = line.indexOf("--");
  170. if (firstCharIndex < commentStartIndex && firstCharIndex >= 0) {
  171. comments.push(line.substr(commentStartIndex));
  172. arr[i] = line.substr(firstCharIndex, commentStartIndex - firstCharIndex) + ILCommentPrefix + (commentIndex++);
  173. }
  174. else if ((firstCharIndex > commentStartIndex && commentStartIndex >= 0) || (firstCharIndex < 0 && commentStartIndex >= 0)) {
  175. comments.push(line.substr(commentStartIndex));
  176. arr[i] = ILCommentPrefix + (commentIndex++);
  177. }
  178. else {
  179. firstCharIndex = firstCharIndex < 0 ? 0 : firstCharIndex;
  180. arr[i] = line.substr(firstCharIndex);
  181. }
  182. }
  183. return commentIndex;
  184. }
  185. function ToLowerCases(arr) {
  186. for (var i = 0; i < arr.length; i++) {
  187. arr[i] = arr[i].toLowerCase();
  188. }
  189. }
  190. function ToCamelCases(arr) {
  191. for (var i = 0; i < arr.length; i++) {
  192. arr[i] = arr[i].charAt(0) + arr[i].slice(1).toLowerCase();
  193. }
  194. }
  195. function ReplaceKeyWords(text, keywords) {
  196. for (var k = 0; k < keywords.length; k++) {
  197. text = text.replace(new RegExp("([^a-zA-Z0-9_@]|^)" + keywords[k] + "([^a-zA-Z0-9_]|$)", 'gi'), "$1" + keywords[k] + "$2");
  198. }
  199. return text;
  200. }
  201. function SetKeywordCase(input, keywordcase, keywords, typenames) {
  202. let inputcase = keywordcase.toLowerCase();
  203. if (inputcase == "lowercase") {
  204. ToLowerCases(keywords);
  205. ToLowerCases(typenames);
  206. }
  207. else if (inputcase == "defaultcase") {
  208. ToCamelCases(keywords);
  209. ToCamelCases(typenames);
  210. }
  211. if (inputcase != "uppercase") {
  212. input = ReplaceKeyWords(input, keywords);
  213. input = ReplaceKeyWords(input, typenames);
  214. }
  215. return input;
  216. }
  217. function SetNewLinesAfterSymbols(text, newLineSettings) {
  218. if (newLineSettings == null) {
  219. return text;
  220. }
  221. if (newLineSettings.newLineAfter != null) {
  222. newLineSettings.newLineAfter.forEach(symbol => {
  223. let regex = new RegExp("(" + symbol.toUpperCase() + ")[ ]?([^ \r\n@])", "g");
  224. text = text.replace(regex, '$1\r\n$2');
  225. });
  226. }
  227. if (newLineSettings.noNewLineAfter != null) {
  228. newLineSettings.noNewLineAfter.forEach(symbol => {
  229. let regex = new RegExp("(" + symbol.toUpperCase() + ")[ \r\n]+([^@])", "g");
  230. text = text.replace(regex, '$1 $2');
  231. });
  232. }
  233. return text;
  234. }
  235. exports.SetNewLinesAfterSymbols = SetNewLinesAfterSymbols;
  236. class BeautifierSettings {
  237. constructor(removeComments, removeReport, checkAlias, signAlign, signAlignAll, keywordCase, indentation, newLineSettings) {
  238. this.RemoveComments = removeComments;
  239. this.RemoveAsserts = removeReport;
  240. this.CheckAlias = checkAlias;
  241. this.SignAlign = signAlign;
  242. this.SignAlignAll = signAlignAll;
  243. this.KeywordCase = keywordCase;
  244. this.Indentation = indentation;
  245. this.NewLineSettings = newLineSettings;
  246. }
  247. }
  248. exports.BeautifierSettings = BeautifierSettings;
  249. 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"];
  250. let TypeNames = ["BOOLEAN", "BIT", "CHARACTER", "INTEGER", "TIME", "NATURAL", "POSITIVE", "STRING"];
  251. function beautify(input, settings) {
  252. input = input.replace("\r\n", "\n");
  253. input = input.replace("\n", "\r\n");
  254. var arr = input.split("\r\n");
  255. var comments = [], commentsIndex = 0;
  256. commentsIndex = EscapeComments(arr, comments, commentsIndex);
  257. input = arr.join("\r\n");
  258. if (settings.RemoveComments) {
  259. input = input.replace(/\r\n[ \t]*@@comments[0-9]+[ \t]*\r\n/g, '\r\n');
  260. input = input.replace(/@@comments[0-9]+/g, '');
  261. commentsIndex = 0;
  262. }
  263. input = RemoveExtraNewLines(input);
  264. input = input.replace(/[\t ]+/g, ' ');
  265. input = input.replace(/\([\t ]+/g, '\(');
  266. input = input.replace(/[ ]+;/g, ';');
  267. input = input.replace(/:[ ]*(PROCESS|ENTITY)/gi, ':$1');
  268. input = ReplaceKeyWords(input, KeyWords);
  269. input = ReplaceKeyWords(input, TypeNames);
  270. arr = input.split("\r\n");
  271. ReserveSemicolonInKeywords(arr);
  272. input = arr.join("\r\n");
  273. input = input.replace(/(PORT|PROCESS|GENERIC)[\s]*\(/g, '$1 (');
  274. input = SetNewLinesAfterSymbols(input, settings.NewLineSettings);
  275. //input = beautify2(input, settings);
  276. //new
  277. input = input.replace(/([a-zA-Z0-9\); ])\);(@@comments[0-9]+)?@@end/g, '$1\r\n);$2@@end');
  278. input = input.replace(/[ ]?([&=:\-<>\+|\*])[ ]?/g, ' $1 ');
  279. input = input.replace(/[ ]?([,])[ ]?/g, '$1 ');
  280. input = input.replace(/[ ]?(['"])(THEN)/g, '$1 $2');
  281. input = input.replace(/[ ]?(\?)?[ ]?(<|:|>|\/)?[ ]+(=)?[ ]?/g, ' $1$2$3 ');
  282. input = input.replace(/(IF)[ ]?([\(\)])/g, '$1 $2');
  283. input = input.replace(/([\(\)])[ ]?(THEN)/gi, '$1 $2');
  284. input = input.replace(/(^|[\(\)])[ ]?(AND|OR|XOR|XNOR)[ ]*([\(])/g, '$1 $2 $3');
  285. input = input.replace(/ ([\-\*\/=+<>])[ ]*([\-\*\/=+<>]) /g, " $1$2 ");
  286. input = input.replace(/\r\n[ \t]+--\r\n/g, "\r\n");
  287. input = input.replace(/[ ]+/g, ' ');
  288. input = input.replace(/\r\n\r\n\r\n/g, '\r\n');
  289. input = input.replace(/[\r\n\s]+$/g, '');
  290. input = input.replace(/[ \t]+\)/g, ')');
  291. arr = input.split("\r\n");
  292. let result = [];
  293. beautify3(arr, result, settings, 0, 0);
  294. arr = FormattedLineToString(result, settings.Indentation);
  295. input = arr.join("\r\n");
  296. for (var k = 0; k < commentsIndex; k++) {
  297. input = input.replace(ILCommentPrefix + k, comments[k]);
  298. }
  299. input = input.replace(/@@semicolon/g, ";");
  300. input = input.replace(/@@[a-z]+/g, "");
  301. return input;
  302. }
  303. exports.beautify = beautify;
  304. class FormattedLine {
  305. constructor(line, indent) {
  306. this.Line = line;
  307. this.Indent = indent;
  308. }
  309. }
  310. exports.FormattedLine = FormattedLine;
  311. function FormattedLineToString(arr, indentation) {
  312. let result = [];
  313. if (arr == null) {
  314. return result;
  315. }
  316. arr.forEach(i => {
  317. if (i instanceof FormattedLine) {
  318. result.push((Array(i.Indent + 1).join(indentation)) + i.Line);
  319. }
  320. else {
  321. result = result.concat(FormattedLineToString(i, indentation));
  322. }
  323. });
  324. return result;
  325. }
  326. exports.FormattedLineToString = FormattedLineToString;
  327. function beautifyCaseBlock(inputs, result, settings, startIndex, indent, isFirstKeyWord) {
  328. if (!inputs[startIndex].regexStartsWith(/(CASE)([\s]|$)/)) {
  329. return startIndex;
  330. }
  331. result.push(new FormattedLine(inputs[startIndex], indent));
  332. let i = beautify3(inputs, result, settings, startIndex + 1, indent + 2);
  333. result[i].Indent = indent;
  334. return i;
  335. }
  336. exports.beautifyCaseBlock = beautifyCaseBlock;
  337. function beautify3(inputs, result, settings, startIndex, indent, isFirstKeyWord) {
  338. let i;
  339. let regexOneLineBlockKeyWords = new RegExp(/(PROCEDURE|FUNCTION|IMPURE FUNCTION)[^\w_](?!.+[^\w_]IS([^\w_]|$))/); //match PROCEDURE..; but not PROCEDURE .. IS;
  340. let blockMidKeyWords = ["ELSE", "ELSIF", "WHEN", "BEGIN"];
  341. let blockStartsKeyWords = [
  342. "IF",
  343. "CASE",
  344. "ARCHITECTURE",
  345. "PROCEDURE",
  346. "PACKAGE",
  347. "PROCESS",
  348. "POSTPONED PROCESS",
  349. "(\\w+:\\s+PROCESS)",
  350. "FUNCTION",
  351. "IMPURE FUNCTION",
  352. "(.+\\sPROTECTED)",
  353. "COMPONENT"
  354. ];
  355. let blockEndsKeyWords = ["END"];
  356. let newLineAfterKeyWordsStr = blockStartsKeyWords.join("|");
  357. let blockEndKeyWordsStr = blockEndsKeyWords.join("|");
  358. let blockMidKeyWordsStr = blockMidKeyWords.join("|");
  359. let regexBlockMidKeyWords = new RegExp("(" + blockMidKeyWordsStr + ")([^\\w_]|$)");
  360. let regexBlockStartsKeywords = new RegExp("(" + newLineAfterKeyWordsStr + ")([^\\w_]|$)");
  361. let regexBlockEndsKeyWords = new RegExp("(" + blockEndKeyWordsStr + ")([^\\w_]|$)");
  362. for (i = startIndex; i < inputs.length; i++) {
  363. let input = inputs[i].trim();
  364. if (input.regexStartsWith(/(CASE)([\s]|$)/)) {
  365. i = beautifyCaseBlock(inputs, result, settings, i, indent);
  366. continue;
  367. }
  368. result.push(new FormattedLine(input, indent));
  369. if (startIndex != 0
  370. && (input.regexStartsWith(regexBlockMidKeyWords))) {
  371. result[i].Indent--;
  372. }
  373. else if (startIndex != 0
  374. && (input.regexStartsWith(regexBlockEndsKeyWords))) {
  375. result[i].Indent--;
  376. return i;
  377. }
  378. if (input.regexStartsWith(regexOneLineBlockKeyWords)) {
  379. continue;
  380. }
  381. if (input.regexStartsWith(regexBlockStartsKeywords)) {
  382. i = beautify3(inputs, result, settings, i + 1, indent + 1);
  383. }
  384. }
  385. i--;
  386. return i;
  387. }
  388. exports.beautify3 = beautify3;
  389. function beautify2(input, settings) {
  390. let arr = input.split("\r\n");
  391. let quotes = EscapeQuotes(arr);
  392. if (settings.RemoveAsserts) {
  393. RemoveAsserts(arr); //RemoveAsserts must be after EscapeQuotes
  394. }
  395. ApplyNoNewLineAfter(arr, settings.NewLineSettings.noNewLineAfter);
  396. var align = [], align_max = [], align_i1 = 0, align_i = 0;
  397. var str = "", str1 = "";
  398. var p = 0;
  399. var n = 0, j = 0;
  400. var tab_n = 0, str_len = 0, port_s = "";
  401. var back_tab = false, forward_tab = false, semi_pos = 0, begin_b = true, port_b = false;
  402. var before_begin = true;
  403. var l = arr.length;
  404. for (i = 0; i < l; i++) {
  405. if (arr[i].indexOf("BEGIN") >= 0) {
  406. before_begin = false;
  407. }
  408. if (port_s) {
  409. port_s += arr[i];
  410. var k_port = port_s.split("(").length;
  411. if (k_port == port_s.split(")").length) {
  412. arr[i] = arr[i] + "@@end";
  413. port_s = "";
  414. port_b = false;
  415. }
  416. }
  417. if ((!port_b && arr[i].regexIndexOf(/(\s|\(|^)(PORT|GENERIC|PROCESS|PROCEDURE)(\s|\(|$)/) >= 0)
  418. || (arr[i].regexIndexOf(/:[ ]?=[ ]?\(/) >= 0 && before_begin)) {
  419. port_b = true;
  420. port_s = arr[i];
  421. var k_port = port_s.split("(").length;
  422. if (k_port == 1) {
  423. port_b = false;
  424. port_s = "";
  425. }
  426. else if (k_port == port_s.split(")").length) {
  427. port_s = "";
  428. port_b = false;
  429. arr[i] = arr[i] + "@@singleend";
  430. }
  431. else {
  432. arr[i] = arr[i].replace(/(PORT|GENERIC|PROCEDURE)([a-z0-9A-Z_ ]+)\(([a-zA-Z0-9_\(\) ]+)/, '$1$2(\r\n$3');
  433. }
  434. }
  435. }
  436. input = arr.join("\r\n");
  437. input = input.replace(/([a-zA-Z0-9\); ])\);(@@comments[0-9]+)?@@end/g, '$1\r\n);$2@@end');
  438. input = input.replace(/[ ]?([&=:\-<>\+|\*])[ ]?/g, ' $1 ');
  439. input = input.replace(/[ ]?([,])[ ]?/g, '$1 ');
  440. input = input.replace(/[ ]?(['"])(THEN)/g, '$1 $2');
  441. input = input.replace(/[ ]?(\?)?[ ]?(<|:|>|\/)?[ ]+(=)?[ ]?/g, ' $1$2$3 ');
  442. input = input.replace(/(IF)[ ]?([\(\)])/g, '$1 $2');
  443. input = input.replace(/([\(\)])[ ]?(THEN)/gi, '$1 $2');
  444. input = input.replace(/(^|[\(\)])[ ]?(AND|OR|XOR|XNOR)[ ]*([\(])/g, '$1 $2 $3');
  445. input = input.replace(/ ([\-\*\/=+<>])[ ]*([\-\*\/=+<>]) /g, " $1$2 ");
  446. input = input.replace(/\r\n[ \t]+--\r\n/g, "\r\n");
  447. input = input.replace(/[ ]+/g, ' ');
  448. input = input.replace(/\r\n\r\n\r\n/g, '\r\n');
  449. input = input.replace(/[\r\n\s]+$/g, '');
  450. input = input.replace(/[ \t]+\)/g, ')');
  451. var matches = input.match(/'([a-zA-Z]+)\s/g);
  452. if (matches != null) {
  453. for (var k2 = 0; k2 < matches.length; k2++) {
  454. input = input.replace(matches[k2], matches[k2].toUpperCase());
  455. }
  456. }
  457. input = input.replace(/(MAP)[ \r\n]+\(/g, '$1(');
  458. //input = input.replace(/(;|THEN)[ ]?(@@comments[0-9]+)([a-zA-Z])/g, '$1 $2\r\n$3');
  459. input = input.replace(/[\r\n ]+RETURN/g, ' RETURN');
  460. input = input.replace(/BEGIN[\r\n ]+/g, 'BEGIN\r\n');
  461. input = input.replace(/ (PORT|GENERIC) /g, '\r\n$1 ');
  462. if (settings.CheckAlias) {
  463. var alias = [], subarr = [], o = 0, p = 0, p2 = 0, l2 = 0, i2 = 0;
  464. arr = input.split("ARCHITECTURE ");
  465. l = arr.length;
  466. for (i = 0; i < l; i++) {
  467. subarr = arr[i].split("ALIAS ");
  468. l2 = subarr.length;
  469. if (l2 > 1) {
  470. o = 0;
  471. for (i2 = 1; i2 < l2; i2++) {
  472. o = subarr[i2].indexOf(";", n);
  473. str = subarr[i2].substring(0, o);
  474. alias[p2++] = str.split(" IS ");
  475. }
  476. i2--;
  477. var str2 = subarr[i2].substr(o);
  478. for (p = 0; p < p2; p++) {
  479. var reg = new RegExp(alias[p][1], 'gi');
  480. str2 = str2.replace(reg, alias[p][0]);
  481. }
  482. subarr[i2] = subarr[i2].substring(0, o) + str2;
  483. }
  484. arr[i] = subarr.join("ALIAS ");
  485. }
  486. input = arr.join("ARCHITECTURE ");
  487. }
  488. arr = input.split("\r\n");
  489. l = arr.length;
  490. var signAlignPos = "";
  491. var if_b = 0, white_space = "", case_b = false, case_n = 0, procfun_b = false, semi_b = false, set_false = false, entity_b = false, then_b = false, conditional_b = false, generic_map_b = false, architecture_begin_b = false, process_begin_b = false, case_indent = [0, 0, 0, 0, 0, 0, 0];
  492. var align_groups = [], align_groups_max = [], lastAlignedSign = "", current_align_group = 0, aligned_group_starts = 0;
  493. var indent_start = [];
  494. for (i = 0; i < l; i++) {
  495. str = arr[i];
  496. str_len = str.length;
  497. if (str.replace(/[ \-\t]*/, "").length > 0) {
  498. var first_word = str.split(/[^\w]/)[0];
  499. var indent_start_last = indent_start.length == 0 ? 0 : indent_start[indent_start.length - 1];
  500. if (then_b) {
  501. arr[i] = " " + arr[i];
  502. if (str.indexOf(" THEN") >= 0) {
  503. then_b = false;
  504. back_tab = true;
  505. }
  506. }
  507. arr[i] = white_space + arr[i];
  508. if (first_word == "ELSIF") {
  509. tab_n = indent_start_last - 1;
  510. indent_start.pop();
  511. back_tab = true;
  512. }
  513. else if (str.indexOf("END CASE") == 0) {
  514. indent_start.pop();
  515. case_n--;
  516. tab_n = indent_start[indent_start.length - 1];
  517. }
  518. else if (first_word == "END") {
  519. tab_n = indent_start_last - 1;
  520. indent_start.pop();
  521. if (str.indexOf("END IF") == 0) {
  522. if_b--;
  523. }
  524. if (i == l - 1) {
  525. tab_n = 1;
  526. }
  527. }
  528. else if (first_word == "ELSE" && if_b) {
  529. tab_n = indent_start_last - 1;
  530. indent_start.pop();
  531. back_tab = true;
  532. }
  533. else if (case_n) {
  534. if (first_word == "WHEN") {
  535. tab_n = case_indent[case_n - 1];
  536. //back_tab = true;
  537. }
  538. }
  539. else if (first_word == "BEGIN") {
  540. if (begin_b) {
  541. if (architecture_begin_b) {
  542. tab_n = indent_start_last - 1;
  543. architecture_begin_b = false;
  544. }
  545. else if (process_begin_b) {
  546. tab_n = indent_start_last - 1;
  547. process_begin_b = false;
  548. }
  549. else {
  550. tab_n = indent_start_last;
  551. indent_start.push(tab_n + 1);
  552. }
  553. //indent_start.pop();
  554. back_tab = true;
  555. begin_b = false;
  556. if (procfun_b) {
  557. tab_n++;
  558. indent_start.push(tab_n);
  559. begin_b = true;
  560. }
  561. }
  562. else {
  563. back_tab = true;
  564. }
  565. }
  566. else if (first_word == "PROCESS") {
  567. begin_b = true;
  568. }
  569. else if (str.indexOf(": PROCESS") >= 0) {
  570. back_tab = true;
  571. begin_b = true;
  572. process_begin_b = true;
  573. }
  574. else if (str.indexOf(": ENTITY") >= 0) {
  575. back_tab = true;
  576. entity_b = true;
  577. }
  578. else if (str.indexOf("PROCEDURE ") >= 0) {
  579. back_tab = true;
  580. begin_b = true;
  581. }
  582. if (port_b && str.indexOf("@@") < 0) {
  583. if (i + 1 <= arr.length - 1 && arr[i + 1].indexOf("@@") < 0) {
  584. if (signAlignPos == ":") {
  585. if (str.indexOf(';') < 0) {
  586. arr[i] += arr[i + 1];
  587. arr[i + 1] = '@@removeline';
  588. }
  589. }
  590. else if (signAlignPos == "=>") {
  591. if (str.indexOf(',') < 0) {
  592. arr[i] += arr[i + 1];
  593. arr[i + 1] = '@@removeline';
  594. }
  595. }
  596. }
  597. }
  598. if (str.indexOf("PORT MAP") >= 0) {
  599. back_tab = true;
  600. port_b = true;
  601. if (str.indexOf(");") < 0) {
  602. align_i1 = align_i;
  603. var t = str.indexOf("=>");
  604. if (t >= 0) {
  605. signAlignPos = "=>";
  606. }
  607. else {
  608. if (i + 1 < arr.length) {
  609. t = arr[i + 1].indexOf("=>");
  610. if (t >= 0) {
  611. signAlignPos = "=>";
  612. }
  613. }
  614. }
  615. }
  616. else {
  617. signAlignPos = "";
  618. }
  619. }
  620. else if (str.indexOf("GENERIC MAP") >= 0) {
  621. tab_n++;
  622. indent_start.push(tab_n);
  623. generic_map_b = true;
  624. if (!begin_b) {
  625. back_tab = false;
  626. }
  627. }
  628. else if (str.indexOf("PORT (") >= 0 && begin_b) {
  629. back_tab = true;
  630. port_b = true;
  631. t = str.indexOf(":");
  632. if (str.indexOf(");") < 0) {
  633. align_i1 = align_i;
  634. if (t >= 0) {
  635. signAlignPos = ":";
  636. }
  637. else {
  638. t = arr[i + 1].indexOf(":");
  639. if (t >= 0) {
  640. signAlignPos = ":";
  641. }
  642. }
  643. }
  644. else {
  645. signAlignPos = "";
  646. }
  647. }
  648. if (set_false) {
  649. procfun_b = false;
  650. set_false = false;
  651. }
  652. if (str.indexOf("(") >= 0) {
  653. if (str.indexOf("PROCEDURE") >= 0 || str.indexOf("FUNCTION") >= 0) {
  654. procfun_b = true;
  655. back_tab = true;
  656. }
  657. if ((str.indexOf("GENERIC") >= 0 || str.indexOf(":= (") >= 0 || str.regexIndexOf(/PROCEDURE[a-zA-Z0-9_ ]+\(/) >= 0) && begin_b) {
  658. port_b = true;
  659. back_tab = true;
  660. }
  661. }
  662. else if (first_word == "FUNCTION") {
  663. back_tab = true;
  664. begin_b = true;
  665. }
  666. if (str.indexOf("@@singleend") >= 0) {
  667. back_tab = false;
  668. port_b = false;
  669. if (!begin_b) {
  670. forward_tab = true;
  671. }
  672. }
  673. else if (str.indexOf("@@end") >= 0 && port_b) {
  674. port_b = false;
  675. indent_start.pop();
  676. tab_n = indent_start[indent_start.length - 1];
  677. if (entity_b) {
  678. forward_tab = true;
  679. }
  680. if (generic_map_b) {
  681. forward_tab = true;
  682. generic_map_b = false;
  683. }
  684. }
  685. if (settings.SignAlignAll) {
  686. var alignedSigns = [":", "<=", "=>"];
  687. for (var currentSign = 0; currentSign < alignedSigns.length; currentSign++) {
  688. if (str.indexOf(alignedSigns[currentSign]) > 0) {
  689. var char_before_sign = str.split(alignedSigns[currentSign])[0];
  690. var char_before_sign_length = char_before_sign.length;
  691. align_groups.push(char_before_sign_length);
  692. align_groups_max.push(char_before_sign_length);
  693. if (alignedSigns[currentSign] == lastAlignedSign) {
  694. if (align_groups_max[current_align_group - 1] < char_before_sign_length) {
  695. for (var k3 = aligned_group_starts; k3 <= current_align_group; k3++) {
  696. align_groups_max[k3] = char_before_sign_length;
  697. }
  698. }
  699. else {
  700. align_groups_max[current_align_group] = align_groups_max[current_align_group - 1];
  701. }
  702. }
  703. else {
  704. aligned_group_starts = current_align_group;
  705. }
  706. arr[i] = char_before_sign + "@@alignall" + (current_align_group++) + str.substring(char_before_sign.length, arr[i].length);
  707. lastAlignedSign = alignedSigns[currentSign];
  708. break;
  709. }
  710. }
  711. if (currentSign == alignedSigns.length) {
  712. lastAlignedSign = "";
  713. }
  714. }
  715. else if (settings.SignAlign) {
  716. if (port_b && signAlignPos != "") {
  717. if (str.indexOf(signAlignPos) >= 0) {
  718. var a1 = arr[i].split(signAlignPos);
  719. var l1 = a1[0].length;
  720. if (align_i >= 0 && align_i > align_i1) {
  721. align_max[align_i] = align_max[align_i - 1];
  722. }
  723. else {
  724. align_max[align_i] = l1;
  725. }
  726. if (align_i > align_i1 && align_max[align_i] < l1) {
  727. for (var k3 = align_i1; k3 <= align_i; k3++) {
  728. align_max[k3] = l1;
  729. }
  730. }
  731. align[align_i] = l1;
  732. arr[i] = a1[0] + "@@align" + (align_i++) + signAlignPos + arr[i].substring(l1 + signAlignPos.length, arr[i].length);
  733. }
  734. }
  735. }
  736. tab_n = tab_n < 1 ? 1 : tab_n;
  737. if (str_len) {
  738. if (isTesting) {
  739. console.log(tab_n, arr[i], indent_start);
  740. }
  741. arr[i] = (Array(tab_n).join(settings.Indentation)) + arr[i]; //indent
  742. if (settings.NewLineSettings.newLineAfter.indexOf("port")) {
  743. if (str.indexOf('@@singleend') < 0) {
  744. arr[i] = arr[i].replace(/(PORT)([ \r\n\w]*)\(/, "$1$2\r\n" + (Array(tab_n).join(settings.Indentation)) + "(");
  745. }
  746. }
  747. if (settings.NewLineSettings.newLineAfter.indexOf("generic")) {
  748. if (str.indexOf('@@singleend') < 0) {
  749. arr[i] = arr[i].replace(/(GENERIC)([ \r\n\w]*)\(/, "$1$2\r\n" + (Array(tab_n).join(settings.Indentation)) + "(");
  750. }
  751. }
  752. }
  753. if (back_tab) {
  754. tab_n++;
  755. indent_start.push(tab_n);
  756. back_tab = false;
  757. }
  758. if (forward_tab) {
  759. tab_n = indent_start_last;
  760. indent_start.pop();
  761. forward_tab = false;
  762. }
  763. if (conditional_b && str.indexOf(";") >= 0) {
  764. conditional_b = false;
  765. white_space = "";
  766. }
  767. else if (str.indexOf(";") >= 0 && semi_b) {
  768. semi_b = false;
  769. tab_n = indent_start_last;
  770. indent_start.pop();
  771. }
  772. else if (!semi_b && str.indexOf(";") < 0 && !port_b) {
  773. if (!conditional_b) {
  774. if (str.indexOf("WHEN") > 3 && str.indexOf("<=") > 1) {
  775. conditional_b = true;
  776. white_space = (Array(str.indexOf("= ") + 3).join(" "));
  777. }
  778. else if (first_word == "WHEN" && i + 1 < arr.length && arr[i + 1].indexOf("WHEN") < 0) {
  779. tab_n = indent_start_last + 1;
  780. }
  781. else if (str.indexOf("=>") < 0 && ((str.indexOf(ILQuotesPrefix) >= 0 && str.indexOf("= " + ILQuotesPrefix) < 0 && str.indexOf("IF") < 0) || (str.indexOf("<=") > 0 && str.indexOf("IF") < 0 && str.indexOf("THEN") < 0))) {
  782. tab_n++;
  783. indent_start.push(tab_n);
  784. semi_b = true;
  785. }
  786. }
  787. }
  788. if (first_word == "ENTITY") {
  789. tab_n++;
  790. indent_start.push(tab_n);
  791. }
  792. else if (",RECORD,PACKAGE,FOR,COMPONENT,CONFIGURATION,".indexOf("," + first_word + ",") >= 0) {
  793. tab_n++;
  794. indent_start.push(tab_n);
  795. }
  796. else if (str.indexOf(": FOR ") >= 0) {
  797. tab_n++;
  798. indent_start.push(tab_n);
  799. }
  800. else if (first_word == "CASE" || str.indexOf(": CASE") >= 0) {
  801. tab_n++;
  802. indent_start.push(tab_n);
  803. case_indent[case_n] = tab_n;
  804. case_n++;
  805. }
  806. else if (first_word == "ARCHITECTURE") {
  807. tab_n++;
  808. indent_start.push(tab_n);
  809. begin_b = true;
  810. architecture_begin_b = true;
  811. }
  812. else if (first_word == "IF") {
  813. if_b++;
  814. tab_n++;
  815. indent_start.push(tab_n);
  816. if (str.indexOf(" THEN") < 0) {
  817. then_b = true;
  818. tab_n = indent_start_last;
  819. //indent_start.pop();
  820. }
  821. }
  822. if (procfun_b) {
  823. if (str.regexIndexOf(/(\))|(RETURN [A-Za-z0-9 ]+)[\r\n ]+IS/) >= 0) {
  824. tab_n = indent_start_last;
  825. indent_start.pop();
  826. set_false = true;
  827. }
  828. }
  829. }
  830. }
  831. input = arr.join("\r\n");
  832. input = input.replace(/[\t]*@@removeline\r\n/g, '');
  833. p = input.indexOf('PROCESS');
  834. while (p >= 0) {
  835. let nextBracket = input.indexOf('(', p);
  836. let nextNewLine = input.indexOf('\r\n', p);
  837. let nextCloseBracket = input.indexOf(')', nextBracket);
  838. if (nextBracket < nextNewLine && nextCloseBracket > nextNewLine) {
  839. let processArray = input.substring(p, nextCloseBracket).split('\r\n');
  840. if (settings.Indentation.replace(/[ ]+/g, '').length == 0) {
  841. for (var i = 1; i < processArray.length; i++) {
  842. processArray[i] = (Array(nextBracket - p + 2).join(' ')) + processArray[i];
  843. }
  844. }
  845. else {
  846. for (var i = 1; i < processArray.length; i++) {
  847. processArray[i] = settings.Indentation + processArray[i];
  848. }
  849. }
  850. input = input.substring(0, p) + processArray.join('\r\n') + input.substring(nextCloseBracket, input.length);
  851. p = input.regexIndexOf('PROCESS[ ]+\\(', nextCloseBracket);
  852. }
  853. else {
  854. p = input.indexOf('PROCESS[ ]+\\(', p + 7);
  855. }
  856. }
  857. input = SetKeywordCase(input, settings.KeywordCase, KeyWords, TypeNames);
  858. if (settings.SignAlignAll) {
  859. for (var k = 0; k < current_align_group; k++) {
  860. input = input.replace("@@alignall" + k, Array((align_groups_max[k] - align_groups[k] + 1)).join(" "));
  861. }
  862. }
  863. if (settings.SignAlign) {
  864. for (var k = 0; k < align_i; k++) {
  865. input = input.replace("@@align" + k, Array((align_max[k] - align[k] + 2)).join(" "));
  866. }
  867. }
  868. for (var k = 0; k < quotes.length; k++) {
  869. input = input.replace(ILQuotesPrefix + k, quotes[k]);
  870. }
  871. input = input.replace(/@@singleline[ \r\n]*/, " ");
  872. return input;
  873. }
  874. function ReserveSemicolonInKeywords(arr) {
  875. for (let i = 0; i < arr.length; i++) {
  876. if (arr[i].match(/FUNCTION|PROCEDURE/) != null) {
  877. arr[i] = arr[i].replace(/;/g, '@@semicolon');
  878. }
  879. }
  880. }
  881. function ApplyNoNewLineAfter(arr, noNewLineAfter) {
  882. if (noNewLineAfter == null) {
  883. return;
  884. }
  885. for (let i = 0; i < arr.length; i++) {
  886. noNewLineAfter.forEach(n => {
  887. let regex = new RegExp("(" + n.toUpperCase + ")[ a-z0-9]+[a-z0-9]+");
  888. if (arr[i].regexIndexOf(regex) >= 0) {
  889. arr[i] += "@@singleline";
  890. }
  891. });
  892. }
  893. }
  894. exports.ApplyNoNewLineAfter = ApplyNoNewLineAfter;
  895. function RemoveAsserts(arr) {
  896. let need_semi = false;
  897. let inAssert = false;
  898. let n = 0;
  899. for (let i = 0; i < arr.length; i++) {
  900. let has_semi = arr[i].indexOf(";") >= 0;
  901. if (need_semi) {
  902. arr[i] = '';
  903. }
  904. n = arr[i].indexOf("ASSERT ");
  905. if (n >= 0) {
  906. inAssert = true;
  907. arr[i] = '';
  908. }
  909. if (!has_semi) {
  910. if (inAssert) {
  911. need_semi = true;
  912. }
  913. }
  914. else {
  915. need_semi = false;
  916. }
  917. }
  918. }
  919. exports.RemoveAsserts = RemoveAsserts;
  920. function EscapeQuotes(arr) {
  921. let quotes = [];
  922. let quotesIndex = 0;
  923. for (let i = 0; i < arr.length; i++) {
  924. let quote = arr[i].match(/"([^"]+)"/g);
  925. if (quote != null) {
  926. for (var j = 0; j < quote.length; j++) {
  927. arr[i] = arr[i].replace(quote[j], ILQuotesPrefix + quotesIndex);
  928. quotes[quotesIndex++] = quote[j];
  929. }
  930. }
  931. }
  932. return quotes;
  933. }
  934. function RemoveExtraNewLines(input) {
  935. input = input.replace(/(?:\r\n|\r|\n)/g, '\r\n');
  936. input = input.replace(/ \r\n/g, '\r\n');
  937. input = input.replace(/\r\n\r\n\r\n/g, '\r\n');
  938. return input;
  939. }
  940. //# sourceMappingURL=VHDLFormatter.js.map