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.

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