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.

962 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 ignoreFirstKeyWords: Array<string> = ["WHEN"];
  324. let blockMidKeyWords: Array<string> = ["ELSE", "ELSIF"].concat(ignoreFirstKeyWords);
  325. let blockStartsKeyWords: Array<string> = ["IF", "CASE"];
  326. let blockEndsKeyWords: Array<string> = ["END"];
  327. let ignoreFirstKeyWordsStr: string = ignoreFirstKeyWords.join("|");
  328. let newLineAfterKeyWordsStr: string = blockStartsKeyWords.join("|");
  329. let blockEndKeyWordsStr: string = blockEndsKeyWords.join("|");
  330. let blockMidKeyWordsStr: string = blockMidKeyWords.join("|");
  331. let regexBlockMidKeyWords: RegExp = new RegExp("(" + blockMidKeyWordsStr + ")([\\s]|$)")
  332. let regexIgnoreFirstKeyWords: RegExp = new RegExp("(" + ignoreFirstKeyWordsStr + ")([\\s]|$)")
  333. let regexBlockStartsKeywords: RegExp = new RegExp("(" + newLineAfterKeyWordsStr + ")([\\s]|$)")
  334. let regexBlockEndsKeyWords: RegExp = new RegExp("(" + blockEndKeyWordsStr + ")([\\s]|$)")
  335. for (i = startIndex; i < inputs.length; i++) {
  336. let input: string = inputs[i];
  337. if (input.regexStartsWith(/(CASE)([\s]|$)/)) {
  338. i = beautifyCaseBlock(inputs, result, settings, i, indent);
  339. continue;
  340. }
  341. result.push(new FormattedLine(input, indent));
  342. if (startIndex != 0
  343. && (input.regexStartsWith(regexBlockMidKeyWords))) {
  344. (<FormattedLine>result[i]).Indent--;
  345. }
  346. else if (startIndex != 0
  347. && (input.regexStartsWith(regexBlockEndsKeyWords))) {
  348. (<FormattedLine>result[i]).Indent--;
  349. return i;
  350. }
  351. if (input.regexStartsWith(regexBlockStartsKeywords)) {
  352. i = beautify3(inputs, result, settings, i + 1, indent + 1);
  353. }
  354. }
  355. i--;
  356. return i;
  357. }
  358. function beautify2(input, settings: BeautifierSettings): string {
  359. let arr = input.split("\r\n");
  360. let quotes = EscapeQuotes(arr);
  361. if (settings.RemoveAsserts) {
  362. RemoveAsserts(arr);//RemoveAsserts must be after EscapeQuotes
  363. }
  364. ApplyNoNewLineAfter(arr, settings.NewLineSettings.noNewLineAfter);
  365. var align = [],
  366. align_max = [],
  367. align_i1 = 0,
  368. align_i = 0;
  369. var str = "",
  370. str1 = "";
  371. var p = 0;
  372. var n = 0,
  373. j = 0;
  374. var tab_n = 0,
  375. str_len = 0,
  376. port_s = "";
  377. var back_tab = false,
  378. forward_tab = false,
  379. semi_pos = 0,
  380. begin_b = true,
  381. port_b = false;
  382. var before_begin = true;
  383. var l = arr.length;
  384. for (i = 0; i < l; i++) {
  385. if (arr[i].indexOf("BEGIN") >= 0) {
  386. before_begin = false;
  387. }
  388. if (port_s) {
  389. port_s += arr[i];
  390. var k_port = port_s.split("(").length;
  391. if (k_port == port_s.split(")").length) {
  392. arr[i] = arr[i] + "@@end";
  393. port_s = "";
  394. port_b = false;
  395. }
  396. }
  397. if ((!port_b && arr[i].regexIndexOf(/(\s|\(|^)(PORT|GENERIC|PROCESS|PROCEDURE)(\s|\(|$)/) >= 0)
  398. || (arr[i].regexIndexOf(/:[ ]?=[ ]?\(/) >= 0 && before_begin)) {
  399. port_b = true;
  400. port_s = arr[i];
  401. var k_port = port_s.split("(").length;
  402. if (k_port == 1) {
  403. port_b = false;
  404. port_s = "";
  405. } else if (k_port == port_s.split(")").length) {
  406. port_s = "";
  407. port_b = false;
  408. arr[i] = arr[i] + "@@singleend";
  409. } else {
  410. arr[i] = arr[i].replace(/(PORT|GENERIC|PROCEDURE)([a-z0-9A-Z_ ]+)\(([a-zA-Z0-9_\(\) ]+)/, '$1$2(\r\n$3');
  411. }
  412. }
  413. }
  414. input = arr.join("\r\n");
  415. input = input.replace(/([a-zA-Z0-9\); ])\);(@@comments[0-9]+)?@@end/g, '$1\r\n);$2@@end');
  416. input = input.replace(/[ ]?([&=:\-<>\+|\*])[ ]?/g, ' $1 ');
  417. input = input.replace(/[ ]?([,])[ ]?/g, '$1 ');
  418. input = input.replace(/[ ]?(['"])(THEN)/g, '$1 $2');
  419. input = input.replace(/[ ]?(\?)?[ ]?(<|:|>|\/)?[ ]+(=)?[ ]?/g, ' $1$2$3 ');
  420. input = input.replace(/(IF)[ ]?([\(\)])/g, '$1 $2');
  421. input = input.replace(/([\(\)])[ ]?(THEN)/gi, '$1 $2');
  422. input = input.replace(/(^|[\(\)])[ ]?(AND|OR|XOR|XNOR)[ ]*([\(])/g, '$1 $2 $3');
  423. input = input.replace(/ ([\-\*\/=+<>])[ ]*([\-\*\/=+<>]) /g, " $1$2 ");
  424. input = input.replace(/\r\n[ \t]+--\r\n/g, "\r\n");
  425. input = input.replace(/[ ]+/g, ' ');
  426. input = input.replace(/\r\n\r\n\r\n/g, '\r\n');
  427. input = input.replace(/[\r\n\s]+$/g, '');
  428. input = input.replace(/[ \t]+\)/g, ')');
  429. var matches = input.match(/'([a-zA-Z]+)\s/g);
  430. if (matches != null) {
  431. for (var k2 = 0; k2 < matches.length; k2++) {
  432. input = input.replace(matches[k2], matches[k2].toUpperCase());
  433. }
  434. }
  435. input = input.replace(/(MAP)[ \r\n]+\(/g, '$1(');
  436. //input = input.replace(/(;|THEN)[ ]?(@@comments[0-9]+)([a-zA-Z])/g, '$1 $2\r\n$3');
  437. input = input.replace(/[\r\n ]+RETURN/g, ' RETURN');
  438. input = input.replace(/BEGIN[\r\n ]+/g, 'BEGIN\r\n');
  439. input = input.replace(/ (PORT|GENERIC) /g, '\r\n$1 ');
  440. if (settings.CheckAlias) {
  441. var alias = [],
  442. subarr = [],
  443. o = 0,
  444. p = 0,
  445. p2 = 0,
  446. l2 = 0,
  447. i2 = 0;
  448. arr = input.split("ARCHITECTURE ");
  449. l = arr.length;
  450. for (i = 0; i < l; i++) {
  451. subarr = arr[i].split("ALIAS ");
  452. l2 = subarr.length;
  453. if (l2 > 1) {
  454. o = 0;
  455. for (i2 = 1; i2 < l2; i2++) {
  456. o = subarr[i2].indexOf(";", n);
  457. str = subarr[i2].substring(0, o);
  458. alias[p2++] = str.split(" IS ");
  459. }
  460. i2--;
  461. var str2 = subarr[i2].substr(o);
  462. for (p = 0; p < p2; p++) {
  463. var reg = new RegExp(alias[p][1], 'gi');
  464. str2 = str2.replace(reg, alias[p][0]);
  465. }
  466. subarr[i2] = subarr[i2].substring(0, o) + str2;
  467. }
  468. arr[i] = subarr.join("ALIAS ");
  469. }
  470. input = arr.join("ARCHITECTURE ");
  471. }
  472. arr = input.split("\r\n");
  473. l = arr.length;
  474. var signAlignPos = "";
  475. var if_b = 0,
  476. white_space = "",
  477. case_b = false,
  478. case_n = 0,
  479. procfun_b = false,
  480. semi_b = false,
  481. set_false = false,
  482. entity_b = false,
  483. then_b = false,
  484. conditional_b = false,
  485. generic_map_b = false,
  486. architecture_begin_b = false,
  487. process_begin_b = false,
  488. case_indent = [0, 0, 0, 0, 0, 0, 0];
  489. var align_groups = [],
  490. align_groups_max = [],
  491. lastAlignedSign = "",
  492. current_align_group = 0,
  493. aligned_group_starts = 0;
  494. var indent_start = [];
  495. for (i = 0; i < l; i++) {
  496. str = arr[i];
  497. str_len = str.length;
  498. if (str.replace(/[ \-\t]*/, "").length > 0) {
  499. var first_word = str.split(/[^\w]/)[0];
  500. var indent_start_last = indent_start.length == 0 ? 0 : indent_start[indent_start.length - 1];
  501. if (then_b) {
  502. arr[i] = " " + arr[i];
  503. if (str.indexOf(" THEN") >= 0) {
  504. then_b = false;
  505. back_tab = true;
  506. }
  507. }
  508. arr[i] = white_space + arr[i];
  509. if (first_word == "ELSIF") {
  510. tab_n = indent_start_last - 1;
  511. indent_start.pop();
  512. back_tab = true;
  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. } else if (first_word == "END") {
  518. tab_n = indent_start_last - 1;
  519. indent_start.pop();
  520. if (str.indexOf("END IF") == 0) {
  521. if_b--;
  522. }
  523. if (i == l - 1) {
  524. tab_n = 1;
  525. }
  526. } else if (first_word == "ELSE" && if_b) {
  527. tab_n = indent_start_last - 1;
  528. indent_start.pop();
  529. back_tab = true;
  530. } else if (case_n) {
  531. if (first_word == "WHEN") {
  532. tab_n = case_indent[case_n - 1];
  533. //back_tab = true;
  534. }
  535. } else if (first_word == "BEGIN") {
  536. if (begin_b) {
  537. if (architecture_begin_b) {
  538. tab_n = indent_start_last - 1;
  539. architecture_begin_b = false;
  540. } else if (process_begin_b) {
  541. tab_n = indent_start_last - 1;
  542. process_begin_b = false;
  543. } else {
  544. tab_n = indent_start_last;
  545. indent_start.push(tab_n + 1);
  546. }
  547. //indent_start.pop();
  548. back_tab = true;
  549. begin_b = false;
  550. if (procfun_b) {
  551. tab_n++;
  552. indent_start.push(tab_n);
  553. begin_b = true;
  554. }
  555. } else {
  556. back_tab = true;
  557. }
  558. } else if (first_word == "PROCESS") {
  559. begin_b = true;
  560. } else if (str.indexOf(": PROCESS") >= 0) {
  561. back_tab = true;
  562. begin_b = true;
  563. process_begin_b = true;
  564. } else if (str.indexOf(": ENTITY") >= 0) {
  565. back_tab = true;
  566. entity_b = true;
  567. } else if (str.indexOf("PROCEDURE ") >= 0) {
  568. back_tab = true;
  569. begin_b = true;
  570. }
  571. if (port_b && str.indexOf("@@") < 0) {
  572. if (i + 1 <= arr.length - 1 && arr[i + 1].indexOf("@@") < 0) {
  573. if (signAlignPos == ":") {
  574. if (str.indexOf(';') < 0) {
  575. arr[i] += arr[i + 1];
  576. arr[i + 1] = '@@removeline';
  577. }
  578. } else if (signAlignPos == "=>") {
  579. if (str.indexOf(',') < 0) {
  580. arr[i] += arr[i + 1];
  581. arr[i + 1] = '@@removeline';
  582. }
  583. }
  584. }
  585. }
  586. if (str.indexOf("PORT MAP") >= 0) {
  587. back_tab = true;
  588. port_b = true;
  589. if (str.indexOf(");") < 0) {
  590. align_i1 = align_i;
  591. var t = str.indexOf("=>");
  592. if (t >= 0) {
  593. signAlignPos = "=>";
  594. } else {
  595. if (i + 1 < arr.length) {
  596. t = arr[i + 1].indexOf("=>");
  597. if (t >= 0) {
  598. signAlignPos = "=>";
  599. }
  600. }
  601. }
  602. } else {
  603. signAlignPos = "";
  604. }
  605. } else if (str.indexOf("GENERIC MAP") >= 0) {
  606. tab_n++;
  607. indent_start.push(tab_n);
  608. generic_map_b = true;
  609. if (!begin_b) {
  610. back_tab = false;
  611. }
  612. } else if (str.indexOf("PORT (") >= 0 && begin_b) {
  613. back_tab = true;
  614. port_b = true;
  615. t = str.indexOf(":");
  616. if (str.indexOf(");") < 0) {
  617. align_i1 = align_i;
  618. if (t >= 0) {
  619. signAlignPos = ":";
  620. } else {
  621. t = arr[i + 1].indexOf(":");
  622. if (t >= 0) {
  623. signAlignPos = ":";
  624. }
  625. }
  626. } else {
  627. signAlignPos = "";
  628. }
  629. }
  630. if (set_false) {
  631. procfun_b = false;
  632. set_false = false;
  633. }
  634. if (str.indexOf("(") >= 0) {
  635. if (str.indexOf("PROCEDURE") >= 0 || str.indexOf("FUNCTION") >= 0) {
  636. procfun_b = true;
  637. back_tab = true;
  638. }
  639. if ((str.indexOf("GENERIC") >= 0 || str.indexOf(":= (") >= 0 || str.regexIndexOf(/PROCEDURE[a-zA-Z0-9_ ]+\(/) >= 0) && begin_b) {
  640. port_b = true;
  641. back_tab = true;
  642. }
  643. } else if (first_word == "FUNCTION") {
  644. back_tab = true;
  645. begin_b = true;
  646. }
  647. if (str.indexOf("@@singleend") >= 0) {
  648. back_tab = false;
  649. port_b = false;
  650. if (!begin_b) {
  651. forward_tab = true;
  652. }
  653. } else if (str.indexOf("@@end") >= 0 && port_b) {
  654. port_b = false;
  655. indent_start.pop();
  656. tab_n = indent_start[indent_start.length - 1];
  657. if (entity_b) {
  658. forward_tab = true;
  659. }
  660. if (generic_map_b) {
  661. forward_tab = true;
  662. generic_map_b = false;
  663. }
  664. }
  665. if (settings.SignAlignAll) {
  666. var alignedSigns = [":", "<=", "=>"];
  667. for (var currentSign = 0; currentSign < alignedSigns.length; currentSign++) {
  668. if (str.indexOf(alignedSigns[currentSign]) > 0) {
  669. var char_before_sign = str.split(alignedSigns[currentSign])[0];
  670. var char_before_sign_length = char_before_sign.length;
  671. align_groups.push(char_before_sign_length);
  672. align_groups_max.push(char_before_sign_length);
  673. if (alignedSigns[currentSign] == lastAlignedSign) {
  674. if (align_groups_max[current_align_group - 1] < char_before_sign_length) {
  675. for (var k3 = aligned_group_starts; k3 <= current_align_group; k3++) {
  676. align_groups_max[k3] = char_before_sign_length;
  677. }
  678. } else {
  679. align_groups_max[current_align_group] = align_groups_max[current_align_group - 1];
  680. }
  681. } else {
  682. aligned_group_starts = current_align_group;
  683. }
  684. arr[i] = char_before_sign + "@@alignall" + (current_align_group++) + str.substring(char_before_sign.length, arr[i].length);
  685. lastAlignedSign = alignedSigns[currentSign];
  686. break;
  687. }
  688. }
  689. if (currentSign == alignedSigns.length) {
  690. lastAlignedSign = "";
  691. }
  692. } else if (settings.SignAlign) {
  693. if (port_b && signAlignPos != "") {
  694. if (str.indexOf(signAlignPos) >= 0) {
  695. var a1 = arr[i].split(signAlignPos);
  696. var l1 = a1[0].length;
  697. if (align_i >= 0 && align_i > align_i1) {
  698. align_max[align_i] = align_max[align_i - 1];
  699. } else {
  700. align_max[align_i] = l1;
  701. }
  702. if (align_i > align_i1 && align_max[align_i] < l1) {
  703. for (var k3 = align_i1; k3 <= align_i; k3++) {
  704. align_max[k3] = l1;
  705. }
  706. }
  707. align[align_i] = l1;
  708. arr[i] = a1[0] + "@@align" + (align_i++) + signAlignPos + arr[i].substring(l1 + signAlignPos.length, arr[i].length);
  709. }
  710. }
  711. }
  712. tab_n = tab_n < 1 ? 1 : tab_n;
  713. if (str_len) {
  714. if (isTesting) {
  715. console.log(tab_n, arr[i], indent_start);
  716. }
  717. arr[i] = (Array(tab_n).join(settings.Indentation)) + arr[i]; //indent
  718. if (settings.NewLineSettings.newLineAfter.indexOf("port")) {
  719. if (str.indexOf('@@singleend') < 0) {
  720. arr[i] = arr[i].replace(/(PORT)([ \r\n\w]*)\(/, "$1$2\r\n" + (Array(tab_n).join(settings.Indentation)) + "(");
  721. }
  722. }
  723. if (settings.NewLineSettings.newLineAfter.indexOf("generic")) {
  724. if (str.indexOf('@@singleend') < 0) {
  725. arr[i] = arr[i].replace(/(GENERIC)([ \r\n\w]*)\(/, "$1$2\r\n" + (Array(tab_n).join(settings.Indentation)) + "(");
  726. }
  727. }
  728. }
  729. if (back_tab) {
  730. tab_n++;
  731. indent_start.push(tab_n);
  732. back_tab = false;
  733. }
  734. if (forward_tab) {
  735. tab_n = indent_start_last;
  736. indent_start.pop();
  737. forward_tab = false;
  738. }
  739. if (conditional_b && str.indexOf(";") >= 0) {
  740. conditional_b = false;
  741. white_space = "";
  742. } else if (str.indexOf(";") >= 0 && semi_b) {
  743. semi_b = false;
  744. tab_n = indent_start_last;
  745. indent_start.pop();
  746. } else if (!semi_b && str.indexOf(";") < 0 && !port_b) {
  747. if (!conditional_b) {
  748. if (str.indexOf("WHEN") > 3 && str.indexOf("<=") > 1) {
  749. conditional_b = true;
  750. white_space = (Array(str.indexOf("= ") + 3).join(" "));
  751. } else if (first_word == "WHEN" && i + 1 < arr.length && arr[i + 1].indexOf("WHEN") < 0) {
  752. tab_n = indent_start_last + 1;
  753. } 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))) {
  754. tab_n++;
  755. indent_start.push(tab_n);
  756. semi_b = true;
  757. }
  758. }
  759. }
  760. if (first_word == "ENTITY") {
  761. tab_n++;
  762. indent_start.push(tab_n);
  763. } else if (",RECORD,PACKAGE,FOR,COMPONENT,CONFIGURATION,".indexOf("," + first_word + ",") >= 0) {
  764. tab_n++;
  765. indent_start.push(tab_n);
  766. } else if (str.indexOf(": FOR ") >= 0) {
  767. tab_n++;
  768. indent_start.push(tab_n);
  769. } else if (first_word == "CASE" || str.indexOf(": CASE") >= 0) {
  770. tab_n++;
  771. indent_start.push(tab_n);
  772. case_indent[case_n] = tab_n;
  773. case_n++;
  774. } else if (first_word == "ARCHITECTURE") {
  775. tab_n++;
  776. indent_start.push(tab_n);
  777. begin_b = true;
  778. architecture_begin_b = true;
  779. } else if (first_word == "IF") {
  780. if_b++;
  781. tab_n++;
  782. indent_start.push(tab_n);
  783. if (str.indexOf(" THEN") < 0) {
  784. then_b = true;
  785. tab_n = indent_start_last;
  786. //indent_start.pop();
  787. }
  788. }
  789. if (procfun_b) {
  790. if (str.regexIndexOf(/(\))|(RETURN [A-Za-z0-9 ]+)[\r\n ]+IS/) >= 0) {
  791. tab_n = indent_start_last;
  792. indent_start.pop();
  793. set_false = true;
  794. }
  795. }
  796. }
  797. }
  798. input = arr.join("\r\n");
  799. input = input.replace(/[\t]*@@removeline\r\n/g, '');
  800. p = input.indexOf('PROCESS');
  801. while (p >= 0) {
  802. let nextBracket = input.indexOf('(', p);
  803. let nextNewLine = input.indexOf('\r\n', p);
  804. let nextCloseBracket = input.indexOf(')', nextBracket);
  805. if (nextBracket < nextNewLine && nextCloseBracket > nextNewLine) {
  806. let processArray = input.substring(p, nextCloseBracket).split('\r\n');
  807. if (settings.Indentation.replace(/[ ]+/g, '').length == 0) {
  808. for (var i = 1; i < processArray.length; i++) {
  809. processArray[i] = (Array(nextBracket - p + 2).join(' ')) + processArray[i];
  810. }
  811. } else {
  812. for (var i = 1; i < processArray.length; i++) {
  813. processArray[i] = settings.Indentation + processArray[i];
  814. }
  815. }
  816. input = input.substring(0, p) + processArray.join('\r\n') + input.substring(nextCloseBracket, input.length);
  817. p = input.regexIndexOf('PROCESS[ ]+\\(', nextCloseBracket);
  818. } else {
  819. p = input.indexOf('PROCESS[ ]+\\(', p + 7);
  820. }
  821. }
  822. input = SetKeywordCase(input, settings.KeywordCase, KeyWords, TypeNames);
  823. if (settings.SignAlignAll) {
  824. for (var k = 0; k < current_align_group; k++) {
  825. input = input.replace("@@alignall" + k, Array((align_groups_max[k] - align_groups[k] + 1)).join(" "));
  826. }
  827. }
  828. if (settings.SignAlign) {
  829. for (var k = 0; k < align_i; k++) {
  830. input = input.replace("@@align" + k, Array((align_max[k] - align[k] + 2)).join(" "));
  831. }
  832. }
  833. for (var k = 0; k < quotes.length; k++) {
  834. input = input.replace(ILQuotesPrefix + k, quotes[k]);
  835. }
  836. input = input.replace(/@@singleline[ \r\n]*/, " ");
  837. return input;
  838. }
  839. function ReserveSemicolonInKeywords(arr: Array<string>) {
  840. for (let i = 0; i < arr.length; i++) {
  841. if (arr[i].match(/FUNCTION|PROCEDURE/) != null) {
  842. arr[i] = arr[i].replace(/;/g, '@@semicolon');
  843. }
  844. }
  845. }
  846. export function ApplyNoNewLineAfter(arr: Array<string>, noNewLineAfter: Array<string>) {
  847. if (noNewLineAfter == null) {
  848. return;
  849. }
  850. for (let i = 0; i < arr.length; i++) {
  851. noNewLineAfter.forEach(n => {
  852. let regex = new RegExp("(" + n.toUpperCase + ")[ a-z0-9]+[a-z0-9]+");
  853. if (arr[i].regexIndexOf(regex) >= 0) {
  854. arr[i] += "@@singleline";
  855. }
  856. });
  857. }
  858. }
  859. export function RemoveAsserts(arr: Array<string>) {
  860. let need_semi: boolean = false;
  861. let inAssert: boolean = false;
  862. let n: number = 0;
  863. for (let i = 0; i < arr.length; i++) {
  864. let has_semi: boolean = arr[i].indexOf(";") >= 0;
  865. if (need_semi) {
  866. arr[i] = '';
  867. }
  868. n = arr[i].indexOf("ASSERT ");
  869. if (n >= 0) {
  870. inAssert = true;
  871. arr[i] = '';
  872. }
  873. if (!has_semi) {
  874. if (inAssert) {
  875. need_semi = true;
  876. }
  877. }
  878. else {
  879. need_semi = false;
  880. }
  881. }
  882. }
  883. function EscapeQuotes(arr: Array<string>): Array<string> {
  884. let quotes: Array<string> = [];
  885. let quotesIndex = 0;
  886. for (let i = 0; i < arr.length; i++) {
  887. let quote = arr[i].match(/"([^"]+)"/g);
  888. if (quote != null) {
  889. for (var j = 0; j < quote.length; j++) {
  890. arr[i] = arr[i].replace(quote[j], ILQuotesPrefix + quotesIndex);
  891. quotes[quotesIndex++] = quote[j];
  892. }
  893. }
  894. }
  895. return quotes;
  896. }
  897. function RemoveExtraNewLines(input: any) {
  898. input = input.replace(/(?:\r\n|\r|\n)/g, '\r\n');
  899. input = input.replace(/ \r\n/g, '\r\n');
  900. input = input.replace(/\r\n\r\n\r\n/g, '\r\n');
  901. return input;
  902. }