-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcomment.js
75 lines (61 loc) · 1.51 KB
/
comment.js
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
'use strict';
import Token from './token';
const ASTERISK = 42;
const SLASH = 47;
/**
* Consumes comment from given stream: either multi-line or single-line
* @param {StreamReader} stream
* @return {CommentToken}
*/
export default function(stream) {
return singleLineComment(stream) || multiLineComment(stream);
}
export function singleLineComment(stream) {
if (eatSingleLineComment(stream)) {
const token = new Token(stream, 'comment');
token.property('type', 'single-line');
return token;
}
}
export function multiLineComment(stream) {
if (eatMultiLineComment(stream)) {
const token = new Token(stream, 'comment');
token.property('type', 'multiline');
return token;
}
}
export function eatComment(stream) {
return eatSingleLineComment(stream) || eatMultiLineComment(stream);
}
export function eatSingleLineComment(stream) {
const start = stream.pos;
if (stream.eat(SLASH) && stream.eat(SLASH)) {
// single-line comment, consume till the end of line
stream.start = start;
while (!stream.eof()) {
if (isLineBreak(stream.next())) {
break;
}
}
return true;
}
stream.pos = start;
return false;
}
export function eatMultiLineComment(stream) {
const start = stream.pos;
if (stream.eat(SLASH) && stream.eat(ASTERISK)) {
while (!stream.eof()) {
if (stream.next() === ASTERISK && stream.eat(SLASH)) {
break;
}
}
stream.start = start;
return true;
}
stream.pos = start;
return false;
}
function isLineBreak(code) {
return code === 10 /* LF */ || code === 13 /* CR */;
}