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.

1140 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
  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", "\n");
  263. input = input.replace("\n", "\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. SignAlignRegional(result, blockBodyStartIndex, blockBodyEndIndex);
  416. }
  417. return i;
  418. }
  419. export function SignAlignRegional(result: (FormattedLine | FormattedLine[])[], startIndex: number, endIndex: number) {
  420. let maxSymbolIndex = {};
  421. let allSymbolIndex = {};
  422. for (let i = startIndex; i <= endIndex; i++) {
  423. let line = (<FormattedLine>result[i]).Line;
  424. SetSymbolIndices(line, ":", maxSymbolIndex, allSymbolIndex, i);
  425. SetSymbolIndices(line, ":=", maxSymbolIndex, allSymbolIndex, i);
  426. SetSymbolIndices(line, "=>", maxSymbolIndex, allSymbolIndex, i);
  427. }
  428. for (let key in maxSymbolIndex) {
  429. let maxIndex = maxSymbolIndex[key];
  430. for (let lineIndex in allSymbolIndex[key]) {
  431. let symbolIndex = allSymbolIndex[key][lineIndex];
  432. if (symbolIndex == maxIndex) {
  433. continue;
  434. }
  435. let line = (<FormattedLine>result[lineIndex]).Line;
  436. (<FormattedLine>result[lineIndex]).Line = line.substring(0, symbolIndex)
  437. + (Array(maxIndex - symbolIndex + 1).join(" "))
  438. + line.substring(symbolIndex);
  439. }
  440. }
  441. }
  442. function SetSymbolIndices(line: string, symbol: string, maxSymbolIndex: {}, allSymbolIndex: {}, index: number) {
  443. let regex: RegExp = new RegExp("([\\s\\w]|^)" + symbol + "([\\s\\w]|$)");
  444. let colonIndex = line.regexIndexOf(regex);
  445. if (colonIndex > 0) {
  446. if (maxSymbolIndex.hasOwnProperty(symbol)) {
  447. maxSymbolIndex[symbol] = Math.max(maxSymbolIndex[symbol], colonIndex);
  448. }
  449. else {
  450. maxSymbolIndex[symbol] = colonIndex;
  451. allSymbolIndex[symbol] = {};
  452. }
  453. allSymbolIndex[symbol][index] = colonIndex;
  454. }
  455. }
  456. export function beautifyCaseBlock(inputs: Array<string>, result: (FormattedLine | FormattedLine[])[], settings: BeautifierSettings, startIndex: number, indent: number): number {
  457. if (!inputs[startIndex].regexStartsWith(/(.+:\s*)?(CASE)([\s]|$)/)) {
  458. return startIndex;
  459. }
  460. result.push(new FormattedLine(inputs[startIndex], indent));
  461. let i = beautify3(inputs, result, settings, startIndex + 1, indent + 2);
  462. (<FormattedLine>result[i]).Indent = indent;
  463. return i;
  464. }
  465. export function beautify3(inputs: Array<string>, result: (FormattedLine | FormattedLine[])[], settings: BeautifierSettings, startIndex: number, indent: number, endIndex?: number): number {
  466. let i: number;
  467. let regexOneLineBlockKeyWords: RegExp = new RegExp(/(PROCEDURE|FUNCTION|IMPURE FUNCTION)[^\w](?!.+[^\w]IS([^\w]|$))/);//match PROCEDURE..; but not PROCEDURE .. IS;
  468. let blockMidKeyWords: Array<string> = ["ELSE", "ELSIF", "WHEN", "BEGIN"];
  469. let blockStartsKeyWords: Array<string> = [
  470. "IF",
  471. "CASE",
  472. "ARCHITECTURE",
  473. "PROCEDURE",
  474. "PACKAGE",
  475. "PROCESS",
  476. "POSTPONED PROCESS",
  477. "([\\w\\s]+:\\s*PROCESS)",
  478. "FUNCTION",
  479. "IMPURE FUNCTION",
  480. "(.+\\sPROTECTED)",
  481. "COMPONENT",
  482. "ENTITY"];
  483. let blockEndsKeyWords: Array<string> = ["END"];
  484. let newLineAfterKeyWordsStr: string = blockStartsKeyWords.join("|");
  485. let blockEndKeyWordsStr: string = blockEndsKeyWords.join("|");
  486. let blockMidKeyWordsStr: string = blockMidKeyWords.join("|");
  487. let regexBlockMidKeyWords: RegExp = new RegExp("(" + blockMidKeyWordsStr + ")([^\\w]|$)")
  488. let regexBlockStartsKeywords: RegExp = new RegExp("(" + newLineAfterKeyWordsStr + ")([^\\w]|$)")
  489. let regexBlockEndsKeyWords: RegExp = new RegExp("(" + blockEndKeyWordsStr + ")([^\\w]|$)")
  490. if (endIndex == null) {
  491. endIndex = inputs.length - 1;
  492. }
  493. for (i = startIndex; i <= endIndex; i++) {
  494. let input: string = inputs[i].trim();
  495. if (input.regexStartsWith(/(.+:\s*)?(CASE)([\s]|$)/)) {
  496. i = beautifyCaseBlock(inputs, result, settings, i, indent);
  497. continue;
  498. }
  499. if (input.regexStartsWith(/[\w\s:]*PORT([\s]|$)/)) {
  500. i = beautifyPortGenericBlock(inputs, result, settings, i, indent, "PORT");
  501. continue;
  502. }
  503. if (input.regexStartsWith(/[\w\s:]*GENERIC([\s]|$)/)) {
  504. i = beautifyPortGenericBlock(inputs, result, settings, i, indent, "GENERIC");
  505. continue;
  506. }
  507. result.push(new FormattedLine(input, indent));
  508. if (startIndex != 0
  509. && (input.regexStartsWith(regexBlockMidKeyWords))) {
  510. (<FormattedLine>result[i]).Indent--;
  511. }
  512. else if (startIndex != 0
  513. && (input.regexStartsWith(regexBlockEndsKeyWords))) {
  514. (<FormattedLine>result[i]).Indent--;
  515. return i;
  516. }
  517. if (input.regexStartsWith(regexOneLineBlockKeyWords)) {
  518. continue;
  519. }
  520. if (input.regexStartsWith(regexBlockStartsKeywords)) {
  521. i = beautify3(inputs, result, settings, i + 1, indent + 1);
  522. }
  523. }
  524. i--;
  525. return i;
  526. }
  527. function beautify2(input, settings: BeautifierSettings): string {
  528. let arr = input.split("\r\n");
  529. let quotes = EscapeQuotes(arr);
  530. if (settings.RemoveAsserts) {
  531. RemoveAsserts(arr);//RemoveAsserts must be after EscapeQuotes
  532. }
  533. ApplyNoNewLineAfter(arr, settings.NewLineSettings.noNewLineAfter);
  534. var align = [],
  535. align_max = [],
  536. align_i1 = 0,
  537. align_i = 0;
  538. var str = "",
  539. str1 = "";
  540. var p = 0;
  541. var n = 0,
  542. j = 0;
  543. var tab_n = 0,
  544. str_len = 0,
  545. port_s = "";
  546. var back_tab = false,
  547. forward_tab = false,
  548. semi_pos = 0,
  549. begin_b = true,
  550. port_b = false;
  551. var before_begin = true;
  552. var l = arr.length;
  553. for (i = 0; i < l; i++) {
  554. if (arr[i].indexOf("BEGIN") >= 0) {
  555. before_begin = false;
  556. }
  557. if (port_s) {
  558. port_s += arr[i];
  559. var k_port = port_s.split("(").length;
  560. if (k_port == port_s.split(")").length) {
  561. arr[i] = arr[i] + "@@end";
  562. port_s = "";
  563. port_b = false;
  564. }
  565. }
  566. if ((!port_b && arr[i].regexIndexOf(/(\s|\(|^)(PORT|GENERIC|PROCESS|PROCEDURE)(\s|\(|$)/) >= 0)
  567. || (arr[i].regexIndexOf(/:[ ]?=[ ]?\(/) >= 0 && before_begin)) {
  568. port_b = true;
  569. port_s = arr[i];
  570. var k_port = port_s.split("(").length;
  571. if (k_port == 1) {
  572. port_b = false;
  573. port_s = "";
  574. } else if (k_port == port_s.split(")").length) {
  575. port_s = "";
  576. port_b = false;
  577. arr[i] = arr[i] + "@@singleend";
  578. } else {
  579. arr[i] = arr[i].replace(/(PORT|GENERIC|PROCEDURE)([a-z0-9A-Z_ ]+)\(([a-zA-Z0-9_\(\) ]+)/, '$1$2(\r\n$3');
  580. }
  581. }
  582. }
  583. input = arr.join("\r\n");
  584. input = input.replace(/([a-zA-Z0-9\); ])\);(@@comments[0-9]+)?@@end/g, '$1\r\n);$2@@end');
  585. input = input.replace(/[ ]?([&=:\-<>\+|\*])[ ]?/g, ' $1 ');
  586. input = input.replace(/[ ]?([,])[ ]?/g, '$1 ');
  587. input = input.replace(/[ ]?(['"])(THEN)/g, '$1 $2');
  588. input = input.replace(/[ ]?(\?)?[ ]?(<|:|>|\/)?[ ]+(=)?[ ]?/g, ' $1$2$3 ');
  589. input = input.replace(/(IF)[ ]?([\(\)])/g, '$1 $2');
  590. input = input.replace(/([\(\)])[ ]?(THEN)/gi, '$1 $2');
  591. input = input.replace(/(^|[\(\)])[ ]?(AND|OR|XOR|XNOR)[ ]*([\(])/g, '$1 $2 $3');
  592. input = input.replace(/ ([\-\*\/=+<>])[ ]*([\-\*\/=+<>]) /g, " $1$2 ");
  593. input = input.replace(/\r\n[ \t]+--\r\n/g, "\r\n");
  594. input = input.replace(/[ ]+/g, ' ');
  595. input = input.replace(/\r\n\r\n\r\n/g, '\r\n');
  596. input = input.replace(/[\r\n\s]+$/g, '');
  597. input = input.replace(/[ \t]+\)/g, ')');
  598. var matches = input.match(/'([a-zA-Z]+)\s/g);
  599. if (matches != null) {
  600. for (var k2 = 0; k2 < matches.length; k2++) {
  601. input = input.replace(matches[k2], matches[k2].toUpperCase());
  602. }
  603. }
  604. input = input.replace(/(MAP)[ \r\n]+\(/g, '$1(');
  605. //input = input.replace(/(;|THEN)[ ]?(@@comments[0-9]+)([a-zA-Z])/g, '$1 $2\r\n$3');
  606. input = input.replace(/[\r\n ]+RETURN/g, ' RETURN');
  607. input = input.replace(/BEGIN[\r\n ]+/g, 'BEGIN\r\n');
  608. input = input.replace(/ (PORT|GENERIC) /g, '\r\n$1 ');
  609. if (settings.CheckAlias) {
  610. var alias = [],
  611. subarr = [],
  612. o = 0,
  613. p = 0,
  614. p2 = 0,
  615. l2 = 0,
  616. i2 = 0;
  617. arr = input.split("ARCHITECTURE ");
  618. l = arr.length;
  619. for (i = 0; i < l; i++) {
  620. subarr = arr[i].split("ALIAS ");
  621. l2 = subarr.length;
  622. if (l2 > 1) {
  623. o = 0;
  624. for (i2 = 1; i2 < l2; i2++) {
  625. o = subarr[i2].indexOf(";", n);
  626. str = subarr[i2].substring(0, o);
  627. alias[p2++] = str.split(" IS ");
  628. }
  629. i2--;
  630. var str2 = subarr[i2].substr(o);
  631. for (p = 0; p < p2; p++) {
  632. var reg = new RegExp(alias[p][1], 'gi');
  633. str2 = str2.replace(reg, alias[p][0]);
  634. }
  635. subarr[i2] = subarr[i2].substring(0, o) + str2;
  636. }
  637. arr[i] = subarr.join("ALIAS ");
  638. }
  639. input = arr.join("ARCHITECTURE ");
  640. }
  641. arr = input.split("\r\n");
  642. l = arr.length;
  643. var signAlignPos = "";
  644. var if_b = 0,
  645. white_space = "",
  646. case_b = false,
  647. case_n = 0,
  648. procfun_b = false,
  649. semi_b = false,
  650. set_false = false,
  651. entity_b = false,
  652. then_b = false,
  653. conditional_b = false,
  654. generic_map_b = false,
  655. architecture_begin_b = false,
  656. process_begin_b = false,
  657. case_indent = [0, 0, 0, 0, 0, 0, 0];
  658. var align_groups = [],
  659. align_groups_max = [],
  660. lastAlignedSign = "",
  661. current_align_group = 0,
  662. aligned_group_starts = 0;
  663. var indent_start = [];
  664. for (i = 0; i < l; i++) {
  665. str = arr[i];
  666. str_len = str.length;
  667. if (str.replace(/[ \-\t]*/, "").length > 0) {
  668. var first_word = str.split(/[^\w]/)[0];
  669. var indent_start_last = indent_start.length == 0 ? 0 : indent_start[indent_start.length - 1];
  670. if (then_b) {
  671. arr[i] = " " + arr[i];
  672. if (str.indexOf(" THEN") >= 0) {
  673. then_b = false;
  674. back_tab = true;
  675. }
  676. }
  677. arr[i] = white_space + arr[i];
  678. if (first_word == "ELSIF") {
  679. tab_n = indent_start_last - 1;
  680. indent_start.pop();
  681. back_tab = true;
  682. } else if (str.indexOf("END CASE") == 0) {
  683. indent_start.pop();
  684. case_n--;
  685. tab_n = indent_start[indent_start.length - 1];
  686. } else if (first_word == "END") {
  687. tab_n = indent_start_last - 1;
  688. indent_start.pop();
  689. if (str.indexOf("END IF") == 0) {
  690. if_b--;
  691. }
  692. if (i == l - 1) {
  693. tab_n = 1;
  694. }
  695. } else if (first_word == "ELSE" && if_b) {
  696. tab_n = indent_start_last - 1;
  697. indent_start.pop();
  698. back_tab = true;
  699. } else if (case_n) {
  700. if (first_word == "WHEN") {
  701. tab_n = case_indent[case_n - 1];
  702. //back_tab = true;
  703. }
  704. } else if (first_word == "BEGIN") {
  705. if (begin_b) {
  706. if (architecture_begin_b) {
  707. tab_n = indent_start_last - 1;
  708. architecture_begin_b = false;
  709. } else if (process_begin_b) {
  710. tab_n = indent_start_last - 1;
  711. process_begin_b = false;
  712. } else {
  713. tab_n = indent_start_last;
  714. indent_start.push(tab_n + 1);
  715. }
  716. //indent_start.pop();
  717. back_tab = true;
  718. begin_b = false;
  719. if (procfun_b) {
  720. tab_n++;
  721. indent_start.push(tab_n);
  722. begin_b = true;
  723. }
  724. } else {
  725. back_tab = true;
  726. }
  727. } else if (first_word == "PROCESS") {
  728. begin_b = true;
  729. } else if (str.indexOf(": PROCESS") >= 0) {
  730. back_tab = true;
  731. begin_b = true;
  732. process_begin_b = true;
  733. } else if (str.indexOf(": ENTITY") >= 0) {
  734. back_tab = true;
  735. entity_b = true;
  736. } else if (str.indexOf("PROCEDURE ") >= 0) {
  737. back_tab = true;
  738. begin_b = true;
  739. }
  740. if (port_b && str.indexOf("@@") < 0) {
  741. if (i + 1 <= arr.length - 1 && arr[i + 1].indexOf("@@") < 0) {
  742. if (signAlignPos == ":") {
  743. if (str.indexOf(';') < 0) {
  744. arr[i] += arr[i + 1];
  745. arr[i + 1] = '@@removeline';
  746. }
  747. } else if (signAlignPos == "=>") {
  748. if (str.indexOf(',') < 0) {
  749. arr[i] += arr[i + 1];
  750. arr[i + 1] = '@@removeline';
  751. }
  752. }
  753. }
  754. }
  755. if (str.indexOf("PORT MAP") >= 0) {
  756. back_tab = true;
  757. port_b = true;
  758. if (str.indexOf(");") < 0) {
  759. align_i1 = align_i;
  760. var t = str.indexOf("=>");
  761. if (t >= 0) {
  762. signAlignPos = "=>";
  763. } else {
  764. if (i + 1 < arr.length) {
  765. t = arr[i + 1].indexOf("=>");
  766. if (t >= 0) {
  767. signAlignPos = "=>";
  768. }
  769. }
  770. }
  771. } else {
  772. signAlignPos = "";
  773. }
  774. } else if (str.indexOf("GENERIC MAP") >= 0) {
  775. tab_n++;
  776. indent_start.push(tab_n);
  777. generic_map_b = true;
  778. if (!begin_b) {
  779. back_tab = false;
  780. }
  781. } else if (str.indexOf("PORT (") >= 0 && begin_b) {
  782. back_tab = true;
  783. port_b = true;
  784. t = str.indexOf(":");
  785. if (str.indexOf(");") < 0) {
  786. align_i1 = align_i;
  787. if (t >= 0) {
  788. signAlignPos = ":";
  789. } else {
  790. t = arr[i + 1].indexOf(":");
  791. if (t >= 0) {
  792. signAlignPos = ":";
  793. }
  794. }
  795. } else {
  796. signAlignPos = "";
  797. }
  798. }
  799. if (set_false) {
  800. procfun_b = false;
  801. set_false = false;
  802. }
  803. if (str.indexOf("(") >= 0) {
  804. if (str.indexOf("PROCEDURE") >= 0 || str.indexOf("FUNCTION") >= 0) {
  805. procfun_b = true;
  806. back_tab = true;
  807. }
  808. if ((str.indexOf("GENERIC") >= 0 || str.indexOf(":= (") >= 0 || str.regexIndexOf(/PROCEDURE[a-zA-Z0-9_ ]+\(/) >= 0) && begin_b) {
  809. port_b = true;
  810. back_tab = true;
  811. }
  812. } else if (first_word == "FUNCTION") {
  813. back_tab = true;
  814. begin_b = true;
  815. }
  816. if (str.indexOf("@@singleend") >= 0) {
  817. back_tab = false;
  818. port_b = false;
  819. if (!begin_b) {
  820. forward_tab = true;
  821. }
  822. } else if (str.indexOf("@@end") >= 0 && port_b) {
  823. port_b = false;
  824. indent_start.pop();
  825. tab_n = indent_start[indent_start.length - 1];
  826. if (entity_b) {
  827. forward_tab = true;
  828. }
  829. if (generic_map_b) {
  830. forward_tab = true;
  831. generic_map_b = false;
  832. }
  833. }
  834. if (settings.SignAlignAll) {
  835. var alignedSigns = [":", "<=", "=>"];
  836. for (var currentSign = 0; currentSign < alignedSigns.length; currentSign++) {
  837. if (str.indexOf(alignedSigns[currentSign]) > 0) {
  838. var char_before_sign = str.split(alignedSigns[currentSign])[0];
  839. var char_before_sign_length = char_before_sign.length;
  840. align_groups.push(char_before_sign_length);
  841. align_groups_max.push(char_before_sign_length);
  842. if (alignedSigns[currentSign] == lastAlignedSign) {
  843. if (align_groups_max[current_align_group - 1] < char_before_sign_length) {
  844. for (var k3 = aligned_group_starts; k3 <= current_align_group; k3++) {
  845. align_groups_max[k3] = char_before_sign_length;
  846. }
  847. } else {
  848. align_groups_max[current_align_group] = align_groups_max[current_align_group - 1];
  849. }
  850. } else {
  851. aligned_group_starts = current_align_group;
  852. }
  853. arr[i] = char_before_sign + "@@alignall" + (current_align_group++) + str.substring(char_before_sign.length, arr[i].length);
  854. lastAlignedSign = alignedSigns[currentSign];
  855. break;
  856. }
  857. }
  858. if (currentSign == alignedSigns.length) {
  859. lastAlignedSign = "";
  860. }
  861. } else if (settings.SignAlignRegional) {
  862. if (port_b && signAlignPos != "") {
  863. if (str.indexOf(signAlignPos) >= 0) {
  864. var a1 = arr[i].split(signAlignPos);
  865. var l1 = a1[0].length;
  866. if (align_i >= 0 && align_i > align_i1) {
  867. align_max[align_i] = align_max[align_i - 1];
  868. } else {
  869. align_max[align_i] = l1;
  870. }
  871. if (align_i > align_i1 && align_max[align_i] < l1) {
  872. for (var k3 = align_i1; k3 <= align_i; k3++) {
  873. align_max[k3] = l1;
  874. }
  875. }
  876. align[align_i] = l1;
  877. arr[i] = a1[0] + "@@align" + (align_i++) + signAlignPos + arr[i].substring(l1 + signAlignPos.length, arr[i].length);
  878. }
  879. }
  880. }
  881. tab_n = tab_n < 1 ? 1 : tab_n;
  882. if (str_len) {
  883. if (isTesting) {
  884. console.log(tab_n, arr[i], indent_start);
  885. }
  886. arr[i] = (Array(tab_n).join(settings.Indentation)) + arr[i]; //indent
  887. if (settings.NewLineSettings.newLineAfter.indexOf("port")) {
  888. if (str.indexOf('@@singleend') < 0) {
  889. arr[i] = arr[i].replace(/(PORT)([ \r\n\w]*)\(/, "$1$2\r\n" + (Array(tab_n).join(settings.Indentation)) + "(");
  890. }
  891. }
  892. if (settings.NewLineSettings.newLineAfter.indexOf("generic")) {
  893. if (str.indexOf('@@singleend') < 0) {
  894. arr[i] = arr[i].replace(/(GENERIC)([ \r\n\w]*)\(/, "$1$2\r\n" + (Array(tab_n).join(settings.Indentation)) + "(");
  895. }
  896. }
  897. }
  898. if (back_tab) {
  899. tab_n++;
  900. indent_start.push(tab_n);
  901. back_tab = false;
  902. }
  903. if (forward_tab) {
  904. tab_n = indent_start_last;
  905. indent_start.pop();
  906. forward_tab = false;
  907. }
  908. if (conditional_b && str.indexOf(";") >= 0) {
  909. conditional_b = false;
  910. white_space = "";
  911. } else if (str.indexOf(";") >= 0 && semi_b) {
  912. semi_b = false;
  913. tab_n = indent_start_last;
  914. indent_start.pop();
  915. } else if (!semi_b && str.indexOf(";") < 0 && !port_b) {
  916. if (!conditional_b) {
  917. if (str.indexOf("WHEN") > 3 && str.indexOf("<=") > 1) {
  918. conditional_b = true;
  919. white_space = (Array(str.indexOf("= ") + 3).join(" "));
  920. } else if (first_word == "WHEN" && i + 1 < arr.length && arr[i + 1].indexOf("WHEN") < 0) {
  921. tab_n = indent_start_last + 1;
  922. } 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))) {
  923. tab_n++;
  924. indent_start.push(tab_n);
  925. semi_b = true;
  926. }
  927. }
  928. }
  929. if (first_word == "ENTITY") {
  930. tab_n++;
  931. indent_start.push(tab_n);
  932. } else if (",RECORD,PACKAGE,FOR,COMPONENT,CONFIGURATION,".indexOf("," + first_word + ",") >= 0) {
  933. tab_n++;
  934. indent_start.push(tab_n);
  935. } else if (str.indexOf(": FOR ") >= 0) {
  936. tab_n++;
  937. indent_start.push(tab_n);
  938. } else if (first_word == "CASE" || str.indexOf(": CASE") >= 0) {
  939. tab_n++;
  940. indent_start.push(tab_n);
  941. case_indent[case_n] = tab_n;
  942. case_n++;
  943. } else if (first_word == "ARCHITECTURE") {
  944. tab_n++;
  945. indent_start.push(tab_n);
  946. begin_b = true;
  947. architecture_begin_b = true;
  948. } else if (first_word == "IF") {
  949. if_b++;
  950. tab_n++;
  951. indent_start.push(tab_n);
  952. if (str.indexOf(" THEN") < 0) {
  953. then_b = true;
  954. tab_n = indent_start_last;
  955. //indent_start.pop();
  956. }
  957. }
  958. if (procfun_b) {
  959. if (str.regexIndexOf(/(\))|(RETURN [A-Za-z0-9 ]+)[\r\n ]+IS/) >= 0) {
  960. tab_n = indent_start_last;
  961. indent_start.pop();
  962. set_false = true;
  963. }
  964. }
  965. }
  966. }
  967. input = arr.join("\r\n");
  968. input = input.replace(/[\t]*@@removeline\r\n/g, '');
  969. p = input.indexOf('PROCESS');
  970. while (p >= 0) {
  971. let nextBracket = input.indexOf('(', p);
  972. let nextNewLine = input.indexOf('\r\n', p);
  973. let nextCloseBracket = input.indexOf(')', nextBracket);
  974. if (nextBracket < nextNewLine && nextCloseBracket > nextNewLine) {
  975. let processArray = input.substring(p, nextCloseBracket).split('\r\n');
  976. if (settings.Indentation.replace(/[ ]+/g, '').length == 0) {
  977. for (var i = 1; i < processArray.length; i++) {
  978. processArray[i] = (Array(nextBracket - p + 2).join(' ')) + processArray[i];
  979. }
  980. } else {
  981. for (var i = 1; i < processArray.length; i++) {
  982. processArray[i] = settings.Indentation + processArray[i];
  983. }
  984. }
  985. input = input.substring(0, p) + processArray.join('\r\n') + input.substring(nextCloseBracket, input.length);
  986. p = input.regexIndexOf('PROCESS[ ]+\\(', nextCloseBracket);
  987. } else {
  988. p = input.indexOf('PROCESS[ ]+\\(', p + 7);
  989. }
  990. }
  991. input = SetKeywordCase(input, settings.KeywordCase, KeyWords, TypeNames);
  992. if (settings.SignAlignAll) {
  993. for (var k = 0; k < current_align_group; k++) {
  994. input = input.replace("@@alignall" + k, Array((align_groups_max[k] - align_groups[k] + 1)).join(" "));
  995. }
  996. }
  997. if (settings.SignAlignRegional) {
  998. for (var k = 0; k < align_i; k++) {
  999. input = input.replace("@@align" + k, Array((align_max[k] - align[k] + 2)).join(" "));
  1000. }
  1001. }
  1002. for (var k = 0; k < quotes.length; k++) {
  1003. input = input.replace(ILQuotesPrefix + k, quotes[k]);
  1004. }
  1005. input = input.replace(/@@singleline[ \r\n]*/, " ");
  1006. return input;
  1007. }
  1008. function ReserveSemicolonInKeywords(arr: Array<string>) {
  1009. for (let i = 0; i < arr.length; i++) {
  1010. if (arr[i].match(/FUNCTION|PROCEDURE/) != null) {
  1011. arr[i] = arr[i].replace(/;/g, '@@semicolon');
  1012. }
  1013. }
  1014. }
  1015. export function ApplyNoNewLineAfter(arr: Array<string>, noNewLineAfter: Array<string>) {
  1016. if (noNewLineAfter == null) {
  1017. return;
  1018. }
  1019. for (let i = 0; i < arr.length; i++) {
  1020. noNewLineAfter.forEach(n => {
  1021. let regex = new RegExp("(" + n.toUpperCase + ")[ a-z0-9]+[a-z0-9]+");
  1022. if (arr[i].regexIndexOf(regex) >= 0) {
  1023. arr[i] += "@@singleline";
  1024. }
  1025. });
  1026. }
  1027. }
  1028. export function RemoveAsserts(arr: Array<string>) {
  1029. let need_semi: boolean = false;
  1030. let inAssert: boolean = false;
  1031. let n: number = 0;
  1032. for (let i = 0; i < arr.length; i++) {
  1033. let has_semi: boolean = arr[i].indexOf(";") >= 0;
  1034. if (need_semi) {
  1035. arr[i] = '';
  1036. }
  1037. n = arr[i].indexOf("ASSERT ");
  1038. if (n >= 0) {
  1039. inAssert = true;
  1040. arr[i] = '';
  1041. }
  1042. if (!has_semi) {
  1043. if (inAssert) {
  1044. need_semi = true;
  1045. }
  1046. }
  1047. else {
  1048. need_semi = false;
  1049. }
  1050. }
  1051. }
  1052. function EscapeQuotes(arr: Array<string>): Array<string> {
  1053. let quotes: Array<string> = [];
  1054. let quotesIndex = 0;
  1055. for (let i = 0; i < arr.length; i++) {
  1056. let quote = arr[i].match(/"([^"]+)"/g);
  1057. if (quote != null) {
  1058. for (var j = 0; j < quote.length; j++) {
  1059. arr[i] = arr[i].replace(quote[j], ILQuotesPrefix + quotesIndex);
  1060. quotes[quotesIndex++] = quote[j];
  1061. }
  1062. }
  1063. }
  1064. return quotes;
  1065. }
  1066. function RemoveExtraNewLines(input: any) {
  1067. input = input.replace(/(?:\r\n|\r|\n)/g, '\r\n');
  1068. input = input.replace(/ \r\n/g, '\r\n');
  1069. input = input.replace(/\r\n\r\n\r\n/g, '\r\n');
  1070. return input;
  1071. }