forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharacterStream.ts
More file actions
133 lines (108 loc) · 3.64 KB
/
characterStream.ts
File metadata and controls
133 lines (108 loc) · 3.64 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import Char from 'typescript-char';
import { isLineBreak, isWhiteSpace } from './characters';
import { TextIterator } from './textIterator';
import { ICharacterStream, ITextIterator } from './types';
export class CharacterStream implements ICharacterStream {
private text: ITextIterator;
private _position: number;
private _currentChar: number;
private _isEndOfStream: boolean;
constructor(text: string | ITextIterator) {
this.text = typeof text === 'string' ? new TextIterator(text) : text;
this._position = 0;
this._currentChar = text.length > 0 ? text.charCodeAt(0) : 0;
this._isEndOfStream = text.length === 0;
}
public getText(): string {
return this.text.getText();
}
public get position(): number {
return this._position;
}
public set position(value: number) {
this._position = value;
this.checkBounds();
}
public get currentChar(): number {
return this._currentChar;
}
public get nextChar(): number {
return this.position + 1 < this.text.length ? this.text.charCodeAt(this.position + 1) : 0;
}
public get prevChar(): number {
return this.position - 1 >= 0 ? this.text.charCodeAt(this.position - 1) : 0;
}
public isEndOfStream(): boolean {
return this._isEndOfStream;
}
public lookAhead(offset: number): number {
const pos = this._position + offset;
return pos < 0 || pos >= this.text.length ? 0 : this.text.charCodeAt(pos);
}
public advance(offset: number) {
this.position += offset;
}
public moveNext(): boolean {
if (this._position < this.text.length - 1) {
// Most common case, no need to check bounds extensively
this._position += 1;
this._currentChar = this.text.charCodeAt(this._position);
return true;
}
this.advance(1);
return !this.isEndOfStream();
}
public isAtWhiteSpace(): boolean {
return isWhiteSpace(this.currentChar);
}
public isAtLineBreak(): boolean {
return isLineBreak(this.currentChar);
}
public skipLineBreak(): void {
if (this._currentChar === Char.CarriageReturn) {
this.moveNext();
if (this.currentChar === Char.LineFeed) {
this.moveNext();
}
} else if (this._currentChar === Char.LineFeed) {
this.moveNext();
}
}
public skipWhitespace(): void {
while (!this.isEndOfStream() && this.isAtWhiteSpace()) {
this.moveNext();
}
}
public skipToEol(): void {
while (!this.isEndOfStream() && !this.isAtLineBreak()) {
this.moveNext();
}
}
public skipToWhitespace(): void {
while (!this.isEndOfStream() && !this.isAtWhiteSpace()) {
this.moveNext();
}
}
public isAtString(): boolean {
return this.currentChar === Char.SingleQuote || this.currentChar === Char.DoubleQuote;
}
public charCodeAt(index: number): number {
return this.text.charCodeAt(index);
}
public get length(): number {
return this.text.length;
}
private checkBounds(): void {
if (this._position < 0) {
this._position = 0;
}
this._isEndOfStream = this._position >= this.text.length;
if (this._isEndOfStream) {
this._position = this.text.length;
}
this._currentChar = this._isEndOfStream ? 0 : this.text.charCodeAt(this._position);
}
}