parse.js
3.35 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
'use strict';
const { Node, Block } = require('./Node');
const languages = require('./languages');
const constants = {
ESCAPED_CHAR_REGEX: /^\\./,
QUOTED_STRING_REGEX: /^(['"`])((?:\\.|[^\1])+?)(\1)/,
NEWLINE_REGEX: /^\r*\n/
};
const parse = (input, options = {}) => {
if (typeof input !== 'string') {
throw new TypeError('Expected input to be a string');
}
const cst = new Block({ type: 'root', nodes: [] });
const stack = [cst];
const name = (options.language || 'javascript').toLowerCase();
const lang = languages[name];
if (typeof lang === 'undefined') {
throw new Error(`Language "${name}" is not supported by strip-comments`);
}
const { LINE_REGEX, BLOCK_OPEN_REGEX, BLOCK_CLOSE_REGEX } = lang;
let block = cst;
let remaining = input;
let token;
let prev;
const source = [BLOCK_OPEN_REGEX, BLOCK_CLOSE_REGEX].filter(Boolean);
let tripleQuotes = false;
if (source.every(regex => regex.source === '^"""')) {
tripleQuotes = true;
}
/**
* Helpers
*/
const consume = (value = remaining[0] || '') => {
remaining = remaining.slice(value.length);
return value;
};
const scan = (regex, type = 'text') => {
const match = regex.exec(remaining);
if (match) {
consume(match[0]);
return { type, value: match[0], match };
}
};
const push = node => {
if (prev && prev.type === 'text' && node.type === 'text') {
prev.value += node.value;
return;
}
block.push(node);
if (node.nodes) {
stack.push(node);
block = node;
}
prev = node;
};
const pop = () => {
if (block.type === 'root') {
throw new SyntaxError('Unclosed block comment');
}
stack.pop();
block = stack[stack.length - 1];
};
/**
* Parse input string
*/
while (remaining !== '') {
// escaped characters
if ((token = scan(constants.ESCAPED_CHAR_REGEX, 'text'))) {
push(new Node(token));
continue;
}
// quoted strings
if (block.type !== 'block' && (!prev || !/\w$/.test(prev.value)) && !(tripleQuotes && remaining.startsWith('"""'))) {
if ((token = scan(constants.QUOTED_STRING_REGEX, 'text'))) {
push(new Node(token));
continue;
}
}
// newlines
if ((token = scan(constants.NEWLINE_REGEX, 'newline'))) {
push(new Node(token));
continue;
}
// block comment open
if (BLOCK_OPEN_REGEX && options.block && !(tripleQuotes && block.type === 'block')) {
if ((token = scan(BLOCK_OPEN_REGEX, 'open'))) {
push(new Block({ type: 'block' }));
push(new Node(token));
continue;
}
}
// block comment close
if (BLOCK_CLOSE_REGEX && block.type === 'block' && options.block) {
if ((token = scan(BLOCK_CLOSE_REGEX, 'close'))) {
token.newline = token.match[1] || '';
push(new Node(token));
pop();
continue;
}
}
// line comment
if (LINE_REGEX && block.type !== 'block' && options.line) {
if ((token = scan(LINE_REGEX, 'line'))) {
push(new Node(token));
continue;
}
}
// Plain text (skip "C" since some languages use "C" to start comments)
if ((token = scan(/^[a-zABD-Z0-9\t ]+/, 'text'))) {
push(new Node(token));
continue;
}
push(new Node({ type: 'text', value: consume(remaining[0]) }));
}
return cst;
};
module.exports = parse;