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.

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