Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/emmet/src/bufferStream.ts
4772 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
/* Based on @sergeche's work in his emmet plugin */
7
8
import { TextDocument } from 'vscode';
9
10
/**
11
* A stream reader for VSCode's `TextDocument`
12
* Based on @emmetio/stream-reader and @emmetio/atom-plugin
13
*/
14
export class DocumentStreamReader {
15
private document: TextDocument;
16
private start: number;
17
private _eof: number;
18
private _sof: number;
19
public pos: number;
20
21
constructor(document: TextDocument, pos?: number, limit?: [number, number]) {
22
this.document = document;
23
this.start = this.pos = pos ? pos : 0;
24
this._sof = limit ? limit[0] : 0;
25
this._eof = limit ? limit[1] : document.getText().length;
26
}
27
28
/**
29
* Returns true only if the stream is at the start of the file.
30
*/
31
sof(): boolean {
32
return this.pos <= this._sof;
33
}
34
35
/**
36
* Returns true only if the stream is at the end of the file.
37
*/
38
eof(): boolean {
39
return this.pos >= this._eof;
40
}
41
42
/**
43
* Creates a new stream instance which is limited to given range for given document
44
*/
45
limit(start: number, end: number): DocumentStreamReader {
46
return new DocumentStreamReader(this.document, start, [start, end]);
47
}
48
49
/**
50
* Returns the next character code in the stream without advancing it.
51
* Will return NaN at the end of the file.
52
*/
53
peek(): number {
54
if (this.eof()) {
55
return NaN;
56
}
57
return this.document.getText().charCodeAt(this.pos);
58
}
59
60
/**
61
* Returns the next character in the stream and advances it.
62
* Also returns NaN when no more characters are available.
63
*/
64
next(): number {
65
if (this.eof()) {
66
return NaN;
67
}
68
69
const code = this.document.getText().charCodeAt(this.pos);
70
this.pos++;
71
72
if (this.eof()) {
73
// restrict pos to eof, if in case it got moved beyond eof
74
this.pos = this._eof;
75
}
76
77
return code;
78
}
79
80
/**
81
* Backs up the stream n characters. Backing it up further than the
82
* start of the current token will cause things to break, so be careful.
83
*/
84
backUp(n: number): number {
85
this.pos -= n;
86
if (this.pos < 0) {
87
this.pos = 0;
88
}
89
return this.peek();
90
}
91
92
/**
93
* Get the string between the start of the current token and the
94
* current stream position.
95
*/
96
current(): string {
97
return this.substring(this.start, this.pos);
98
}
99
100
/**
101
* Returns contents for given range
102
*/
103
substring(from: number, to: number): string {
104
return this.document.getText().substring(from, to);
105
}
106
107
/**
108
* Creates error object with current stream state
109
*/
110
error(message: string): Error {
111
const err = new Error(`${message} at offset ${this.pos}`);
112
return err;
113
}
114
115
/**
116
* `match` can be a character code or a function that takes a character code
117
* and returns a boolean. If the next character in the stream 'matches'
118
* the given argument, it is consumed and returned.
119
* Otherwise, `false` is returned.
120
*/
121
eat(match: number | Function): boolean {
122
const ch = this.peek();
123
const ok = typeof match === 'function' ? match(ch) : ch === match;
124
125
if (ok) {
126
this.next();
127
}
128
129
return ok;
130
}
131
132
/**
133
* Repeatedly calls <code>eat</code> with the given argument, until it
134
* fails. Returns <code>true</code> if any characters were eaten.
135
*/
136
eatWhile(match: number | Function): boolean {
137
const start = this.pos;
138
while (!this.eof() && this.eat(match)) { }
139
return this.pos !== start;
140
}
141
}
142
143