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.

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