| 83 | // The semi-colon is not treated as the start of a comment when enclosed in parentheses. |
| 84 | const stripComments = (() => { |
| 85 | const _stripComments = (line) => { |
| 86 | let result = ''; |
| 87 | let currentComment = ''; |
| 88 | let comments = []; |
| 89 | let openParens = 0; |
| 90 | |
| 91 | // Detect semicolon comments before parentheses |
| 92 | for (let i = 0; i < line.length; i++) { |
| 93 | const char = line[i]; |
| 94 | |
| 95 | if (char === ';' && openParens === 0) { |
| 96 | // Start semicolon comment outside parentheses |
| 97 | comments.push(line.slice(i + 1).trim()); |
| 98 | openParens = 0; // Reset parentheses counter |
| 99 | break; // Stop further processing after a semicolon comment |
| 100 | } |
| 101 | |
| 102 | if (char === '(') { |
| 103 | // Start parentheses comment |
| 104 | if (openParens === 0) { |
| 105 | currentComment = ''; |
| 106 | } else if (openParens > 0) { |
| 107 | currentComment += char; |
| 108 | } |
| 109 | openParens = Math.min(openParens + 1, Number.MAX_SAFE_INTEGER); |
| 110 | } else if (char === ')') { |
| 111 | // End parentheses comment |
| 112 | openParens = Math.max(0, openParens - 1); |
| 113 | if (openParens === 0) { |
| 114 | comments.push(currentComment.trim()); |
| 115 | currentComment = ''; |
| 116 | } else if (openParens > 0) { |
| 117 | currentComment += char; |
| 118 | } |
| 119 | } else if (openParens > 0) { |
| 120 | // Inside parentheses comment |
| 121 | currentComment += char; |
| 122 | } else { |
| 123 | // Normal text outside comments |
| 124 | result += char; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | result = result.trim(); |
| 129 | return [result, comments]; |
| 130 | }; |
| 131 | |
| 132 | return (line) => { |
| 133 | const [strippedLine, comments] = _stripComments(line); |