forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyntaxTrivia.ts
More file actions
178 lines (138 loc) · 5.7 KB
/
syntaxTrivia.ts
File metadata and controls
178 lines (138 loc) · 5.7 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
///<reference path='references.ts' />
module TypeScript {
export interface ISyntaxTrivia {
parent?: ISyntaxTriviaList;
kind(): SyntaxKind;
isWhitespace(): boolean;
isComment(): boolean;
isNewLine(): boolean;
isSkippedToken(): boolean;
fullStart(): number;
fullWidth(): number;
// Text for this trivia.
fullText(): string;
// If this is a skipped token trivia, then this was the token that was skipped.
skippedToken(): ISyntaxToken;
clone(): ISyntaxTrivia;
}
}
module TypeScript.Syntax {
class AbstractTrivia implements ISyntaxTrivia {
constructor(private _kind: SyntaxKind) {
}
public kind(): SyntaxKind {
return this._kind;
}
public clone(): ISyntaxTrivia {
throw Errors.abstract();
}
public fullStart(): number {
throw Errors.abstract();
}
public fullWidth(): number {
throw Errors.abstract();
}
public fullText(): string {
throw Errors.abstract();
}
public skippedToken(): ISyntaxToken {
throw Errors.abstract();
}
public isWhitespace(): boolean {
return this.kind() === SyntaxKind.WhitespaceTrivia;
}
public isComment(): boolean {
return this.kind() === SyntaxKind.SingleLineCommentTrivia || this.kind() === SyntaxKind.MultiLineCommentTrivia;
}
public isNewLine(): boolean {
return this.kind() === SyntaxKind.NewLineTrivia;
}
public isSkippedToken(): boolean {
return this.kind() === SyntaxKind.SkippedTokenTrivia;
}
}
class SkippedTokenTrivia extends AbstractTrivia {
constructor(private _skippedToken: ISyntaxToken, private _fullText: string) {
super(SyntaxKind.SkippedTokenTrivia);
_skippedToken.parent = <ISyntaxElement><any>this;
}
public clone(): ISyntaxTrivia {
return new SkippedTokenTrivia(this._skippedToken.clone(), this._fullText);
}
public fullStart(): number {
return this._skippedToken.fullStart();
}
public fullWidth(): number {
return this.fullText().length;
}
public fullText(): string {
return this._fullText;
}
public skippedToken(): ISyntaxToken {
return this._skippedToken;
}
}
class DeferredTrivia extends AbstractTrivia {
constructor(kind: SyntaxKind, private _text: ISimpleText, private _fullStart: number, private _fullWidth: number) {
super(kind);
}
public clone(): ISyntaxTrivia {
return new DeferredTrivia(this.kind(), this._text, this._fullStart, this._fullWidth);
}
public fullStart(): number {
return this._fullStart;
}
public fullWidth(): number {
return this._fullWidth;
}
public fullText(): string {
return this._text.substr(this._fullStart, this._fullWidth);
}
public skippedToken(): ISyntaxToken {
throw Errors.invalidOperation();
}
}
export function deferredTrivia(kind: SyntaxKind, text: ISimpleText, fullStart: number, fullWidth: number): ISyntaxTrivia {
return new DeferredTrivia(kind, text, fullStart, fullWidth);
}
export function skippedTokenTrivia(token: ISyntaxToken, text: ISimpleText): ISyntaxTrivia {
Debug.assert(!token.hasLeadingTrivia());
Debug.assert(!token.hasTrailingTrivia());
Debug.assert(token.fullWidth() > 0);
return new SkippedTokenTrivia(token, token.fullText(text));
}
// Breaks a multiline trivia up into individual line components. If the trivia doesn't span
// any lines, then the result will be a single string with the entire text of the trivia.
// Otherwise, there will be one entry in the array for each line spanned by the trivia. Each
// entry will contain the line separator at the end of the string.
export function splitMultiLineCommentTriviaIntoMultipleLines(trivia: ISyntaxTrivia): string[] {
// Debug.assert(trivia.kind === SyntaxKind.MultiLineCommentTrivia);
var result: string[] = [];
var triviaText = trivia.fullText();
var currentIndex = 0;
for (var i = 0; i < triviaText.length; i++) {
var ch = triviaText.charCodeAt(i);
// When we run into a newline for the first time, create the string builder and copy
// all the values up to this newline into it.
var isCarriageReturnLineFeed = false;
switch (ch) {
case CharacterCodes.carriageReturn:
if (i < triviaText.length - 1 && triviaText.charCodeAt(i + 1) === CharacterCodes.lineFeed) {
// Consume the \r
i++;
}
// Fall through.
case CharacterCodes.lineFeed:
case CharacterCodes.paragraphSeparator:
case CharacterCodes.lineSeparator:
// Eat from the last starting position through to the end of the newline.
result.push(triviaText.substring(currentIndex, i + 1));
// Set the current index to *after* the newline.
currentIndex = i + 1;
continue;
}
}
result.push(triviaText.substring(currentIndex));
return result;
}
}