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.

996 lines
37 KiB

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