aboutsummaryrefslogtreecommitdiff
path: root/plugin/src/lexical.js
blob: ff01166b9370a4d48c0fdd5d977ff442fd6aea0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

'use strict';

// Preserve UTF-16 offsets and newlines, masking CIE quotes and // comments.
// Unlike C++, CIE has no escapes or multiline literals.
function codeOnly(text) {
	let quote = null;
	let comment = false;
	const result = [];
	for (let i = 0; i < text.length; i++) {
		const char = text[i];
		if (char === '\n' || char === '\r') {
			quote = null;
			comment = false;
			result.push(char);
		} else if (comment) {
			result.push(' ');
		} else if (quote) {
			if (char === quote) quote = null;
			result.push(' ');
		} else if (text.slice(i, i + 3) === "'''") {
			// The compiler accepts a quote character as a three-quote CHAR literal.
			result.push(' ', ' ', ' ');
			i += 2;
		} else if (char === '"' || char === "'") {
			quote = char;
			result.push(' ');
		} else if (char === '/' && text[i + 1] === '/') {
			comment = true;
			result.push(' ');
		} else {
			result.push(char);
		}
	}
	return result.join('');
}

module.exports = { codeOnly };