1 | import { TSDocMessageId } from './TSDocMessageId';
|
2 |
|
3 | var State;
|
4 | (function (State) {
|
5 |
|
6 | State[State["BeginComment1"] = 0] = "BeginComment1";
|
7 |
|
8 | State[State["BeginComment2"] = 1] = "BeginComment2";
|
9 |
|
10 | State[State["CollectingFirstLine"] = 2] = "CollectingFirstLine";
|
11 |
|
12 | State[State["CollectingLine"] = 3] = "CollectingLine";
|
13 |
|
14 | State[State["AdvancingLine"] = 4] = "AdvancingLine";
|
15 |
|
16 | State[State["Done"] = 5] = "Done";
|
17 | })(State || (State = {}));
|
18 |
|
19 |
|
20 |
|
21 | var LineExtractor = (function () {
|
22 | function LineExtractor() {
|
23 | }
|
24 | |
25 |
|
26 |
|
27 |
|
28 |
|
29 | LineExtractor.extract = function (parserContext) {
|
30 | var range = parserContext.sourceRange;
|
31 | var buffer = range.buffer;
|
32 | var commentRangeStart = 0;
|
33 | var commentRangeEnd = 0;
|
34 |
|
35 | var collectingLineStart = 0;
|
36 | var collectingLineEnd = 0;
|
37 | var nextIndex = range.pos;
|
38 | var state = State.BeginComment1;
|
39 | var lines = [];
|
40 | while (state !== State.Done) {
|
41 | if (nextIndex >= range.end) {
|
42 |
|
43 | switch (state) {
|
44 | case State.BeginComment1:
|
45 | case State.BeginComment2:
|
46 | parserContext.log.addMessageForTextRange(TSDocMessageId.CommentNotFound, 'Expecting a "/**" comment', range);
|
47 | return false;
|
48 | default:
|
49 | parserContext.log.addMessageForTextRange(TSDocMessageId.CommentMissingClosingDelimiter, 'Unexpected end of input', range);
|
50 | return false;
|
51 | }
|
52 | }
|
53 | var current = buffer[nextIndex];
|
54 | var currentIndex = nextIndex;
|
55 | ++nextIndex;
|
56 | var next = nextIndex < range.end ? buffer[nextIndex] : '';
|
57 | switch (state) {
|
58 | case State.BeginComment1:
|
59 | if (current === '/' && next === '*') {
|
60 | commentRangeStart = currentIndex;
|
61 | ++nextIndex;
|
62 | state = State.BeginComment2;
|
63 | }
|
64 | else if (!LineExtractor._whitespaceCharacterRegExp.test(current)) {
|
65 | parserContext.log.addMessageForTextRange(TSDocMessageId.CommentOpeningDelimiterSyntax, 'Expecting a leading "/**"', range.getNewRange(currentIndex, currentIndex + 1));
|
66 | return false;
|
67 | }
|
68 | break;
|
69 | case State.BeginComment2:
|
70 | if (current === '*') {
|
71 | if (next === ' ') {
|
72 | ++nextIndex;
|
73 | }
|
74 | collectingLineStart = nextIndex;
|
75 | collectingLineEnd = nextIndex;
|
76 | state = State.CollectingFirstLine;
|
77 | }
|
78 | else {
|
79 | parserContext.log.addMessageForTextRange(TSDocMessageId.CommentOpeningDelimiterSyntax, 'Expecting a leading "/**"', range.getNewRange(currentIndex, currentIndex + 1));
|
80 | return false;
|
81 | }
|
82 | break;
|
83 | case State.CollectingFirstLine:
|
84 | case State.CollectingLine:
|
85 | if (current === '\n') {
|
86 |
|
87 | if (state !== State.CollectingFirstLine || collectingLineEnd > collectingLineStart) {
|
88 |
|
89 | lines.push(range.getNewRange(collectingLineStart, collectingLineEnd));
|
90 | }
|
91 | collectingLineStart = nextIndex;
|
92 | collectingLineEnd = nextIndex;
|
93 | state = State.AdvancingLine;
|
94 | }
|
95 | else if (current === '*' && next === '/') {
|
96 | if (collectingLineEnd > collectingLineStart) {
|
97 | lines.push(range.getNewRange(collectingLineStart, collectingLineEnd));
|
98 | }
|
99 | collectingLineStart = 0;
|
100 | collectingLineEnd = 0;
|
101 | ++nextIndex;
|
102 | commentRangeEnd = nextIndex;
|
103 | state = State.Done;
|
104 | }
|
105 | else if (!LineExtractor._whitespaceCharacterRegExp.test(current)) {
|
106 | collectingLineEnd = nextIndex;
|
107 | }
|
108 | break;
|
109 | case State.AdvancingLine:
|
110 | if (current === '*') {
|
111 | if (next === '/') {
|
112 | collectingLineStart = 0;
|
113 | collectingLineEnd = 0;
|
114 | ++nextIndex;
|
115 | commentRangeEnd = nextIndex;
|
116 | state = State.Done;
|
117 | }
|
118 | else {
|
119 |
|
120 | if (next === ' ') {
|
121 | ++nextIndex;
|
122 | }
|
123 | collectingLineStart = nextIndex;
|
124 | collectingLineEnd = nextIndex;
|
125 | state = State.CollectingLine;
|
126 | }
|
127 | }
|
128 | else if (current === '\n') {
|
129 |
|
130 | lines.push(range.getNewRange(currentIndex, currentIndex));
|
131 | collectingLineStart = nextIndex;
|
132 | }
|
133 | else if (!LineExtractor._whitespaceCharacterRegExp.test(current)) {
|
134 |
|
135 |
|
136 |
|
137 | collectingLineEnd = nextIndex;
|
138 | state = State.CollectingLine;
|
139 | }
|
140 | break;
|
141 | }
|
142 | }
|
143 | |
144 |
|
145 |
|
146 | parserContext.commentRange = range.getNewRange(commentRangeStart, commentRangeEnd);
|
147 | parserContext.lines = lines;
|
148 | return true;
|
149 | };
|
150 | LineExtractor._whitespaceCharacterRegExp = /^\s$/;
|
151 | return LineExtractor;
|
152 | }());
|
153 | export { LineExtractor };
|
154 |
|
\ | No newline at end of file |