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.

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