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.

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