Skip to content

Commit 7356eb4

Browse files
committed
Refactor skipBlank* methods to use peekBlank*
1 parent 45b2b75 commit 7356eb4

File tree

1 file changed

+22
-37
lines changed

1 file changed

+22
-37
lines changed

fluent-syntax/src/stream.js

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -67,64 +67,44 @@ export const EOF = undefined;
6767
const SPECIAL_LINE_START_CHARS = ["}", ".", "[", "*"];
6868

6969
export class FluentParserStream extends ParserStream {
70-
skipBlankInline() {
71-
let blank = "";
72-
while (this.currentChar === " ") {
73-
blank += " ";
74-
this.next();
75-
}
76-
return blank;
77-
}
78-
7970
peekBlankInline() {
71+
const start = this.index + this.peekOffset;
8072
while (this.currentPeek === " ") {
8173
this.peek();
8274
}
75+
return this.string.slice(start, this.index + this.peekOffset);
8376
}
8477

85-
skipBlankBlock() {
78+
skipBlankInline() {
79+
const blank = this.peekBlankInline();
80+
this.skipToPeek();
81+
return blank;
82+
}
83+
84+
peekBlankBlock() {
8685
let blank = "";
8786
while (true) {
87+
const lineStart = this.peekOffset;
8888
this.peekBlankInline();
89-
9089
if (this.currentPeek === EOL) {
91-
this.skipToPeek();
92-
this.next();
9390
blank += EOL;
91+
this.peek();
9492
continue;
9593
}
96-
9794
if (this.currentPeek === EOF) {
98-
// Consume any inline blanks before the EOF.
99-
this.skipToPeek();
95+
// Treat the blank line at EOF as a blank block.
10096
return blank;
10197
}
102-
10398
// Any other char; reset to column 1 on this line.
104-
this.resetPeek();
99+
this.resetPeek(lineStart);
105100
return blank;
106101
}
107102
}
108103

109-
peekBlankBlock() {
110-
let blank = "";
111-
while (true) {
112-
const lineStart = this.peekOffset;
113-
this.peekBlankInline();
114-
if (this.currentPeek === EOL) {
115-
this.peek();
116-
blank += EOL;
117-
} else {
118-
this.resetPeek(lineStart);
119-
return blank;
120-
}
121-
}
122-
}
123-
124-
skipBlank() {
125-
while (this.currentChar === " " || this.currentChar === EOL) {
126-
this.next();
127-
}
104+
skipBlankBlock() {
105+
const blank = this.peekBlankBlock();
106+
this.skipToPeek();
107+
return blank;
128108
}
129109

130110
peekBlank() {
@@ -133,6 +113,11 @@ export class FluentParserStream extends ParserStream {
133113
}
134114
}
135115

116+
skipBlank() {
117+
this.peekBlank();
118+
this.skipToPeek();
119+
}
120+
136121
expectChar(ch) {
137122
if (this.currentChar === ch) {
138123
this.next();

0 commit comments

Comments
 (0)