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.

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