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.

1136 lines
43 KiB

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